CustomService.php 32 KB

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