Wallet.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. public function getWallettotal($user_id = '')
  50. {
  51. //所有钱包余额
  52. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
  53. if(!$wallet) {
  54. abort(500,'钱包余额获取失败');
  55. }
  56. return bcadd($wallet['gold'],$wallet['jewel'],1);
  57. }
  58. /**
  59. *
  60. * @param floatval $number 金额(正数进账,负数出账)
  61. * @param $accountType 货币类型,money,score
  62. * @param $logtype 日志的类型
  63. * @param $remark 备注
  64. * @param $user_id 用户id
  65. * @param $table 来源表
  66. * @param $data_id 表id
  67. * @param $isAdmin 是否是管理员处理
  68. * @return array
  69. * @return array[status]
  70. * @return array[msg]
  71. * @return array[log_table]
  72. * @return array[log_id]
  73. */
  74. public function lockChangeAccountRemain($user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  75. {
  76. //初始化
  77. $result = array(
  78. 'status'=>false,
  79. 'msg'=>'',
  80. 'log_table' => '',
  81. 'log_id' => '',
  82. );
  83. //获取小数点
  84. $point = $accountType == 'money' ? 2 : 0;
  85. bcscale($point);
  86. //钱包名称
  87. $wallet_name = $this->getwalletname($accountType);
  88. //检测
  89. $number = floatval( $number );
  90. if( $number == 0 )
  91. {
  92. $result['msg'] = '交易金额:0';
  93. return $result;
  94. }
  95. if(0 === bccomp($number, 0)){
  96. $result['msg'] = '交易金额:0';
  97. return $result;
  98. }
  99. //检测
  100. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id'=>$user_id])->find();
  101. if(!$wallet)
  102. {
  103. $result['msg'] = '不存在的用户';
  104. return $result;
  105. }
  106. if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
  107. {
  108. $result['msg'] = $wallet_name.'余额不足!';
  109. return $result;
  110. }
  111. else
  112. {
  113. if(0 !== bccomp($number, 0))
  114. {
  115. //钱币记录
  116. $data = array();
  117. $data['user_id'] = $user_id;
  118. $data['log_type'] = $logtype;
  119. $data['money_type'] = $accountType;
  120. $data['before'] = $wallet[$accountType];
  121. $data['change_value'] = $number;
  122. $data['remain'] = bcadd($wallet[$accountType], $number);
  123. $data['table'] = $table;
  124. $data['table_id'] = $table_id;
  125. $data['remark'] = $remark;
  126. $data['createtime'] = time();
  127. $data['updatetime'] = time();
  128. //新的方式
  129. $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->update([$accountType => $data['remain']]);
  130. /////////////
  131. $log_table = 'user_gold_log';
  132. $rs2_id = Db::name($log_table)->insertGetId($data);
  133. if($rs1 === false || $rs2_id === false){
  134. $result['msg'] = '更新财务记录失败!';
  135. return $result;
  136. }
  137. if( $rs1 !== false && $rs2_id !== false )
  138. {
  139. $result['status'] = true;
  140. $result['msg'] = '账户余额已更新!';
  141. $result['log_table'] = $log_table;
  142. $result['log_id'] = $rs2_id;
  143. return $result;
  144. }
  145. else
  146. {
  147. $result['msg'] = '更新财务记录失败!';
  148. return $result;
  149. }
  150. } else {
  151. $result['msg'] = '金额不足0.01';
  152. return $result;
  153. }
  154. }
  155. }
  156. }