CustomService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. const CHECK_NOTE = '该客户已在其他顾问那到访';
  15. /**
  16. * Notes: 选项配置
  17. * User: yb
  18. * Date: 2024/8/8
  19. * Time: 15:18
  20. * @return array
  21. */
  22. public static function config()
  23. {
  24. $options = MarketCustomer::config();
  25. if (isset($options['visit_type'])) {
  26. //如果存在拜访方式,直接在后台处理完成后返回前台
  27. foreach ($options['visit_type'] as $key => $val) {
  28. $data = [];
  29. foreach ($val as $index => $item) {
  30. $data[] = ['text' => $item, 'value' => $index];
  31. }
  32. $options['visit_type'][$key] = $data;
  33. }
  34. }
  35. return $options;
  36. }
  37. /**
  38. * Notes: 客户列表
  39. * User: yb
  40. * Date: 2024/8/12
  41. * Time: 11:08
  42. * @param $params
  43. */
  44. public static function index($params)
  45. {
  46. $page = $params['page'] ?? 1;
  47. $size = $params['size'] ?? 10;
  48. //查询顾问信息
  49. $consultantId = JwtToken::getCurrentId();
  50. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  51. $deptId = $consultantInfo->dept_id;
  52. $type = $consultantInfo->type;
  53. $currentTime = time();
  54. $diffNums = self::DIFF_TIME;
  55. $where = [
  56. [function($query) use ($diffNums, $currentTime){
  57. $query->orWhereRaw("check_status IN (-1, 1)")
  58. ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
  59. ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
  60. }]
  61. ];
  62. $whereFollow = [];
  63. if ($type == 1) {
  64. //团队下所有的
  65. $deptIds = TeamService::getIds($deptId);
  66. $where[] = [function($query) use ($deptIds) {
  67. $query->whereIn('dept_id', $deptIds);
  68. }];
  69. if (!empty($params['consultant_id'])) {
  70. $consultantId = $params['consultant_id'];
  71. $where[] = [function($query) use ($consultantId) {
  72. $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
  73. }];
  74. $whereFollow[] = ['consultant_id', '=', $params['consultant_id']];
  75. } else {
  76. $consultantIds = UserService::getIds(1);
  77. $whereFollow[] = [function($query) use ($consultantIds) {
  78. $query->whereIn('consultant_id', $consultantIds);
  79. }];
  80. }
  81. } else if ($type == 2){
  82. //个人的
  83. $where[] = [function($query) use ($consultantId){
  84. $query->orWhere('consultant_id', $consultantId)->orWhere('report_consultant_id', $consultantId);
  85. }];
  86. $whereFollow[] = ['consultant_id', '=', $consultantId];
  87. } else {
  88. //所有团队的
  89. }
  90. if (!empty($params['custom'])) {
  91. $keywords = $params['custom'];
  92. $where[] = [function($query) use ($keywords) {
  93. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  94. }];
  95. }
  96. if (!empty($params['report_status'])) {
  97. if ($params['report_status'] == 1) {
  98. //已报备
  99. $where[] = [function($query) use ($diffNums, $currentTime) {
  100. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1)->where('check_status', '=', 2);
  101. }];
  102. } else if ($params['report_status'] == 3) {
  103. //已锁定
  104. $where[] = [function($query) {
  105. $query->whereIn('current_status', [2,3,4])->where('check_status', '=', 2);
  106. }];
  107. } else if ($params['report_status'] == 2) {
  108. //已失效
  109. $where[] = [function($query) use ($diffNums, $currentTime){
  110. $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1)");
  111. }];
  112. $where[] = ['check_status', '=', 2];
  113. } else if ($params['report_status'] == '-1') {
  114. //待审核
  115. $where[] = ['check_status', '=', 1];
  116. }
  117. }
  118. if (!empty($params['check_status'])) {
  119. $where[] = ['check_status', '=', $params['check_status']];
  120. }
  121. if (!empty($params['type'])) {
  122. $where[] = ['type', '=', $params['type']];
  123. }
  124. if (!empty($params['visit_time'])) {
  125. $datetime = $params['visit_time'];
  126. $startTime = strtotime($datetime['start'].' 00:00:00');
  127. $endTime = strtotime($datetime['end'].' 23:59:59');
  128. $where[] = [function($query) use ($startTime, $endTime) {
  129. $query->whereBetween('visit_time', [$startTime, $endTime]);
  130. }];
  131. }
  132. if (!empty($params['current_status'])) {
  133. $currentStatus = $params['current_status'];
  134. $where[] = [function($query) use ($currentStatus) {
  135. $query->where('current_status', $currentStatus);
  136. }];
  137. }
  138. if (!empty($params['created_at'])) {
  139. $createdAt = $params['created_at'];
  140. if (is_array($createdAt)) {
  141. $createdAtStart = strtotime($createdAt[0]);
  142. $createdAtEnd = strtotime($createdAt[1]);
  143. } else {
  144. //本月开始和结束
  145. $createdAtStart = strtotime(date('Y-m-01 00:00:00'));
  146. $createdAtEnd = strtotime(date('Y-m-t 23:59:59'));
  147. }
  148. $where[] = [function($query) use ($createdAtStart,$createdAtEnd) {
  149. $query->whereBetween('created_at', [$createdAtStart, $createdAtEnd]);
  150. }];
  151. }
  152. $fn = function ($query) {
  153. $query->select('name', 'mobile', 'id', 'dept_id');
  154. };
  155. $paginator = MarketCustomer::with(['consultant' => $fn, 'report' => $fn])->where($where)->orderBy('visit_time', 'desc')->paginate($size, '*', 'page', $page);
  156. $total = $paginator->total();
  157. $items = $paginator->items();
  158. if (!empty($items)) {
  159. //查询所有团队信息
  160. $teamKeys = TeamService::getTeams()->pluck('dept_name', 'dept_id');
  161. $now = time();
  162. foreach ($items as &$item) {
  163. $item->mask_mobile = self::handlePhone($item->mobile);
  164. $visitTimeInt = strtotime($item->visit_time);
  165. $endTime = $visitTimeInt + 60 * 60 * 24 * 30;
  166. $visitTimeInt = $endTime - $now;
  167. if ($visitTimeInt <= 0) {
  168. $visitTimeInt = 0;
  169. }
  170. if (!empty($item->consultant)) {
  171. $item->consultant->dept_name = $teamKeys[$item->consultant->dept_id] ?? '';
  172. }
  173. if (!empty($item->report)) {
  174. $item->report->dept_name = $teamKeys[$item->report->dept_id] ?? '';
  175. }
  176. $item->visit_time_int = $visitTimeInt;
  177. }
  178. }
  179. //本日新增
  180. // $date = date('Y-m-d');
  181. // $start = strtotime($date.' 00:00:00');
  182. // $end = strtotime($date.' 23:59:59');
  183. // $newNums = MarketCustomer::where($where)->whereBetween('created_at', [$start, $end])->count();
  184. // $followNums = MarketCustomerFollow::where($whereFollow)->whereBetween('created_at', [$start, $end])->count();
  185. $data = [
  186. 'new_follow_num' => $followNums ?? 0,
  187. 'new_custom_num' => $newNums ?? 0,
  188. 'total' => $total,
  189. 'rows' => $items
  190. ];
  191. return json_success('success', $data);
  192. }
  193. /**
  194. * Notes: 我的客户
  195. * User: yb
  196. * Date: 2024/8/13
  197. * Time: 15:14
  198. * @param $params
  199. */
  200. public static function myCustomList($params)
  201. {
  202. $currentTime = time();
  203. $diffNums = self::DIFF_TIME;
  204. $page = $params['page'] ?? 1;
  205. $size = $params['size'] ?? 10;
  206. //查询有效顾问信息
  207. $consultantId = JwtToken::getCurrentId();
  208. $where = [
  209. ['consultant_id', '=', $consultantId],
  210. ['current_status', '<>', '-1'],
  211. ['check_status', '<>', '-1']
  212. ];
  213. $where[] = [function($query) use ($diffNums, $currentTime){
  214. $query->orWhereRaw("check_status IN (-1, 1)")
  215. ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
  216. ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
  217. }];
  218. if (!empty($params['custom'])) {
  219. $keywords = $params['custom'];
  220. $where[] = [function($query) use ($keywords) {
  221. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  222. }];
  223. }
  224. $paginator = MarketCustomer::where($where)->orderBy('created_at', 'desc')->paginate($size, ['id','name','mobile','gender'], 'page', $page);
  225. $total = $paginator->total();
  226. $items = $paginator->items();
  227. if (!empty($items)) {
  228. foreach ($items as &$item) {
  229. $item->mask_mobile = self::handlePhone($item->mobile);
  230. }
  231. }
  232. $data = [
  233. 'total' => $total,
  234. 'rows' => $items
  235. ];
  236. return json_success('success', $data);
  237. }
  238. /**
  239. * Notes: 添加客户
  240. * User: yb
  241. * Date: 2024/8/6
  242. * Time: 11:20
  243. */
  244. public static function add($params)
  245. {
  246. $params = MarketCustomer::handleNumParams($params);
  247. $mobile = $params['mobile'];
  248. //查询客户手机号是否已经存在
  249. if (MarketCustomer::checkCustomExists($mobile)) {
  250. return json_fail('客户已经到访');
  251. }
  252. //查询顾问信息
  253. $consultantId = JwtToken::getCurrentId();
  254. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  255. if (empty($consultantInfo)) {
  256. return json_fail('顾问信息不存在');
  257. }
  258. $deptId = $consultantInfo->dept_id;
  259. $insertData = [
  260. 'name' => $params['name'],
  261. 'mobile' => $mobile,
  262. 'consultant_id' => 0,
  263. 'report_consultant_id' => $consultantId,
  264. 'create_consultant_id' => $consultantId,
  265. 'dept_id' => $deptId,
  266. 'gender' => $params['gender'] ?? null,
  267. 'visit_type' => $params['visit_type'] ?? null,
  268. 'require_area' => $params['require_area'] ?? null,
  269. 'area' => $params['area'] ?? null,
  270. 'requirement' => $params['requirement'] ?? null,
  271. 'age_range' => $params['age_range'] ?? null,
  272. 'focus' => $params['focus'] ?? null,
  273. 'region' => $params['region'] ?? null,
  274. 'purpose' => $params['purpose'] ?? null,
  275. 'level' => $params['level'] ?? null,
  276. 'type' => $params['type'] ?? null,
  277. 'visit_time' => time(),
  278. 'note' => $params['note'] ?? '',
  279. 'check_status' => 1,
  280. 'current_status' => 1,
  281. 'belong_status' => 1,
  282. 'created_at' => time()
  283. ];
  284. Db::beginTransaction();
  285. try {
  286. $customId = MarketCustomer::insertGetId($insertData);
  287. Db::commit();
  288. }catch (BusinessException|\Exception $e){
  289. Db::rollBack();
  290. return json_fail($e->getMessage());
  291. }
  292. if ($customId) {
  293. return json_success('添加成功');
  294. } else {
  295. return json_fail('添加失败');
  296. }
  297. }
  298. /**
  299. * Notes: 编辑客户
  300. * User: yb
  301. * Date: 2024/8/12
  302. * Time: 10:23
  303. * @param $params
  304. * @return \support\Response
  305. */
  306. public static function edit($params)
  307. {
  308. $consultantId = JwtToken::getCurrentId();
  309. $params = MarketCustomer::handleNumParams($params);
  310. if (empty($params['id'])) {
  311. return json_fail('客户id不能为空');
  312. }
  313. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  314. if (empty($consultantInfo)) {
  315. return json_fail('顾问信息不存在');
  316. }
  317. $info = MarketCustomer::firstWhere(['id' => $params['id'], 'consultant_id' => $consultantId]);
  318. if (empty($info)) {
  319. return json_fail('客户信息不存在');
  320. }
  321. $updateData = [
  322. 'name' => $params['name'],
  323. 'gender' => $params['gender'] ?? null,
  324. 'visit_type' => $params['visit_type'] ?? null,
  325. 'require_area' => $params['require_area'] ?? null,
  326. 'area' => $params['area'] ?? null,
  327. 'requirement' => $params['requirement'] ?? null,
  328. 'age_range' => $params['age_range'] ?? null,
  329. 'focus' => $params['focus'] ?? null,
  330. 'region' => $params['region'] ?? null,
  331. 'purpose' => $params['purpose'] ?? null,
  332. 'level' => $params['level'] ?? null,
  333. 'type' => $params['type'] ?? null,
  334. 'note' => $params['note'] ?? '',
  335. 'updated_at' => time()
  336. ];
  337. if (empty($params['mobile'])) {
  338. return json_fail('手机号不能为空');
  339. }
  340. $mobile = $params['mobile'];
  341. if (false === strpos($mobile,'****')) {
  342. //校验手机号格式
  343. $validate = new CustomValidate();
  344. if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
  345. return json_fail($validate->getError());
  346. }
  347. //修改手机号操作
  348. if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
  349. return json_fail('客户已经登记过了');
  350. }
  351. $updateData['mobile'] = $mobile;
  352. }
  353. $currentStatus = $info->current_status;
  354. if ($currentStatus == 1) {
  355. //已来电改为已到访
  356. if ($params['type'] == 2) {
  357. $updateData['current_status'] = 2;
  358. }
  359. }
  360. $result = false;
  361. Db::beginTransaction();
  362. try {
  363. $result = MarketCustomer::where('id', $params['id'])->update($updateData);
  364. Db::commit();
  365. }catch (BusinessException|\Exception $e){
  366. Db::rollBack();
  367. return json_fail($e->getMessage());
  368. }
  369. if ($result) {
  370. return json_success('编辑成功');
  371. } else {
  372. return json_fail('编辑失败');
  373. }
  374. }
  375. /**
  376. * Notes: 客户详情
  377. * User: yb
  378. * Date: 2024/8/8
  379. * Time: 15:36
  380. * @param $params
  381. */
  382. public static function info($id)
  383. {
  384. $fn = function ($query) {
  385. $query->select('name', 'mobile', 'id', 'dept_id');
  386. };
  387. $customInfo = MarketCustomer::with(['consultant' => $fn, 'report' => $fn])->firstWhere(['id' => $id]);
  388. if (empty($customInfo)) {
  389. return json_fail('客户不存在');
  390. }
  391. $teamKeys = TeamService::getTeams()->pluck('dept_name', 'dept_id');
  392. if (!empty($customInfo->consultant)) {
  393. $customInfo->consultant->dept_name = $teamKeys[$customInfo->consultant->dept_id] ?? '';
  394. }
  395. if (!empty($customInfo->report)) {
  396. $customInfo->report->dept_name = $teamKeys[$customInfo->report->dept_id] ?? '';
  397. }
  398. //手机号加密处理
  399. $customInfo->mask_mobile = self::handlePhone($customInfo->mobile);
  400. //查询最新的跟进记录
  401. $follow = MarketCustomerFollow::where('market_customer_id', $id)->orderBy('created_at', 'desc')->first();
  402. $customInfo->follow = $follow;
  403. $focus = $customInfo->focus;
  404. if (!empty($focus)) {
  405. $customInfo->focus = explode(',', $focus);
  406. }
  407. return json_success('', $customInfo);
  408. }
  409. /**
  410. * Notes: 更新客户状态
  411. * User: yb
  412. * Date: 2024/8/14
  413. * Time: 13:23
  414. * @param $params
  415. */
  416. public static function updateStatus($params)
  417. {
  418. $customId = $params['id'];
  419. $currentStatus = $params['current_status'];
  420. if (!in_array($currentStatus, [-1, 2, 3, 4])) {
  421. return json_fail('状态值非法');
  422. }
  423. $info = MarketCustomer::firstWhere(['id' => $customId]);
  424. if (empty($info)) {
  425. return json_fail('客户不存在');
  426. }
  427. if ($info->current_status != 1) {
  428. return json_fail('客户非已报备状态');
  429. }
  430. if ($currentStatus >= 2) {
  431. //验证是否已经存在其他到访客户
  432. if (MarketCustomer::checkCustomExists($info->mobile)) {
  433. return json_fail('操作失败,客户已存在');
  434. }
  435. }
  436. Db::beginTransaction();
  437. $result = false;
  438. try {
  439. if ($currentStatus == 2) {
  440. $info->visit_time = time();
  441. }
  442. $info->current_status = $currentStatus;
  443. $result = $info->save();
  444. if ($result) {
  445. if ($currentStatus >= 2) {
  446. $where = [
  447. ['id', '<>', $customId]
  448. ];
  449. //将其他的改为无效客户
  450. MarketCustomer::where($where)->where('check_status', 2)->update(['current_status' => -1]);
  451. //将待审核的改为拒绝
  452. MarketCustomer::where($where)->where('check_status', 1)->update(['check_status' => -1, 'check_note' => self::CHECK_NOTE]);
  453. }
  454. }
  455. Db::commit();
  456. }catch (BusinessException|\Exception $e){
  457. Db::rollBack();
  458. return json_fail($e->getMessage());
  459. }
  460. if ($result) {
  461. return json_success('操作成功');
  462. } else {
  463. return json_fail('操作失败');
  464. }
  465. }
  466. /**
  467. * Notes: 指定顾问
  468. * User: yb
  469. * Date: 2024/9/5
  470. * Time: 12:52
  471. * @param $params
  472. */
  473. public static function appoint($params)
  474. {
  475. if (empty($params['id'])) {
  476. return json_fail('请选择客户');
  477. }
  478. if (empty($params['report_consultant_id'])) {
  479. return json_fail('请选择指定顾问');
  480. }
  481. $userId = JwtToken::getCurrentId();
  482. //获取绑定的管理信息
  483. $userInfo = Consultant::firstWhere(['id' => $userId]);
  484. if (empty($userInfo)) {
  485. return json_fail('管理员信息不存在');
  486. }
  487. if (!in_array($userInfo->type, [1,3])) {
  488. return json_fail('操作权限不足');
  489. }
  490. $relationUserId = $userInfo->relation_user_id;
  491. $customId = $params['id'];
  492. $reportConsultantId = $params['report_consultant_id'];
  493. $customInfo = MarketCustomer::firstWhere(['id' => $customId]);
  494. if (empty($customInfo)) {
  495. return json_fail('客户信息不存在');
  496. }
  497. $consultantInfo = Consultant::firstWhere(['id' => $reportConsultantId]);
  498. if (empty($consultantInfo)) {
  499. return json_fail('顾问信息不存在');
  500. }
  501. $currentDeptId = $consultantInfo->dept_id; //当前部门
  502. $now = time();
  503. $logData = [
  504. 'market_customer_id' => $customId,
  505. 'consultant_id' => $reportConsultantId,
  506. 'dept_id' => $currentDeptId,
  507. 'before_consultant_id' => $customInfo->consultant_id,
  508. 'before_dept_id' => $customInfo->dept_id,
  509. 'note' => $params['note'] ?? '判客指定报备顾问',
  510. 'change_user_id' => $relationUserId,
  511. 'change_consultant_id' => $userId,
  512. 'created_at' => $now
  513. ];
  514. Db::beginTransaction();
  515. $result = false;
  516. try {
  517. $customInfo->report_consultant_id = $reportConsultantId;
  518. $customInfo->dept_id = $currentDeptId;
  519. $result = $customInfo->save();
  520. MarketCustomerLogs::insert($logData);
  521. Db::commit();
  522. }catch (BusinessException|\Exception $e) {
  523. Db::rollBack();
  524. return json_fail($e->getMessage());
  525. }
  526. if ($result) {
  527. return json_success('指定成功');
  528. } else {
  529. return json_fail('指定失败');
  530. }
  531. }
  532. /**
  533. * Notes: 转移客户
  534. * User: yb
  535. * Date: 2024/8/7
  536. * Time: 14:27
  537. * @param $params
  538. */
  539. public static function moveCustom($params)
  540. {
  541. $userId = JwtToken::getCurrentId();
  542. //获取绑定的管理信息
  543. $userInfo = Consultant::firstWhere(['id' => $userId]);
  544. if (empty($userInfo)) {
  545. return json_fail('管理员信息不存在');
  546. }
  547. if (!in_array($userInfo->type, [1,3])) {
  548. return json_fail('操作权限不足');
  549. }
  550. $relationUserId = $userInfo->relation_user_id;
  551. $currentConsultantId = $params['consultant_id']; //当前顾问
  552. $consultantId = $params['move_consultant_id'] ?? 0;
  553. $customId = $params['move_market_customer_id'] ?? 0;
  554. $note = $params['note'] ?? '';
  555. $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
  556. if (empty($consultantInfo)) {
  557. return json_fail('接收成员不存在');
  558. }
  559. $currentDeptId = $consultantInfo->dept_id; //当前部门
  560. $logsInertData = [];
  561. $now = time();
  562. $whereCustom = [];
  563. if (!empty($customId)) {
  564. $whereCustom[] = ['id', '=', $customId];
  565. }
  566. if (!empty($consultantId)) {
  567. $whereCustom[] = ['consultant_id', '=', $consultantId];
  568. }
  569. $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id'])->get();
  570. if (!$customList->isEmpty()) {
  571. foreach ($customList as$item) {
  572. $logsInertData[] = [
  573. 'market_customer_id' => $item->id,
  574. 'consultant_id' => $currentConsultantId,
  575. 'dept_id' => $currentDeptId,
  576. 'before_consultant_id' => $item->consultant_id,
  577. 'before_dept_id' => $item->dept_id,
  578. 'note' => $note,
  579. 'change_user_id' => $relationUserId,
  580. 'change_consultant_id' => $userId,
  581. 'created_at' => $now
  582. ];
  583. }
  584. }
  585. if ($customList->isEmpty()) {
  586. return json_fail('成员没有客户记录');
  587. }
  588. if (empty($logsInertData)) {
  589. return json_fail('移交记录不能为空');
  590. }
  591. Db::beginTransaction();
  592. $result = false;
  593. try {
  594. foreach ($customList as $item) {
  595. $item->consultant_id = $currentConsultantId;
  596. $item->dept_id = $currentDeptId;
  597. $item->save();
  598. }
  599. $result = MarketCustomerLogs::insert($logsInertData);
  600. Db::commit();
  601. }catch (BusinessException|\Exception $e) {
  602. Db::rollBack();
  603. return json_fail($e->getMessage());
  604. }
  605. if ($result) {
  606. return json_success('移交成功');
  607. } else {
  608. return json_fail('移交失败');
  609. }
  610. }
  611. /**
  612. * Notes: 转移记录
  613. * User: yb
  614. * Date: 2024/8/7
  615. * Time: 15:33
  616. * @param $params
  617. */
  618. public static function moveLogs($params)
  619. {
  620. $page = $params['page'] ?? 1;
  621. $size = $params['size'] ?? 10;
  622. $where = [];
  623. if (!empty($params['custom_id'])) {
  624. $where[] = ['market_customer_id', '=', $params['custom_id']];
  625. }
  626. $selectFn = function ($query){
  627. $query->select('name', 'mobile', 'id');
  628. };
  629. $paginator = MarketCustomerLogs::with(['custom' => $selectFn, 'beforeMan' => $selectFn, 'currentMan' => $selectFn])
  630. ->where($where)
  631. ->orderBy('created_at', 'desc')
  632. ->paginate($size, '*', 'page', $page);
  633. $total = $paginator->total();
  634. $items = $paginator->items();
  635. $data = [
  636. 'total' => $total,
  637. 'rows' => $items
  638. ];
  639. return json_success('success', $data);
  640. }
  641. /**
  642. * Notes: 审核客户
  643. * User: yb
  644. * Date: 2024/8/16
  645. * Time: 11:21
  646. * @param $params
  647. */
  648. public static function checkCustom($params)
  649. {
  650. $userId = JwtToken::getCurrentId();
  651. $adminId = Consultant::where('id', '=', $userId)->value('relation_user_id');
  652. //查询客户是否已经存在
  653. $customId = $params['id'];
  654. $checkStatus = $params['check_status'];
  655. if (!in_array($checkStatus, [-1,2])) {
  656. return json_fail('状态值非法');
  657. }
  658. $info = MarketCustomer::firstWhere(['id' => $customId]);
  659. if (empty($info)) {
  660. return json_fail('客户不存在');
  661. }
  662. $status = $info->check_status;
  663. if ($status != 1) {
  664. return json_fail('客户不是待转状态');
  665. }
  666. $mobile = $info->mobile;
  667. if ($checkStatus == 2) {
  668. if (MarketCustomer::checkCustomExists($mobile)) {
  669. return json_fail('客户已经存在');
  670. }
  671. }
  672. $consultantId = $params['consultant_id'];
  673. $result = false;
  674. Db::beginTransaction();
  675. try {
  676. $info->check_status = $checkStatus;
  677. if ($checkStatus == -1) {
  678. //拒绝录入拒绝理由
  679. $info->check_note = $params['note'] ?? '无拒绝理由';
  680. }
  681. $info->consultant_id = $consultantId;
  682. $info->current_status = $checkStatus;
  683. $info->check_time = time();
  684. $info->check_admin_id = $adminId;
  685. $info->check_consultant_id = $userId;
  686. $result = $info->save();
  687. if ($checkStatus == 2) {
  688. //将其他待审的相同手机号的改拒绝
  689. $where = [
  690. ['mobile' , '=', $mobile],
  691. ['check_status', '=', 1],
  692. ['id', '<>', $customId]
  693. ];
  694. MarketCustomer::where($where)->update(['current_status' => -1, 'check_time' => time(),'check_status' => -1,'check_note' => self::CHECK_NOTE]);
  695. }
  696. Db::commit();
  697. }catch (BusinessException|\Exception $e){
  698. Db::rollBack();
  699. return json_fail($e->getMessage());
  700. }
  701. if ($result) {
  702. return json_success('审核成功');
  703. } else {
  704. return json_fail('审核失败');
  705. }
  706. }
  707. /**
  708. * Notes: 检查客户是否已经登记
  709. * User: yb
  710. * Date: 2024/8/21
  711. * Time: 9:40
  712. * @param $params
  713. * @return \support\Response
  714. */
  715. public static function checkPhone($params)
  716. {
  717. if (empty($params['mobile'])) {
  718. return json_fail('请输入联系电话');
  719. }
  720. $mobile = $params['mobile'];
  721. //添加和编辑
  722. if (!empty($params['id'])) {
  723. //编辑的情况
  724. if (false === strpos($mobile,'****')) {
  725. //校验手机号格式
  726. $validate = new CustomValidate();
  727. if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
  728. return json_fail($validate->getError());
  729. }
  730. //修改手机号操作
  731. if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
  732. return json_fail('客户已经登记过了');
  733. }
  734. }
  735. return json_success('请求成功',['is_ex' => 2]);
  736. } else {
  737. $validate = new CustomValidate();
  738. if (!$validate->scene('phone')->check(['mobile' => $mobile])) {
  739. return json_fail($validate->getError());
  740. }
  741. //添加
  742. $result = MarketCustomer::checkCustomExists($mobile);
  743. if ($result) {
  744. $isEx = 1;
  745. } else {
  746. $isEx = 2;
  747. }
  748. return json_success('请求成功',['is_ex' => $isEx]);
  749. }
  750. }
  751. /**
  752. * Notes: 对手机号加密处理
  753. * User: yb
  754. * Date: 2024/8/8
  755. * Time: 15:41
  756. * @param $val
  757. * @return string|string[]
  758. */
  759. public static function handlePhone($val)
  760. {
  761. return substr_replace($val, '****', 3, 4);
  762. }
  763. /**
  764. * Notes: 处理拜访时间
  765. * User: yb
  766. * Date: 2024/8/8
  767. * Time: 16:06
  768. * @param $val
  769. * @return float|int
  770. */
  771. protected static function handleVisitTime($val) {
  772. return (strtotime($val) * 1000);
  773. }
  774. }