ConsultantService.php 11 KB

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