Apply.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\Apply as ApplyModel;
  5. use app\common\model\commission\Identity as IdentityModel;
  6. use app\common\Service\Commission\AgentApply as AgentApplyService;
  7. use think\Db;
  8. use think\Exception;
  9. class Apply extends Backend
  10. {
  11. protected $model = null;
  12. protected $searchFields = 'id,user.nickname,real_name,company_name,status,agent_type,identity.name';
  13. protected $relationSearch = true;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new ApplyModel();
  18. }
  19. /**
  20. * 查看
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. if ($this->request->request('keyField')) {
  28. return $this->selectpage();
  29. }
  30. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  31. $list = $this->model
  32. ->with(['user', 'identity', 'province', 'city', 'district', 'admin'])
  33. ->where($where)
  34. ->order($sort, $order)
  35. ->paginate($limit);
  36. // 标识是否是重新审核的申请
  37. $rows = $list->items();
  38. // 使用IN查询一次性获取所有相关代理商数据,避免N+1查询问题
  39. $userIds = array_column($rows, 'user_id');
  40. $existAgents = [];
  41. if (!empty($userIds)) {
  42. $existAgents = \app\common\model\commission\Agent::where('user_id', 'in', $userIds)
  43. ->column('user_id', 'user_id');
  44. }
  45. foreach ($rows as $row) {
  46. // 检查用户是否已经是代理商
  47. $isExist = isset($existAgents[$row['user_id']]);
  48. $row['is_reaudit'] = $isExist;
  49. $row['is_reaudit_text'] = $isExist ? '重新审核' : '首次申请';
  50. }
  51. $result = ['total' => $list->total(), 'rows' => $rows];
  52. return json($result);
  53. }
  54. /**
  55. * 详情
  56. */
  57. public function detail($ids = null)
  58. {
  59. $row = $this->model::with(['user', 'identity', 'province', 'city', 'district', 'admin'])
  60. ->where('id', $ids)
  61. ->find();
  62. if (!$row) {
  63. $this->error(__('No Results were found'));
  64. }
  65. $this->view->assign('row', $row);
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 审核通过
  70. */
  71. public function approve($ids = null)
  72. {
  73. if (!$this->request->isPost()) {
  74. $this->error(__('Invalid parameters'));
  75. }
  76. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. if ($row->status != ApplyModel::STATUS_PENDING) {
  81. $this->error('该申请已经处理过了');
  82. }
  83. // 检查是否是重新审核(用户已经是代理商)
  84. $existAgent = \app\common\model\commission\Agent::where('user_id', $row->user_id)->find();
  85. $isReaudit = $existAgent ? true : false;
  86. Db::transaction(function () use ($row) {
  87. $service = new AgentApplyService();
  88. $service->approveApply($row, $this->auth->id);
  89. });
  90. if ($isReaudit) {
  91. $this->success('重新审核通过成功,代理商资料已更新');
  92. } else {
  93. $this->success('审核通过成功,代理商已创建');
  94. }
  95. }
  96. /**
  97. * 审核拒绝
  98. */
  99. public function reject($ids = null)
  100. {
  101. if (!$this->request->isPost()) {
  102. $this->error(__('Invalid parameters'));
  103. }
  104. $row = $this->model->get($ids);
  105. if (!$row) {
  106. $this->error(__('No Results were found'));
  107. }
  108. if ($row->status != ApplyModel::STATUS_PENDING) {
  109. $this->error('该申请已经处理过了');
  110. }
  111. $reason = $this->request->post('reason', '');
  112. if (empty($reason)) {
  113. $this->error('请填写拒绝原因');
  114. }
  115. $service = new AgentApplyService();
  116. $service->rejectApply($row, $reason, $this->auth->id);
  117. $this->success('审核拒绝成功');
  118. }
  119. /**
  120. * 删除
  121. */
  122. public function del($ids = null)
  123. {
  124. if (!$this->request->isPost()) {
  125. $this->error(__('Invalid parameters'));
  126. }
  127. if (!$ids) {
  128. $this->error(__('Parameter %s can not be empty', 'ids'));
  129. }
  130. $ids = explode(',', $ids);
  131. $deleteCount = 0;
  132. Db::transaction(function () use ($ids, &$deleteCount) {
  133. foreach ($ids as $id) {
  134. $row = $this->model->get($id);
  135. if ($row) {
  136. // 只允许删除待审核和已拒绝的申请
  137. //if (in_array($row->status, [ApplyModel::STATUS_PENDING, ApplyModel::STATUS_REJECTED])) {
  138. $row->delete();
  139. $deleteCount++;
  140. //}
  141. }
  142. }
  143. });
  144. if ($deleteCount > 0) {
  145. $this->success("成功删除 {$deleteCount} 条申请记录");
  146. } else {
  147. $this->error('没有可删除的记录(只能删除待审核和已拒绝的申请)');
  148. }
  149. }
  150. /**
  151. * 批量审核通过
  152. */
  153. public function multi($ids = null)
  154. {
  155. if (!$this->request->isPost()) {
  156. $this->error(__('Invalid parameters'));
  157. }
  158. $action = $this->request->post('action');
  159. if ($action == 'approve') {
  160. // 批量审核通过
  161. if (!$ids) {
  162. $this->error(__('Parameter %s can not be empty', 'ids'));
  163. }
  164. $ids = explode(',', $ids);
  165. $service = new AgentApplyService();
  166. $successCount = 0;
  167. $reauditCount = 0;
  168. Db::transaction(function () use ($ids, $service, &$successCount, &$reauditCount) {
  169. // 获取所有待审核的申请记录
  170. $rows = $this->model->where('id', 'in', $ids)
  171. ->where('status', ApplyModel::STATUS_PENDING)
  172. ->select();
  173. if (!$rows->isEmpty()) {
  174. // 使用IN查询一次性获取所有相关代理商数据,避免N+1查询问题
  175. $userIds = $rows->column('user_id');
  176. $existAgents = [];
  177. if (!empty($userIds)) {
  178. $existAgents = \app\common\model\commission\Agent::where('user_id', 'in', $userIds)
  179. ->column('user_id', 'user_id');
  180. }
  181. foreach ($rows as $row) {
  182. // 检查是否是重新审核
  183. if (isset($existAgents[$row->user_id])) {
  184. $reauditCount++;
  185. } else {
  186. $successCount++;
  187. }
  188. $service->approveApply($row, $this->auth->id);
  189. }
  190. }
  191. });
  192. $message = "批量审核完成";
  193. if ($successCount > 0) {
  194. $message .= ",新创建代理商 {$successCount} 个";
  195. }
  196. if ($reauditCount > 0) {
  197. $message .= ",重新审核通过 {$reauditCount} 个";
  198. }
  199. $this->success($message);
  200. } elseif ($action == 'del') {
  201. // 批量删除
  202. return $this->del($ids);
  203. } else {
  204. return parent::multi($ids);
  205. }
  206. }
  207. }