Cash.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class Cash extends Base {
  5. // 设置数据表(不含前缀)
  6. protected $name = 'cash';
  7. // 定义时间戳字段名
  8. protected $createTime = '';
  9. protected $updateTime = '';
  10. // 自动完成
  11. protected $auto = [];
  12. protected $insert = [];
  13. protected $update = [];
  14. public function listData($where,$order,$page=1,$limit=20,$start=0)
  15. {
  16. if(!is_array($where)){
  17. $where = json_decode($where,true);
  18. }
  19. $limit_str = ($limit * ($page-1) + $start) .",".$limit;
  20. $total = $this->where($where)->count();
  21. $list = Db::name('Cash')->where($where)->order($order)->limit($limit_str)->select();
  22. $user_ids=[];
  23. foreach($list as $k=>&$v){
  24. if($v['user_id'] >0){
  25. $user_ids[$v['user_id']] = $v['user_id'];
  26. }
  27. }
  28. if(!empty($user_ids)){
  29. $where2=[];
  30. $where['user_id'] = ['in', $user_ids];
  31. $order='user_id desc';
  32. $user_list = model('User')->listData($where2,$order,1,999);
  33. $user_list = mac_array_rekey($user_list['list'],'user_id');
  34. foreach($list as $k=>&$v){
  35. $list[$k]['user_name'] = $user_list[$v['user_id']]['user_name'];
  36. }
  37. }
  38. return ['code'=>1,'msg'=>lang('data_list'),'page'=>$page,'pagecount'=>ceil($total/$limit),'limit'=>$limit,'total'=>$total,'list'=>$list];
  39. }
  40. public function infoData($where,$field='*')
  41. {
  42. if(empty($where) || !is_array($where)){
  43. return ['code'=>1001,'msg'=>lang('param_err')];
  44. }
  45. $info = $this->field($field)->where($where)->find();
  46. if(empty($info)){
  47. return ['code'=>1002,'msg'=>lang('obtain_err')];
  48. }
  49. $info = $info->toArray();
  50. return ['code'=>1,'msg'=>lang('obtain_ok'),'info'=>$info];
  51. }
  52. public function saveData($param)
  53. {
  54. $data=[];
  55. $data['cash_money'] = floatval($param['cash_money']);
  56. if($GLOBALS['config']['user']['cash_status'] !='1'){
  57. return ['code'=>1005,'msg'=>lang('model/cash/not_open')];
  58. }
  59. if($data['cash_money'] < $GLOBALS['config']['user']['cash_min']){
  60. return ['code'=>1006,'msg'=>lang('model/cash/min_money_err').':'.$GLOBALS['config']['user']['cash_min'] ];
  61. }
  62. $tx_points = intval($data['cash_money'] * $GLOBALS['config']['user']['cash_ratio']);
  63. if($tx_points > $GLOBALS['user']['user_points']){
  64. return ['code'=>1007,'msg'=>lang('model/cash/mush_money_err')];
  65. }
  66. $data['user_id'] = $GLOBALS['user']['user_id'];
  67. $data['cash_bank_name'] = htmlspecialchars(urldecode(trim($param['cash_bank_name'])));
  68. $data['cash_bank_no'] = htmlspecialchars(urldecode(trim($param['cash_bank_no'])));
  69. $data['cash_payee_name'] = htmlspecialchars(urldecode(trim($param['cash_payee_name'])));
  70. $data['cash_points'] = $tx_points;
  71. $data['cash_time'] = time();
  72. $validate = \think\Loader::validate('Cash');
  73. if(!$validate->check($data)){
  74. return ['code'=>1001,'msg'=>lang('param_err').':'.$validate->getError() ];
  75. }
  76. if($data['user_id']==0 ) {
  77. return ['code'=>1002,'msg'=>lang('param_err')];
  78. }
  79. $res = $this->allowField(true)->insert($data);
  80. if(false === $res){
  81. return ['code'=>1004,'msg'=>lang('save_err').':'.$this->getError() ];
  82. }
  83. //更新用户表
  84. $update=[];
  85. $update['user_points'] = $GLOBALS['user']['user_points'] - $tx_points;
  86. $update['user_points_froze'] = $GLOBALS['user']['user_points_froze'] + $tx_points;
  87. $where=[];
  88. $where['user_id'] = $GLOBALS['user']['user_id'];
  89. $res = model('user')->where($where)->update($update);
  90. if(false === $res){
  91. return ['code'=>1005,'msg'=>'更新用户积分失败:'.$this->getError() ];
  92. }
  93. return ['code'=>1,'msg'=>lang('save_ok')];
  94. }
  95. public function delData($where)
  96. {
  97. $list = $this->where($where)->select();
  98. foreach($list as $k=>$v){
  99. $where=[];
  100. $where['cash_id'] = $v['cash_id'];
  101. $res = $this->where($where)->delete();
  102. if($res===false){
  103. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  104. }
  105. //如果未审核则恢复冻结积分
  106. if($v['cash_status'] ==0){
  107. $where=[];
  108. $where['user_id'] = $v['user_id'];
  109. $user = model('User')->where($where)->find();
  110. $update=[];
  111. $update['user_points'] = $user['user_points'] + $v['cash_points'];
  112. $update['user_points_froze'] = $user['user_points_froze'] - $v['cash_points'];
  113. $res = model('user')->where($where)->update($update);
  114. if(false === $res){
  115. return ['code'=>1005,'msg'=>'更新用户积分失败:'.$this->getError() ];
  116. }
  117. }
  118. }
  119. return ['code'=>1,'msg'=>lang('del_ok')];
  120. }
  121. public function fieldData($where,$col,$val)
  122. {
  123. if(!isset($col) || !isset($val)){
  124. return ['code'=>1001,'msg'=>lang('param_err')];
  125. }
  126. $data = [];
  127. $data[$col] = $val;
  128. $res = $this->allowField(true)->where($where)->update($data);
  129. if($res===false){
  130. return ['code'=>1001,'msg'=>lang('set_err').':'.$this->getError() ];
  131. }
  132. return ['code'=>1,'msg'=>lang('set_ok')];
  133. }
  134. public function auditData($where)
  135. {
  136. $list = $this->where($where)->select();
  137. foreach($list as $k=>$v){
  138. $where2=[];
  139. $where2['user_id'] = $v['user_id'];
  140. $update=[];
  141. $update['cash_status'] = 1;
  142. $update['cash_time_audit'] = time();
  143. $res = model('Cash')->where($where)->update($update);
  144. if($res===false){
  145. return ['code'=>1001,'msg'=>lang('del_err').':'.$this->getError() ];
  146. }
  147. $res = model('User')->where($where2)->setDec('user_points_froze', $v['cash_points']);
  148. if(false === $res){
  149. return ['code'=>1005,'msg'=>'更新用户积分失败:'.$this->getError() ];
  150. }
  151. }
  152. return ['code'=>1,'msg'=>'审核成功'];
  153. }
  154. }