CustomService.php 22 KB

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