CustomService.php 31 KB

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