Withdrawal.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller\trade;
  3. use app\admin\model\User;
  4. use app\api\model\MoneyRecord;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 提现记录管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Withdrawal extends Backend
  15. {
  16. /**
  17. * Withdrawal模型对象
  18. * @var \app\admin\model\trade\Withdrawal
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\trade\Withdrawal;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model->alias('record')
  47. ->field('record.*')
  48. ->field('user.nickname,user.avatar')
  49. ->join('user user', 'user.id = record.user_id')
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit)
  53. ->each(function ($item) {
  54. $item->avatar = $item->avatar ? cdnurl($item->avatar, true) : letter_avatar($item->nickname);
  55. });
  56. $totalMoney = $this->model->alias('record')->join('user user', 'user.id = record.user_id')->where($where)->sum('amount');
  57. // 清空对应角标
  58. $filter = json_decode(input('filter'), true);
  59. if ($filter && !$list->isEmpty()) {
  60. \app\admin\model\trade\Withdrawal::where('backend_read', 0)->where('status', $list[0]->status)->update(['backend_read' => 1]);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items(), 'totalmoney' => $totalMoney);
  63. return json($result);
  64. }
  65. // 渲染角标数字
  66. $sidebar = [
  67. 'all' => \app\admin\model\trade\Withdrawal::where('backend_read', 0)->count(),
  68. 'review' => \app\admin\model\trade\Withdrawal::where('backend_read', 0)->where('status', 'review')->count(),
  69. 'success' => \app\admin\model\trade\Withdrawal::where('backend_read', 0)->where('status', 'success')->count(),
  70. 'reject' => \app\admin\model\trade\Withdrawal::where('backend_read', 0)->where('status', 'reject')->count(),
  71. ];
  72. $this->assignconfig('sidebar_number', $sidebar);
  73. $this->assign('sidebar_number', $sidebar);
  74. return $this->view->fetch();
  75. }
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. $adminIds = $this->getDataLimitAdminIds();
  83. if (is_array($adminIds)) {
  84. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  85. $this->error(__('You have no permission'));
  86. }
  87. }
  88. if ($this->request->isPost()) {
  89. $params = $this->request->post("row/a");
  90. if ($params) {
  91. $params = $this->preExcludeFields($params);
  92. if ('review' != $row->status) {
  93. $this->error('进制修改已审核的申请');
  94. }
  95. // 驳回申请,则驳回原因不能为空
  96. if ('reject' == $params['status'] && empty($params['reason'])) {
  97. $this->error('驳回原因不能为空');
  98. }
  99. $result = false;
  100. Db::startTrans();
  101. try {
  102. //是否采用模型验证
  103. if ($this->modelValidate) {
  104. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  105. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  106. $row->validateFailException(true)->validate($validate);
  107. }
  108. // 驳回申请,则增加用户余额
  109. if ('reject' == $params['status']) {
  110. $user = User::where('id', $row->user_id)->find();
  111. $money_before = $user->retail_balance;
  112. $row->drawal_type? $user->setInc('retail_balance', $row->amount) : $user->setInc('money', $row->amount);
  113. // // 创建余额记录
  114. // MoneyRecord::create([
  115. // 'user_id' => $row->user_id,
  116. // 'before' => $money_before,
  117. // 'after' => $user->money,
  118. // 'money' => $row->amount,
  119. // 'type' => 'withdrawal_fail', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  120. // ]);
  121. }
  122. $result = $row->allowField(true)->save($params);
  123. Db::commit();
  124. } catch (ValidateException $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. } catch (PDOException $e) {
  128. Db::rollback();
  129. $this->error($e->getMessage());
  130. } catch (\Exception $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. }
  134. if ($result !== false) {
  135. $this->success();
  136. } else {
  137. $this->error(__('No rows were updated'));
  138. }
  139. }
  140. $this->error(__('Parameter %s can not be empty', ''));
  141. }
  142. $this->view->assign("row", $row);
  143. return $this->view->fetch();
  144. }
  145. public function multi($ids = "")
  146. {
  147. if (!$this->request->isPost()) {
  148. $this->error(__("Invalid parameters"));
  149. }
  150. $ids = $ids ? $ids : $this->request->post("ids");
  151. if ($ids) {
  152. if ($this->request->has('params')) {
  153. parse_str($this->request->post("params"), $values);
  154. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  155. if ($values) {
  156. $adminIds = $this->getDataLimitAdminIds();
  157. if (is_array($adminIds)) {
  158. $this->model->where($this->dataLimitField, 'in', $adminIds);
  159. }
  160. $count = 0;
  161. Db::startTrans();
  162. try {
  163. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  164. foreach ($list as $index => $item) {
  165. if ('review' != $item->status) {
  166. throw new \Exception('不能修改已审核的申请');
  167. }
  168. // 驳回申请,则增加用户余额
  169. if (isset($values['status']) && 'reject' == $values['status']) {
  170. $user = User::where('id', $item->user_id)->find();
  171. $money_before = $user->money;
  172. $user->setInc('money', $item->amount);
  173. // 创建余额记录
  174. MoneyRecord::create([
  175. 'user_id' => $item->user_id,
  176. 'before' => $money_before,
  177. 'after' => $user->money,
  178. 'money' => $item->amount,
  179. 'type' => 'withdrawal_fail', // 变更类型:box_exchange=盲盒回收,refund=库存不足,支付返回,withdrawal=提现,to_coin=转到钱包,withdrawal_fail=提现失败
  180. ]);
  181. }
  182. $count += $item->allowField(true)->isUpdate(true)->save($values);
  183. }
  184. Db::commit();
  185. } catch (PDOException $e) {
  186. Db::rollback();
  187. $this->error($e->getMessage());
  188. } catch (\Exception $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. }
  192. if ($count) {
  193. $this->success();
  194. } else {
  195. $this->error(__('No rows were updated'));
  196. }
  197. } else {
  198. $this->error(__('You have no permission'));
  199. }
  200. }
  201. }
  202. $this->error(__('Parameter %s can not be empty', 'ids'));
  203. }
  204. }