Rebatewithdraw.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 返利提现管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Rebatewithdraw extends Backend
  14. {
  15. protected $searchFields = 'user.u_id,user.nickname';
  16. /**
  17. * Rebatewithdraw模型对象
  18. * @var \app\admin\model\user\Rebatewithdraw
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\user\Rebatewithdraw;
  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. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //当前是否为关联查询
  42. $this->relationSearch = true;
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model
  52. ->with(['user'])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->getRelation('user')->visible(['u_id', 'nickname']);
  58. }
  59. $totalMoney = $this->model->with(['user'])->where($where)->sum('rebatewithdraw.money');
  60. $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['total_money' => $totalMoney]);
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = null)
  69. {
  70. $row = $this->model->get($ids);
  71. if (!$row) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $adminIds = $this->getDataLimitAdminIds();
  75. if (is_array($adminIds)) {
  76. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. }
  80. if ($this->request->isPost()) {
  81. $params = $this->request->post("row/a");
  82. if ($params) {
  83. $params = $this->preExcludeFields($params);
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. if ($this->modelValidate) {
  89. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  90. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  91. $row->validateFailException(true)->validate($validate);
  92. }
  93. $result = $row->allowField(true)->save($params);
  94. if ($params['status'] == 2) {
  95. // 插入声币记录
  96. $userModel = new \app\common\model\User();
  97. $usersoundcoinlogModel = new \app\common\model\UserSoundcoinLog();
  98. $userInfo = $userModel->where('id', $row['user_id'])->find();
  99. $adjustmentValue = $row['money'] * 100;
  100. $usersoundcoinlogModel->addUserSoundcoinLog($row['user_id'], $adjustmentValue, "+", $userInfo["sound_coin"], "返利提现到声币", 5, $row['id']);
  101. // 调整用户声币余额
  102. $userModel->where(["id" => $row['user_id']])->update([
  103. 'sound_coin' => Db::raw("sound_coin+{$adjustmentValue}"),
  104. ]);
  105. }
  106. Db::commit();
  107. } catch (ValidateException $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. } catch (PDOException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (Exception $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. }
  117. if ($result !== false) {
  118. $this->success();
  119. } else {
  120. $this->error(__('No rows were updated'));
  121. }
  122. }
  123. $this->error(__('Parameter %s can not be empty', ''));
  124. }
  125. $this->view->assign("row", $row);
  126. return $this->view->fetch();
  127. }
  128. /**
  129. * 批量更新
  130. */
  131. public function multi($ids = "")
  132. {
  133. if (!$this->request->isPost()) {
  134. $this->error(__("Invalid parameters"));
  135. }
  136. $ids = $ids ? $ids : $this->request->post("ids");
  137. if ($ids) {
  138. if ($this->request->has('params')) {
  139. parse_str($this->request->post("params"), $values);
  140. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  141. if ($values) {
  142. $adminIds = $this->getDataLimitAdminIds();
  143. if (is_array($adminIds)) {
  144. $this->model->where($this->dataLimitField, 'in', $adminIds);
  145. }
  146. $count = 0;
  147. Db::startTrans();
  148. try {
  149. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  150. foreach ($list as $index => $item) {
  151. $count += $item->allowField(true)->isUpdate(true)->save($values);
  152. // 批量审核通过
  153. if ($values['status'] == 2) {
  154. // 插入声币记录
  155. $userModel = new \app\common\model\User();
  156. $usersoundcoinlogModel = new \app\common\model\UserSoundcoinLog();
  157. $userInfo = $userModel->where('id', $item->user_id)->find();
  158. $adjustmentValue = $item['money'] * 100;
  159. $usersoundcoinlogModel->addUserSoundcoinLog($item->user_id, $adjustmentValue, "+", $userInfo->sound_coin, "返利提现到声币", 5, $item->id);
  160. // 调整用户声币余额
  161. $userModel->where(["id" => $item->user_id])->update([
  162. 'sound_coin' => Db::raw("sound_coin+{$adjustmentValue}"),
  163. ]);
  164. }
  165. }
  166. Db::commit();
  167. } catch (PDOException $e) {
  168. Db::rollback();
  169. $this->error($e->getMessage());
  170. } catch (Exception $e) {
  171. Db::rollback();
  172. $this->error($e->getMessage());
  173. }
  174. if ($count) {
  175. $this->success();
  176. } else {
  177. $this->error(__('No rows were updated'));
  178. }
  179. } else {
  180. $this->error(__('You have no permission'));
  181. }
  182. }
  183. }
  184. $this->error(__('Parameter %s can not be empty', 'ids'));
  185. }
  186. }