Cash.php 6.7 KB

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