CustomerService.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\CustomerInfo;
  6. use App\Model\CustomerRepeat;
  7. use App\Model\Depart;
  8. use App\Model\Employee;
  9. use App\Model\FollowUpRecord;
  10. use App\Model\Product;
  11. use App\Model\RoleMenuButton;
  12. use App\Model\SeeRange;
  13. use App\Model\WxEmployeeOfficial;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 客户管理相关
  18. */
  19. class CustomerService extends Service
  20. {
  21. /**
  22. * 客户编辑
  23. * @param $data
  24. * @param $user
  25. * @return array
  26. */
  27. public function customerEdit($data,$user){
  28. list($status,$msg) = $this->customerRule($data,$user, false);
  29. if(!$status) return [$status,$msg];
  30. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $data['title'];
  31. $params = $this->getDataFile($data);
  32. (new OperationLogService())->setOperationList($params,$user,2);
  33. try {
  34. DB::beginTransaction();
  35. //车型
  36. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  37. $model_2 = new BasicType();
  38. $model_2->title = $data['car_type_title'];
  39. $model_2->type = 10;
  40. $model_2->depart_id = $data['depart_id'] ?? 0;
  41. $model_2->top_depart_id = $data['top_depart_id'] ?? 0;
  42. $model_2->crt_id = $user['id'];
  43. $model_2->save();
  44. $data['car_type'] = $model_2->id;
  45. }
  46. $model = Customer::where('id',$data['id'])->first();
  47. $model->title = $data['title'];
  48. $model->code = $data['code'] ?? "";
  49. $model->model_type = $data['model_type'];
  50. $model->customer_intention = $data['customer_intention'] ?? 0;
  51. $model->customer_from = $data['customer_from'] ?? 0;
  52. $model->customer_type = $data['customer_type'] ?? 0;
  53. $model->car_type = $data['car_type'] ?? 0;
  54. $model->consulting_product = $data['consulting_product'] ?? '';
  55. $model->intention_product = $data['intention_product'] ?? 0;
  56. $model->progress_stage = $data['progress_stage'] ?? 0;
  57. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  58. $model->address2 = $data['address2'] ?? '';
  59. $model->mark = $data['mark'] ?? '';
  60. $model->importance = $data['importance'] ?? '';
  61. $model->company = $data['company'] ?? '';
  62. // $model->company_short_name = $data['company_short_name'] ?? '';
  63. $model->state_type = $data['state_type'] ?? 0;
  64. $model->customer_state = $data['customer_state'] ?? 0;
  65. $model->enter_time = $data['enter_time'] ?? 0;
  66. $model->save();
  67. $time = time();
  68. $old = CustomerInfo::where('del_time',0)
  69. ->where('customer_id',$data['id'])
  70. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  71. ->select('file')
  72. ->get()->toArray();
  73. $old = array_column($old,'file');
  74. CustomerInfo::where('del_time',0)
  75. ->where('customer_id',$data['id'])
  76. ->whereNotIn('type',CustomerInfo::$no_edit)
  77. ->update(['del_time' => $time]);
  78. if(! empty($data['customer_contact'])){
  79. $insert = [];
  80. foreach ($data['customer_contact'] as $value){
  81. $insert[] = [
  82. 'customer_id' => $model->id,
  83. 'contact_type' => $value['id'],
  84. 'contact_info' => $value['info'],
  85. 'type' => CustomerInfo::type_one,
  86. 'crt_time' => $time,
  87. ];
  88. }
  89. CustomerInfo::insert($insert);
  90. }
  91. // if(! empty($data['employee_one'])){
  92. // $insert = [];
  93. // foreach ($data['employee_one'] as $value){
  94. // $insert[] = [
  95. // 'customer_id' => $model->id,
  96. // 'data_id' => $value,
  97. // 'type' => CustomerInfo::type_two,
  98. // 'crt_time' => $time,
  99. // ];
  100. // }
  101. // CustomerInfo::insert($insert);
  102. // }
  103. if(! empty($data['employee_two'])){
  104. $insert = [];
  105. foreach ($data['employee_two'] as $value){
  106. $insert[] = [
  107. 'customer_id' => $model->id,
  108. 'data_id' => $value,
  109. 'type' => CustomerInfo::type_three,
  110. 'crt_time' => $time,
  111. ];
  112. }
  113. CustomerInfo::insert($insert);
  114. }
  115. if(! empty($data['employee_three'])){
  116. $insert = [];
  117. foreach ($data['employee_three'] as $value){
  118. $insert[] = [
  119. 'customer_id' => $model->id,
  120. 'data_id' => $value,
  121. 'type' => CustomerInfo::type_four,
  122. 'crt_time' => $time,
  123. ];
  124. }
  125. CustomerInfo::insert($insert);
  126. }
  127. $new = [];
  128. if(! empty($data['img'])){
  129. $insert = [];
  130. foreach ($data['img'] as $value){
  131. $insert[] = [
  132. 'customer_id' => $model->id,
  133. 'file' => $value['url'],
  134. 'type' => CustomerInfo::type_five,
  135. 'name' => $value['name'],
  136. 'crt_time' => $time,
  137. ];
  138. if(in_array($value['url'], $old)) {
  139. foreach ($old as $o_k => $o_v){
  140. if($o_v == $value['url']) unset($old[$o_k]);
  141. }
  142. }else{
  143. $new[] = $value['url'];
  144. }
  145. }
  146. CustomerInfo::insert($insert);
  147. }
  148. if(! empty($data['file'])){
  149. $insert = [];
  150. foreach ($data['file'] as $value){
  151. $insert[] = [
  152. 'customer_id' => $model->id,
  153. 'file' => $value['url'],
  154. 'type' => CustomerInfo::type_six,
  155. 'name' => $value['name'],
  156. 'crt_time' => $time,
  157. ];
  158. if(in_array($value['url'], $old)) {
  159. foreach ($old as $o_k => $o_v){
  160. if($o_v == $value['url']) unset($old[$o_k]);
  161. }
  162. }else{
  163. $new[] = $value['url'];
  164. }
  165. }
  166. CustomerInfo::insert($insert);
  167. }
  168. DB::commit();
  169. }catch (\Exception $exception){
  170. DB::rollBack();
  171. return [false,$exception->getMessage()];
  172. }
  173. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  174. }
  175. /**
  176. * 客户新增
  177. * @param $data
  178. * @param $user
  179. * @return array
  180. */
  181. public function customerAdd($data,$user){
  182. list($status,$msg) = $this->customerRule($data,$user);
  183. if(!$status) return [$status,$msg];
  184. try {
  185. DB::beginTransaction();
  186. //车型
  187. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  188. $model_2 = new BasicType();
  189. $model_2->title = $data['car_type_title'];
  190. $model_2->type = 10;
  191. $model_2->depart_id = $data['depart_id'] ?? 0;
  192. $model_2->top_depart_id = $data['top_depart_id'] ?? 0;
  193. $model_2->crt_id = $user['id'];
  194. $model_2->save();
  195. $data['car_type'] = $model_2->id;
  196. }
  197. $model = new Customer();
  198. $model->title = $data['title'];
  199. $model->code = $data['code'] ?? "";
  200. $model->model_type = $data['model_type'];
  201. $model->customer_intention = $data['customer_intention'] ?? 0;
  202. $model->customer_from = $data['customer_from'] ?? 0;
  203. $model->customer_type = $data['customer_type'] ?? 0;
  204. $model->car_type = $data['car_type'] ?? 0;
  205. $model->consulting_product = $data['consulting_product'] ?? '';
  206. $model->intention_product = $data['intention_product'] ?? 0;
  207. $model->progress_stage = $data['progress_stage'] ?? 0;
  208. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  209. $model->address2 = $data['address2'] ?? '';
  210. $model->crt_id = $user['id'];
  211. $model->mark = $data['mark'] ?? '';
  212. $model->importance = $data['importance'] ?? '';
  213. $model->company = $data['company'] ?? '';
  214. // $model->company_short_name = $data['company_short_name'] ?? '';
  215. $model->depart_id = $data['depart_id'] ?? 0;
  216. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  217. $model->state_type = $data['state_type'] ?? 0;
  218. $model->customer_state = $data['customer_state'] ?? 0;
  219. $model->enter_time = $data['enter_time'] ?? 0;
  220. $model->save();
  221. $time = time();
  222. if(! empty($data['customer_contact'])){
  223. $insert = [];
  224. foreach ($data['customer_contact'] as $value){
  225. $insert[] = [
  226. 'customer_id' => $model->id,
  227. 'contact_type' => $value['id'],
  228. 'contact_info' => $value['info'],
  229. 'type' => CustomerInfo::type_one,
  230. 'crt_time' => $time,
  231. ];
  232. }
  233. CustomerInfo::insert($insert);
  234. }
  235. if(! empty($data['employee_one'])){
  236. $insert = [];
  237. foreach ($data['employee_one'] as $value){
  238. $insert[] = [
  239. 'customer_id' => $model->id,
  240. 'data_id' => $value,
  241. 'type' => CustomerInfo::type_two,
  242. 'crt_time' => $time,
  243. ];
  244. }
  245. CustomerInfo::insert($insert);
  246. // Customer::where('id',$model->id)->update([
  247. // 'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0,
  248. // 'fp_time' => $time,
  249. // ]);
  250. }
  251. if(! empty($data['employee_two'])){
  252. $insert = [];
  253. foreach ($data['employee_two'] as $value){
  254. $insert[] = [
  255. 'customer_id' => $model->id,
  256. 'data_id' => $value,
  257. 'type' => CustomerInfo::type_three,
  258. 'crt_time' => $time,
  259. ];
  260. }
  261. CustomerInfo::insert($insert);
  262. }
  263. if(! empty($data['employee_three'])){
  264. $insert = [];
  265. foreach ($data['employee_three'] as $value){
  266. $insert[] = [
  267. 'customer_id' => $model->id,
  268. 'data_id' => $value,
  269. 'type' => CustomerInfo::type_four,
  270. 'crt_time' => $time,
  271. ];
  272. }
  273. CustomerInfo::insert($insert);
  274. }
  275. $new = [];
  276. if(! empty($data['img'])){
  277. $insert = [];
  278. foreach ($data['img'] as $value){
  279. $insert[] = [
  280. 'customer_id' => $model->id,
  281. 'file' => $value['url'],
  282. 'type' => CustomerInfo::type_five,
  283. 'name' => $value['name'],
  284. 'crt_time' => $time,
  285. ];
  286. if(! empty($value['url'])) $new[] = $value['url'];
  287. }
  288. CustomerInfo::insert($insert);
  289. }
  290. if(! empty($data['file'])){
  291. $insert = [];
  292. foreach ($data['file'] as $value){
  293. $insert[] = [
  294. 'customer_id' => $model->id,
  295. 'file' => $value['url'],
  296. 'type' => CustomerInfo::type_six,
  297. 'name' => $value['name'],
  298. 'crt_time' => $time,
  299. ];
  300. if(! empty($value['url'])) $new[] = $value['url'];
  301. }
  302. CustomerInfo::insert($insert);
  303. }
  304. DB::commit();
  305. }catch (\Exception $exception){
  306. DB::rollBack();
  307. return [false,$exception->getMessage()];
  308. }
  309. $data['order_number'] = Customer::$order_number . "|" . $model->id . "|" . $data['title'];
  310. (new OperationLogService())->setOperationList($data,$user);
  311. return [true, ['file' => ['new' => $new]]];
  312. }
  313. /**
  314. * 客户删除
  315. * @param $data
  316. * @return array
  317. */
  318. public function customerDel($data){
  319. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  320. if($data['id'] < 0) return [false, '系统数据,操作失败!'];
  321. try {
  322. DB::beginTransaction();
  323. Customer::where('id',$data['id'])->update([
  324. 'del_time'=> time()
  325. ]);
  326. $old = CustomerInfo::where('del_time',0)
  327. ->where('customer_id',$data['id'])
  328. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  329. ->select('file')
  330. ->get()->toArray();
  331. $old = array_column($old,'file');
  332. CustomerInfo::where('del_time',0)
  333. ->where('customer_id',$data['id'])
  334. ->update(['del_time' => time()]);
  335. (new RangeService())->RangeDelete($data['id'],SeeRange::type_one);
  336. DB::commit();
  337. }catch (\Exception $exception){
  338. DB::rollBack();
  339. return [false,$exception->getMessage()];
  340. }
  341. return [true, ['file' => ['old' => $old]]];
  342. }
  343. /**
  344. * 客户详情
  345. * @param $data
  346. * @return array
  347. */
  348. public function customerDetail($data,$user){
  349. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  350. $customer = Customer::where('del_time',0)
  351. ->where('id',$data['id'])
  352. ->first();
  353. if(empty($customer)) return [false,'客户不存在或已被删除'];
  354. $customer = $customer->toArray();
  355. //是否是总社的客户
  356. $is_main = 0;
  357. if($customer['top_depart_id'] == $user['head']['id']) $is_main = 1;
  358. $customer['is_belong_to_main'] = $is_main;
  359. $customer['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $customer['title'];
  360. $customer['intention_product_title'] = Product::where('id',$customer['intention_product'])->value('title') ?? "";
  361. $customer['product_category'] = [];
  362. if(! empty($customer['intention_product'])){
  363. $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);
  364. $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';
  365. $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];
  366. }
  367. $address_map = config('address');
  368. $address_str = [];
  369. if(! empty($customer['address1'])) {
  370. $tmp = json_decode($customer['address1'],true);
  371. $this->findLabelsByValue($address_map,$tmp,$address_str);
  372. $customer['address1'] = $tmp;
  373. $tmp = implode(' ',$address_str);
  374. $tmp .= ' ' . $customer['address2'];
  375. $address = $tmp;
  376. }else{
  377. $address = $customer['address2'];
  378. }
  379. $customer['address'] = $address;
  380. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  381. $array = [
  382. $customer['customer_intention'],
  383. $customer['customer_from'],
  384. $customer['customer_type'],
  385. $customer['car_type'],
  386. $customer['progress_stage'],
  387. $customer['state_type'],
  388. $customer['customer_state'],
  389. ];
  390. $basic_map = BasicType::whereIn('id',$array)
  391. ->pluck('title','id')
  392. ->toArray();
  393. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  394. $customer = [$customer];
  395. foreach ($customer as $key => $value){
  396. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  397. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  398. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  399. $this->isTopDispatch($user, $customer[$key]);
  400. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  401. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  402. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  403. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  404. $customer[$key]['depart_title'] = $depart_title ?? '';
  405. }
  406. $customer = $customer[0];
  407. $customer_info = CustomerInfo::where('del_time',0)
  408. ->where('customer_id',$customer['id'])
  409. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  410. ->get()->toArray();
  411. $emp_id = [];
  412. $emp_id[] = $customer['crt_id'];
  413. foreach ($customer_info as $value){
  414. if(in_array($value['type'], CustomerInfo::$man)){
  415. $emp_id[] = $value['data_id'];
  416. }
  417. }
  418. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  419. ->pluck('emp_name','id')
  420. ->toArray();
  421. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  422. ->pluck('title','id')
  423. ->toArray();
  424. $fileUploadService = new FileUploadService();
  425. foreach ($customer_info as $value){
  426. if($value['type'] == CustomerInfo::type_one){
  427. $tmp = [
  428. 'id' => $value['contact_type'],
  429. 'title' => $basic_map2[$value['contact_type']] ?? '',
  430. 'info' => $value['contact_info']
  431. ];
  432. $customer['customer_contact'][] = $tmp;
  433. }elseif ($value['type'] == CustomerInfo::type_two){
  434. $tmp = [
  435. 'id' => $value['data_id'],
  436. 'name' => $emp_map[$value['data_id']] ?? '',
  437. ];
  438. $customer['employee_one'][] = $tmp;
  439. }elseif ($value['type'] == CustomerInfo::type_three){
  440. $tmp = [
  441. 'id' => $value['data_id'],
  442. 'name' => $emp_map[$value['data_id']] ?? '',
  443. ];
  444. $customer['employee_two'][] = $tmp;
  445. }elseif ($value['type'] == CustomerInfo::type_four){
  446. $tmp = [
  447. 'id' => $value['data_id'],
  448. 'name' => $emp_map[$value['data_id']] ?? '',
  449. ];
  450. $customer['employee_three'][] = $tmp;
  451. }elseif ($value['type'] == CustomerInfo::type_five){
  452. $tmp = [
  453. 'url' => $value['file'],
  454. 'name' => $value['name'],
  455. 'show_url' => $fileUploadService->getFileShow($value['file']),
  456. ];
  457. $customer['img'][] = $tmp;
  458. }elseif ($value['type'] == CustomerInfo::type_six){
  459. $tmp = [
  460. 'url' => $value['file'],
  461. 'name' => $value['name'],
  462. 'show_url' => $fileUploadService->getFileShow($value['file']),
  463. ];
  464. $customer['file'][] = $tmp;
  465. }elseif ($value['type'] == CustomerInfo::type_nine){
  466. $tmp = [
  467. 'id' => $value['data_id'],
  468. 'name' => $emp_map[$value['data_id']] ?? '',
  469. ];
  470. $customer['old_employee_one'][] = $tmp;
  471. }
  472. }
  473. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  474. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  475. //可见范围
  476. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  477. $customer['depart'] = $return[0] ?? [];
  478. $customer['employee'] = $return[1] ?? [];
  479. return [true, $customer];
  480. }
  481. public function customerCommonSearch($data,$user, $field = []){
  482. if(empty($field)){
  483. $field = ['title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id','enter_time'];
  484. }
  485. $model = Customer::Clear($user,$data);
  486. $model = $model->where('del_time',0)
  487. ->select($field)
  488. ->orderby('id', 'desc');
  489. if(! empty($data['title_t'])) {
  490. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  491. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  492. $id = (new RangeService())->crtContactSearch($data);
  493. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  494. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  495. ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')
  496. ->orWhereIn('id', $id);
  497. }
  498. if(! empty($data['id'])) $model->where('id', $data['id']);
  499. if(! empty($data['customer_id'])){
  500. $customer_id = explode(',',$data['customer_id']);
  501. $model->whereIn('id', $customer_id);
  502. }
  503. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  504. $id = $this->getCustomerId($data,$user);
  505. $model->whereIn('id',$id);
  506. }
  507. if(! empty($data['pond_state'])) {
  508. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  509. if(empty($search_depart_id)){
  510. $top_depart_id = $user['depart_top'][0] ?? [];
  511. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  512. }else{
  513. //查询 顶级公司
  514. $top_depart_id = $search_depart_id;
  515. }
  516. // 进入公海池的客户 公司下所有人可见
  517. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  518. }
  519. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  520. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  521. if(! empty($data['time_type'])) {
  522. if($data['time_type'] == 1) {
  523. $start = strtotime('today');
  524. $end = strtotime('tomorrow') - 1;
  525. }elseif ($data['time_type'] == 2){
  526. $start = strtotime('this week',strtotime('today'));
  527. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  528. }
  529. if(! empty($start) && ! empty($end)) {
  530. $model->where('crt_time','>=',$start);
  531. $model->where('crt_time','<=',$end);
  532. }
  533. }
  534. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  535. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  536. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  537. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  538. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  539. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  540. if(! empty($data['customer_intention_title'])){
  541. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  542. $model->whereIn('customer_intention', $id);
  543. }
  544. if(! empty($data['customer_from_title'])){
  545. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  546. $model->whereIn('customer_from', $id);
  547. }
  548. if(! empty($data['customer_type_title'])) {
  549. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  550. $model->whereIn('customer_type', $id);
  551. }
  552. if(! empty($data['progress_stage_title'])){
  553. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  554. $model->whereIn('progress_stage', $id);
  555. }
  556. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  557. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  558. $model->where('crt_time','>=',$return[0]);
  559. $model->where('crt_time','<=',$return[1]);
  560. }
  561. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  562. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  563. $model->where('enter_time','>=',$return[0]);
  564. $model->where('enter_time','<=',$return[1]);
  565. }
  566. if(! empty($data['crt_name'])){
  567. $id = (new RangeService())->crtNameSearch($data);
  568. $model->whereIn('crt_id',$id);
  569. }
  570. if(! empty($data['fz'])){
  571. $id = (new RangeService())->customerSearch($data);
  572. $model->whereIn('id',$id);
  573. }
  574. if(! empty($data['last_visit_time'])){
  575. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  576. $model->whereIn('id',$id);
  577. }
  578. if(! empty($data['contact_info'])){
  579. $customer_info = CustomerInfo::where('del_time',0)
  580. ->where("type",CustomerInfo::type_one)
  581. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  582. ->select('customer_id')
  583. ->get()->toArray();
  584. $model->whereIn('id',array_column($customer_info,'customer_id'));
  585. }
  586. if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
  587. $is_fp = -1;
  588. if(isset($data['is_fp'])) {
  589. if($data['is_fp']){
  590. $model->where('fp_time', '>', 0);
  591. $is_fp = 1;
  592. } else{
  593. $model->where('fp_time', 0);
  594. $is_fp = 0;
  595. }
  596. }
  597. if(! empty($data['belong_top_depart_title'])){
  598. list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  599. if($is_fp < 0){
  600. $model->whereIn('id', $id)
  601. ->orWhereIn('top_depart_id', $depart_id);
  602. }elseif ($is_fp > 0){
  603. $model->whereIn('id', $id);
  604. }else{
  605. $model->where('top_depart_id', $depart_id);
  606. }
  607. }
  608. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  609. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  610. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  611. }
  612. return $model;
  613. }
  614. /**
  615. * 客户列表
  616. * @param $data
  617. * @param $user
  618. * @return array
  619. */
  620. public function customerList($data,$user){
  621. $model = $this->customerCommonSearch($data,$user);
  622. $list = $this->limit($model,'',$data);
  623. $list = $this->fillData($list,$data, $user);
  624. //微信数据
  625. $count = $this->countData("crt_time", $data, $user);
  626. $list['today'] = $count[0] ?? 0;
  627. $list['yesterday'] = $count[1] ?? 0;
  628. return [true, $list];
  629. }
  630. public function customer2CommonSearch($data,$user){
  631. $model = Customer::Clear($user,$data);
  632. $model = $model->where('del_time',0)
  633. ->where('fp_time','>',0)
  634. ->select('title','id','model_type','customer_intention','customer_from','customer_type','car_type','consulting_product','intention_product','progress_stage','address1','address2','crt_id','crt_time','mark','importance','company','company_short_name','depart_id','state_type','customer_state','pond_state','top_depart_id','fp_time','fp_top_depart_id','enter_time')
  635. ->orderby('fp_time', 'desc');
  636. if(! empty($data['customer_id'])){
  637. $customer_id = explode(',',$data['customer_id']);
  638. $model->whereIn('id', $customer_id);
  639. }
  640. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  641. $id = $this->getCustomerId($data,$user);
  642. $model->whereIn('id',$id);
  643. }
  644. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  645. if(! empty($data['time_type'])) {
  646. if($data['time_type'] == 1) {
  647. $start = strtotime('today');
  648. $end = strtotime('tomorrow') - 1;
  649. }elseif ($data['time_type'] == 2){
  650. $start = strtotime('this week',strtotime('today'));
  651. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  652. }
  653. if(! empty($start) && ! empty($end)) {
  654. $model->where('crt_time','>=',$start);
  655. $model->where('crt_time','<=',$end);
  656. }
  657. }
  658. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  659. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  660. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  661. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  662. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  663. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  664. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  665. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  666. $model->where('crt_time','>=',$return[0]);
  667. $model->where('crt_time','<=',$return[1]);
  668. }
  669. if(! empty($data['crt_name'])){
  670. $id = (new RangeService())->crtNameSearch($data);
  671. $model->whereIn('crt_id',$id);
  672. }
  673. if(! empty($data['fz'])){
  674. $id = (new RangeService())->customerSearch($data);
  675. $model->whereIn('id',$id);
  676. }
  677. if(! empty($data['last_visit_time'])){
  678. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  679. $model->whereIn('id',$id);
  680. }
  681. if(! empty($data['contact_info'])){
  682. $customer_info = CustomerInfo::where('del_time',0)
  683. ->where("type",CustomerInfo::type_one)
  684. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  685. ->select('customer_id')
  686. ->get()->toArray();
  687. $model->whereIn('id',array_column($customer_info,'customer_id'));
  688. }
  689. if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){
  690. $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);
  691. $model->where('fp_time','>=',$return[0]);
  692. $model->where('fp_time','<=',$return[1]);
  693. }
  694. if(! empty($data['belong_top_depart_title'])){
  695. list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  696. $model->whereIn('id', $id);
  697. }
  698. if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
  699. $model->where('fp_time','>=',$data['wx_fp_time'][0]);
  700. $model->where('fp_time','<=',$data['wx_fp_time'][1]);
  701. }
  702. return $model;
  703. }
  704. public function customerList2($data,$user){
  705. $model = $this->customer2CommonSearch($data, $user);
  706. $list = $this->limit($model,'',$data);
  707. $list = $this->fillData($list,$data,$user);
  708. //微信数据
  709. $count = $this->countData2("fp_time", $data, $user);
  710. $list['today'] = $count[0] ?? 0;
  711. $list['yesterday'] = $count[1] ?? 0;
  712. return [true, $list];
  713. }
  714. /**
  715. * 客户重复
  716. * @param $data
  717. * @param $user
  718. * @return array
  719. */
  720. public function customerRepeatList($data,$user){
  721. $model = $this->customerRepeatCommonSearch($data,$user);
  722. $list = $this->limit($model,'',$data);
  723. $list = $this->fillRepeatData($list,$data, $user);
  724. return [true, $list];
  725. }
  726. public function customerRepeatCommonSearch($data,$user, $field = []){
  727. if(empty($field)){
  728. $field = ['title','id','model_type','customer_from','car_type','address2','crt_id','crt_time','enter_time','contact_type','contact','mark'];
  729. }
  730. $model = CustomerRepeat::Clear($user,$data);
  731. $model = $model->where('del_time',0)
  732. ->select($field)
  733. ->orderby('id', 'desc');
  734. if(! empty($data['title_t'])) {
  735. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  736. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  737. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  738. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  739. ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')
  740. ->orWhere('contact', 'LIKE', '%'.$data['title_t'].'%');
  741. }
  742. if(! empty($data['customer_id'])){
  743. $customer_id = explode(',',$data['customer_id']);
  744. $model->whereIn('id', $customer_id);
  745. }
  746. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  747. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  748. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  749. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  750. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  751. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  752. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  753. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  754. if(! empty($data['customer_intention_title'])){
  755. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  756. $model->whereIn('customer_intention', $id);
  757. }
  758. if(! empty($data['customer_from_title'])){
  759. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  760. $model->whereIn('customer_from', $id);
  761. }
  762. if(! empty($data['customer_type_title'])) {
  763. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  764. $model->whereIn('customer_type', $id);
  765. }
  766. if(! empty($data['progress_stage_title'])){
  767. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  768. $model->whereIn('progress_stage', $id);
  769. }
  770. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  771. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  772. $model->where('crt_time','>=',$return[0]);
  773. $model->where('crt_time','<=',$return[1]);
  774. }
  775. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  776. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  777. $model->where('enter_time','>=',$return[0]);
  778. $model->where('enter_time','<=',$return[1]);
  779. }
  780. if(! empty($data['crt_name'])){
  781. $id = (new RangeService())->crtNameSearch($data);
  782. $model->whereIn('crt_id',$id);
  783. }
  784. if(! empty($data['fz'])){
  785. $emp_id = Employee::where('del_time',0)
  786. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  787. ->select('id')->get()->toArray();
  788. $emp_id = array_column($emp_id,'id');
  789. $model->whereIn('fz_man', $emp_id);
  790. }
  791. if(! empty($data['contact_info'])) $model->where('contact', 'LIKE', '%'.$data['contact_info'].'%');
  792. return $model;
  793. }
  794. /**
  795. * 拼接数据
  796. * @param $data
  797. * @return array
  798. */
  799. public function fillRepeatData($data,$ergs,$user){
  800. if(empty($data['data'])) return $data;
  801. $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_from'),array_column($data['data'],'car_type')));
  802. $basic_map = BasicType::whereIn('id',$array)
  803. ->pluck('title','id')
  804. ->toArray();
  805. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  806. ->pluck('emp_name','id')
  807. ->toArray();
  808. foreach ($data['data'] as $key => $value){
  809. $data['data'][$key]['address'] = $value['address2'];
  810. //客户来源
  811. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  812. // $this->isTopDispatch($user, $data['data'][$key]);
  813. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  814. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  815. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  816. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  817. }
  818. return $data;
  819. }
  820. public function getCustomerId($data,$user){
  821. $return = [];
  822. if(! empty($data['my_fz'])){
  823. $info = CustomerInfo::where('del_time',0)
  824. ->where('data_id',$user['id'])
  825. ->where('type',CustomerInfo::type_two)
  826. ->select('customer_id')
  827. ->get()->toArray();
  828. $return = array_unique(array_column($info,'customer_id'));
  829. }
  830. if(! empty($data['my_xt'])){
  831. $info = CustomerInfo::where('del_time',0)
  832. ->where('data_id',$user['id'])
  833. ->where('type',CustomerInfo::type_three)
  834. ->select('customer_id')
  835. ->get()->toArray();
  836. $return = array_unique(array_column($info,'customer_id'));
  837. }
  838. return $return;
  839. }
  840. /**
  841. * 客户参数规则
  842. * @param $data
  843. * @param $is_add
  844. * @return array
  845. */
  846. public function customerRule(&$data, $user, $is_add = true){
  847. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  848. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  849. if(empty($data['title'])) return [false,'客户名称不能为空'];
  850. if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
  851. $data['enter_time'] = $this->changeDateToDateMin($data['enter_time']);
  852. //所属部门 以及 顶级部门
  853. if(empty($data['depart_id'])) {
  854. $data['depart_id'] = $this->getDepart($user);
  855. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  856. }
  857. if($data['model_type'] == Customer::Model_type_one){
  858. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  859. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  860. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  861. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  862. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  863. }else{
  864. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  865. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  866. }
  867. list($status,$msg) = $this->limitingSendRequestBackgExpire($data['top_depart_id'] . $data['title'],3);
  868. if(! $status) return [false, $msg];
  869. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  870. $bool = BasicType::where('title',$data['car_type_title'])
  871. ->where('top_depart_id',$data['top_depart_id'])
  872. ->where('type',10)
  873. ->where('del_time',0)
  874. ->exists();
  875. if($bool) return [false,'车型名称已存在'];
  876. }
  877. if($is_add){
  878. $bool = Customer::where('del_time',0)
  879. ->where('top_depart_id',$data['top_depart_id'])
  880. ->where('title',$data['title'])
  881. // ->where('model_type',$data['model_type'])
  882. ->exists();
  883. if(! empty($data['customer_contact'])) {
  884. $search = [];
  885. foreach ($data['customer_contact'] as $value){
  886. $search[] = $value['info'];
  887. }
  888. $boolean = CustomerInfo::from('customer_info as a')
  889. ->join('customer as b','b.id','a.customer_id')
  890. ->where('a.del_time',0)
  891. ->where('b.del_time',0)
  892. ->where('b.top_depart_id',$data['top_depart_id'])
  893. ->whereIn('a.contact_info', $search)
  894. ->exists();
  895. if($boolean) return [false,'客户联系内容已存在'];
  896. }
  897. }else{
  898. if(empty($data['id'])) return [false,'ID不能为空'];
  899. $bool = Customer::where('del_time',0)
  900. ->where('id','<>',$data['id'])
  901. ->where('top_depart_id',$data['top_depart_id'])
  902. ->where('title',$data['title'])
  903. // ->where('model_type',$data['model_type'])
  904. ->exists();
  905. if(! empty($data['customer_contact'])) {
  906. $search = [];
  907. foreach ($data['customer_contact'] as $value){
  908. $search[] = $value['info'];
  909. }
  910. $boolean = CustomerInfo::from('customer_info as a')
  911. ->join('customer as b','b.id','a.customer_id')
  912. ->where('b.id','<>',$data['id'])
  913. ->where('a.del_time',0)
  914. ->where('b.del_time',0)
  915. ->where('b.top_depart_id',$data['top_depart_id'])
  916. ->whereIn('a.contact_info', $search)
  917. ->exists();
  918. if($boolean) return [false,'客户联系内容已存在'];
  919. }
  920. }
  921. if($bool) return [false,'客户名称不能重复'];
  922. return [true, ''];
  923. }
  924. /**
  925. * 拼接数据
  926. * @param $data
  927. * @return array
  928. */
  929. public function fillData($data,$ergs,$user){
  930. if(empty($data['data'])) return $data;
  931. $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_intention'),array_column($data['data'],'customer_from'),array_column($data['data'],'customer_type'),array_column($data['data'],'car_type'),array_column($data['data'],'progress_stage'),array_column($data['data'],'state_type'),array_column($data['data'],'customer_state')));
  932. $basic_map = BasicType::whereIn('id',$array)
  933. ->pluck('title','id')
  934. ->toArray();
  935. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  936. ->pluck('title','id')
  937. ->toArray();
  938. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  939. ->pluck('emp_name','id')
  940. ->toArray();
  941. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  942. ->pluck('title','id')
  943. ->toArray();
  944. $customer_id = array_column($data['data'],'id');
  945. //跟进记录
  946. $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);
  947. $customer_info = CustomerInfo::where('del_time',0)
  948. ->whereIn('customer_id',$customer_id)
  949. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])
  950. ->select('type','contact_type','contact_info','customer_id','data_id')
  951. ->get()->toArray();
  952. //客户的联系方式 客户的负责人 协同人
  953. $customer_info_map = $fz = $customer_info_map2 = $xt = [];
  954. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  955. ->pluck('emp_name','id')
  956. ->toArray();
  957. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  958. foreach ($customer_info as $value){
  959. if($value['type'] == CustomerInfo::type_one){
  960. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  961. $customer_info_map[$value['customer_id']][] = $value;
  962. if(! isset($customer_info_map2[$value['customer_id']])){
  963. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  964. }else{
  965. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  966. }
  967. }elseif($value['type'] == CustomerInfo::type_two){
  968. $tmp = $emp_map[$value['data_id']] ?? "";
  969. if(isset($fz[$value['customer_id']])){
  970. $fz[$value['customer_id']] .= ',' . $tmp;
  971. }else{
  972. $fz[$value['customer_id']] = $tmp;
  973. }
  974. }else{
  975. $tmp = $emp_map[$value['data_id']] ?? "";
  976. if(isset($xt[$value['customer_id']])){
  977. $xt[$value['customer_id']] .= ',' . $tmp;
  978. }else{
  979. $xt[$value['customer_id']] = $tmp;
  980. }
  981. }
  982. }
  983. $array2 = array_unique(array_column($data['data'],'top_depart_id'));
  984. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  985. $depart2_map = $this->getCustomerDepart($customer_id);
  986. //地址
  987. $address_map = config('address');
  988. foreach ($data['data'] as $key => $value){
  989. $address_str = [];
  990. if(! empty($value['address1'])) {
  991. $tmp = json_decode($value['address1'],true);
  992. $this->findLabelsByValue($address_map,$tmp,$address_str);
  993. $tmp = implode(' ',$address_str);
  994. $tmp .= ' ' . $value['address2'];
  995. $address = $tmp;
  996. }else{
  997. $address = $value['address2'];
  998. }
  999. $data['data'][$key]['address'] = $address;
  1000. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  1001. //客户来源
  1002. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  1003. $this->isTopDispatch($user, $data['data'][$key]);
  1004. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  1005. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  1006. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  1007. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  1008. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  1009. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  1010. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  1011. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1012. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  1013. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  1014. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1015. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  1016. $data['data'][$key]['customer_detail'] = $customer_tmp;
  1017. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  1018. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  1019. $record_tmp = $record_array[$value['id']] ?? "";
  1020. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  1021. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  1022. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  1023. $top_depart_title = $depart[$value['top_depart_id']] ?? "";
  1024. $data['data'][$key]['top_depart_title'] = $top_depart_title;
  1025. //分配门店
  1026. $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
  1027. if($value['fp_time'] > 0) {
  1028. //分配过所属门店是分配门店
  1029. $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;
  1030. }else{
  1031. //没分配过所属门店是创建门店
  1032. $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;
  1033. }
  1034. $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
  1035. $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
  1036. }
  1037. return $data;
  1038. }
  1039. private function isTopDispatch($user, &$value){
  1040. //特殊功能按钮
  1041. $special_button = $user['special_button'] ?? [];
  1042. //总社
  1043. $head = $user['head']['id'] ?? 0;
  1044. //当前门店
  1045. $top_depart_id = $user['depart_top'][0] ?? [];
  1046. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  1047. //分社账号
  1048. if($top_depart_id != $head){
  1049. //总社创建分配给分社的客户
  1050. if($value['top_depart_id'] == $head && $value['fp_time'] > 0 && $value['fp_top_depart_id'] != $head){
  1051. if(! in_array(RoleMenuButton::special_nine,$special_button)) $value['customer_from_title'] = "";
  1052. }
  1053. }
  1054. }
  1055. public function countData($column = "", $data, $user){
  1056. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1057. // 获取今天的开始和结束时间戳
  1058. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1059. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1060. // 获取昨天的开始和结束时间戳
  1061. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1062. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1063. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1064. $model = $this->customerCommonSearch($data, $user);
  1065. // 查询今天的数据条数
  1066. $todayCount = $model->count();
  1067. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1068. $model = $this->customerCommonSearch($data, $user);
  1069. // 查询昨天的数据条数
  1070. $yesterdayCount = $model->count();
  1071. return [$todayCount, $yesterdayCount];
  1072. }
  1073. public function countData2($column = "", $data, $user){
  1074. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1075. // 获取今天的开始和结束时间戳
  1076. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1077. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1078. // 获取昨天的开始和结束时间戳
  1079. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1080. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1081. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1082. $model = $this->customer2CommonSearch($data, $user);
  1083. // 查询今天的数据条数
  1084. $todayCount = $model->count();
  1085. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1086. $model = $this->customer2CommonSearch($data, $user);
  1087. // 查询昨天的数据条数
  1088. $yesterdayCount = $model->count();
  1089. return [$todayCount, $yesterdayCount];
  1090. }
  1091. //获取客资门店
  1092. public function getCustomerDepart($customer_id = []){
  1093. if(empty($customer_id)) return [];
  1094. $result = SeeRange::where('del_time',0)
  1095. ->whereIn('data_id', $customer_id)
  1096. ->where('data_type',SeeRange::type_one)
  1097. ->where('type',SeeRange::data_three)
  1098. ->select('data_id as customer_id','param_id as top_depart_id')
  1099. ->get()->toArray();
  1100. $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();
  1101. $return = [];
  1102. foreach ($result as $value){
  1103. $tmp = $depart_map[$value['top_depart_id']] ?? "";
  1104. if(! $tmp) continue;
  1105. if(isset($return[$value['customer_id']])){
  1106. $return[$value['customer_id']] .= ',' . $tmp;
  1107. }else{
  1108. $return[$value['customer_id']] = $tmp;
  1109. }
  1110. }
  1111. return $return;
  1112. }
  1113. //客资门店搜索
  1114. public function searchCustomerDepart($depart_title = ""){
  1115. $customer_id = [];
  1116. if(empty($depart_title)) return $customer_id;
  1117. $depart_id = Depart::where('del_time', 0)
  1118. ->where('parent_id', 0)
  1119. ->where('title', 'LIKE', '%'. $depart_title .'%')
  1120. ->select('id')->get()->toArray();
  1121. $depart_id = array_column($depart_id, 'id');
  1122. if(empty($depart_id)) return [$customer_id, $depart_id];
  1123. $customer_id = SeeRange::where('del_time',0)
  1124. ->whereIn('param_id', $depart_id)
  1125. ->where('data_type',SeeRange::type_one)
  1126. ->where('type',SeeRange::data_three)
  1127. ->select('data_id as customer_id')
  1128. ->get()->toArray();
  1129. $customer_id = array_column($customer_id, 'customer_id');
  1130. return [$customer_id, $depart_id];
  1131. }
  1132. //抢客户
  1133. public function customerGrabbing($data, $user){
  1134. $key = Customer::$limitKey . $data['customer_id'];
  1135. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  1136. if(! $status) {
  1137. //释放锁
  1138. $this->dellimitingSendRequestBackg($key);
  1139. return [false,$msg];
  1140. }
  1141. try {
  1142. DB::beginTransaction();
  1143. $time = time();
  1144. $insert = [];
  1145. //负责人获取 改为前负责人
  1146. $man = CustomerInfo::where('del_time',0)
  1147. ->where('customer_id',$data['customer_id'])
  1148. ->where('type', CustomerInfo::type_two)
  1149. ->get()->toArray();
  1150. foreach ($man as $value){
  1151. $insert[] = [
  1152. 'customer_id' => $data['customer_id'],
  1153. 'data_id' => $value['data_id'],
  1154. 'type' => CustomerInfo::type_nine,
  1155. 'crt_time' => $time
  1156. ];
  1157. }
  1158. //增加自己为负责人
  1159. $insert[] = [
  1160. 'customer_id' => $data['customer_id'],
  1161. 'data_id' => $user['id'],
  1162. 'type' => CustomerInfo::type_two,
  1163. 'crt_time' => $time
  1164. ];
  1165. //人员清空
  1166. CustomerInfo::where('del_time',0)
  1167. ->where('customer_id',$data['customer_id'])
  1168. ->whereIn('type', CustomerInfo::$man2)
  1169. ->update(['del_time' => $time]);
  1170. //写入最新人员
  1171. CustomerInfo::insert($insert);
  1172. //更新公海池状态
  1173. Customer::where('id',$data['customer_id'])->update([
  1174. 'pond_state' => 0
  1175. ]);
  1176. DB::commit();
  1177. }catch (\Exception $exception){
  1178. //释放锁
  1179. $this->dellimitingSendRequestBackg($key);
  1180. DB::rollBack();
  1181. return [false,$exception->getMessage()];
  1182. }
  1183. //释放锁
  1184. $this->dellimitingSendRequestBackg($key);
  1185. return [true, ''];
  1186. }
  1187. public function customerGrabbingRule($data, $key){
  1188. if(empty($data['customer_id'])) return [false,'请选择客户'];
  1189. //上锁
  1190. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1191. if(! $status) return [false,$msg];
  1192. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  1193. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1194. $customer = $customer->toArray();
  1195. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  1196. return [true,''];
  1197. }
  1198. public function searchBy($data,$user){
  1199. $model = BasicType::TopClear($user,$data);
  1200. $result = $model->where('del_time',0)
  1201. ->where('type', 2)
  1202. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  1203. ->select('id')
  1204. ->get()->toArray();
  1205. $model2 = Customer::Clear($user,$data);
  1206. $customer = $model2->where('del_time',0)
  1207. ->whereIn('customer_from', array_column($result,'id'))
  1208. ->select('id')
  1209. ->get()->toArray();
  1210. return array_column($customer,'id');
  1211. }
  1212. public function customerImportanceEdit($data,$user){
  1213. if(empty($data['id'])) return [false, '请选择客户'];
  1214. $model = Customer::where('id',$data['id'])->first();
  1215. if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
  1216. if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
  1217. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
  1218. $params = $this->getDataFile($data);
  1219. (new OperationLogService())->setOperationList($params,$user,2);
  1220. try {
  1221. DB::beginTransaction();
  1222. $model = Customer::where('id',$data['id'])->first();
  1223. $model->importance = $data['importance'] ?? '';
  1224. $model->save();
  1225. DB::commit();
  1226. }catch (\Exception $exception){
  1227. DB::rollBack();
  1228. return [false,$exception->getMessage()];
  1229. }
  1230. return [true, ''];
  1231. }
  1232. //发送客户消息
  1233. public function customerSendWx($data, $user){
  1234. list($status,$msg) = $this->customerSendWxRule($data);
  1235. if(! $status) return [false,$msg];
  1236. //发送消息
  1237. $send_data = $msg;
  1238. (new OaService())->sendWxOaCheckMessage($send_data);
  1239. return [true, ''];
  1240. }
  1241. public function customerSendWxRule($data){
  1242. if(empty($data['id'])) return [false,'请选择客户'];
  1243. $customer = Customer::where('del_time',0)
  1244. ->whereIn('id',$data['id'])
  1245. ->get()->toArray();
  1246. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1247. $man = CustomerInfo::where('del_time',0)
  1248. ->whereIn('customer_id',$data['id'])
  1249. ->where('type', CustomerInfo::type_two)
  1250. ->get()->toArray();
  1251. if(empty($man)) return [true, '客户暂无负责人信息,公众号消息发送结束'];
  1252. $customer_map = [];
  1253. foreach ($man as $value){
  1254. $customer_map[$value['data_id']][] = $value['customer_id'];
  1255. }
  1256. $employee_id = array_unique(array_column($man,'data_id'));
  1257. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  1258. ->pluck('openid','employee_id')
  1259. ->toArray();
  1260. $emp = Employee::whereIn('id', $employee_id)
  1261. ->pluck('emp_name','id')
  1262. ->toArray();
  1263. $send_data = [];
  1264. foreach ($employee_id as $value){
  1265. $tmp = $emp[$value] ?? "";
  1266. $open_id = $wx_map[$value] ?? "";
  1267. // if(empty($open_id)) return [false, $tmp . "暂未关注微信公众号,公众号消息发送失败"];
  1268. if(empty($open_id)) continue;
  1269. $customer_id = $customer_map[$value] ?? [];
  1270. $customer_id = implode(',', $customer_id);
  1271. //提醒创建人
  1272. $send_data[] = [
  1273. 'employee_id' => $value,
  1274. 'type' => 2,
  1275. 'state' => 0,
  1276. 'menu_id' => "16|list",
  1277. 'openid' => $open_id,
  1278. 'order_number' => $customer_id,
  1279. 'tmp_data' => [
  1280. time(),
  1281. "客户消息提醒",
  1282. "已发送",
  1283. $tmp,
  1284. date('Y-m-d H:i:s'),
  1285. ],
  1286. ];
  1287. }
  1288. return [true, $send_data];
  1289. }
  1290. }