Wallet.php 8.3 KB

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