Walletcompany.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. /**
  6. * 货币模型
  7. */
  8. class Walletcompany 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('company_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 $table 来源表
  57. * @param $data_id 表id
  58. * @param $isAdmin 是否是管理员处理
  59. * @return array
  60. * @return array[status]
  61. * @return array[msg]
  62. * @return array[log_table]
  63. * @return array[log_id]
  64. */
  65. public function lockChangeAccountRemain($user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
  66. {
  67. //初始化
  68. $result = array(
  69. 'status'=>false,
  70. 'code' => 0, //报错时候采用
  71. 'msg'=>'',
  72. 'log_table' => '',
  73. 'log_id' => '',
  74. );
  75. //获取小数点
  76. $point = $accountType == 'money' ? 2 : 0;
  77. bcscale($point);
  78. //钱包名称
  79. $wallet_name = $this->getwalletname($accountType);
  80. //检测
  81. $number = floatval( $number );
  82. if( $number == 0 )
  83. {
  84. $result['msg'] = '交易金额:0';
  85. return $result;
  86. }
  87. if(0 === bccomp($number, 0)){
  88. $result['msg'] = '交易金额:0';
  89. return $result;
  90. }
  91. //检测
  92. $wallet = Db::name('company_wallet')->lock(true)->where(['user_id'=>$user_id])->find();
  93. if(!$wallet)
  94. {
  95. $result['msg'] = '不存在的用户';
  96. return $result;
  97. }
  98. if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
  99. {
  100. $result['code'] = 10;
  101. $result['msg'] = $wallet_name.'余额不足!';
  102. return $result;
  103. }
  104. if(0 !== bccomp($number, 0))
  105. {
  106. //钱币记录
  107. $data = array();
  108. $data['user_id'] = $user_id;
  109. $data['log_type'] = $logtype;
  110. $data['before'] = $wallet[$accountType];
  111. $data['change_value'] = $number;
  112. $data['remain'] = bcadd($wallet[$accountType], $number);
  113. $data['table'] = $table;
  114. $data['table_id'] = $table_id;
  115. $data['remark'] = $remark;
  116. $data['createtime'] = time();
  117. $data['updatetime'] = time();
  118. //新的方式
  119. $rs1 = Db::name('company_wallet')->where(['user_id'=>$user_id])->update([$accountType => $data['remain']]);
  120. /////////////
  121. $log_table = 'company_'.$accountType.'_log';
  122. $rs2_id = Db::name($log_table)->insertGetId($data);
  123. if($rs1 === false || $rs2_id === false){
  124. $result['msg'] = '更新财务记录失败!';
  125. return $result;
  126. }
  127. if( $rs1 !== false && $rs2_id !== false )
  128. {
  129. $result['status'] = true;
  130. $result['msg'] = '账户余额已更新!';
  131. $result['log_table'] = $accountType.'_log';
  132. $result['log_id'] = $rs2_id;
  133. return $result;
  134. }
  135. else
  136. {
  137. $result['msg'] = '更新财务记录失败!';
  138. return $result;
  139. }
  140. } else {
  141. $result['msg'] = '金额不足0.01';
  142. return $result;
  143. }
  144. }
  145. }