Wallet.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 货币模型
  7. */
  8. class Wallet extends Model
  9. {
  10. // 当前模型名称
  11. protected $name = 'user_wallet';
  12. /**
  13. * 获取交易类型配置
  14. * @return mixed
  15. */
  16. public function getlogtype($type = '')
  17. {
  18. $conf = config('wallet.logtype');
  19. if($type){
  20. return $conf[$type] ?: $type;
  21. }
  22. return $conf;
  23. }
  24. //获取钱包名称
  25. public function getwalletname($name = ''){
  26. $conf = config('wallet.moneyname');
  27. if($name){
  28. return $conf[$name] ?: $name;
  29. }
  30. return $conf;
  31. }
  32. /**
  33. * 获取钱包余额
  34. * @param string $username 用户编号
  35. * @param string $wallet_name 指定钱包名
  36. * @return array|float
  37. */
  38. public function getWallet($user_id = '',$company_id = '', $wallet_name = '')
  39. {
  40. //所有钱包余额
  41. $wallet = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->lock(true)->find();
  42. if(!$wallet) {
  43. return 0;
  44. //abort(500,'钱包余额获取失败');
  45. }
  46. if($wallet_name) { //返回指定钱包
  47. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  48. } else { //返回所有钱包
  49. return $wallet;
  50. }
  51. }
  52. /**
  53. *
  54. * @param floatval $number 金额(正数进账,负数出账)
  55. * @param $accountType 货币类型,money,score
  56. * @param $logtype 日志的类型
  57. * @param $remark 备注
  58. * @param $user_id 用户id
  59. * @param $table 来源表
  60. * @param $data_id 表id
  61. * @param $isAdmin 是否是管理员处理
  62. * @return array
  63. * @return array[status]
  64. * @return array[msg]
  65. * @return array[log_table]
  66. * @return array[log_id]
  67. */
  68. public function lockChangeAccountRemain($company_id,$user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  69. {
  70. //初始化
  71. $result = array(
  72. 'status'=>false,
  73. 'code' => 0, //报错时候采用
  74. 'msg'=>'',
  75. 'log_table' => '',
  76. 'log_id' => '',
  77. );
  78. //获取小数点
  79. $point = $accountType == 'money' ? 2 : 0;
  80. bcscale($point);
  81. //钱包名称
  82. $wallet_name = $this->getwalletname($accountType);
  83. //检测
  84. $number = floatval( $number );
  85. if( $number == 0 )
  86. {
  87. $result['msg'] = '交易金额:0';
  88. return $result;
  89. }
  90. if(0 === bccomp($number, 0)){
  91. $result['msg'] = '交易金额:0';
  92. return $result;
  93. }
  94. //检测
  95. $wallet = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->lock(true)->find();
  96. if(!$wallet)
  97. {
  98. $result['msg'] = '不存在的用户';
  99. return $result;
  100. }
  101. if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
  102. {
  103. $result['code'] = 10;
  104. $result['msg'] = $wallet_name.'余额不足!';
  105. return $result;
  106. }
  107. if(0 !== bccomp($number, 0))
  108. {
  109. //钱币记录
  110. $data = array();
  111. $data['company_id'] = $company_id;
  112. $data['user_id'] = $user_id;
  113. $data['log_type'] = $logtype;
  114. $data['before'] = $wallet[$accountType];
  115. $data['change_value'] = $number;
  116. $data['remain'] = bcadd($wallet[$accountType], $number);
  117. $data['table'] = $table;
  118. $data['table_id'] = $table_id;
  119. $data['remark'] = $remark;
  120. $data['createtime'] = time();
  121. $data['updatetime'] = time();
  122. //新的方式
  123. $rs1 = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->update([$accountType => $data['remain']]);
  124. /////////////
  125. $log_table = 'user_'.$accountType.'_log';
  126. $rs2_id = Db::name($log_table)->insertGetId($data);
  127. if($rs1 === false || $rs2_id === false){
  128. $result['msg'] = '更新财务记录失败!';
  129. return $result;
  130. }
  131. if( $rs1 !== false && $rs2_id !== false )
  132. {
  133. $result['status'] = true;
  134. $result['msg'] = '账户余额已更新!';
  135. $result['log_table'] = $accountType.'_log';
  136. $result['log_id'] = $rs2_id;
  137. return $result;
  138. }
  139. else
  140. {
  141. $result['msg'] = '更新财务记录失败!';
  142. return $result;
  143. }
  144. } else {
  145. $result['msg'] = '金额不足0.01';
  146. return $result;
  147. }
  148. }
  149. }