CustomService.php 40 KB

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