ConsultantService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace app\admin\service\consultant;
  3. use app\model\Consultant;
  4. use app\model\MarketCustomer;
  5. use app\model\SysDept;
  6. use app\model\SysUser;
  7. use support\Request;
  8. class ConsultantService
  9. {
  10. public static function index(Request $request)
  11. {
  12. $format = $request->get('format', 'normal');
  13. $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
  14. $limit = $limit <= 0 ? 10 : $limit;
  15. $params = $request->get();
  16. $page = (int)$request->get('page');
  17. $page = $page > 0 ? $page : 1;
  18. $where = [];
  19. $whereDept = [];
  20. $whereTopDept = [];
  21. $deptIds = TeamService::getIdsByUser();
  22. if (false === $deptIds) {
  23. //无权限
  24. $where[] = ['dept_id', '=', 0];
  25. } else if (is_array($deptIds)) {
  26. //指定团队下的权限
  27. $where[] = [function($query) use ($deptIds) {
  28. $query->whereIn('dept_id', $deptIds);
  29. }];
  30. }
  31. if (!empty($params['name'])) {
  32. $where[] = ['name', 'like', "%{$params['name']}%"];
  33. }
  34. if (!empty($params['mobile'])) {
  35. $where[] = ['mobile', 'like', "%{$params['mobile']}%"];
  36. }
  37. if (!empty($params['status'])) {
  38. $where[] = ['status', '=', $params['status']];
  39. }
  40. if (!empty($params['dept_id'])) {
  41. $deptId = end($params['dept_id']);
  42. $whereDept[] = ['dept_id', '=', $deptId];
  43. $whereTopDept[] = ['top_dept_id', '=', $deptId];
  44. }
  45. if (!empty($params['type'])) {
  46. $where[] = ['type', '=', $params['type']];
  47. }
  48. $paginator = Consultant::where(function ($query) use ($whereDept, $whereTopDept) {
  49. $query->orWhere($whereDept)
  50. ->orWhere($whereTopDept);
  51. })->where($where)->paginate($limit, '*', 'page', $page);
  52. $total = $paginator->total();
  53. $items = $paginator->items();
  54. if (!empty($items)) {
  55. $teamKeys = TeamService::getKeys();
  56. foreach ($items as &$item) {
  57. $teamName = [];
  58. if (isset($teamKeys[$item->top_dept_id])) {
  59. $teamName[] = $teamKeys[$item->top_dept_id];
  60. }
  61. if (isset($teamKeys[$item->dept_id])) {
  62. $teamName[] = $teamKeys[$item->dept_id];
  63. }
  64. $item->team_name = !empty($teamName) ? implode('-', $teamName) : '';
  65. }
  66. }
  67. $data = [
  68. 'total' => $total,
  69. 'rows' => $items
  70. ];
  71. return json_success('success', $data);
  72. }
  73. /**
  74. * Notes: 添加员工
  75. * User: yb
  76. * Date: 2024/8/2
  77. * Time: 11:03
  78. * @param $params
  79. */
  80. public static function add($params)
  81. {
  82. if (!self::checkMobile($params['mobile'])) {
  83. return json_fail('请输入正确的手机号');
  84. }
  85. //查询员工是否已被注册
  86. if (Consultant::where('mobile', $params['mobile'])->exists()) {
  87. return json_fail('手机号已存在,员工已被注册');
  88. }
  89. //校验密码规则
  90. if (!empty($params['password'])) {
  91. $passwordLen = strlen($params['password']);
  92. if ($passwordLen > 20 || $passwordLen < 6) {
  93. return json_fail('请输入6-20位密码');
  94. }
  95. }
  96. //查询上级团队
  97. $topDeptId = SysDept::where('dept_id', $params['dept_id'])->where('dept_category', TeamService::DEPT_CATEGORY)->value('dept_super_id');
  98. if (!is_numeric($topDeptId)) {
  99. return json_fail('团队不存在');
  100. }
  101. if (empty($params['password'])) {
  102. $showPassword = substr($params['mobile'], 5);
  103. $password = self::handlePassword($showPassword);
  104. } else {
  105. $password = self::handlePassword($params['password']);
  106. }
  107. $type = 2;
  108. if (!empty($params['type'])) {
  109. if (in_array($params['type'], [1,2])) {
  110. $type = $params['type'];
  111. if ($type == 2) {
  112. $params['relation_user_id'] = null;
  113. }
  114. }
  115. }
  116. $insertData = [
  117. 'name' => $params['name'],
  118. 'mobile' => $params['mobile'],
  119. 'dept_id' => $params['dept_id'],
  120. 'top_dept_id' => $topDeptId,
  121. 'job' => $params['job'] ?? '顾问',
  122. 'gender' => $params['gender'] ?? 1,
  123. 'password' => $password,
  124. 'status' => $params['status'] ?? 1,
  125. 'relation_user_id' => $params['relation_user_id'] ?? null,
  126. 'type' => $type,
  127. 'created_at' => time()
  128. ];
  129. $result = Consultant::insert($insertData);
  130. if ($result) {
  131. return json_success('新增成功');
  132. } else {
  133. return json_fail('新增失败');
  134. }
  135. }
  136. /**
  137. * Notes: 更新员工信息
  138. * User: yb
  139. * Date: 2024/8/2
  140. * Time: 11:54
  141. * @param $params
  142. */
  143. public static function update($params)
  144. {
  145. if (empty($params['id'])) {
  146. return json_fail('员工id不能为空');
  147. }
  148. //查找员工信息
  149. $info = Consultant::find($params['id']);
  150. if (empty($info)) {
  151. return json_fail('员工不存在');
  152. }
  153. if (!self::checkMobile($params['mobile'])) {
  154. return json_fail('请输入正确的手机号');
  155. }
  156. //查询员工是否已被注册
  157. if (Consultant::where('mobile', $params['mobile'])->where('id', '<>', $params['id'])->exists()) {
  158. return json_fail('手机号已存在,员工已被注册');
  159. }
  160. //校验密码规则
  161. if (!empty($params['password'])) {
  162. $passwordLen = strlen($params['password']);
  163. if ($passwordLen > 20 || $passwordLen < 6) {
  164. return json_fail('请输入6-20位密码');
  165. }
  166. $password = self::handlePassword($params['password']);
  167. }
  168. $type = 2;
  169. if (!empty($params['type'])) {
  170. if (in_array($params['type'], [1,2])) {
  171. $type = $params['type'];
  172. if ($type == 2) {
  173. $params['relation_user_id'] = null;
  174. }
  175. }
  176. }
  177. $updateData = [
  178. 'name' => $params['name'],
  179. 'mobile' => $params['mobile'],
  180. 'job' => $params['job'] ?? '顾问',
  181. 'gender' => $params['gender'] ?? 1,
  182. 'status' => $params['status'] ?? 1,
  183. 'relation_user_id' => $params['relation_user_id'] ?? null,
  184. 'type' => $type,
  185. 'updated_at' => time()
  186. ];
  187. if (!empty($params['password'])) {
  188. $updateData['password'] = $password;
  189. }
  190. $result = Consultant::where('id', $params['id'])->update($updateData);
  191. if ($result) {
  192. return json_success('更新成功');
  193. } else {
  194. return json_fail('更新失败');
  195. }
  196. }
  197. /**
  198. * Notes: 删除员工
  199. * User: yb
  200. * Date: 2024/8/2
  201. * Time: 13:33
  202. * @param $ids
  203. * @return \support\Response
  204. */
  205. public static function delete($ids)
  206. {
  207. if (!$ids) {
  208. return json_fail("数据错误");
  209. }
  210. if (!is_array($ids)) {
  211. $ids = [$ids];
  212. }
  213. try {
  214. if (is_array($ids)) {
  215. if (MarketCustomer::whereIn('consultant_id', $ids)->exists()) {
  216. throw new \Exception('员工下存在客户');
  217. }
  218. Consultant::whereIn('id', $ids)->delete();
  219. } else {
  220. if (MarketCustomer::where('consultant_id', $ids)->exists()) {
  221. throw new \Exception('员工下存在客户');
  222. }
  223. Consultant::where('id', $ids)->delete();
  224. }
  225. } catch (\Exception $e) {
  226. return json_fail($e->getMessage());
  227. }
  228. return json_success('删除成功');
  229. }
  230. /**
  231. * Notes:
  232. * User: yb
  233. * Date: 2024/8/5
  234. * Time: 10:52
  235. */
  236. public static function userList()
  237. {
  238. $deptIds = TeamService::getIdsByUser();
  239. $where = [];
  240. if (false === $deptIds) {
  241. //无权限
  242. $where[] = ['join_user_dept_id', '=', 0];
  243. } else if (is_array($deptIds)) {
  244. //指定团队下的权限
  245. $where[] = [function($query) use ($deptIds) {
  246. $query->whereIn('join_user_dept_id', $deptIds);
  247. }];
  248. } else {
  249. $deptIds = SysDept::where('dept_category', '=', '获客团队')->pluck('dept_id');
  250. $deptIds = $deptIds->toArray();
  251. $where[] = [function($query) use ($deptIds) {
  252. $query->whereIn('join_user_dept_id', $deptIds);
  253. }];
  254. }
  255. $userList = SysUser::where($where)->select(['user_id', 'user_name', 'user_mobile'])->get();
  256. return json_success('请求成功', $userList);
  257. }
  258. /**
  259. * Notes:校验手机号
  260. * User: yb
  261. * Date: 2024/8/2
  262. * Time: 11:12
  263. * @param $mobile
  264. * @return bool
  265. */
  266. protected static function checkMobile($mobile)
  267. {
  268. if (preg_match('/^1[0-9]\d{9}$/', $mobile)) {
  269. return true;
  270. } else {
  271. return false;
  272. }
  273. }
  274. /**
  275. * Notes: 处理密码
  276. * User: yb
  277. * Date: 2024/8/2
  278. * Time: 11:19
  279. * @param $password
  280. * @return mixed
  281. */
  282. protected static function handlePassword($password)
  283. {
  284. return md5(md5($password));
  285. }
  286. /**
  287. * Notes: 处理出生日期
  288. * User: yb
  289. * Date: 2024/8/2
  290. * Time: 11:28
  291. * @param $birth
  292. * @return false|string
  293. */
  294. protected static function handleBirth($birth)
  295. {
  296. return date('Y-m-d',strtotime($birth));
  297. }
  298. }