Wallet.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * 获取交易类型配置
  12. * @return mixed
  13. */
  14. public function getlogtype($type = '')
  15. {
  16. $conf = config('wallet.logtype');
  17. if($type){
  18. return $conf[$type] ?: $type;
  19. }
  20. return $conf;
  21. }
  22. //获取钱包名称
  23. public function getwalletname($name = ''){
  24. $conf = config('wallet.moneyname');
  25. if($name){
  26. return $conf[$name] ?: $name;
  27. }
  28. return $conf;
  29. }
  30. /**
  31. * 获取钱包余额
  32. * @param string $username 用户编号
  33. * @param string $wallet_name 指定钱包名
  34. * @return array|float
  35. */
  36. public function getWallet($user_id = '', $wallet_name = '')
  37. {
  38. //所有钱包余额
  39. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
  40. if(!$wallet) {
  41. abort(500,'钱包余额获取失败');
  42. }
  43. if($wallet_name) { //返回指定钱包
  44. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  45. } else { //返回所有钱包
  46. return $wallet;
  47. }
  48. }
  49. /**
  50. *
  51. * @param floatval $number 金额(正数进账,负数出账)
  52. * @param $accountType 货币类型,money,score
  53. * @param $logtype 日志的类型
  54. * @param $remark 备注
  55. * @param $user_id 用户id
  56. * @param $other_user_id 对方用户id
  57. * @param $table 来源表
  58. * @param $table_id 表id
  59. * @param $is_pay_get 1累计消费金额 2累计获得金额
  60. * @param $request_id 请求标识
  61. * @return array
  62. * @return array[status]
  63. * @return array[msg]
  64. * @return array[log_table]
  65. * @return array[log_id]
  66. */
  67. public function lockChangeAccountRemain($user_id, $other_user_id = 0,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$is_pay_get=0,$request_id='')
  68. {
  69. //初始化
  70. $result = array(
  71. 'status'=>false,
  72. 'code' => 0, //报错时候采用
  73. 'msg'=>'',
  74. 'log_table' => '',
  75. 'log_id' => '',
  76. );
  77. //获取小数点
  78. $point = in_array($accountType,['money']) ? 2 : 0;
  79. bcscale($point);
  80. //钱包名称
  81. $wallet_name = $this->getwalletname($accountType);
  82. //检测
  83. $number = floatval( $number );
  84. if( $number == 0 )
  85. {
  86. $result['msg'] = '交易金额:0';
  87. return $result;
  88. }
  89. if(0 === bccomp($number, 0)){
  90. $result['msg'] = '交易金额:0';
  91. return $result;
  92. }
  93. //检测
  94. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id'=>$user_id])->find();
  95. if(!$wallet)
  96. {
  97. $result['msg'] = '不存在的用户';
  98. return $result;
  99. }
  100. if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
  101. {
  102. $result['code'] = 10;
  103. $result['msg'] = $wallet_name.'余额不足!';
  104. return $result;
  105. }
  106. if(0 !== bccomp($number, 0))
  107. {
  108. $log_table = 'user_'.$accountType.'_log';
  109. //查询是否有消费记录
  110. $user_log_info = [];
  111. if ($request_id) {
  112. $user_log_info = Db::name($log_table)->where(['user_id' => $user_id, 'log_type' => $logtype, 'request_id' => $request_id])->find();
  113. }
  114. if ($user_log_info) {
  115. //修改钱币记录
  116. $data = array();
  117. $data['change_value'] = bcadd($user_log_info['change_value'], $number);
  118. $data['remain'] = bcadd($wallet[$accountType], $number);
  119. $data['remark'] = $remark;
  120. $data['createtime'] = time();
  121. $data['updatetime'] = time();
  122. /////////////
  123. $rs2_id = Db::name($log_table)->where(['id' => $user_log_info['id'], 'createtime' => $user_log_info['createtime']])->setField($data);
  124. } else {
  125. //添加钱币记录
  126. $data = array();
  127. $data['user_id'] = $user_id;
  128. $data['other_user_id'] = $other_user_id;
  129. $data['log_type'] = $logtype;
  130. $data['before'] = $wallet[$accountType];
  131. $data['change_value'] = $number;
  132. $data['remain'] = bcadd($wallet[$accountType], $number);
  133. $data['table'] = $table;
  134. $data['table_id'] = $table_id;
  135. $data['remark'] = $remark;
  136. $data['createtime'] = time();
  137. $data['updatetime'] = time();
  138. $data['request_id'] = $request_id;
  139. /////////////
  140. $rs2_id = Db::name($log_table)->insertGetId($data);
  141. }
  142. //新的方式
  143. $wallet_data[$accountType] = $data['remain'];
  144. if ($is_pay_get == 1) { //累计消费:后台充值updatemoney,支付充值goldpaysucc
  145. $wallet_data['pay_money'] = bcadd($wallet['pay_money'], bcdiv($number, config('site.rmb_to_gold'), 2)); //pay_money第1处修改
  146. } elseif ($is_pay_get == 2) { //累计获得:聊天礼物得收益,动态礼物得收益,私聊+语音+视频得收益,被守护得收益
  147. $wallet_data['get_money'] = bcadd($wallet['get_money'], $number); //get_money第1处修改,也是唯一一处
  148. }
  149. $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->update($wallet_data);
  150. if($rs1 === false || $rs2_id === false){
  151. $result['msg'] = '更新财务记录失败!';
  152. return $result;
  153. }
  154. if( $rs1 !== false && $rs2_id !== false )
  155. {
  156. $result['status'] = true;
  157. $result['msg'] = '账户余额已更新!';
  158. $result['log_table'] = $accountType.'_log';
  159. $result['log_id'] = $rs2_id;
  160. return $result;
  161. }
  162. else
  163. {
  164. $result['msg'] = '更新财务记录失败!';
  165. return $result;
  166. }
  167. } else {
  168. $result['msg'] = '金额不足0.01';
  169. return $result;
  170. }
  171. }
  172. }