CustomService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. <?php
  2. namespace app\admin\service\consultant;
  3. use app\admin\service\member\MemberService;
  4. use app\model\Consultant;
  5. use app\model\MarketCustomer;
  6. use app\model\MarketCustomerFollow;
  7. use app\model\MarketCustomerLogs;
  8. use app\model\Member;
  9. use app\model\SysDept;
  10. use support\Db;
  11. use support\exception\BusinessException;
  12. use support\Request;
  13. use Tinywan\Jwt\JwtToken;
  14. class CustomService
  15. {
  16. const DIFF_TIME = (60 * 60 * 24 * 30);
  17. /**
  18. * Notes: 返回选项配置信息
  19. * User: yb
  20. * Date: 2024/8/5
  21. * Time: 16:05
  22. * @return array
  23. */
  24. public static function config()
  25. {
  26. return MarketCustomer::config();
  27. }
  28. public static function commonSearch($params)
  29. {
  30. $where = [];
  31. $currentTime = time();
  32. $diffNums = self::DIFF_TIME;
  33. $deptIds = TeamService::getIdsByUser();
  34. if (false === $deptIds) {
  35. //无权限
  36. $where[] = ['dept_id', '=', 0];
  37. } else if (is_array($deptIds)) {
  38. //指定团队下的权限
  39. if (!empty($params['dept_id'])) {
  40. $deptId = end($params['dept_id']);
  41. $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
  42. }
  43. $where[] = [function($query) use ($deptIds) {
  44. $query->whereIn('dept_id', $deptIds);
  45. }];
  46. } else {
  47. if (!empty($params['dept_id'])) {
  48. $deptId = end($params['dept_id']);
  49. $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
  50. $where[] = [function($query) use ($deptIds) {
  51. $query->whereIn('dept_id', $deptIds);
  52. }];
  53. }
  54. }
  55. if (!empty($params['name'])) {
  56. $keywords = $params['name'];
  57. $where[] = [function($query) use ($keywords) {
  58. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  59. }];
  60. }
  61. if (!empty($params['mobile'])) {
  62. $mobile = $params['mobile'];
  63. $where[] = [function($query) use ($mobile) {
  64. $query->where('mobile', 'like', "%{$mobile}%");
  65. }];
  66. }
  67. if (!empty($params['report_status'])) {
  68. if ($params['report_status'] == -1) {
  69. //待审核
  70. $where[] = ['check_status', '=', 1];
  71. } else {
  72. $where[] = ['check_status', '=', 2];
  73. }
  74. if ($params['report_status'] == 1) {
  75. //已报备
  76. $where[] = [function($query) use ($diffNums, $currentTime) {
  77. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
  78. }];
  79. } else if ($params['report_status'] == 3) {
  80. //已锁定
  81. $where[] = [function($query) {
  82. $query->whereIn('current_status', [2,3,4]);
  83. }];
  84. } else if ($params['report_status'] == 2) {
  85. //已失效
  86. $where[] = [function($query) use ($diffNums, $currentTime){
  87. $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1 AND check_status = 2)");
  88. }];
  89. }
  90. }
  91. if (!empty($params['consultant_name'])) {
  92. $consultantIds = Consultant::where('name', 'like', "%{$params['consultant_name']}%")->pluck('id');
  93. $where[] = [function($query) use ($consultantIds) {
  94. $query->whereIn('consultant_id', $consultantIds);
  95. }];
  96. }
  97. if (!empty($params['type'])) {
  98. $where[] = ['type', '=', $params['type']];
  99. }
  100. if (!empty($params['visit_time'])) {
  101. $datetime = $params['visit_time'];
  102. $datetime[0] = strtotime($datetime[0].' 00:00:00');
  103. $datetime[1] = strtotime($datetime[1].' 23:59:59');
  104. $where[] = [function($query) use ($datetime) {
  105. $query->whereBetween('visit_time', $datetime);
  106. }];
  107. }
  108. if (!empty($params['visit_date'])) {
  109. $visitDate = $params['visit_date'];
  110. $visitDate[0] = strtotime($visitDate[0].' 00:00:00');
  111. $visitDate[1] = strtotime($visitDate[1].' 23:59:59');
  112. $where[] = [function($query) use ($visitDate) {
  113. $query->whereBetween('visit_time', $visitDate);
  114. }];
  115. }
  116. if (!empty($params['created_date'])) {
  117. $createdDate = $params['created_date'];
  118. $createdDate[0] = strtotime($createdDate[0].' 00:00:00');
  119. $createdDate[1] = strtotime($createdDate[1].' 23:59:59');
  120. $where[] = [function($query) use ($createdDate) {
  121. $query->whereBetween('created_at', $createdDate);
  122. }];
  123. }
  124. if (!empty($params['current_status'])) {
  125. $currentStatus = $params['current_status'];
  126. $where[] = [function($query) use ($currentStatus){
  127. $query->where('current_status', '=', $currentStatus)->where('check_status', '=', 2);
  128. }];
  129. }
  130. if (!empty($params['consultant_id'])) {
  131. $where[] = ['consultant_id', '=', $params['consultant_id']];
  132. }
  133. if (!empty($params['custom'])) {
  134. $keywords = $params['custom'];
  135. $where[] = [function($query) use ($keywords) {
  136. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  137. }];
  138. }
  139. if (!empty($params['check_status'])) {
  140. $where[] = ['check_status', '=', $params['check_status']];
  141. }
  142. if (!empty($params['stat_report_status'])) {
  143. //已报备 1
  144. $statReportStatus = $params['stat_report_status'];
  145. if ($statReportStatus == 1) {
  146. $where[] = [function($query) use ($diffNums, $currentTime) {
  147. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1)->where('check_status', '=', 2);
  148. }];
  149. }
  150. //已锁定 2
  151. if ($statReportStatus == 2) {
  152. $where[] = [function($query) {
  153. $query->whereIn('current_status', [2,3,4])->where('check_status', '=', 2);
  154. }];
  155. }
  156. //已失效 -1
  157. if ($statReportStatus == -1) {
  158. $where[] = [function($query) use ($diffNums, $currentTime){
  159. $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1 AND check_status = 2)")->orWhere('check_status', '=', -1);
  160. }];
  161. }
  162. }
  163. if (isset($params['require_area'])) {
  164. if (is_numeric($params['require_area'])) {
  165. $where[] = ['require_area', '=', ($params['require_area'] + 1)];
  166. }
  167. }
  168. if (isset($params['requirement'])) {
  169. if (is_numeric($params['requirement'])) {
  170. $where[] = ['requirement', '=', ($params['requirement'] + 1)];
  171. }
  172. }
  173. if (isset($params['age_range'])) {
  174. if (is_numeric($params['age_range'])) {
  175. $where[] = ['age_range', '=', ($params['age_range'] + 1)];
  176. }
  177. }
  178. if (isset($params['visit_type'])) {
  179. if (is_numeric($params['visit_type'])) {
  180. $where[] = ['visit_type', '=', $params['visit_type']];
  181. }
  182. }
  183. if (isset($params['region'])) {
  184. if (is_numeric($params['region'])) {
  185. $where[] = ['region', '=', ($params['region'] + 1)];
  186. }
  187. }
  188. if (isset($params['purpose'])) {
  189. if (is_numeric($params['purpose'])) {
  190. $where[] = ['purpose', '=', ($params['purpose'] + 1)];
  191. }
  192. }
  193. if (isset($params['level'])) {
  194. if (is_numeric($params['level'])) {
  195. $where[] = ['level', '=', ($params['level'] + 1)];
  196. }
  197. }
  198. return $where;
  199. }
  200. /**
  201. * Notes: 列表
  202. * User: yb
  203. * Date: 2024/8/6
  204. * Time: 15:07
  205. * @param Request $request
  206. */
  207. public static function index(Request $request)
  208. {
  209. $format = $request->get('format', 'normal');
  210. $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
  211. $limit = $limit <= 0 ? 10 : $limit;
  212. $params = $request->get();
  213. $page = (int)$request->get('page');
  214. $page = $page > 0 ? $page : 1;
  215. $currentTime = time();
  216. $diffNums = self::DIFF_TIME;
  217. $where = self::commonSearch($params);
  218. $paginator = MarketCustomer::where($where)->orderBy('visit_time', 'desc')->paginate($limit, '*', 'page', $page);
  219. $total = $paginator->total();
  220. $items = $paginator->items();
  221. if (!empty($items)) {
  222. $consultantKeys = [];
  223. $consultantIds = $paginator->pluck('consultant_id')->toArray();
  224. if (!empty($consultantIds)) {
  225. //去重
  226. $consultantIds = array_unique($consultantIds);
  227. //排序
  228. $consultantIds = array_values($consultantIds);
  229. //查询顾问信息
  230. $consultantList = Consultant::whereIn('id', $consultantIds)->select(['id', 'name', 'mobile'])->get();
  231. if (!$consultantList->isEmpty()) {
  232. foreach ($consultantList->toArray() as $consultantItem) {
  233. $consultantKeys[$consultantItem['id']] = $consultantItem;
  234. }
  235. }
  236. }
  237. $teamKeys = TeamService::getItemKeys();
  238. foreach ($items as &$item) {
  239. $visitTime = $item->visit_time;
  240. $visitTimeStamp = strtotime($visitTime);
  241. $diff = ($visitTimeStamp + $diffNums) - $currentTime;
  242. $item->diff_nums = ($diff > 0) ? $diff : 0;
  243. $item->consultant_name = $consultantKeys[$item->consultant_id]['name'] ?? '';
  244. $item->consultant_mobile = $consultantKeys[$item->consultant_id]['mobile'] ?? '';
  245. $item->team_name = TeamService::getTeamName($teamKeys, $item->dept_id);
  246. $item->mask_mobile = self::handlePhone($item->mobile);
  247. }
  248. }
  249. $data = [
  250. 'total' => $total,
  251. 'rows' => $items
  252. ];
  253. return json_success('success', $data);
  254. }
  255. /**
  256. * Notes: 添加客户
  257. * User: yb
  258. * Date: 2024/8/6
  259. * Time: 11:20
  260. */
  261. public static function add($params)
  262. {
  263. $params = MarketCustomer::handleNumParams($params);
  264. $mobile = $params['mobile'];
  265. //查询客户手机号是否已经存在
  266. if (MarketCustomer::checkCustomExists($mobile)) {
  267. return json_fail('客户已经登记过了');
  268. }
  269. //查询顾问信息
  270. $consultantId = $params['consultant_id'];
  271. $consultantInfo = Consultant::firstWhere(['id' => $consultantId]);
  272. if (empty($consultantInfo)) {
  273. return json_fail('顾问信息不存在');
  274. }
  275. $deptId = $consultantInfo->dept_id;
  276. $insertData = [
  277. 'name' => $params['name'],
  278. 'mobile' => $mobile,
  279. 'consultant_id' => $consultantId,
  280. 'dept_id' => $deptId,
  281. 'gender' => $params['gender'] ?? null,
  282. 'visit_type' => $params['visit_type'] ?? null,
  283. 'require_area' => $params['require_area'] ?? null,
  284. 'area' => $params['area'] ?? null,
  285. 'requirement' => $params['requirement'] ?? null,
  286. 'age_range' => $params['age_range'] ?? null,
  287. 'focus' => $params['focus'] ?? null,
  288. 'region' => $params['region'] ?? null,
  289. 'purpose' => $params['purpose'] ?? null,
  290. 'level' => $params['level'] ?? null,
  291. 'type' => $params['type'] ?? null,
  292. 'check_status' => 1,
  293. 'visit_time' => $params['visit_time'] ?? null,
  294. 'note' => $params['note'] ?? '',
  295. 'current_status' => $params['current_status'] ?? null,
  296. 'created_at' => time()
  297. ];
  298. Db::beginTransaction();
  299. try {
  300. //查询会员表中是否存在该客户
  301. // $memberId = Member::where('member_mobile', $insertData['mobile'])->value('member_id');
  302. // if (empty($memberId)) {
  303. // $result = MemberService::add([
  304. // 'account_name'=> $insertData['name'],
  305. // 'member_category' => '售房客户',
  306. // 'mobile' => $insertData['mobile'],
  307. // 'source' => 'HOUSE']);
  308. // $code = $result->getStatusCode();
  309. // if ($code == 200) {
  310. // $content = $result->rawBody();
  311. // if (is_json($content)) {
  312. // $content = json_decode($content, 1);
  313. // if ($content['code'] == 200) {
  314. // $memberId = $content['data']['member_id'];
  315. // } else {
  316. // throw new BusinessException('新增会员失败');
  317. // }
  318. // } else {
  319. // throw new BusinessException('新增会员失败');
  320. // }
  321. // } else {
  322. // throw new BusinessException('新增会员失败');
  323. // }
  324. // }
  325. // $insertData['member_id'] = $memberId;
  326. $customId = MarketCustomer::insertGetId($insertData);
  327. Db::commit();
  328. }catch (BusinessException|\Exception $e){
  329. Db::rollBack();
  330. return json_fail($e->getMessage());
  331. }
  332. if ($customId) {
  333. return json_success('添加成功');
  334. } else {
  335. return json_fail('添加失败');
  336. }
  337. }
  338. /**
  339. * Notes: 编辑客户
  340. * User: yb
  341. * Date: 2024/8/6
  342. * Time: 16:53
  343. * @param $params
  344. */
  345. public static function update($params)
  346. {
  347. $params = MarketCustomer::handleNumParams($params);
  348. $mobile = $params['mobile'];
  349. //查询客户手机号是否已经存在
  350. if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
  351. return json_fail('客户已经登记过了');
  352. }
  353. $updateData = [
  354. 'name' => $params['name'],
  355. 'mobile' => $params['mobile'],
  356. 'gender' => $params['gender'] ?? null,
  357. 'visit_type' => $params['visit_type'] ?? null,
  358. 'require_area' => $params['require_area'] ?? null,
  359. 'area' => $params['area'] ?? null,
  360. 'requirement' => $params['requirement'] ?? null,
  361. 'age_range' => $params['age_range'] ?? null,
  362. 'focus' => $params['focus'] ?? null,
  363. 'region' => $params['region'] ?? null,
  364. 'purpose' => $params['purpose'] ?? null,
  365. 'level' => $params['level'] ?? null,
  366. 'type' => $params['type'] ?? null,
  367. 'note' => $params['note'] ?? '',
  368. 'current_status' => $params['current_status'] ?? null,
  369. 'updated_at' => time()
  370. ];
  371. if (!empty($params['update_visit_time'])) {
  372. if ($params['update_visit_time'] == 1) {
  373. $updateData['visit_time'] = $params['visit_time'] ?? null;
  374. }
  375. }
  376. $result = MarketCustomer::where('id', $params['id'])->update($updateData);
  377. if ($result) {
  378. return json_success('编辑成功');
  379. } else {
  380. return json_fail('编辑失败');
  381. }
  382. }
  383. /**
  384. * Notes: 删除客户
  385. * User: yb
  386. * Date: 2024/8/2
  387. * Time: 13:33
  388. * @param $ids
  389. * @return \support\Response
  390. */
  391. public static function delete($ids)
  392. {
  393. if (!$ids) {
  394. return json_fail("数据错误");
  395. }
  396. if (!is_array($ids)) {
  397. $ids = [$ids];
  398. }
  399. try {
  400. if (is_array($ids)) {
  401. MarketCustomer::whereIn('id', $ids)->delete();
  402. } else {
  403. MarketCustomer::where('id', $ids)->delete();
  404. }
  405. } catch (\Exception $e) {
  406. return json_fail('删除失败');
  407. }
  408. return json_success('删除成功');
  409. }
  410. /**
  411. * Notes: 跟进记录
  412. * User: yb
  413. * Date: 2024/8/7
  414. * Time: 9:00
  415. * @param Request $request
  416. */
  417. public static function follow(Request $request)
  418. {
  419. $format = $request->get('format', 'normal');
  420. $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
  421. $limit = $limit <= 0 ? 10 : $limit;
  422. $params = $request->get();
  423. $page = (int)$request->get('page');
  424. $page = $page > 0 ? $page : 1;
  425. $where = [];
  426. $whereCustom = [];
  427. $whereConsultant = [];
  428. if (!empty($params['market_customer_id'])) {
  429. $marketCustomerIds = $params['market_customer_id'];
  430. $where[] = [function($query) use ($marketCustomerIds) {
  431. $query->whereIn('market_customer_id', $marketCustomerIds);
  432. }];
  433. }
  434. if (!empty($params['consultant_id'])) {
  435. $consultantId = $params['consultant_id'];
  436. $where[] = [function($query) use ($consultantId) {
  437. $query->whereIn('consultant_id', $consultantId);
  438. }];
  439. }
  440. if (!empty($params['follow_way'])) {
  441. $where[] = ['follow_way', '=', $params['follow_way']];
  442. }
  443. if (!empty($params['custom'])) {
  444. $custom = $params['custom'];
  445. $whereCustom[] = [function($query) use ($custom) {
  446. $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
  447. }];
  448. }
  449. if (!empty($params['consultant'])) {
  450. $consultant = $params['consultant'];
  451. $whereConsultant[] = [function($query) use ($consultant) {
  452. $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
  453. }];
  454. }
  455. if (!empty($params['follow_time'])) {
  456. $datetime = $params['follow_time'];
  457. $datetime[0] = strtotime($datetime[0].' 00:00:00');
  458. $datetime[1] = strtotime($datetime[1].' 23:59:59');
  459. $where[] = [function($query) use ($datetime) {
  460. $query->whereBetween('follow_time', $datetime);
  461. }];
  462. }
  463. $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
  464. $query->where($whereCustom);
  465. })->whereHas('consultant', function($query) use ($whereConsultant) {
  466. $query->where($whereConsultant);
  467. })->where($where)->orderBy('follow_time', 'desc')->paginate($limit, '*', 'page', $page);
  468. $total = $paginator->total();
  469. $items = $paginator->items();
  470. foreach ($items as &$item) {
  471. if (!empty($item->custom)) {
  472. $item->custom->mask_mobile = self::handlePhone($item->custom->mobile);
  473. }
  474. }
  475. $data = [
  476. 'total' => $total,
  477. 'rows' => $items
  478. ];
  479. return json_success('success', $data);
  480. }
  481. /**
  482. * Notes: 删除跟进记录
  483. * User: yb
  484. * Date: 2024/8/7
  485. * Time: 11:25
  486. * @param $ids
  487. */
  488. public static function deleteFollow($ids)
  489. {
  490. if (!$ids) {
  491. return json_fail("数据错误");
  492. }
  493. if (!is_array($ids)) {
  494. $ids = [$ids];
  495. }
  496. try {
  497. if (is_array($ids)) {
  498. MarketCustomerFollow::whereIn('id', $ids)->delete();
  499. } else {
  500. MarketCustomerFollow::where('id', $ids)->delete();
  501. }
  502. } catch (\Exception $e) {
  503. return json_fail('删除失败');
  504. }
  505. return json_success('删除成功');
  506. }
  507. /**
  508. * Notes: 转移客户
  509. * User: yb
  510. * Date: 2024/8/7
  511. * Time: 14:27
  512. * @param $params
  513. */
  514. public static function moveCustom($params)
  515. {
  516. $userId = JwtToken::getCurrentId();
  517. $currentConsultantId = $params['consultant_id']; //当前顾问
  518. $consultantIds = $params['move_consultant_id'] ?? [];
  519. $customIds = $params['move_market_customer_id'] ?? [];
  520. $note = $params['note'] ?? '';
  521. $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
  522. if (empty($consultantInfo)) {
  523. return json_fail('顾问不存在');
  524. }
  525. $currentDeptId = $consultantInfo->dept_id; //当前部门
  526. $logsInertData = [];
  527. $now = time();
  528. $whereCustom = [];
  529. if (!empty($customIds)) {
  530. $whereCustom[] = [function($query) use ($customIds) {
  531. $query->whereIn('id', $customIds);
  532. }];
  533. }
  534. if (!empty($consultantIds)) {
  535. $whereCustom[] = [function($query) use ($consultantIds) {
  536. $query->whereIn('consultant_id', $consultantIds);
  537. }];
  538. }
  539. $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id'])->get();
  540. if (!$customList->isEmpty()) {
  541. foreach ($customList as$item) {
  542. $logsInertData[] = [
  543. 'market_customer_id' => $item->id,
  544. 'consultant_id' => $currentConsultantId,
  545. 'dept_id' => $currentDeptId,
  546. 'before_consultant_id' => $item->consultant_id,
  547. 'before_dept_id' => $item->dept_id,
  548. 'note' => $note,
  549. 'change_user_id' => $userId,
  550. 'created_at' => $now
  551. ];
  552. }
  553. }
  554. if ($customList->isEmpty()) {
  555. return json_fail('移交的客户信息不存在');
  556. }
  557. if (empty($logsInertData)) {
  558. return json_fail('移交记录不能为空');
  559. }
  560. Db::beginTransaction();
  561. $result = false;
  562. try {
  563. foreach ($customList as $item) {
  564. $item->consultant_id = $currentConsultantId;
  565. $item->dept_id = $currentDeptId;
  566. $item->save();
  567. }
  568. $result = MarketCustomerLogs::insert($logsInertData);
  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: 15:33
  585. * @param Request $request
  586. */
  587. public static function moveLogs(Request $request)
  588. {
  589. $format = $request->get('format', 'normal');
  590. $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
  591. $limit = $limit <= 0 ? 10 : $limit;
  592. $params = $request->get();
  593. $page = (int)$request->get('page');
  594. $page = $page > 0 ? $page : 1;
  595. $platform = 0;
  596. $where = [];
  597. $whereCustom = [];
  598. $whereCurrent = [];
  599. $whereBefore = [];
  600. $whereOpBack = [];
  601. $whereOpFront = [];
  602. if (!empty($params['market_customer_id'])) {
  603. $where[] = ['market_customer_id', '=', $params['market_customer_id']];
  604. }
  605. if (!empty($params['consultant_id'])) {
  606. $consultantIds = $params['consultant_id'];
  607. $where[] = [function($query) use ($consultantIds) {
  608. $query->orWhereIn('consultant_id', $consultantIds)->orWhereIn('before_consultant_id', $consultantIds);
  609. }];
  610. }
  611. if (!empty($params['custom'])) {
  612. //客户信息
  613. $custom = $params['custom'];
  614. $whereCustom[] = ['name', 'like', "%{$custom}%"];
  615. }
  616. if (!empty($params['current_consultant'])) {
  617. //当前员工信息
  618. $current = $params['current_consultant'];
  619. $whereCurrent[] = ['name', 'like', "%{$current}%"];
  620. }
  621. if (!empty($params['before_consultant'])) {
  622. //转移前员工信息
  623. $before = $params['before_consultant'];
  624. $whereBefore[] = ['name', 'like', "%{$before}%"];
  625. }
  626. if (!empty($params['platform'])) {
  627. $platform = $params['platform'];
  628. if ($platform == 1) {
  629. //后台
  630. $where[] = ['change_user_id', '<>', null];
  631. $where[] = ['change_user_id', '=', null];
  632. } else {
  633. //小程序
  634. $where[] = ['change_user_id', '=', null];
  635. $where[] = ['change_user_id', '<>', null];
  636. }
  637. }
  638. if (!empty($params['op_name'])) {
  639. $opName = $params['op_name'];
  640. if (!empty($platform)) {
  641. if ($platform == 1) {
  642. $whereOpBack[] = ['user_name', 'like', "%{$opName}%"];
  643. } else {
  644. $whereOpFront[] = ['name', 'like', "%{$opName}%"];
  645. }
  646. } else {
  647. $whereOpBack[] = [function($query) use ($opName) {
  648. $query->orWhere('user_name', 'like', "%{$opName}%");
  649. }];
  650. $whereOpFront[] = [function($query) use ($opName) {
  651. $query->orWhere('name', 'like', "%{$opName}%");
  652. }];
  653. }
  654. }
  655. if (!empty($params['created_at'])) {
  656. $createdDate = $params['created_at'];
  657. $createdDate[0] = strtotime($createdDate[0].' 00:00:00');
  658. $createdDate[1] = strtotime($createdDate[1].' 23:59:59');
  659. $where[] = [function($query) use ($createdDate) {
  660. $query->whereBetween('created_at', $createdDate);
  661. }];
  662. }
  663. $paginator = MarketCustomerLogs::with(['custom', 'beforeMan', 'currentMan', 'opBackPerson', 'opFrontPerson'])
  664. ->whereHas('custom', function ($query) use ($whereCustom) {
  665. $query->where($whereCustom);
  666. })
  667. ->whereHas('currentMan', function ($query) use ($whereCurrent) {
  668. $query->where($whereCurrent);
  669. })
  670. ->whereHas('beforeMan', function ($query) use ($whereBefore) {
  671. $query->where($whereBefore);
  672. })
  673. ->where($where)
  674. ->orderBy('created_at', 'desc')
  675. ->paginate($limit, '*', 'page', $page);
  676. $total = $paginator->total();
  677. $items = $paginator->items();
  678. foreach ($items as &$item) {
  679. if (!empty($item->custom)) {
  680. $item->custom->mask_mobile = self::handlePhone($item->custom->mobile);
  681. }
  682. }
  683. $data = [
  684. 'total' => $total,
  685. 'rows' => $items
  686. ];
  687. return json_success('success', $data);
  688. }
  689. /**
  690. * Notes: 审核客户
  691. * User: yb
  692. * Date: 2024/8/16
  693. * Time: 11:21
  694. * @param $params
  695. */
  696. public static function checkCustom($params)
  697. {
  698. $adminId = JwtToken::getCurrentId();
  699. //查询客户是否已经存在
  700. $customId = $params['id'];
  701. $checkStatus = $params['check_status'];
  702. if (!in_array($checkStatus, [-1,2])) {
  703. return json_fail('状态值非法');
  704. }
  705. $info = MarketCustomer::firstWhere(['id' => $customId]);
  706. if (empty($info)) {
  707. return json_fail('客户不存在');
  708. }
  709. $status = $info->check_status;
  710. if ($status != 1) {
  711. return json_fail('客户不是待审核状态');
  712. }
  713. $mobile = $info->mobile;
  714. if ($checkStatus == 2) {
  715. if (MarketCustomer::checkCustomExists($mobile)) {
  716. return json_fail('客户已经存在');
  717. }
  718. }
  719. $result = false;
  720. Db::beginTransaction();
  721. try {
  722. $info->check_status = $checkStatus;
  723. if ($checkStatus == -1) {
  724. //拒绝录入拒绝理由
  725. $info->check_note = $params['note'] ?? '无拒绝理由';
  726. }
  727. $info->check_time = time();
  728. $info->check_admin_id = $adminId;
  729. $result = $info->save();
  730. if ($checkStatus == 2) {
  731. //将其他待审的相同手机号的改拒绝
  732. $where = [
  733. ['mobile' , '=', $mobile],
  734. ['check_status', '=', 1],
  735. ['id', '<>', $customId]
  736. ];
  737. MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1,'check_note' => '客户已经被其他顾问报备']);
  738. }
  739. Db::commit();
  740. }catch (BusinessException|\Exception $e){
  741. Db::rollBack();
  742. return json_fail($e->getMessage());
  743. }
  744. if ($result) {
  745. return json_success('审核成功');
  746. } else {
  747. return json_fail('审核失败');
  748. }
  749. }
  750. /**
  751. * Notes: 数据统计分析
  752. * User: yb
  753. * Date: 2024/8/17
  754. * Time: 11:27
  755. * @param $params
  756. */
  757. public static function statisticsIndex(Request $request)
  758. {
  759. $params = $request->get();
  760. $where = self::commonSearch($params);
  761. //客户总数
  762. $total = MarketCustomer::where($where)->count();
  763. //待审核
  764. $checkTotal = MarketCustomer::where($where)->where('check_status', 1)->count();
  765. //已报备
  766. $reportTotal = MarketCustomer::where($where)->where(
  767. [[function($query) {
  768. $currentTime = time();
  769. $diffNums = self::DIFF_TIME;
  770. $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0) AND current_status = 1 AND check_status = 2");
  771. }]]
  772. )->count();
  773. //已来电
  774. $mobileTotal = MarketCustomer::where($where)->where([[function($query) {
  775. $query->where('check_status', '=', 2)->where('current_status', '=', 1);
  776. }]])->count();
  777. //已到访
  778. $visitTotal = MarketCustomer::where($where)->where([[function($query) {
  779. $query->where('check_status', '=', 2)->where('current_status', '=', 2);
  780. }]])->count();
  781. //已缴费
  782. $payTotal = MarketCustomer::where($where)->where([[function($query) {
  783. $query->where('check_status', '=', 2)->where('current_status', '=', 3);
  784. }]])->count();
  785. //已成交
  786. $dealTotal = MarketCustomer::where($where)->where([[function($query) {
  787. $query->where('check_status', '=', 2)->where('current_status', '=', 4);
  788. }]])->count();
  789. //已到访 + 已缴费 + 已成交 / 客户总数
  790. $rant = 0;
  791. if ($total > 0) {
  792. $rant = $payTotal + $dealTotal / $total;
  793. }
  794. $rant = sprintf('%.2f', $rant);
  795. $data = [
  796. 'total' => $total,
  797. 'check_total' => $checkTotal,
  798. 'report_total' => $reportTotal,
  799. 'mobile_total' => $mobileTotal,
  800. 'visit_total' => $visitTotal,
  801. 'pay_total' => $payTotal,
  802. 'deal_total' => $dealTotal,
  803. 'rant' => $rant,
  804. ];
  805. return json_success('请求成功', $data);
  806. }
  807. /**
  808. * Notes: 前12个月统计数据
  809. * User: yb
  810. * Date: 2024/8/21
  811. * Time: 15:56
  812. */
  813. public static function statisticsMonth()
  814. {
  815. $months = month_12();
  816. sort($months);
  817. $startDate = $months[0];
  818. $startDateTime = strtotime($startDate.'/01 00:00:00');
  819. $endDateTime = time();
  820. $where = [
  821. ['created_at', '>=', $startDateTime],
  822. ['created_at', '<=', $endDateTime]
  823. ];
  824. $currentTime = time();
  825. $diffNums = self::DIFF_TIME;
  826. $deptIds = TeamService::getIdsByUser();
  827. if (false === $deptIds) {
  828. //无权限
  829. $where[] = ['dept_id', '=', 0];
  830. } else if (is_array($deptIds)) {
  831. //指定团队下的权限
  832. if (!empty($params['dept_id'])) {
  833. $deptId = end($params['dept_id']);
  834. $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
  835. }
  836. $where[] = [function($query) use ($deptIds) {
  837. $query->whereIn('dept_id', $deptIds);
  838. }];
  839. } else {
  840. if (!empty($params['dept_id'])) {
  841. $deptId = end($params['dept_id']);
  842. $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
  843. $where[] = [function($query) use ($deptIds) {
  844. $query->whereIn('dept_id', $deptIds);
  845. }];
  846. }
  847. }
  848. $selectRaw = Db::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y/%m') as date,count(*) as nums");
  849. $groupRaw = Db::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y/%m')");
  850. $totalData = MarketCustomer::where($where)->selectRaw($selectRaw)->groupByRaw($groupRaw)->get();
  851. $payData = MarketCustomer::where($where)->where([['current_status', '=', 3], ['check_status', '=', 2]])->selectRaw($selectRaw)->groupByRaw($groupRaw)->get();
  852. $visitData = MarketCustomer::where($where)->where([['current_status', '=', 2], ['check_status', '=', 2]])->selectRaw($selectRaw)->groupByRaw($groupRaw)->get();
  853. $dealData = MarketCustomer::where($where)->where([['current_status', '=', 4], ['check_status', '=', 2]])->selectRaw($selectRaw)->groupByRaw($groupRaw)->get();
  854. $returnData = [
  855. 'total' => $totalData->pluck('nums','date'),
  856. 'pay' => $payData->pluck('nums','date'),
  857. 'visit' => $visitData->pluck('nums','date'),
  858. 'deal' => $dealData->pluck('nums','date'),
  859. 'date' => $months
  860. ];
  861. return json_success('请求成功', $returnData);
  862. }
  863. /**
  864. * Notes: 导出数据
  865. * User: yb
  866. * Date: 2024/8/17
  867. * Time: 13:01
  868. */
  869. public static function exportData(Request $request)
  870. {
  871. $currentTime = time();
  872. $diffNums = self::DIFF_TIME;
  873. $params = $request->get();
  874. $where = self::commonSearch($params);
  875. if (!empty($params['custom_ids'])) {
  876. $customIds = $params['custom_ids'];
  877. $where[] = [function($query) use ($customIds){
  878. $query->whereIn('id', $customIds);
  879. }];
  880. }
  881. $list = MarketCustomer::where($where)->orderBy('visit_time', 'desc')->get();
  882. if (!$list->isEmpty()) {
  883. $consultantKeys = [];
  884. //查询顾问信息
  885. $consultantList = Consultant::select(['id', 'name', 'mobile'])->get();
  886. if (!$consultantList->isEmpty()) {
  887. foreach ($consultantList->toArray() as $consultantItem) {
  888. $consultantKeys[$consultantItem['id']] = $consultantItem;
  889. }
  890. }
  891. $teamKeys = TeamService::getItemKeys();
  892. $returnData = [];
  893. $genderData = ['', '男', '女'];
  894. $typeData = ['', '来电', '来访'];
  895. $currentStatusData = [-1 => '无效客户', 1 => '已来电', 2 => '已到访', 3 => '已缴费', 4 => '已成交'];
  896. foreach ($list as &$item) {
  897. $visitTime = $item->visit_time;
  898. $visitTimeStamp = strtotime($visitTime);
  899. $diff = ($visitTimeStamp + $diffNums) - $currentTime;
  900. $currentStatus = $item->current_status;//当前状态
  901. $checkStatus = $item->check_status;//审核状态
  902. if ($checkStatus == 1) {
  903. $currentStatusText = '待审核';
  904. }
  905. if ($checkStatus == -1) {
  906. $currentStatusText = '已失效';
  907. }
  908. if ($checkStatus == 2) {
  909. //审核通过
  910. if ($currentStatus > 1) {
  911. $currentStatusText = $currentStatusData[$currentStatus] ?? '';
  912. }
  913. if ($currentStatus == 1) {
  914. if ($diff > 0) {
  915. $currentStatusText = $currentStatusData[$currentStatus] ?? '';
  916. } else {
  917. $currentStatusText = '已失效';
  918. }
  919. }
  920. if ($currentStatus == -1) {
  921. $currentStatusText = '无效客户';
  922. }
  923. }
  924. $returnData[] = [
  925. 'name' => $item->name,
  926. 'mobile' => $item->mobile,
  927. 'mask_mobile' => self::handlePhone($item->mobile),
  928. 'gender' => $genderData[$item->gender] ?? '',
  929. 'type' => $typeData[$item->type] ?? '',
  930. 'consultant_name' => $consultantKeys[$item->consultant_id]['name'] ?? '',
  931. 'consultant_mobile' => $consultantKeys[$item->consultant_id]['mobile'] ?? '',
  932. 'team_name' => TeamService::getTeamName($teamKeys, $item->dept_id),
  933. 'current_status' => $currentStatusText, //当前状态
  934. 'visit_time' => $item->visit_time,
  935. 'created_at' => date('Y-m-d H:i:s',strtotime($item->created_at))
  936. ];
  937. }
  938. return json_success('请求成功', $returnData);
  939. }
  940. }
  941. /**
  942. * Notes: 对手机号加密处理
  943. * User: yb
  944. * Date: 2024/8/8
  945. * Time: 15:41
  946. * @param $val
  947. * @return string|string[]
  948. */
  949. public static function handlePhone($val)
  950. {
  951. return substr_replace($val, '****', 3, 4);
  952. }
  953. }