CustomService.php 44 KB

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