ConsultantService.php 9.1 KB

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