Withdraw.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace app\admin\controller\reward;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\helper\StringHelper;
  6. use think\Controller;
  7. use think\Log;
  8. use think\Request;
  9. /**
  10. * 赏金提现管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Withdraw extends Backend
  15. {
  16. /**
  17. * RewardWithdraw模型对象
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('RewardWithdraw');
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 打款管理列表
  28. */
  29. public function index()
  30. {
  31. $this->assign('group', $this->group);
  32. $this->assignconfig('group', $this->group);
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags']);
  35. $where = [];
  36. if ($this->request->isAjax()) {
  37. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  38. // Log::write($where,'congtest');
  39. }
  40. if ($this->group == 3) {
  41. $channels = model('Admin')
  42. ->join('auth_group_access a', 'a.uid= admin.id')
  43. ->join('admin_extend d', 'admin.id = d.admin_id')
  44. ->where('a.group_id', 'eq', 4)
  45. ->where('d.create_by', 'eq', $this->auth->id)
  46. ->field('admin.id,admin.nickname,d.pay_method')
  47. ->select();
  48. $cNames = [];
  49. foreach ($channels as $channel) {
  50. $cNames[$channel->id] = ['nickname' => $channel->nickname, 'dkfs' => $channel->pay_method];
  51. }
  52. $daili_id = implode(',', array_keys($cNames));
  53. /*
  54. * 统计,sum三个字段
  55. */
  56. //提现中
  57. $state1 = $this->model
  58. ->join('auth_group_access g', 'g.uid= reward_withdraw.admin_id')
  59. ->join('admin', 'reward_withdraw.admin_id = admin.id')
  60. ->where('reward_withdraw.admin_id', 'in', $daili_id)
  61. ->where($where)
  62. ->where('reward_withdraw.status', '0')
  63. ->sum('money');
  64. //打款中
  65. $state2 = $this->model
  66. ->join('auth_group_access g', 'g.uid= reward_withdraw.admin_id')
  67. ->join('admin', 'reward_withdraw.admin_id = admin.id')
  68. ->where('reward_withdraw.admin_id', 'in', $daili_id)
  69. ->where($where)
  70. ->where('reward_withdraw.status', '1')
  71. ->sum('money');
  72. //已打款
  73. $state3 = $this->model
  74. ->join('auth_group_access g', 'g.uid= reward_withdraw.admin_id')
  75. ->join('admin', 'reward_withdraw.admin_id = admin.id')
  76. ->where('reward_withdraw.admin_id', 'in', $daili_id)
  77. ->where($where)
  78. ->where('reward_withdraw.status', '2')
  79. ->sum('money');
  80. $this->assignconfig('state1', $state1);
  81. $this->assignconfig('state2', $state2);
  82. $this->assignconfig('state3', $state3);
  83. } else {
  84. $this->assignconfig('state1', 0);
  85. $this->assignconfig('state2', 0);
  86. $this->assignconfig('state3', 0);
  87. }
  88. if ($this->request->isAjax()) {
  89. //如果发送的来源是Selectpage,则转发到Selectpage
  90. if ($this->request->request('pkey_name')) {
  91. return $this->selectpage();
  92. }
  93. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  94. $sort = 'reward_withdraw.id';
  95. if ($this->group == 3) { //渠道商
  96. //获取渠道商下面的代理商列表
  97. $channels = model('Admin')
  98. ->join('auth_group_access a', 'a.uid= admin.id')
  99. ->join('admin_extend d', 'admin.id = d.admin_id')
  100. ->where('a.group_id', 'eq', 4)
  101. ->where('d.create_by', 'eq', $this->auth->id)
  102. ->field('admin.id,admin.nickname,d.pay_method')
  103. ->select();
  104. $cNames = [];
  105. foreach ($channels as $channel) {
  106. $cNames[$channel->id] = ['nickname' => $channel->nickname, 'dkfs' => $channel->pay_method];
  107. }
  108. $daili_id = implode(',', array_keys($cNames));
  109. $total = $this->model
  110. ->join('auth_group_access g', 'g.uid= reward_withdraw.admin_id')
  111. ->join('admin', 'reward_withdraw.admin_id = admin.id')
  112. ->where('reward_withdraw.admin_id', 'in', $daili_id)
  113. ->where($where)
  114. ->order($sort, $order)
  115. ->count();
  116. $list = $this->model
  117. ->join('auth_group_access g', 'g.uid= reward_withdraw.admin_id')
  118. ->join('admin', 'reward_withdraw.admin_id = admin.id')
  119. ->field('reward_withdraw.*,admin.id admin_id')
  120. ->where('reward_withdraw.admin_id', 'in', $daili_id)
  121. ->where($where)
  122. ->order('reward_withdraw.status asc,reward_withdraw.id desc')
  123. ->limit($offset, $limit)
  124. ->select();
  125. foreach ($list as $key => $val) {
  126. $list[$key]['admin.nickname'] = empty($cNames[$val->admin_id]['nickname']) ? '' : $cNames[$val->admin_id]['nickname'];
  127. $list[$key]['dkfs'] = empty($cNames[$val->admin_id]['dkfs']) ? '' : $cNames[$val->admin_id]['dkfs'];
  128. $list[$key]['reward_withdraw.createtime'] = $val['createtime'];
  129. }
  130. $result = array("total" => $total, "rows" => $list, "others" => array('state1' => $state1, 'state2' => $state2, 'state3' => $state3));
  131. } else { //无权查看数据的用户组
  132. $result = array("total" => 0, "rows" => []);
  133. }
  134. return json($result);
  135. }
  136. return $this->view->fetch();
  137. }
  138. public function add()
  139. {
  140. $t = time();
  141. $start_time = strtotime(date('Y-m-d 23:30:00'));
  142. $end_time = strtotime(date('Y-m-d 01:30:00'));
  143. if ($t >= $start_time || $t <= $end_time) {
  144. $this->error('每天00:00-01:30与23:30-24:00,不支持提现');
  145. }
  146. $isIdCard = 0;
  147. $reward_money = model('RewardMoney')->where('admin_id', 'eq', $this->auth->id)->find();
  148. $this->assign('reward_money', $reward_money);
  149. $no_cash_money = 0;
  150. if ($this->group == 4) {
  151. //代理赏金
  152. $yesterday_reward_collect = model('reward_collect')
  153. ->where('invite_id', 'eq', $this->auth->id)
  154. ->where('type', 'eq', '1')
  155. ->where('flag', 'eq', '1')
  156. ->where('createdate', 'lt', date('Ymd'))
  157. ->where('createdate', 'neq', 'null')
  158. ->field('sum(reward) as reward')
  159. ->find();
  160. // Log::write(model('reward_collect')->getLastSql(),'111111');
  161. $this->assign('reward_all', $yesterday_reward_collect['reward']??0);
  162. $no_cash_money = ($yesterday_reward_collect['reward']??0) - $reward_money['cash_money'] - $reward_money['count_cash_money'];
  163. $extends=model('AdminExtend')->where(['admin_id'=>$this->auth->id])->find();
  164. $cb = $extends->create_by;
  165. $fAdmin = model('AdminExtend')->where(['admin_id'=>$cb])->find();
  166. $isIdCard = empty($fAdmin->is_idcard)?0:$fAdmin->is_idcard;
  167. if(!empty($extends->idcard_no)){
  168. $isIdCard = 0;
  169. }
  170. }
  171. $no_cash_money = StringHelper::moneyFormat($no_cash_money);
  172. if ($this->request->isPost()) {
  173. $timedate = date('Ymd',time());
  174. $withdrawKey = 'SJ:'.$this->auth->id.':'.$timedate;
  175. $redis = Redis::instance();
  176. if($redis->exists($withdrawKey)){
  177. $this->error('提现过于频繁,请十分钟以后再试');
  178. }
  179. if(empty($this->request->post('idcard_no'))) {
  180. $row = $this->request->post('row/a');
  181. if ($row) {
  182. $money = $row['money'];
  183. if ($money < 100) {
  184. $this->error('最低提现金额100元');
  185. }
  186. //未提现余额 严格校验
  187. if ($no_cash_money != $money) {
  188. $this->error('ERROR:提现金额与计算后的提现金额不一致');
  189. }
  190. if ($reward_money['no_cash_money'] < $money) {
  191. $this->error('超出最大可提现金额' . $reward_money['no_cash_money']);
  192. }
  193. $save['etime'] = date('Ymd', strtotime('-1 day'));
  194. $stime = model('reward_withdraw')->where('admin_id', $this->auth->id)->field('etime')->order('id desc')->find();
  195. if (empty($stime['etime'])) {
  196. $save['stime'] = null;
  197. } else {
  198. $save['stime'] = date('Ymd', strtotime($stime['etime']) + 3600 * 24);
  199. }
  200. //$this->error($save['stime']);
  201. $save['admin_id'] = $this->auth->id;
  202. $save['money'] = $money;//含手续费
  203. $save['status'] = 0;
  204. if (model('reward_withdraw')->save($save)) {
  205. model('RewardMoney')->where('admin_id', $this->auth->id)->setInc('cash_money', $money);
  206. model('RewardMoney')->where('admin_id', $this->auth->id)->setDec('no_cash_money', $money);
  207. $redis->setex($withdrawKey,600,'1'); //redis存10分钟,避免重复提现
  208. $this->success('提现成功');
  209. }
  210. $this->error('提交申请失败');
  211. }
  212. }else{
  213. $idCardNo = trim($this->request->post('idcard_no'));
  214. $flag = validateIDCard($idCardNo);
  215. if($flag){
  216. model('AdminExtend')->update(['idcard_no'=>$idCardNo],['admin_id'=>$this->auth->id]);
  217. $key = 'AE:'.$this->auth->id;
  218. $redis->del($key);
  219. $this->success();
  220. }else{
  221. $this->error('身份证号码不合法');
  222. }
  223. }
  224. $this->error('错误');
  225. }
  226. $this->assign('isIdcard',$isIdCard);
  227. $this->assign('group', $this->group);
  228. $this->assignconfig('group', $this->group);
  229. $this->view->assign('no_cash_money', $no_cash_money ? $no_cash_money : 0);
  230. $this->assignconfig('no_cash_money', $no_cash_money ? $no_cash_money : 0);
  231. return $this->view->fetch();
  232. }
  233. /*
  234. * 我的结算单
  235. */
  236. public function my()
  237. {
  238. if ($this->request->isAjax()) {
  239. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  240. $total = $this->model
  241. ->where('reward_withdraw.admin_id', 'eq', $this->auth->id)
  242. ->where($where)
  243. ->order($sort, $order)
  244. ->count();
  245. $list = $this->model
  246. ->where('reward_withdraw.admin_id', 'eq', $this->auth->id)
  247. ->where($where)
  248. ->order('reward_withdraw.status asc,reward_withdraw.id desc')
  249. ->limit($offset, $limit)
  250. ->select();
  251. $result = array("total" => $total, "rows" => $list);
  252. return json($result);
  253. }
  254. $reward_money = model('RewardMoney')->where('admin_id', 'eq', $this->auth->id)->find();
  255. $this->assign('reward_money', $reward_money);
  256. if ($this->group == 4) {
  257. //代理赏金 按天累加赏金金额
  258. $yesterday_reward_collect = model('reward_collect')
  259. ->where('invite_id', 'eq', $this->auth->id)
  260. ->where('type', 'eq', '1')
  261. ->where('flag', 'eq', '1')
  262. ->where('createdate', 'lt', date('Ymd'))
  263. ->where('createdate', 'neq', 'null')
  264. ->field('sum(reward) as reward')
  265. ->find();
  266. // Log::write(model('reward_collect')->getLastSql(),'111111');
  267. $this->assign('reward_all', $yesterday_reward_collect['reward']??0);
  268. $this->assign('no_cash_money', ($yesterday_reward_collect['reward']??0) - $reward_money['cash_money'] - $reward_money['count_cash_money']);
  269. $today_reward_collect = model('reward_collect')
  270. ->where('invite_id', 'eq', $this->auth->id)
  271. ->where('type', 'eq', '1')
  272. ->where('flag', 'eq', '1')
  273. ->where('createdate', 'eq', date('Ymd'))
  274. ->field('sum(reward) as reward')
  275. ->find();
  276. // Log::write(model('reward_collect')->getLastSql(),'111111');
  277. $this->assign('today_reward_all', $today_reward_collect['reward']??0);
  278. }
  279. return $this->view->fetch();
  280. }
  281. public function edit($ids = NULL)
  282. {
  283. $row = $this->model->get($ids);
  284. if (!$row) {
  285. $this->error(__('No Results were found'));
  286. }
  287. $adminIds = $this->getDataLimitAdminIds();
  288. if (is_array($adminIds)) {
  289. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  290. $this->error(__('You have no permission'));
  291. }
  292. }
  293. if ($this->request->isPost()) {
  294. $params = $this->request->post("row/a");
  295. if ($params) {
  296. try {
  297. //是否采用模型验证
  298. if ($this->modelValidate) {
  299. $name = basename(str_replace('\\', '/', get_class($this->model)));
  300. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  301. $row->validate($validate);
  302. }
  303. if ($row['status'] == 2) {
  304. $this->error('已打款 请勿重复处理');
  305. }
  306. if (empty($params['status'])) {
  307. $this->error('请选择一种打款状态');
  308. }
  309. $result = $row->allowField(true)->save($params, ['id' => $params['id']]);
  310. if ($result !== false) {
  311. $withdraw = $this->model->get($params['id']);
  312. if ($params['status'] == 2) {
  313. //提现中的减少
  314. $admin_update1 = model('reward_money')->where('admin_id', $withdraw['admin_id'])->setDec('cash_money', $withdraw['money']);
  315. //已打款的增加
  316. $admin_update2 = model('reward_money')->where('admin_id', $withdraw['admin_id'])->setInc('count_cash_money', $withdraw['money']);
  317. //更新完成时间
  318. $time = time();
  319. $updateTime = model('reward_withdraw')->where('id', $params['id'])->update(['remit_time' => $time]);
  320. }
  321. $this->success();
  322. } else {
  323. $this->error($row->getError());
  324. }
  325. } catch (\think\exception\PDOException $e) {
  326. $this->error($e->getMessage());
  327. }
  328. }
  329. $this->error(__('Parameter %s can not be empty', ''));
  330. }
  331. $admin = model("Admin")->where('id', $row['admin_id'])->find();
  332. if ($admin) {
  333. $row['admin_nickname'] = $admin['nickname'];
  334. } else {
  335. $row['admin_nickname'] = '';
  336. }
  337. $ae = model('AdminExtend')->where('admin_id', $row['admin_id'])->find();
  338. $this->view->assign('ae', $ae);
  339. $this->view->assign("row", $row);
  340. $dingdan = [];
  341. if ($row['stime'] && $row['etime']) {
  342. $auth = model('AuthGroupAccess')->where('uid', $row['admin_id'])->find();
  343. if ($auth->group_id == 3) {
  344. //渠道
  345. $dingdan = model('Rewardcollect')
  346. ->where('invite_id', 'eq',$row['admin_id'])
  347. ->where('createtime', 'egt', $row['stime'])
  348. ->where('createtime', 'elt', $row['etime'])
  349. ->where('type', 'eq', '1')
  350. ->where('flag', 'eq', '1')
  351. ->field('id,createtime,reward,admin_id as aid')
  352. ->select();
  353. Log::write(model('Rewardcollect')->getLastSql(), '11111');
  354. }
  355. }
  356. // Log::write(model('Rewardcollect')->getLastSql(),'11111');
  357. $this->view->assign('dingdan', $dingdan);
  358. return $this->view->fetch();
  359. }
  360. }