Agent.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\commission\Agent as AgentModel;
  5. use app\admin\model\shopro\user\User as UserModel;
  6. use app\admin\model\shopro\commission\Log as LogModel;
  7. use app\admin\model\shopro\commission\Level as LevelModel;
  8. use addons\shopro\service\commission\Agent as AgentService;
  9. use think\Db;
  10. class Agent extends Common
  11. {
  12. protected $noNeedRight = ['select'];
  13. protected $model = null;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new AgentModel();
  18. }
  19. /**
  20. * 查看
  21. */
  22. public function index()
  23. {
  24. if (!$this->request->isAjax()) {
  25. return $this->view->fetch();
  26. }
  27. $list = $this->model->sheepFilter()->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])->paginate($this->request->param('list_rows', 10));
  28. $this->success('分销商列表', null, $list);
  29. }
  30. /**
  31. * 详情
  32. *
  33. * @param $id
  34. */
  35. public function detail($id)
  36. {
  37. if (!$this->request->isAjax()) {
  38. return $this->view->fetch();
  39. }
  40. $detail = $this->model->with(['user.parent_user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  41. if (!$detail) {
  42. $this->error(__('No Results were found'));
  43. }
  44. $this->success('分销商详情', null, $detail);
  45. }
  46. /**
  47. * 团队
  48. *
  49. * @param $id
  50. */
  51. public function team($id)
  52. {
  53. if (!$this->request->isAjax()) {
  54. return $this->view->fetch();
  55. }
  56. $detail = $this->model->with(['user.parent_user', 'level_info'])->where('user_id', $id)->find();
  57. if (!$detail) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $detail->agent_team = AgentModel::hasWhere('user', function ($query) use ($detail) {
  61. return $query->where('parent_user_id', $detail->user_id);
  62. })->with(['user', 'level_info'])->select();
  63. $this->success('分销商详情', null, $detail);
  64. }
  65. // 选择分销商
  66. public function select()
  67. {
  68. if (!$this->request->isAjax()) {
  69. return $this->view->fetch();
  70. }
  71. $data = $this->model->sheepFilter()->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])
  72. ->paginate($this->request->param('list_rows', 10));
  73. $this->success('选择分销商', null, $data);
  74. }
  75. /**
  76. * 编辑
  77. *
  78. * @param $id
  79. */
  80. public function edit($id = null)
  81. {
  82. $params = $this->request->only(['status', 'upgrade_lock', 'level_status', 'level', 'apply_info']);
  83. $result = Db::transaction(function () use ($id, $params) {
  84. $row = $this->model->with(['user', 'level_info', 'level_status_info', 'upgrade_level'])->where('user_id', $id)->find();
  85. if (!$row) {
  86. $this->error('未找到该分销商');
  87. }
  88. foreach ($params as $field => $value) {
  89. switch ($field) {
  90. case 'status': // 修改状态
  91. return $this->changeStatus($row, $value);
  92. break;
  93. case 'level_status': // 审核等级
  94. return $this->changeLevelStatus($row, $value);
  95. break;
  96. case 'level': // 修改等级
  97. return $this->changeLevel($row, $value);
  98. break;
  99. default:
  100. return $row->save([$field => $value]);
  101. }
  102. }
  103. });
  104. if ($result) {
  105. $this->success('更新成功', null, $result);
  106. } else {
  107. $this->error('更新失败');
  108. }
  109. }
  110. // 修改状态
  111. private function changeStatus($row, $value)
  112. {
  113. $result = $row->save(['status' => $value]);
  114. if ($result) {
  115. LogModel::add($row->user_id, 'agent', ['type' => 'status', 'value' => $value]);
  116. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  117. }
  118. return $result;
  119. }
  120. // 审核等级
  121. private function changeLevelStatus($row, $value)
  122. {
  123. if ($row->level_status == 0 && $value > 0) {
  124. $this->error('非法操作');
  125. }
  126. if ($value == 0) { // 拒绝操作
  127. return $row->save(['level_status' => 0]);
  128. } else { // 同意操作
  129. if ($row->upgrade_level) {
  130. $result = $row->save(['level_status' => 0, 'level' => $row->upgrade_level->level]);
  131. if ($result) {
  132. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $row->upgrade_level]);
  133. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  134. }
  135. return $result;
  136. }
  137. }
  138. return false;
  139. }
  140. // 修改等级
  141. private function changeLevel($row, $value)
  142. {
  143. $level = LevelModel::find($value);
  144. if ($level) {
  145. $result = $row->save(['level' => $level->level]);
  146. if ($result) {
  147. LogModel::add($row->user_id, 'agent', ['type' => 'level', 'level' => $level]);
  148. (new AgentService($row->user_id))->createAsyncAgentUpgrade();
  149. }
  150. return $result;
  151. } else {
  152. $this->error('未找到该等级');
  153. }
  154. }
  155. // 更换推荐人
  156. public function changeParentUser($id)
  157. {
  158. $userAgent = new AgentService($id);
  159. if (!$userAgent->user) {
  160. $this->error('未找到该用户');
  161. }
  162. $parentUserId = $this->request->param('parent_user_id', 0);
  163. // 更换推荐人检查
  164. if ($parentUserId != 0) {
  165. $parentAgent = new AgentService($parentUserId);
  166. if (!$parentAgent->isAgentAvaliable()) {
  167. $this->error('选中用户暂未成为分销商,不能成为推荐人');
  168. }
  169. if (!$this->checkChangeParentAgent($id, $parentUserId)) {
  170. $this->error('不能绑定该上级');
  171. }
  172. LogModel::add($parentUserId, 'share', ['user' => $userAgent->user]);
  173. if ($userAgent->isAgentAvaliable()) {
  174. LogModel::add($id, 'bind', ['user' => $parentAgent->user ?? NULL]);
  175. }
  176. }
  177. $lastParentUserId = $userAgent->user->parent_user_id;
  178. $userAgent->user->parent_user_id = $parentUserId;
  179. $userAgent->user->save();
  180. if ($lastParentUserId > 0) {
  181. $userAgent->createAsyncAgentUpgrade($lastParentUserId);
  182. }
  183. if ($parentUserId > 0) {
  184. $userAgent->createAsyncAgentUpgrade($parentUserId);
  185. }
  186. $this->success('绑定成功');
  187. }
  188. // 递归往上找推荐人,防止出现推荐循环
  189. private function checkChangeParentAgent($userId, $parentUserId)
  190. {
  191. if ($userId == $parentUserId) {
  192. $this->error('推荐人不能是本人');
  193. }
  194. if ($parentUserId == 0) {
  195. return true;
  196. }
  197. $parentAgent = UserModel::find($parentUserId);
  198. if ($parentAgent) {
  199. if ($parentAgent->parent_user_id == $userId) {
  200. $this->error("已选中分销商的上级团队中已存在该用户");
  201. }
  202. if ($parentAgent->parent_user_id == 0) {
  203. return true;
  204. } else {
  205. return $this->checkChangeParentAgent($userId, $parentAgent->parent_user_id);
  206. }
  207. }
  208. return false;
  209. }
  210. }