Wallet.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 货币模型
  7. */
  8. class Wallet extends BaseModel
  9. {
  10. // 日志变动类型
  11. const log_type = [
  12. 1 => '系统调节',//money + -
  13. 10 => '用户充值',//money +
  14. 20 => '老年大学活动报名',//money + -
  15. 31 => '商城订单支付', //money -
  16. 32 => '商城订单退款', //money +
  17. //善豆
  18. 101 => '签到', //bean +
  19. 102 => '完成任务', //bean +
  20. 131 => '商城订单支付抵扣', //bean -
  21. 132 => '商城订单退款', //bean +
  22. ];
  23. // 操作钱包余额类型
  24. const money_type = [
  25. 'money' => '余额',
  26. 'bean' => '善豆',
  27. ];
  28. protected $message = '';
  29. protected $data = [];
  30. /**
  31. * 获取交易类型配置
  32. * @return mixed
  33. */
  34. public function getLogType($type = '')
  35. {
  36. $conf = self::log_type;
  37. if ($type) {
  38. return $conf[$type] ?: $type;
  39. }
  40. return $conf;
  41. }
  42. //获取钱包名称
  43. public function getWalletName($name = '')
  44. {
  45. $conf = self::money_type;
  46. if ($name) {
  47. return $conf[$name] ?: $name;
  48. }
  49. return $conf;
  50. }
  51. /**
  52. * 获取钱包余额
  53. * @param int $user_id 用户编号
  54. * @param string $wallet_name 指定钱包名
  55. * @return array|float
  56. */
  57. public function getWalletBak($user_id = 0, $wallet_name = '')
  58. {
  59. //所有钱包余额
  60. if (!$wallet = Db::name('user_wallet')->where(['user_id' => $user_id])->find()) {
  61. abort(500, '钱包余额获取失败');
  62. }
  63. if ($wallet_name) { //返回指定钱包
  64. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  65. } else { //返回所有钱包
  66. return $wallet;
  67. }
  68. }
  69. public function getWallet($user_id = 0, $wallet_name = 'money')
  70. {
  71. return Db::name('user')->where('id',$user_id)->value($wallet_name);
  72. }
  73. /**
  74. *
  75. * @param floatval $number 金额(正数进账,负数出账)
  76. * @param $accountType 货币类型,money,score
  77. * @param $log_type 日志的类型
  78. * @param $remark 备注
  79. * @param $user_id 用户id
  80. * @param $table 来源表
  81. * @param $data_id 表id
  82. * @param $isAdmin 是否是管理员处理
  83. * @return array
  84. * @return array[status]
  85. * @return array[msg]
  86. * @return array[log_table]
  87. * @return array[log_id]
  88. */
  89. public function lockChangeAccountRemain($user_id, $number, $accountType = 'money', $log_type = '', $remark = '', $table = '', $table_id = 0)
  90. {
  91. //初始化
  92. $result = array(
  93. 'status' => false,
  94. 'msg' => '',
  95. 'log_table' => '',
  96. 'log_id' => '',
  97. );
  98. //获取小数点
  99. $point = 0;
  100. if(in_array($accountType,['money','bean'])){
  101. $point = 2;
  102. }
  103. bcscale($point);
  104. //钱包名称
  105. $wallet_name = $this->getWalletName($accountType);
  106. //检测
  107. $number = floatval($number);
  108. if ($number == 0) {
  109. $result['msg'] = '交易金额:0';
  110. return $result;
  111. }
  112. if (0 === bccomp($number, 0)) {
  113. $result['msg'] = '交易金额:0';
  114. return $result;
  115. }
  116. //检测
  117. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
  118. if (!$wallet) {
  119. $result['msg'] = '不存在的用户';
  120. return $result;
  121. }
  122. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  123. $result['msg'] = $wallet_name . '余额不足!';
  124. return $result;
  125. } else {
  126. if (0 !== bccomp($number, 0)) {
  127. //钱币记录
  128. $data = array();
  129. $data['user_id'] = $user_id;
  130. $data['log_type'] = $log_type;
  131. // $data['money_type'] = $accountType;
  132. $data['before'] = $wallet[$accountType];
  133. $data['change_value'] = $number;
  134. $data['remain'] = bcadd($wallet[$accountType], $number);
  135. $data['table'] = $table;
  136. $data['table_id'] = $table_id;
  137. $data['remark'] = $remark;
  138. $data['createtime'] = time();
  139. $data['updatetime'] = time();
  140. //新的方式
  141. $rs1 = Db::name('user_wallet')->where(['user_id' => $user_id])->update([$accountType => $data['remain']]);
  142. /////////////
  143. $log_table = 'user_' . $accountType . '_log';
  144. $rs2_id = Db::name($log_table)->insertGetId($data);
  145. if ($rs1 === false || $rs2_id === false) {
  146. $result['msg'] = '更新财务记录失败!';
  147. return $result;
  148. }
  149. if ($rs1 !== false && $rs2_id !== false) {
  150. $result['status'] = true;
  151. $result['msg'] = '账户余额已更新!';
  152. $result['log_table'] = $log_table;
  153. $result['log_id'] = $rs2_id;
  154. return $result;
  155. } else {
  156. $result['msg'] = '更新财务记录失败!';
  157. return $result;
  158. }
  159. } else {
  160. $result['msg'] = '金额不足0.01';
  161. return $result;
  162. }
  163. }
  164. }
  165. /**
  166. * 余额变更
  167. * @param int $user_id 用户id
  168. * @param float $number 金额(正数进账,负数出账)
  169. * @param string $accountType 货币类型 self::money_type
  170. * @param int $log_type 日志的类型 self::log_type
  171. * @param string $remark 备注
  172. * @param string $table 来源表
  173. * @param int $table_id 表id
  174. * @return bool
  175. */
  176. public function change(int $user_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
  177. {
  178. //获取小数点
  179. $point = 0;
  180. if(in_array($accountType,['money','bean'])){
  181. $point = 2;
  182. }
  183. bcscale($point);
  184. //钱包名称
  185. $wallet_name = $this->getWalletName($accountType);
  186. //检测
  187. if ($number == 0 || 0 === bccomp($number, 0)) {
  188. return $this->error('交易金额:0');
  189. }
  190. //检测
  191. if (!$wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find()) {
  192. return $this->error('不存在的用户');
  193. }
  194. if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
  195. return $this->error("{$wallet_name}余额不足!");
  196. }
  197. //钱币记录
  198. $data = [
  199. 'user_id' => $user_id,
  200. 'log_type' => $log_type,
  201. 'before' => $wallet[$accountType],
  202. 'change_value' => $number,
  203. 'remain' => bcadd($wallet[$accountType], $number),
  204. 'table' => $table,
  205. 'table_id' => $table_id,
  206. 'remark' => $remark,
  207. 'createtime' => time(),
  208. 'updatetime' => time(),
  209. ];
  210. //新的方式
  211. $upWallet = Db::name('user_wallet')->where(['user_id' => $user_id])->update([$accountType => $data['remain']]);
  212. if ($upWallet === false) {
  213. return $this->error("更新账户余额失败!");
  214. }
  215. if (!Db::name("user_{$accountType}_log")->insertGetId($data)) {
  216. return $this->error("更新财务记录失败!");
  217. }
  218. return $this->success("账户余额已更新!");
  219. }
  220. protected function success($message = '', $data = [])
  221. {
  222. $this->message = $message;
  223. $this->data = $data;
  224. return true;
  225. }
  226. protected function error($message = '', $data = [])
  227. {
  228. $this->message = $message;
  229. $this->data = $data;
  230. return false;
  231. }
  232. public function getMessage()
  233. {
  234. return $this->message;
  235. }
  236. public function getData($name = null)
  237. {
  238. $data = $this->data;
  239. !empty($name) && $data = $this->data[$name];
  240. return $data;
  241. }
  242. }