CustomService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <?php
  2. namespace app\wechat\service;
  3. use app\model\Consultant;
  4. use app\model\MarketCustomer;
  5. use app\model\MarketCustomerFollow;
  6. use app\model\MarketCustomerLogs;
  7. use app\wechat\validate\CustomValidate;
  8. use support\Db;
  9. use support\exception\BusinessException;
  10. use Tinywan\Jwt\JwtToken;
  11. class CustomService
  12. {
  13. const DIFF_TIME = (60 * 60 * 24 * 30);
  14. /**
  15. * Notes: 选项配置
  16. * User: yb
  17. * Date: 2024/8/8
  18. * Time: 15:18
  19. * @return array
  20. */
  21. public static function config()
  22. {
  23. $options = MarketCustomer::config();
  24. if (isset($options['visit_type'])) {
  25. //如果存在拜访方式,直接在后台处理完成后返回前台
  26. foreach ($options['visit_type'] as $key => $val) {
  27. $data = [];
  28. foreach ($val as $index => $item) {
  29. $data[] = ['text' => $item, 'value' => $index];
  30. }
  31. $options['visit_type'][$key] = $data;
  32. }
  33. }
  34. return $options;
  35. }
  36. /**
  37. * Notes: 客户列表
  38. * User: yb
  39. * Date: 2024/8/12
  40. * Time: 11:08
  41. * @param $params
  42. */
  43. public static function index($params)
  44. {
  45. $page = $params['page'] ?? 1;
  46. $size = $params['size'] ?? 10;
  47. //查询顾问信息
  48. $consultantId = JwtToken::getCurrentId();
  49. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  50. $deptId = $consultantInfo->dept_id;
  51. $type = $consultantInfo->type;
  52. $currentTime = time();
  53. $diffNums = self::DIFF_TIME;
  54. $where = [];
  55. $whereFollow = [];
  56. if ($type == 1) {
  57. //团队下所有的
  58. $deptIds = TeamService::getIds($deptId);
  59. $where[] = [function($query) use ($deptIds) {
  60. $query->whereIn('dept_id', $deptIds);
  61. }];
  62. if (!empty($params['consultant_id'])) {
  63. $where[] = ['consultant_id', '=', $params['consultant_id']];
  64. $whereFollow[] = ['consultant_id', '=', $params['consultant_id']];
  65. } else {
  66. $consultantIds = UserService::getIds(1);
  67. $whereFollow[] = [function($query) use ($consultantIds) {
  68. $query->whereIn('consultant_id', $consultantIds);
  69. }];
  70. }
  71. } else {
  72. //个人的
  73. $where[] = ['consultant_id', '=', $consultantId];
  74. $whereFollow[] = ['consultant_id', '=', $consultantId];
  75. }
  76. if (!empty($params['custom'])) {
  77. $keywords = $params['custom'];
  78. $where[] = [function($query) use ($keywords) {
  79. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  80. }];
  81. }
  82. if (!empty($params['report_status'])) {
  83. if ($params['report_status'] == 1) {
  84. //已报备
  85. $where[] = [function($query) use ($diffNums, $currentTime) {
  86. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1)->where('check_status', '=', 2);
  87. }];
  88. } else if ($params['report_status'] == 3) {
  89. //已锁定
  90. $where[] = [function($query) {
  91. $query->whereIn('current_status', [2,3,4])->where('check_status', '=', 2);
  92. }];
  93. } else if ($params['report_status'] == 2) {
  94. //已失效
  95. $where[] = [function($query) use ($diffNums, $currentTime){
  96. $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1)");
  97. }];
  98. $where[] = ['check_status', '=', 2];
  99. } else if ($params['report_status'] == '-1') {
  100. //待审核
  101. $where[] = ['check_status', '=', 1];
  102. }
  103. }
  104. if (!empty($params['type'])) {
  105. $where[] = ['type', '=', $params['type']];
  106. }
  107. if (!empty($params['visit_time'])) {
  108. $datetime = $params['visit_time'];
  109. $startTime = strtotime($datetime['start'].' 00:00:00');
  110. $endTime = strtotime($datetime['end'].' 23:59:59');
  111. $where[] = [function($query) use ($startTime, $endTime) {
  112. $query->whereBetween('visit_time', [$startTime, $endTime]);
  113. }];
  114. }
  115. if (!empty($params['current_status'])) {
  116. $where[] = ['current_status', '=', $params['current_status']];
  117. }
  118. $paginator = MarketCustomer::where($where)->orderBy('visit_time', 'desc')->paginate($size, '*', 'page', $page);
  119. $total = $paginator->total();
  120. $items = $paginator->items();
  121. if (!empty($items)) {
  122. $now = time();
  123. foreach ($items as &$item) {
  124. $item->mask_mobile = self::handlePhone($item->mobile);
  125. $visitTimeInt = strtotime($item->visit_time);
  126. $endTime = $visitTimeInt + 60 * 60 * 24 * 30;
  127. $visitTimeInt = $endTime - $now;
  128. if ($visitTimeInt <= 0) {
  129. $visitTimeInt = 0;
  130. }
  131. $item->visit_time_int = $visitTimeInt;
  132. }
  133. }
  134. //本日新增
  135. $date = date('Y-m-d');
  136. $start = strtotime($date.' 00:00:00');
  137. $end = strtotime($date.' 23:59:59');
  138. $newNums = MarketCustomer::where($where)->whereBetween('created_at', [$start, $end])->count();
  139. $followNums = MarketCustomerFollow::where($whereFollow)->whereBetween('created_at', [$start, $end])->count();
  140. $data = [
  141. 'new_follow_num' => $followNums,
  142. 'new_custom_num' => $newNums,
  143. 'total' => $total,
  144. 'rows' => $items
  145. ];
  146. return json_success('success', $data);
  147. }
  148. /**
  149. * Notes: 我的客户
  150. * User: yb
  151. * Date: 2024/8/13
  152. * Time: 15:14
  153. * @param $params
  154. */
  155. public static function myCustomList($params)
  156. {
  157. $page = $params['page'] ?? 1;
  158. $size = $params['size'] ?? 10;
  159. //查询有效顾问信息
  160. $consultantId = JwtToken::getCurrentId();
  161. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  162. $deptId = $consultantInfo->dept_id;
  163. $type = $consultantInfo->type;
  164. $where = [];
  165. if ($type == 1) {
  166. //团队下所有的
  167. $deptIds = TeamService::getIds($deptId);
  168. $where[] = [function($query) use ($deptIds) {
  169. $query->whereIn('dept_id', $deptIds);
  170. }];
  171. } else {
  172. //个人的
  173. $where[] = ['consultant_id', '=', $consultantId];
  174. }
  175. $where[] = ['check_status', '=', 2];
  176. $where[] = [function($query) {
  177. $query->orWhere(function ($query){
  178. $diffNums = (60 * 60 * 24 * 30);
  179. $currentTime = time();
  180. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
  181. })->orWhereIn('current_status', [2,3,4]);
  182. }];
  183. if (!empty($params['custom'])) {
  184. $keywords = $params['custom'];
  185. $where[] = [function($query) use ($keywords) {
  186. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  187. }];
  188. }
  189. $paginator = MarketCustomer::where($where)->orderBy('created_at', 'desc')->paginate($size, ['id','name','mobile','gender'], 'page', $page);
  190. $total = $paginator->total();
  191. $items = $paginator->items();
  192. if (!empty($items)) {
  193. foreach ($items as &$item) {
  194. $item->mask_mobile = self::handlePhone($item->mobile);
  195. }
  196. }
  197. $data = [
  198. 'total' => $total,
  199. 'rows' => $items
  200. ];
  201. return json_success('success', $data);
  202. }
  203. /**
  204. * Notes: 添加客户
  205. * User: yb
  206. * Date: 2024/8/6
  207. * Time: 11:20
  208. */
  209. public static function add($params)
  210. {
  211. $params = MarketCustomer::handleNumParams($params);
  212. $mobile = $params['mobile'];
  213. //查询客户手机号是否已经存在
  214. if (MarketCustomer::checkCustomExists($mobile)) {
  215. return json_fail('客户已经登记过了');
  216. }
  217. //查询顾问信息
  218. $consultantId = JwtToken::getCurrentId();
  219. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  220. if (empty($consultantInfo)) {
  221. return json_fail('顾问信息不存在');
  222. }
  223. $deptId = $consultantInfo->dept_id;
  224. $insertData = [
  225. 'name' => $params['name'],
  226. 'mobile' => $mobile,
  227. 'consultant_id' => $consultantId,
  228. 'dept_id' => $deptId,
  229. 'gender' => $params['gender'] ?? null,
  230. 'visit_type' => $params['visit_type'] ?? null,
  231. 'require_area' => $params['require_area'] ?? null,
  232. 'area' => $params['area'] ?? null,
  233. 'requirement' => $params['requirement'] ?? null,
  234. 'age_range' => $params['age_range'] ?? null,
  235. 'focus' => $params['focus'] ?? null,
  236. 'region' => $params['region'] ?? null,
  237. 'purpose' => $params['purpose'] ?? null,
  238. 'level' => $params['level'] ?? null,
  239. 'type' => $params['type'] ?? null,
  240. 'visit_time' => time(),
  241. 'note' => $params['note'] ?? '',
  242. 'check_status' => 1,
  243. 'current_status' => $params['current_status'] ?? null,
  244. 'created_at' => time()
  245. ];
  246. if ($insertData['type'] == 1) {
  247. $insertData['current_status'] = 1;
  248. } else if ($insertData['type'] == 2) {
  249. $insertData['current_status'] = 2;
  250. }
  251. Db::beginTransaction();
  252. try {
  253. $customId = MarketCustomer::insertGetId($insertData);
  254. Db::commit();
  255. }catch (BusinessException|\Exception $e){
  256. Db::rollBack();
  257. return json_fail($e->getMessage());
  258. }
  259. if ($customId) {
  260. return json_success('添加成功');
  261. } else {
  262. return json_fail('添加失败');
  263. }
  264. }
  265. /**
  266. * Notes: 编辑客户
  267. * User: yb
  268. * Date: 2024/8/12
  269. * Time: 10:23
  270. * @param $params
  271. * @return \support\Response
  272. */
  273. public static function edit($params)
  274. {
  275. $consultantId = JwtToken::getCurrentId();
  276. $params = MarketCustomer::handleNumParams($params);
  277. if (empty($params['id'])) {
  278. return json_fail('客户id不能为空');
  279. }
  280. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  281. if (empty($consultantInfo)) {
  282. return json_fail('顾问信息不存在');
  283. }
  284. $info = MarketCustomer::firstWhere(['id' => $params['id'], 'consultant_id' => $consultantId]);
  285. if (empty($info)) {
  286. return json_fail('客户信息不存在');
  287. }
  288. $updateData = [
  289. 'name' => $params['name'],
  290. 'gender' => $params['gender'] ?? null,
  291. 'visit_type' => $params['visit_type'] ?? null,
  292. 'require_area' => $params['require_area'] ?? null,
  293. 'area' => $params['area'] ?? null,
  294. 'requirement' => $params['requirement'] ?? null,
  295. 'age_range' => $params['age_range'] ?? null,
  296. 'focus' => $params['focus'] ?? null,
  297. 'region' => $params['region'] ?? null,
  298. 'purpose' => $params['purpose'] ?? null,
  299. 'level' => $params['level'] ?? null,
  300. 'type' => $params['type'] ?? null,
  301. 'note' => $params['note'] ?? '',
  302. 'updated_at' => time()
  303. ];
  304. if (empty($params['mobile'])) {
  305. return json_fail('手机号不能为空');
  306. }
  307. $mobile = $params['mobile'];
  308. if (false === strpos($mobile,'****')) {
  309. //校验手机号格式
  310. $validate = new CustomValidate();
  311. if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
  312. return json_fail($validate->getError());
  313. }
  314. //修改手机号操作
  315. if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
  316. return json_fail('客户已经登记过了');
  317. }
  318. $updateData['mobile'] = $mobile;
  319. }
  320. $currentStatus = $info->current_status;
  321. if ($currentStatus == 1) {
  322. //已来电改为已到访
  323. if ($params['type'] == 2) {
  324. $updateData['current_status'] = 2;
  325. }
  326. }
  327. $result = false;
  328. Db::beginTransaction();
  329. try {
  330. $result = MarketCustomer::where('id', $params['id'])->update($updateData);
  331. Db::commit();
  332. }catch (BusinessException|\Exception $e){
  333. Db::rollBack();
  334. return json_fail($e->getMessage());
  335. }
  336. if ($result) {
  337. return json_success('编辑成功');
  338. } else {
  339. return json_fail('编辑失败');
  340. }
  341. }
  342. /**
  343. * Notes: 客户详情
  344. * User: yb
  345. * Date: 2024/8/8
  346. * Time: 15:36
  347. * @param $params
  348. */
  349. public static function info($id)
  350. {
  351. $customInfo = MarketCustomer::firstWhere(['id' => $id]);
  352. if (empty($customInfo)) {
  353. return json_fail('客户不存在');
  354. }
  355. //手机号加密处理
  356. $customInfo->mask_mobile = self::handlePhone($customInfo->mobile);
  357. //查询负责顾问
  358. $consultantName = Consultant::where('id', $customInfo->consultant_id)->value('name');
  359. $customInfo->consultant_name = $consultantName;
  360. //查询最新的跟进记录
  361. $follow = MarketCustomerFollow::where('market_customer_id', $id)->orderBy('created_at', 'desc')->first();
  362. $customInfo->follow = $follow;
  363. return json_success('', $customInfo);
  364. }
  365. /**
  366. * Notes: 更新客户状态
  367. * User: yb
  368. * Date: 2024/8/14
  369. * Time: 13:23
  370. * @param $params
  371. */
  372. public static function updateStatus($params)
  373. {
  374. $customId = $params['id'];
  375. $currentStatus = $params['current_status'];
  376. if (!in_array($currentStatus, [-1, 3, 4])) {
  377. return json_fail('状态值非法');
  378. }
  379. $info = MarketCustomer::firstWhere(['id' => $customId]);
  380. if (empty($info)) {
  381. return json_fail('客户不存在');
  382. }
  383. $info->current_status = $currentStatus;
  384. $result = $info->save();
  385. if ($result) {
  386. return json_success('更新成功');
  387. } else {
  388. return json_fail('更新失败');
  389. }
  390. }
  391. /**
  392. * Notes: 转移客户
  393. * User: yb
  394. * Date: 2024/8/7
  395. * Time: 14:27
  396. * @param $params
  397. */
  398. public static function moveCustom($params)
  399. {
  400. $userId = JwtToken::getCurrentId();
  401. //获取绑定的管理信息
  402. $userInfo = Consultant::firstWhere(['id' => $userId]);
  403. if (empty($userInfo)) {
  404. return json_fail('管理员信息不存在');
  405. }
  406. if ($userInfo->type != 1) {
  407. return json_fail('操作权限不足');
  408. }
  409. $relationUserId = $userInfo->relation_user_id;
  410. $currentConsultantId = $params['consultant_id']; //当前顾问
  411. $consultantId = $params['move_consultant_id'] ?? 0;
  412. $customId = $params['move_market_customer_id'] ?? 0;
  413. $note = $params['note'] ?? '';
  414. $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
  415. if (empty($consultantInfo)) {
  416. return json_fail('接收成员不存在');
  417. }
  418. $currentDeptId = $consultantInfo->dept_id; //当前部门
  419. $logsInertData = [];
  420. $now = time();
  421. $whereCustom = [];
  422. if (!empty($customId)) {
  423. $whereCustom[] = ['id', '=', $customId];
  424. }
  425. if (!empty($consultantId)) {
  426. $whereCustom[] = ['consultant_id', '=', $consultantId];
  427. }
  428. $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id'])->get();
  429. if (!$customList->isEmpty()) {
  430. foreach ($customList as$item) {
  431. $logsInertData[] = [
  432. 'market_customer_id' => $item->id,
  433. 'consultant_id' => $currentConsultantId,
  434. 'dept_id' => $currentDeptId,
  435. 'before_consultant_id' => $item->consultant_id,
  436. 'before_dept_id' => $item->dept_id,
  437. 'note' => $note,
  438. 'change_user_id' => $relationUserId,
  439. 'change_consultant_id' => $userId,
  440. 'created_at' => $now
  441. ];
  442. }
  443. }
  444. if ($customList->isEmpty()) {
  445. return json_fail('成员没有客户记录');
  446. }
  447. if (empty($logsInertData)) {
  448. return json_fail('移交记录不能为空');
  449. }
  450. Db::beginTransaction();
  451. $result = false;
  452. try {
  453. foreach ($customList as $item) {
  454. $item->consultant_id = $currentConsultantId;
  455. $item->dept_id = $currentDeptId;
  456. $item->save();
  457. }
  458. $result = MarketCustomerLogs::insert($logsInertData);
  459. Db::commit();
  460. }catch (BusinessException|\Exception $e) {
  461. Db::rollBack();
  462. return json_fail($e->getMessage());
  463. }
  464. if ($result) {
  465. return json_success('移交成功');
  466. } else {
  467. return json_fail('移交失败');
  468. }
  469. }
  470. /**
  471. * Notes: 转移记录
  472. * User: yb
  473. * Date: 2024/8/7
  474. * Time: 15:33
  475. * @param $params
  476. */
  477. public static function moveLogs($params)
  478. {
  479. $page = $params['page'] ?? 1;
  480. $size = $params['size'] ?? 10;
  481. $where = [];
  482. if (!empty($params['custom_id'])) {
  483. $where[] = ['market_customer_id', '=', $params['custom_id']];
  484. }
  485. $selectFn = function ($query){
  486. $query->select('name', 'mobile', 'id');
  487. };
  488. $paginator = MarketCustomerLogs::with(['custom' => $selectFn, 'beforeMan' => $selectFn, 'currentMan' => $selectFn])
  489. ->where($where)
  490. ->orderBy('created_at', 'desc')
  491. ->paginate($size, '*', 'page', $page);
  492. $total = $paginator->total();
  493. $items = $paginator->items();
  494. $data = [
  495. 'total' => $total,
  496. 'rows' => $items
  497. ];
  498. return json_success('success', $data);
  499. }
  500. /**
  501. * Notes: 审核客户
  502. * User: yb
  503. * Date: 2024/8/16
  504. * Time: 11:21
  505. * @param $params
  506. */
  507. public static function checkCustom($params)
  508. {
  509. $userId = JwtToken::getCurrentId();
  510. $adminId = Consultant::where('id', '=', $userId)->value('relation_user_id');
  511. //查询客户是否已经存在
  512. $customId = $params['id'];
  513. $checkStatus = $params['check_status'];
  514. if (!in_array($checkStatus, [-1,2])) {
  515. return json_fail('状态值非法');
  516. }
  517. $info = MarketCustomer::firstWhere(['id' => $customId]);
  518. if (empty($info)) {
  519. return json_fail('客户不存在');
  520. }
  521. $status = $info->check_status;
  522. if ($status != 1) {
  523. return json_fail('客户不是待审核状态');
  524. }
  525. $mobile = $info->mobile;
  526. if ($checkStatus == 2) {
  527. if (MarketCustomer::checkCustomExists($mobile)) {
  528. return json_fail('客户已经存在');
  529. }
  530. }
  531. $result = false;
  532. Db::beginTransaction();
  533. try {
  534. $info->check_status = $checkStatus;
  535. if ($checkStatus == -1) {
  536. //拒绝录入拒绝理由
  537. $info->check_note = $params['note'] ?? '无拒绝理由';
  538. }
  539. $info->check_time = time();
  540. $info->check_admin_id = $adminId;
  541. $info->check_consultant_id = $userId;
  542. $result = $info->save();
  543. if ($checkStatus == 2) {
  544. //将其他待审的相同手机号的改拒绝
  545. $where = [
  546. ['mobile' , '=', $mobile],
  547. ['check_status', '=', 1],
  548. ['id', '<>', $customId]
  549. ];
  550. MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1,'check_note' => '客户已经被其他顾问报备']);
  551. }
  552. Db::commit();
  553. }catch (BusinessException|\Exception $e){
  554. Db::rollBack();
  555. return json_fail($e->getMessage());
  556. }
  557. if ($result) {
  558. return json_success('审核成功');
  559. } else {
  560. return json_fail('审核失败');
  561. }
  562. }
  563. /**
  564. * Notes: 对手机号加密处理
  565. * User: yb
  566. * Date: 2024/8/8
  567. * Time: 15:41
  568. * @param $val
  569. * @return string|string[]
  570. */
  571. public static function handlePhone($val)
  572. {
  573. return substr_replace($val, '****', 3, 4);
  574. }
  575. /**
  576. * Notes: 处理拜访时间
  577. * User: yb
  578. * Date: 2024/8/8
  579. * Time: 16:06
  580. * @param $val
  581. * @return float|int
  582. */
  583. protected static function handleVisitTime($val) {
  584. return (strtotime($val) * 1000);
  585. }
  586. }