Wallet.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. abort(500,'钱包余额获取失败');
  44. }
  45. if($wallet_name) { //返回指定钱包
  46. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  47. } else { //返回所有钱包
  48. return $wallet;
  49. }
  50. }
  51. /**
  52. *
  53. * @param floatval $number 金额(正数进账,负数出账)
  54. * @param $accountType 货币类型,money,score
  55. * @param $logtype 日志的类型
  56. * @param $remark 备注
  57. * @param $user_id 用户id
  58. * @param $table 来源表
  59. * @param $data_id 表id
  60. * @param $isAdmin 是否是管理员处理
  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($company_id,$user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  68. {
  69. //初始化
  70. $result = array(
  71. 'status'=>false,
  72. 'code' => 0, //报错时候采用
  73. 'msg'=>'',
  74. 'log_table' => '',
  75. 'log_id' => '',
  76. );
  77. //获取小数点
  78. $point = $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')->where(['user_id' => $user_id,'company_id' => $company_id])->lock(true)->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. //钱币记录
  109. $data = array();
  110. $data['company_id'] = $company_id;
  111. $data['user_id'] = $user_id;
  112. $data['log_type'] = $logtype;
  113. $data['before'] = $wallet[$accountType];
  114. $data['change_value'] = $number;
  115. $data['remain'] = bcadd($wallet[$accountType], $number);
  116. $data['table'] = $table;
  117. $data['table_id'] = $table_id;
  118. $data['remark'] = $remark;
  119. $data['createtime'] = time();
  120. $data['updatetime'] = time();
  121. //新的方式
  122. $rs1 = Db::name('user_wallet')->where(['user_id' => $user_id,'company_id' => $company_id])->update([$accountType => $data['remain']]);
  123. /////////////
  124. $log_table = 'user_'.$accountType.'_log';
  125. $rs2_id = Db::name($log_table)->insertGetId($data);
  126. if($rs1 === false || $rs2_id === false){
  127. $result['msg'] = '更新财务记录失败!';
  128. return $result;
  129. }
  130. if( $rs1 !== false && $rs2_id !== false )
  131. {
  132. $result['status'] = true;
  133. $result['msg'] = '账户余额已更新!';
  134. $result['log_table'] = $accountType.'_log';
  135. $result['log_id'] = $rs2_id;
  136. return $result;
  137. }
  138. else
  139. {
  140. $result['msg'] = '更新财务记录失败!';
  141. return $result;
  142. }
  143. } else {
  144. $result['msg'] = '金额不足0.01';
  145. return $result;
  146. }
  147. }
  148. }