Agent.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $list = $this->model
  41. ->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])
  42. ->where($where)
  43. ->order($sort, $order)
  44. ->paginate($limit);
  45. $result = ['total' => $list->total(), 'rows' => $list->items()];
  46. return json($result);
  47. }
  48. /**
  49. * 详情
  50. *
  51. * @param $id
  52. */
  53. public function detail($id)
  54. {
  55. if (!$this->request->isAjax()) {
  56. return $this->view->fetch();
  57. }
  58. $detail = $this->model->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  59. if (!$detail) {
  60. $this->error(__('No Results were found'));
  61. }
  62. $this->success('分销商详情', null, $detail);
  63. }
  64. /**
  65. * 团队
  66. *
  67. * @param $id
  68. */
  69. public function team($id)
  70. {
  71. if (!$this->request->isAjax()) {
  72. return $this->view->fetch();
  73. }
  74. $detail = $this->model->with(['user.parent_user', 'level_info'])->where('user_id', $id)->find();
  75. if (!$detail) {
  76. $this->error(__('No Results were found'));
  77. }
  78. $detail->agent_team = AgentModel::hasWhere('user', function ($query) use ($detail) {
  79. return $query->where('parent_user_id', $detail->user_id);
  80. })->with(['user', 'level_info'])->select();
  81. $this->success('分销商详情', null, $detail);
  82. }
  83. // 选择分销商
  84. public function select()
  85. {
  86. if (!$this->request->isAjax()) {
  87. return $this->view->fetch();
  88. }
  89. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  90. $data = $this->model
  91. ->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])
  92. ->where($where)
  93. ->order($sort, $order)
  94. ->paginate($limit);
  95. $result = ['total' => $data->total(), 'rows' => $data->items()];
  96. return json($result);
  97. }
  98. /**
  99. * 编辑
  100. *
  101. * @param $id
  102. */
  103. public function edit($id = null)
  104. {
  105. $params = $this->request->only(['status', 'upgrade_lock', 'level_status', 'level']);
  106. $result = Db::transaction(function () use ($id, $params) {
  107. $row = $this->model->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  108. if (!$row) {
  109. $this->error('未找到该分销商');
  110. }
  111. foreach ($params as $field => $value) {
  112. switch ($field) {
  113. case 'status': // 修改状态
  114. return $this->changeStatus($row, $value);
  115. break;
  116. case 'level_status': // 审核等级
  117. return $this->changeLevelStatus($row, $value);
  118. break;
  119. case 'level': // 修改等级
  120. return $this->changeLevel($row, $value);
  121. break;
  122. default:
  123. return $row->save([$field => $value]);
  124. }
  125. }
  126. });
  127. if ($result) {
  128. $this->success('更新成功', null, $result);
  129. } else {
  130. $this->error('更新失败');
  131. }
  132. }
  133. // 修改状态
  134. private function changeStatus($row, $value)
  135. {
  136. $result = $row->save(['status' => $value]);
  137. if ($result) {
  138. LogModel::add($row->user_id, 'agent', ['type' => 'status', 'value' => $value]);
  139. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  140. }
  141. return $result;
  142. }
  143. // 审核等级
  144. private function changeLevelStatus($row, $value)
  145. {
  146. if ($row->level_status == 0 && $value > 0) {
  147. $this->error('非法操作');
  148. }
  149. if ($value == 0) { // 拒绝操作
  150. return $row->save(['level_status' => 0]);
  151. } else { // 同意操作
  152. if ($row->upgrade_level) {
  153. $result = $row->save(['level_status' => 0, 'level' => $row->upgrade_level->level]);
  154. if ($result) {
  155. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $row->upgrade_level]);
  156. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  157. }
  158. return $result;
  159. }
  160. }
  161. return false;
  162. }
  163. // 修改等级
  164. private function changeLevel($row, $value)
  165. {
  166. $level = LevelModel::find($value);
  167. if ($level) {
  168. $result = $row->save(['level' => $level->level]);
  169. if ($result) {
  170. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $level]);
  171. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  172. }
  173. return $result;
  174. } else {
  175. $this->error('未找到该等级');
  176. }
  177. }
  178. // 更换推荐人
  179. public function changeParentUser($id)
  180. {
  181. $userAgent = new AgentService($id);
  182. if (!$userAgent->user) {
  183. $this->error('未找到该用户');
  184. }
  185. $parentUserId = $this->request->param('parent_user_id', 0);
  186. // 更换推荐人检查
  187. if ($parentUserId != 0) {
  188. $parentAgent = new AgentService($parentUserId);
  189. if (!$parentAgent->isAgentAvaliable()) {
  190. $this->error('选中用户暂未成为分销商,不能成为推荐人');
  191. }
  192. if (!$this->checkChangeParentAgent($id, $parentUserId)) {
  193. $this->error('不能绑定该上级');
  194. }
  195. LogModel::add($parentUserId, 'share', ['user' => $userAgent->user]);
  196. if ($userAgent->isAgentAvaliable()) {
  197. LogModel::add($id, 'bind', ['user' => $parentAgent->user ?? NULL]);
  198. }
  199. }
  200. $lastParentUserId = $userAgent->user->parent_user_id;
  201. $userAgent->user->parent_user_id = $parentUserId;
  202. $userAgent->user->save();
  203. if ($lastParentUserId > 0) {
  204. $userAgent->createAsyncAgentUpgrade($lastParentUserId);
  205. }
  206. if ($parentUserId > 0) {
  207. $userAgent->createAsyncAgentUpgrade($parentUserId);
  208. }
  209. $this->success('绑定成功');
  210. }
  211. // 递归往上找推荐人,防止出现推荐循环
  212. private function checkChangeParentAgent($userId, $parentUserId)
  213. {
  214. if ($userId == $parentUserId) {
  215. $this->error('推荐人不能是本人');
  216. }
  217. if ($parentUserId == 0) {
  218. return true;
  219. }
  220. $parentAgent = UserModel::find($parentUserId);
  221. if ($parentAgent) {
  222. if ($parentAgent->parent_user_id == $userId) {
  223. $this->error("已选中分销商的上级团队中已存在该用户");
  224. }
  225. if ($parentAgent->parent_user_id == 0) {
  226. return true;
  227. } else {
  228. return $this->checkChangeParentAgent($userId, $parentAgent->parent_user_id);
  229. }
  230. }
  231. return false;
  232. }
  233. }