Wallet.php 8.0 KB

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