CustomService.php 28 KB

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