Agent.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use think\Db;
  4. use app\admin\controller\shopro\Base;
  5. use addons\shopro\library\commission\Log as LogLibrary;
  6. use addons\shopro\exception\Exception;
  7. /**
  8. * 分销商管理
  9. *
  10. */
  11. class Agent extends Base
  12. {
  13. // protected $relationSearch = true;
  14. /**
  15. * @var \app\admin\model\shopro\commission\Agent
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\shopro\commission\Agent;
  22. $this->userModel = new \app\admin\model\shopro\user\User;
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. //设置过滤方法
  30. $this->request->filter(['strip_tags']);
  31. if ($this->request->isAjax()) {
  32. $nobuildfields = ['nickname', 'mobile'];
  33. list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
  34. $total = $this->buildSearch()
  35. ->where($where)
  36. ->order($sort, $order)
  37. ->count();
  38. $list = $this->buildSearch()
  39. ->with(['user' => function ($query) {
  40. return $query->withField('id, avatar, nickname, mobile, child_user_count, child_user_count_1, total_consume');
  41. }, 'agent_level' => function ($query) {
  42. return $query->withField('image, name, level, commission_rules');
  43. }, 'parent_agent' => function ($query) {
  44. return $query->withField('id, avatar, nickname');
  45. }])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->limit($offset, $limit)
  49. ->select();
  50. $result = array("total" => $total, "rows" => $list);
  51. $this->success('查看分销商', null, $result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 查看团队
  57. */
  58. public function team()
  59. {
  60. $this->request->filter(['strip_tags']);
  61. if ($this->request->isAjax()) {
  62. $filter = json_decode($this->request->get("filter", ''), true);
  63. $parent_user_id = $filter && isset($filter['parent_user_id']) ? $filter['parent_user_id'] : 0;
  64. $nobuildfields = ['level'];
  65. list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
  66. $sort = $sort == 'user_id' ? 'id' : $sort; // 这里用的是 userModel , sort 会自动取 this->model 的主键(user_id)
  67. $total = $this->buildUserSearch()
  68. ->where($where)
  69. ->order($sort, $order)
  70. ->count();
  71. $list = $this->buildUserSearch()
  72. ->with(['agent.agentLevel'])
  73. ->where($where)
  74. ->order($sort, $order)
  75. ->limit($offset, $limit)
  76. ->select();
  77. $parentUser = $this->userModel->with(['agent.agentLevel'])->where('id', $parent_user_id)->find();
  78. $result = array("total" => $total, "rows" => $list, 'parent_user' => $parentUser);
  79. $this->success('查看分销商', null, $result);
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 分销商详情
  85. */
  86. public function profile($id)
  87. {
  88. $row = $this->model->with(['user' => function ($query) {
  89. return $query->withField('id, avatar, nickname, mobile, child_user_count, child_user_count_1, child_user_count_2, total_consume');
  90. }, 'agent_level' => function ($query) {
  91. return $query->withField('image, name, level, commission_rules');
  92. }, 'parent_agent' => function ($query) {
  93. return $query->withField('id, avatar, nickname');
  94. }])->where('user_id', $id)->find();
  95. if (!$row) {
  96. $this->error('未找到分销商');
  97. }
  98. if ($this->request->isAjax()) {
  99. $this->success('分销商详情', null, $row);
  100. }
  101. $this->assignconfig('row', $row);
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 更新信息
  106. */
  107. public function update($id)
  108. {
  109. $agent = new \addons\shopro\library\commission\Agent($id);
  110. if (!$agent->agent) {
  111. $this->error('未找到分销商');
  112. }
  113. $key = $this->request->post('key');
  114. $value = $this->request->post('value');
  115. $data = null;
  116. $runUpgradeAgent = true; // 升级当前分销商
  117. $runUpgradeLastAgentId = 0; // 升级原分销商
  118. $result = Db::transaction(function () use ($agent, $key, $value, &$data, &$runUpgradeAgent, &$runUpgradeLastAgentId) {
  119. switch ($key) {
  120. case 'status': // 变更分销商状态
  121. $data = $value;
  122. if ($value === 'normal' && $agent->user->parent_user_id === 0 && $agent->agent->parent_agent_id !== 0) {
  123. $agent->user->parent_user_id = $agent->agent->parent_agent_id;
  124. $agent->user->save();
  125. \addons\shopro\library\commission\Log::write($agent->agent->parent_agent_id, 'share', ['user' => $agent->user], ['oper_type' => 'user', 'oper_id' => $agent->user->id]);
  126. }
  127. break;
  128. case 'level': // 变更分销商等级
  129. $data = \app\admin\model\shopro\commission\Level::get($value);
  130. if (!$data) {
  131. new Exception('未找到该等级');
  132. }
  133. break;
  134. case 'level_status': // 审核分销商升级
  135. if ($agent->agent->level_status == 0) {
  136. new Exception('该分销商没有审核升级条件');
  137. } else {
  138. $level = \app\admin\model\shopro\commission\Level::get(['level' => $agent->agent->level_status]);
  139. if ($value != 0) {
  140. if ($level) {
  141. $data['level'] = $level;
  142. $data['action'] = 'agree';
  143. $agent->agent->level = $level->level;
  144. $agent->agent->save();
  145. } else {
  146. new Exception('该等级不存在');
  147. }
  148. } else {
  149. $data['action'] = 'reject';
  150. }
  151. $value = 0;
  152. }
  153. break;
  154. case 'parent_agent_id': // 变更分销商上级
  155. $data = null;
  156. if ($agent->agent->parent_agent_id == $value) {
  157. new Exception('请勿重复选择');
  158. }
  159. if ($agent->agent->user_id == $value) {
  160. new Exception('不能绑定本人');
  161. }
  162. if ($value != 0) {
  163. $data = \app\admin\model\shopro\user\User::get($value);
  164. if (!$data) {
  165. new Exception('未找到该分销商');
  166. }
  167. if (!$this->checkChangeParentAgent($agent->agent->user_id, $value)) {
  168. new Exception('不能绑定该分销商');
  169. }
  170. }
  171. $runUpgradeLastAgentId = $agent->agent->parent_agent_id;
  172. $agent->user->parent_user_id = $value;
  173. $agent->user->save();
  174. break;
  175. case 'upgrade_lock':
  176. case 'apply_info':
  177. case 'info_status':
  178. if ($value === '') {
  179. $value = null;
  180. }
  181. $data = $value;
  182. $runUpgradeAgent = false;
  183. break;
  184. }
  185. return $agent->agent->allowField('status,level,apply_info,parent_agent_id,upgrade_lock,info_status,level_status')->save([
  186. $key => $value
  187. ]);
  188. });
  189. if ($result) {
  190. if ($runUpgradeAgent) {
  191. $agent = new \addons\shopro\library\commission\Agent($agent->agent->user_id);
  192. $agent->asyncAgentUpgrade($agent->agent->user_id);
  193. if ($runUpgradeLastAgentId) {
  194. $agent->asyncAgentUpgrade($runUpgradeLastAgentId);
  195. }
  196. }
  197. LogLibrary::write($agent->agent->user_id, 'agent', ['type' => $key, $key => $data]);
  198. return $this->success('更新成功', null, $agent->agent);
  199. } else {
  200. return $this->error('更新失败');
  201. }
  202. }
  203. /**
  204. * 选择
  205. */
  206. public function select()
  207. {
  208. //设置过滤方法
  209. $this->request->filter(['strip_tags']);
  210. if ($this->request->isAjax()) {
  211. //如果发送的来源是Selectpage,则转发到Selectpage
  212. if ($this->request->request('keyField')) {
  213. return $this->selectpage();
  214. }
  215. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  216. $searchWhere = $this->request->request('searchWhere');
  217. $total = $this->model
  218. ->where($where)
  219. ->whereOr('id', '=', $searchWhere)
  220. ->whereOr('nickname', 'like', "%$searchWhere%")
  221. ->whereOr('mobile', 'like', "%$searchWhere%")
  222. ->order($sort, $order)
  223. ->field('id, nickname, mobile, avatar')
  224. ->count();
  225. $list = $this->model
  226. ->where($where)
  227. ->whereOr('id', '=', $searchWhere)
  228. ->whereOr('nickname', 'like', "%$searchWhere%")
  229. ->whereOr('mobile', 'like', "%$searchWhere%")
  230. ->order($sort, $order)
  231. ->field('id, nickname, mobile, avatar')
  232. ->limit($offset, $limit)
  233. ->select();
  234. $result = array("total" => $total, "rows" => $list);
  235. $this->success('选择用户', null, $result);
  236. }
  237. return $this->view->fetch();
  238. }
  239. /**
  240. * 分销商搜索
  241. */
  242. public function buildSearch()
  243. {
  244. $filter = $this->request->get("filter", '');
  245. $filter = (array)json_decode($filter, true);
  246. $filter = $filter ? $filter : [];
  247. $nickname = isset($filter['nickname']) ? $filter['nickname'] : '';
  248. $mobile = isset($filter['mobile']) ? $filter['mobile'] : '';
  249. $name = $this->model->getQuery()->getTable();
  250. $tableName = $name . '.';
  251. $agents = $this->model;
  252. if ($nickname || $mobile) {
  253. $agents = $agents->whereExists(function ($query) use ($nickname, $mobile, $tableName) {
  254. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  255. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . 'user_id');
  256. if ($nickname) {
  257. $query = $query->where('nickname', 'like', "%{$nickname}%");
  258. }
  259. if ($mobile) {
  260. $query = $query->where('mobile', 'like', "%{$mobile}%");
  261. }
  262. return $query;
  263. });
  264. }
  265. return $agents;
  266. }
  267. /**
  268. * 用户 team 搜索
  269. */
  270. public function buildUserSearch()
  271. {
  272. $filter = $this->request->get("filter", '');
  273. $filter = (array)json_decode($filter, true);
  274. $filter = $filter ? $filter : [];
  275. $level = isset($filter['level']) ? $filter['level'] : '';
  276. $name = $this->userModel->getQuery()->getTable();
  277. $tableName = $name . '.';
  278. $users = $this->userModel;
  279. if ($level) {
  280. $users = $users->whereExists(function ($query) use ($level, $tableName) {
  281. $agentTableName = (new \app\admin\model\shopro\commission\Agent())->getQuery()->getTable();
  282. $query = $query->table($agentTableName)->where($tableName . 'id=' . $agentTableName . '.user_id')
  283. ->where('level', $level);
  284. return $query;
  285. });
  286. }
  287. return $users;
  288. }
  289. // 递归往上找推荐人,防止出现推荐闭环
  290. private function checkChangeParentAgent($agentId, $parentAgentId)
  291. {
  292. if ($agentId === $parentAgentId) {
  293. return false;
  294. }
  295. if ($parentAgentId == 0) {
  296. return true;
  297. }
  298. $parentAgent = \app\admin\model\shopro\commission\Agent::get($parentAgentId);
  299. if ($parentAgent) {
  300. if ($parentAgent->parent_agent_id === 0) {
  301. return true;
  302. } else {
  303. return $this->checkChangeParentAgent($agentId, $parentAgent->parent_agent_id);
  304. }
  305. }
  306. return false;
  307. }
  308. }