Agent.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\Agent as AgentModel;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\commission\Log as LogModel;
  7. use app\common\model\commission\Level as LevelModel;
  8. use app\common\Service\Commission\Agent as AgentService;
  9. use think\Db;
  10. class Agent extends Backend
  11. {
  12. protected $noNeedRight = ['select'];
  13. protected $model = null;
  14. /**
  15. * 快速搜索时执行查找的字段
  16. */
  17. protected $searchFields = 'user_id,user.nickname,user.mobile';
  18. /**
  19. * 是否是关联查询
  20. */
  21. protected $relationSearch = true;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new AgentModel();
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. if (!$this->request->isAjax()) {
  33. return $this->view->fetch();
  34. }
  35. //如果发送的来源是 Selectpage,则转发到 Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  40. // 处理特殊筛选条件
  41. $filter = $this->request->get("filter", '');
  42. if ($filter) {
  43. $filter = json_decode($filter, true);
  44. if (isset($filter['tabActive'])) {
  45. if ($filter['tabActive'] === 'pending') {
  46. $where['status'] = 'pending';
  47. } elseif ($filter['tabActive'] === '0') {
  48. $where['level_status'] = ['>', 0];
  49. }
  50. }
  51. }
  52. $list = $this->model
  53. ->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 详情
  62. *
  63. * @param $id
  64. */
  65. public function detail($ids = null)
  66. {
  67. $row = $this->model->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
  68. ->where('user_id', $ids)
  69. ->find();
  70. if (!$row) {
  71. $this->error(__('No Results were found'));
  72. }
  73. $this->view->assign('row', $row);
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 团队
  78. *
  79. * @param $id
  80. */
  81. public function team($ids = null)
  82. {
  83. if (!$this->request->isAjax()) {
  84. return $this->view->fetch();
  85. }
  86. $detail = $this->model->with(['user.parent_user', 'level_info'])->where('user_id', $ids)->find();
  87. if (!$detail) {
  88. $this->error(__('No Results were found'));
  89. }
  90. $detail->agent_team = AgentModel::hasWhere('user', function ($query) use ($detail) {
  91. return $query->where('parent_user_id', $detail->user_id);
  92. })->with(['user', 'level_info'])->select();
  93. $this->success('分销商详情', null, $detail);
  94. }
  95. // 选择分销商
  96. public function select()
  97. {
  98. if (!$this->request->isAjax()) {
  99. return $this->view->fetch();
  100. }
  101. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  102. $data = $this->model
  103. ->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])
  104. ->where($where)
  105. ->order($sort, $order)
  106. ->paginate($limit);
  107. $result = ['total' => $data->total(), 'rows' => $data->items()];
  108. return json($result);
  109. }
  110. /**
  111. * 编辑
  112. *
  113. * @param $id
  114. */
  115. public function edit($id = null)
  116. {
  117. $params = $this->request->only(['status', 'upgrade_lock', 'level_status', 'level']);
  118. $result = Db::transaction(function () use ($id, $params) {
  119. $row = $this->model->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  120. if (!$row) {
  121. $this->error('未找到该分销商');
  122. }
  123. foreach ($params as $field => $value) {
  124. switch ($field) {
  125. case 'status': // 修改状态
  126. return $this->changeStatus($row, $value);
  127. break;
  128. case 'level_status': // 审核等级
  129. return $this->changeLevelStatus($row, $value);
  130. break;
  131. case 'level': // 修改等级
  132. return $this->changeLevel($row, $value);
  133. break;
  134. default:
  135. return $row->save([$field => $value]);
  136. }
  137. }
  138. });
  139. if ($result) {
  140. $this->success('更新成功', null, $result);
  141. } else {
  142. $this->error('更新失败');
  143. }
  144. }
  145. // 修改状态
  146. private function changeStatus($row, $value)
  147. {
  148. $result = $row->save(['status' => $value]);
  149. if ($result) {
  150. LogModel::add($row->user_id, 'agent', ['type' => 'status', 'value' => $value]);
  151. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  152. }
  153. return $result;
  154. }
  155. // 审核等级
  156. private function changeLevelStatus($row, $value)
  157. {
  158. if ($row->level_status == 0 && $value > 0) {
  159. $this->error('非法操作');
  160. }
  161. if ($value == 0) { // 拒绝操作
  162. return $row->save(['level_status' => 0]);
  163. } else { // 同意操作
  164. if ($row->upgrade_level) {
  165. $result = $row->save(['level_status' => 0, 'level' => $row->upgrade_level->level]);
  166. if ($result) {
  167. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $row->upgrade_level]);
  168. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  169. }
  170. return $result;
  171. }
  172. }
  173. return false;
  174. }
  175. // 修改等级
  176. private function changeLevel($row, $value)
  177. {
  178. $level = LevelModel::find($value);
  179. if ($level) {
  180. $result = $row->save(['level' => $level->level]);
  181. if ($result) {
  182. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $level]);
  183. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  184. }
  185. return $result;
  186. } else {
  187. $this->error('未找到该等级');
  188. }
  189. }
  190. // 更换推荐人
  191. public function changeParentUser($id)
  192. {
  193. $userAgent = new AgentService($id);
  194. if (!$userAgent->user) {
  195. $this->error('未找到该用户');
  196. }
  197. $parentUserId = $this->request->param('parent_user_id', 0);
  198. // 更换推荐人检查
  199. if ($parentUserId != 0) {
  200. $parentAgent = new AgentService($parentUserId);
  201. if (!$parentAgent->isAgentAvaliable()) {
  202. $this->error('选中用户暂未成为分销商,不能成为推荐人');
  203. }
  204. if (!$this->checkChangeParentAgent($id, $parentUserId)) {
  205. $this->error('不能绑定该上级');
  206. }
  207. LogModel::add($parentUserId, 'share', ['user' => $userAgent->user]);
  208. if ($userAgent->isAgentAvaliable()) {
  209. LogModel::add($id, 'bind', ['user' => $parentAgent->user ?? NULL]);
  210. }
  211. }
  212. $lastParentUserId = $userAgent->user->parent_user_id;
  213. $userAgent->user->parent_user_id = $parentUserId;
  214. $userAgent->user->save();
  215. if ($lastParentUserId > 0) {
  216. $userAgent->createAsyncAgentUpgrade($lastParentUserId);
  217. }
  218. if ($parentUserId > 0) {
  219. $userAgent->createAsyncAgentUpgrade($parentUserId);
  220. }
  221. $this->success('绑定成功');
  222. }
  223. // 递归往上找推荐人,防止出现推荐循环
  224. private function checkChangeParentAgent($userId, $parentUserId)
  225. {
  226. if ($userId == $parentUserId) {
  227. $this->error('推荐人不能是本人');
  228. }
  229. if ($parentUserId == 0) {
  230. return true;
  231. }
  232. $parentAgent = UserModel::find($parentUserId);
  233. if ($parentAgent) {
  234. if ($parentAgent->parent_user_id == $userId) {
  235. $this->error("已选中分销商的上级团队中已存在该用户");
  236. }
  237. if ($parentAgent->parent_user_id == 0) {
  238. return true;
  239. } else {
  240. return $this->checkChangeParentAgent($userId, $parentAgent->parent_user_id);
  241. }
  242. }
  243. return false;
  244. }
  245. }