ConsultantService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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,3])) {
  110. $type = $params['type'];
  111. if ($type != 1) {
  112. $params['relation_user_id'] = null;
  113. } else {
  114. if (empty($params['relation_user_id'])) {
  115. return json_fail('请绑定后台账号');
  116. }
  117. }
  118. }
  119. }
  120. $insertData = [
  121. 'name' => $params['name'],
  122. 'mobile' => $params['mobile'],
  123. 'dept_id' => $params['dept_id'],
  124. 'top_dept_id' => $topDeptId,
  125. 'job' => $params['job'] ?? '顾问',
  126. 'gender' => $params['gender'] ?? 1,
  127. 'password' => $password,
  128. 'status' => $params['status'] ?? 1,
  129. 'relation_user_id' => $params['relation_user_id'] ?? null,
  130. 'type' => $type,
  131. 'created_at' => time()
  132. ];
  133. $result = Consultant::insert($insertData);
  134. if ($result) {
  135. return json_success('新增成功');
  136. } else {
  137. return json_fail('新增失败');
  138. }
  139. }
  140. /**
  141. * Notes: 更新员工信息
  142. * User: yb
  143. * Date: 2024/8/2
  144. * Time: 11:54
  145. * @param $params
  146. */
  147. public static function update($params)
  148. {
  149. if (empty($params['id'])) {
  150. return json_fail('员工id不能为空');
  151. }
  152. //查找员工信息
  153. $info = Consultant::find($params['id']);
  154. if (empty($info)) {
  155. return json_fail('员工不存在');
  156. }
  157. if (!self::checkMobile($params['mobile'])) {
  158. return json_fail('请输入正确的手机号');
  159. }
  160. //查询员工是否已被注册
  161. if (Consultant::where('mobile', $params['mobile'])->where('id', '<>', $params['id'])->exists()) {
  162. return json_fail('手机号已存在,员工已被注册');
  163. }
  164. //校验密码规则
  165. if (!empty($params['password'])) {
  166. $passwordLen = strlen($params['password']);
  167. if ($passwordLen > 20 || $passwordLen < 6) {
  168. return json_fail('请输入6-20位密码');
  169. }
  170. $password = self::handlePassword($params['password']);
  171. }
  172. $type = 2;
  173. if (!empty($params['type'])) {
  174. if (in_array($params['type'], [1,2,3])) {
  175. $type = $params['type'];
  176. if ($type != 1) {
  177. $params['relation_user_id'] = null;
  178. } else {
  179. if (empty($params['relation_user_id'])) {
  180. return json_fail('请绑定后台账号');
  181. }
  182. }
  183. }
  184. }
  185. $updateData = [
  186. 'name' => $params['name'],
  187. 'mobile' => $params['mobile'],
  188. 'job' => $params['job'] ?? '顾问',
  189. 'gender' => $params['gender'] ?? 1,
  190. 'status' => $params['status'] ?? 1,
  191. 'relation_user_id' => $params['relation_user_id'] ?? null,
  192. 'type' => $type,
  193. 'updated_at' => time()
  194. ];
  195. if (!empty($params['password'])) {
  196. $updateData['password'] = $password;
  197. }
  198. $result = Consultant::where('id', $params['id'])->update($updateData);
  199. if ($result) {
  200. return json_success('更新成功');
  201. } else {
  202. return json_fail('更新失败');
  203. }
  204. }
  205. /**
  206. * Notes: 删除员工
  207. * User: yb
  208. * Date: 2024/8/2
  209. * Time: 13:33
  210. * @param $ids
  211. * @return \support\Response
  212. */
  213. public static function delete($ids)
  214. {
  215. if (!$ids) {
  216. return json_fail("数据错误");
  217. }
  218. if (!is_array($ids)) {
  219. $ids = [$ids];
  220. }
  221. try {
  222. if (is_array($ids)) {
  223. if (MarketCustomer::whereIn('consultant_id', $ids)->exists()) {
  224. throw new \Exception('员工下存在客户');
  225. }
  226. Consultant::whereIn('id', $ids)->delete();
  227. } else {
  228. if (MarketCustomer::where('consultant_id', $ids)->exists()) {
  229. throw new \Exception('员工下存在客户');
  230. }
  231. Consultant::where('id', $ids)->delete();
  232. }
  233. } catch (\Exception $e) {
  234. return json_fail($e->getMessage());
  235. }
  236. return json_success('删除成功');
  237. }
  238. /**
  239. * Notes:
  240. * User: yb
  241. * Date: 2024/8/5
  242. * Time: 10:52
  243. */
  244. public static function userList()
  245. {
  246. $deptIds = TeamService::getIdsByUser();
  247. $where = [];
  248. if (false === $deptIds) {
  249. //无权限
  250. $where[] = ['join_user_dept_id', '=', 0];
  251. } else if (is_array($deptIds)) {
  252. //指定团队下的权限
  253. $where[] = [function($query) use ($deptIds) {
  254. $query->whereIn('join_user_dept_id', $deptIds);
  255. }];
  256. } else {
  257. $deptIds = SysDept::where('dept_category', '=', '获客团队')->pluck('dept_id');
  258. $deptIds = $deptIds->toArray();
  259. $where[] = [function($query) use ($deptIds) {
  260. $query->whereIn('join_user_dept_id', $deptIds);
  261. }];
  262. }
  263. $userList = SysUser::where($where)->select(['user_id', 'user_name', 'user_mobile'])->get();
  264. return json_success('请求成功', $userList);
  265. }
  266. /**
  267. * Notes:校验手机号
  268. * User: yb
  269. * Date: 2024/8/2
  270. * Time: 11:12
  271. * @param $mobile
  272. * @return bool
  273. */
  274. protected static function checkMobile($mobile)
  275. {
  276. if (preg_match('/^1[0-9]\d{9}$/', $mobile)) {
  277. return true;
  278. } else {
  279. return false;
  280. }
  281. }
  282. /**
  283. * Notes: 处理密码
  284. * User: yb
  285. * Date: 2024/8/2
  286. * Time: 11:19
  287. * @param $password
  288. * @return mixed
  289. */
  290. protected static function handlePassword($password)
  291. {
  292. return md5(md5($password));
  293. }
  294. /**
  295. * Notes: 处理出生日期
  296. * User: yb
  297. * Date: 2024/8/2
  298. * Time: 11:28
  299. * @param $birth
  300. * @return false|string
  301. */
  302. protected static function handleBirth($birth)
  303. {
  304. return date('Y-m-d',strtotime($birth));
  305. }
  306. }