CustomerService.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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_id = $this->contactSearch($data['contact_info']);
  580. $model->whereIn('id', $customer_id);
  581. }
  582. if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
  583. $is_fp = -1;
  584. if(isset($data['is_fp'])) {
  585. if($data['is_fp']){
  586. $model->where('fp_time', '>', 0);
  587. $is_fp = 1;
  588. } else{
  589. $model->where('fp_time', 0);
  590. $is_fp = 0;
  591. }
  592. }
  593. if(! empty($data['belong_top_depart_title'])){
  594. list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  595. if($is_fp < 0){
  596. $model->whereIn('id', $id)
  597. ->orWhereIn('top_depart_id', $depart_id);
  598. }elseif ($is_fp > 0){
  599. $model->whereIn('id', $id);
  600. }else{
  601. $model->where('top_depart_id', $depart_id);
  602. }
  603. }
  604. if(! empty($data['smart_search'])){
  605. $customer_id = $this->contactSearch($data['smart_search']);
  606. $progress_stage_id = (new RangeService())->customerBasicTypeSearch($data['smart_search'],[5]);
  607. $model->where('title', 'LIKE', '%'.$data['smart_search'].'%')
  608. ->orWhereIn('id', $customer_id)
  609. ->orWhereIn('progress_stage', $progress_stage_id);
  610. }
  611. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  612. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  613. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  614. }
  615. return $model;
  616. }
  617. public function contactSearch($contact_info){
  618. $customer_info = CustomerInfo::where('del_time',0)
  619. ->where("type",CustomerInfo::type_one)
  620. ->where('contact_info','LIKE', '%'.$contact_info.'%')
  621. ->select('customer_id')
  622. ->get()->toArray();
  623. return array_column($customer_info,'customer_id');
  624. }
  625. public function titleSearch($user, $data, $message){
  626. $model2 = Customer::Clear($user,$data);
  627. $customer = $model2->where('del_time',0)
  628. ->where('title', 'LIKE', '%'. $message .'%')
  629. ->select('id')
  630. ->get()->toArray();
  631. return array_column($customer,'id');
  632. }
  633. /**
  634. * 客户列表
  635. * @param $data
  636. * @param $user
  637. * @return array
  638. */
  639. public function customerList($data,$user){
  640. $model = $this->customerCommonSearch($data,$user);
  641. $list = $this->limit($model,'',$data);
  642. $list = $this->fillData($list,$data, $user);
  643. //微信数据
  644. $count = $this->countData("crt_time", $data, $user);
  645. $list['today'] = $count[0] ?? 0;
  646. $list['yesterday'] = $count[1] ?? 0;
  647. return [true, $list];
  648. }
  649. public function customer2CommonSearch($data,$user){
  650. $model = Customer::Clear($user,$data);
  651. $model = $model->where('del_time',0)
  652. ->where('fp_time','>',0)
  653. ->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')
  654. ->orderby('fp_time', 'desc');
  655. if(! empty($data['customer_id'])){
  656. $customer_id = explode(',',$data['customer_id']);
  657. $model->whereIn('id', $customer_id);
  658. }
  659. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  660. $id = $this->getCustomerId($data,$user);
  661. $model->whereIn('id',$id);
  662. }
  663. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  664. if(! empty($data['time_type'])) {
  665. if($data['time_type'] == 1) {
  666. $start = strtotime('today');
  667. $end = strtotime('tomorrow') - 1;
  668. }elseif ($data['time_type'] == 2){
  669. $start = strtotime('this week',strtotime('today'));
  670. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  671. }
  672. if(! empty($start) && ! empty($end)) {
  673. $model->where('crt_time','>=',$start);
  674. $model->where('crt_time','<=',$end);
  675. }
  676. }
  677. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  678. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  679. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  680. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  681. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  682. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  683. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  684. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  685. $model->where('crt_time','>=',$return[0]);
  686. $model->where('crt_time','<=',$return[1]);
  687. }
  688. if(! empty($data['crt_name'])){
  689. $id = (new RangeService())->crtNameSearch($data);
  690. $model->whereIn('crt_id',$id);
  691. }
  692. if(! empty($data['fz'])){
  693. $id = (new RangeService())->customerSearch($data);
  694. $model->whereIn('id',$id);
  695. }
  696. if(! empty($data['last_visit_time'])){
  697. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  698. $model->whereIn('id',$id);
  699. }
  700. if(! empty($data['contact_info'])){
  701. $customer_info = CustomerInfo::where('del_time',0)
  702. ->where("type",CustomerInfo::type_one)
  703. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  704. ->select('customer_id')
  705. ->get()->toArray();
  706. $model->whereIn('id',array_column($customer_info,'customer_id'));
  707. }
  708. if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){
  709. $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);
  710. $model->where('fp_time','>=',$return[0]);
  711. $model->where('fp_time','<=',$return[1]);
  712. }
  713. if(! empty($data['belong_top_depart_title'])){
  714. list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  715. $model->whereIn('id', $id);
  716. }
  717. if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
  718. $model->where('fp_time','>=',$data['wx_fp_time'][0]);
  719. $model->where('fp_time','<=',$data['wx_fp_time'][1]);
  720. }
  721. return $model;
  722. }
  723. public function customerList2($data,$user){
  724. $model = $this->customer2CommonSearch($data, $user);
  725. $list = $this->limit($model,'',$data);
  726. $list = $this->fillData($list,$data,$user);
  727. //微信数据
  728. $count = $this->countData2("fp_time", $data, $user);
  729. $list['today'] = $count[0] ?? 0;
  730. $list['yesterday'] = $count[1] ?? 0;
  731. return [true, $list];
  732. }
  733. /**
  734. * 客户重复
  735. * @param $data
  736. * @param $user
  737. * @return array
  738. */
  739. public function customerRepeatList($data,$user){
  740. $model = $this->customerRepeatCommonSearch($data,$user);
  741. $list = $this->limit($model,'',$data);
  742. $list = $this->fillRepeatData($list,$data, $user);
  743. return [true, $list];
  744. }
  745. public function customerRepeatCommonSearch($data,$user, $field = []){
  746. if(empty($field)){
  747. $field = ['title','id','model_type','customer_from','car_type','address2','crt_id','crt_time','enter_time','contact_type','contact','mark'];
  748. }
  749. $model = CustomerRepeat::Clear($user,$data);
  750. $model = $model->where('del_time',0)
  751. ->select($field)
  752. ->orderby('id', 'desc');
  753. if(! empty($data['title_t'])) {
  754. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  755. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  756. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  757. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  758. ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')
  759. ->orWhere('contact', 'LIKE', '%'.$data['title_t'].'%');
  760. }
  761. if(! empty($data['customer_id'])){
  762. $customer_id = explode(',',$data['customer_id']);
  763. $model->whereIn('id', $customer_id);
  764. }
  765. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  766. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  767. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  768. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  769. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  770. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  771. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  772. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  773. if(! empty($data['customer_intention_title'])){
  774. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  775. $model->whereIn('customer_intention', $id);
  776. }
  777. if(! empty($data['customer_from_title'])){
  778. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  779. $model->whereIn('customer_from', $id);
  780. }
  781. if(! empty($data['customer_type_title'])) {
  782. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  783. $model->whereIn('customer_type', $id);
  784. }
  785. if(! empty($data['progress_stage_title'])){
  786. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  787. $model->whereIn('progress_stage', $id);
  788. }
  789. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  790. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  791. $model->where('crt_time','>=',$return[0]);
  792. $model->where('crt_time','<=',$return[1]);
  793. }
  794. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  795. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  796. $model->where('enter_time','>=',$return[0]);
  797. $model->where('enter_time','<=',$return[1]);
  798. }
  799. if(! empty($data['crt_name'])){
  800. $id = (new RangeService())->crtNameSearch($data);
  801. $model->whereIn('crt_id',$id);
  802. }
  803. if(! empty($data['fz'])){
  804. $emp_id = Employee::where('del_time',0)
  805. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  806. ->select('id')->get()->toArray();
  807. $emp_id = array_column($emp_id,'id');
  808. $model->whereIn('fz_man', $emp_id);
  809. }
  810. if(! empty($data['contact_info'])) $model->where('contact', 'LIKE', '%'.$data['contact_info'].'%');
  811. return $model;
  812. }
  813. /**
  814. * 拼接数据
  815. * @param $data
  816. * @return array
  817. */
  818. public function fillRepeatData($data,$ergs,$user){
  819. if(empty($data['data'])) return $data;
  820. $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_from'),array_column($data['data'],'car_type')));
  821. $basic_map = BasicType::whereIn('id',$array)
  822. ->pluck('title','id')
  823. ->toArray();
  824. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  825. ->pluck('emp_name','id')
  826. ->toArray();
  827. foreach ($data['data'] as $key => $value){
  828. $data['data'][$key]['address'] = $value['address2'];
  829. //客户来源
  830. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  831. // $this->isTopDispatch($user, $data['data'][$key]);
  832. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  833. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  834. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  835. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  836. }
  837. return $data;
  838. }
  839. public function getCustomerId($data,$user){
  840. $return = [];
  841. if(! empty($data['my_fz'])){
  842. $info = CustomerInfo::where('del_time',0)
  843. ->where('data_id',$user['id'])
  844. ->where('type',CustomerInfo::type_two)
  845. ->select('customer_id')
  846. ->get()->toArray();
  847. $return = array_unique(array_column($info,'customer_id'));
  848. }
  849. if(! empty($data['my_xt'])){
  850. $info = CustomerInfo::where('del_time',0)
  851. ->where('data_id',$user['id'])
  852. ->where('type',CustomerInfo::type_three)
  853. ->select('customer_id')
  854. ->get()->toArray();
  855. $return = array_unique(array_column($info,'customer_id'));
  856. }
  857. return $return;
  858. }
  859. /**
  860. * 客户参数规则
  861. * @param $data
  862. * @param $is_add
  863. * @return array
  864. */
  865. public function customerRule(&$data, $user, $is_add = true){
  866. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  867. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  868. if(empty($data['title'])) return [false,'客户名称不能为空'];
  869. if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
  870. $data['enter_time'] = $this->changeDateToDateMin($data['enter_time']);
  871. //所属部门 以及 顶级部门
  872. if(empty($data['depart_id'])) {
  873. $data['depart_id'] = $this->getDepart($user);
  874. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  875. }
  876. if($data['model_type'] == Customer::Model_type_one){
  877. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  878. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  879. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  880. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  881. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  882. }else{
  883. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  884. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  885. }
  886. list($status,$msg) = $this->limitingSendRequestBackgExpire($data['top_depart_id'] . $data['title'],3);
  887. if(! $status) return [false, $msg];
  888. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  889. $bool = BasicType::where('title',$data['car_type_title'])
  890. ->where('top_depart_id',$data['top_depart_id'])
  891. ->where('type',10)
  892. ->where('del_time',0)
  893. ->exists();
  894. if($bool) return [false,'车型名称已存在'];
  895. }
  896. if($is_add){
  897. $bool = Customer::where('del_time',0)
  898. ->where('top_depart_id',$data['top_depart_id'])
  899. ->where('title',$data['title'])
  900. // ->where('model_type',$data['model_type'])
  901. ->exists();
  902. if(! empty($data['customer_contact'])) {
  903. $search = [];
  904. foreach ($data['customer_contact'] as $value){
  905. $search[] = $value['info'];
  906. }
  907. $boolean = CustomerInfo::from('customer_info as a')
  908. ->join('customer as b','b.id','a.customer_id')
  909. ->where('a.del_time',0)
  910. ->where('b.del_time',0)
  911. ->where('b.top_depart_id',$data['top_depart_id'])
  912. ->whereIn('a.contact_info', $search)
  913. ->exists();
  914. if($boolean) return [false,'客户联系内容已存在'];
  915. }
  916. }else{
  917. if(empty($data['id'])) return [false,'ID不能为空'];
  918. $bool = Customer::where('del_time',0)
  919. ->where('id','<>',$data['id'])
  920. ->where('top_depart_id',$data['top_depart_id'])
  921. ->where('title',$data['title'])
  922. // ->where('model_type',$data['model_type'])
  923. ->exists();
  924. if(! empty($data['customer_contact'])) {
  925. $search = [];
  926. foreach ($data['customer_contact'] as $value){
  927. $search[] = $value['info'];
  928. }
  929. $boolean = CustomerInfo::from('customer_info as a')
  930. ->join('customer as b','b.id','a.customer_id')
  931. ->where('b.id','<>',$data['id'])
  932. ->where('a.del_time',0)
  933. ->where('b.del_time',0)
  934. ->where('b.top_depart_id',$data['top_depart_id'])
  935. ->whereIn('a.contact_info', $search)
  936. ->exists();
  937. if($boolean) return [false,'客户联系内容已存在'];
  938. }
  939. }
  940. if($bool) return [false,'客户名称不能重复'];
  941. return [true, ''];
  942. }
  943. /**
  944. * 拼接数据
  945. * @param $data
  946. * @return array
  947. */
  948. public function fillData($data,$ergs,$user){
  949. if(empty($data['data'])) return $data;
  950. $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')));
  951. $basic_map = BasicType::whereIn('id',$array)
  952. ->pluck('title','id')
  953. ->toArray();
  954. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  955. ->pluck('title','id')
  956. ->toArray();
  957. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  958. ->pluck('emp_name','id')
  959. ->toArray();
  960. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  961. ->pluck('title','id')
  962. ->toArray();
  963. $customer_id = array_column($data['data'],'id');
  964. //跟进记录
  965. $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);
  966. $customer_info = CustomerInfo::where('del_time',0)
  967. ->whereIn('customer_id',$customer_id)
  968. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])
  969. ->select('type','contact_type','contact_info','customer_id','data_id')
  970. ->get()->toArray();
  971. //客户的联系方式 客户的负责人 协同人
  972. $customer_info_map = $fz = $customer_info_map2 = $xt = [];
  973. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  974. ->pluck('emp_name','id')
  975. ->toArray();
  976. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  977. foreach ($customer_info as $value){
  978. if($value['type'] == CustomerInfo::type_one){
  979. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  980. $customer_info_map[$value['customer_id']][] = $value;
  981. if(! isset($customer_info_map2[$value['customer_id']])){
  982. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  983. }else{
  984. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  985. }
  986. }elseif($value['type'] == CustomerInfo::type_two){
  987. $tmp = $emp_map[$value['data_id']] ?? "";
  988. if(isset($fz[$value['customer_id']])){
  989. $fz[$value['customer_id']] .= ',' . $tmp;
  990. }else{
  991. $fz[$value['customer_id']] = $tmp;
  992. }
  993. }else{
  994. $tmp = $emp_map[$value['data_id']] ?? "";
  995. if(isset($xt[$value['customer_id']])){
  996. $xt[$value['customer_id']] .= ',' . $tmp;
  997. }else{
  998. $xt[$value['customer_id']] = $tmp;
  999. }
  1000. }
  1001. }
  1002. $array2 = array_unique(array_column($data['data'],'top_depart_id'));
  1003. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  1004. $depart2_map = $this->getCustomerDepart($customer_id);
  1005. //地址
  1006. $address_map = config('address');
  1007. foreach ($data['data'] as $key => $value){
  1008. $address_str = [];
  1009. if(! empty($value['address1'])) {
  1010. $tmp = json_decode($value['address1'],true);
  1011. $this->findLabelsByValue($address_map,$tmp,$address_str);
  1012. $tmp = implode(' ',$address_str);
  1013. $tmp .= ' ' . $value['address2'];
  1014. $address = $tmp;
  1015. }else{
  1016. $address = $value['address2'];
  1017. }
  1018. $data['data'][$key]['address'] = $address;
  1019. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  1020. //客户来源
  1021. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  1022. $this->isTopDispatch($user, $data['data'][$key]);
  1023. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  1024. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  1025. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  1026. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  1027. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  1028. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  1029. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  1030. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1031. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  1032. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  1033. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1034. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  1035. $data['data'][$key]['customer_detail'] = $customer_tmp;
  1036. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  1037. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  1038. $record_tmp = $record_array[$value['id']] ?? "";
  1039. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  1040. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  1041. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  1042. $top_depart_title = $depart[$value['top_depart_id']] ?? "";
  1043. $data['data'][$key]['top_depart_title'] = $top_depart_title;
  1044. //分配门店
  1045. $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
  1046. if($value['fp_time'] > 0) {
  1047. //分配过所属门店是分配门店
  1048. $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;
  1049. }else{
  1050. //没分配过所属门店是创建门店
  1051. $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;
  1052. }
  1053. $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
  1054. $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
  1055. }
  1056. return $data;
  1057. }
  1058. private function isTopDispatch($user, &$value){
  1059. //特殊功能按钮
  1060. $special_button = $user['special_button'] ?? [];
  1061. //总社
  1062. $head = $user['head']['id'] ?? 0;
  1063. //当前门店
  1064. $top_depart_id = $user['depart_top'][0] ?? [];
  1065. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  1066. //分社账号
  1067. if($top_depart_id != $head){
  1068. //总社创建分配给分社的客户
  1069. if($value['top_depart_id'] == $head && $value['fp_time'] > 0 && $value['fp_top_depart_id'] != $head){
  1070. if(! in_array(RoleMenuButton::special_nine,$special_button)) $value['customer_from_title'] = "";
  1071. }
  1072. }
  1073. }
  1074. public function countData($column = "", $data, $user){
  1075. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1076. // 获取今天的开始和结束时间戳
  1077. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1078. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1079. // 获取昨天的开始和结束时间戳
  1080. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1081. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1082. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1083. $model = $this->customerCommonSearch($data, $user);
  1084. // 查询今天的数据条数
  1085. $todayCount = $model->count();
  1086. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1087. $model = $this->customerCommonSearch($data, $user);
  1088. // 查询昨天的数据条数
  1089. $yesterdayCount = $model->count();
  1090. return [$todayCount, $yesterdayCount];
  1091. }
  1092. public function countData2($column = "", $data, $user){
  1093. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1094. // 获取今天的开始和结束时间戳
  1095. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1096. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1097. // 获取昨天的开始和结束时间戳
  1098. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1099. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1100. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1101. $model = $this->customer2CommonSearch($data, $user);
  1102. // 查询今天的数据条数
  1103. $todayCount = $model->count();
  1104. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1105. $model = $this->customer2CommonSearch($data, $user);
  1106. // 查询昨天的数据条数
  1107. $yesterdayCount = $model->count();
  1108. return [$todayCount, $yesterdayCount];
  1109. }
  1110. //获取客资门店
  1111. public function getCustomerDepart($customer_id = []){
  1112. if(empty($customer_id)) return [];
  1113. $result = SeeRange::where('del_time',0)
  1114. ->whereIn('data_id', $customer_id)
  1115. ->where('data_type',SeeRange::type_one)
  1116. ->where('type',SeeRange::data_three)
  1117. ->select('data_id as customer_id','param_id as top_depart_id')
  1118. ->get()->toArray();
  1119. $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();
  1120. $return = [];
  1121. foreach ($result as $value){
  1122. $tmp = $depart_map[$value['top_depart_id']] ?? "";
  1123. if(! $tmp) continue;
  1124. if(isset($return[$value['customer_id']])){
  1125. $return[$value['customer_id']] .= ',' . $tmp;
  1126. }else{
  1127. $return[$value['customer_id']] = $tmp;
  1128. }
  1129. }
  1130. return $return;
  1131. }
  1132. //客资门店搜索
  1133. public function searchCustomerDepart($depart_title = ""){
  1134. $customer_id = [];
  1135. if(empty($depart_title)) return $customer_id;
  1136. $depart_id = Depart::where('del_time', 0)
  1137. ->where('parent_id', 0)
  1138. ->where('title', 'LIKE', '%'. $depart_title .'%')
  1139. ->select('id')->get()->toArray();
  1140. $depart_id = array_column($depart_id, 'id');
  1141. if(empty($depart_id)) return [$customer_id, $depart_id];
  1142. $customer_id = SeeRange::where('del_time',0)
  1143. ->whereIn('param_id', $depart_id)
  1144. ->where('data_type',SeeRange::type_one)
  1145. ->where('type',SeeRange::data_three)
  1146. ->select('data_id as customer_id')
  1147. ->get()->toArray();
  1148. $customer_id = array_column($customer_id, 'customer_id');
  1149. return [$customer_id, $depart_id];
  1150. }
  1151. //抢客户
  1152. public function customerGrabbing($data, $user){
  1153. $key = Customer::$limitKey . $data['customer_id'];
  1154. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  1155. if(! $status) {
  1156. //释放锁
  1157. $this->dellimitingSendRequestBackg($key);
  1158. return [false,$msg];
  1159. }
  1160. try {
  1161. DB::beginTransaction();
  1162. $time = time();
  1163. $insert = [];
  1164. //负责人获取 改为前负责人
  1165. $man = CustomerInfo::where('del_time',0)
  1166. ->where('customer_id',$data['customer_id'])
  1167. ->where('type', CustomerInfo::type_two)
  1168. ->get()->toArray();
  1169. foreach ($man as $value){
  1170. $insert[] = [
  1171. 'customer_id' => $data['customer_id'],
  1172. 'data_id' => $value['data_id'],
  1173. 'type' => CustomerInfo::type_nine,
  1174. 'crt_time' => $time
  1175. ];
  1176. }
  1177. //增加自己为负责人
  1178. $insert[] = [
  1179. 'customer_id' => $data['customer_id'],
  1180. 'data_id' => $user['id'],
  1181. 'type' => CustomerInfo::type_two,
  1182. 'crt_time' => $time
  1183. ];
  1184. //人员清空
  1185. CustomerInfo::where('del_time',0)
  1186. ->where('customer_id',$data['customer_id'])
  1187. ->whereIn('type', CustomerInfo::$man2)
  1188. ->update(['del_time' => $time]);
  1189. //写入最新人员
  1190. CustomerInfo::insert($insert);
  1191. //更新公海池状态
  1192. Customer::where('id',$data['customer_id'])->update([
  1193. 'pond_state' => 0
  1194. ]);
  1195. DB::commit();
  1196. }catch (\Exception $exception){
  1197. //释放锁
  1198. $this->dellimitingSendRequestBackg($key);
  1199. DB::rollBack();
  1200. return [false,$exception->getMessage()];
  1201. }
  1202. //释放锁
  1203. $this->dellimitingSendRequestBackg($key);
  1204. return [true, ''];
  1205. }
  1206. public function customerGrabbingRule($data, $key){
  1207. if(empty($data['customer_id'])) return [false,'请选择客户'];
  1208. //上锁
  1209. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1210. if(! $status) return [false,$msg];
  1211. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  1212. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1213. $customer = $customer->toArray();
  1214. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  1215. return [true,''];
  1216. }
  1217. public function searchBy($data,$user){
  1218. $model = BasicType::TopClear($user,$data);
  1219. $result = $model->where('del_time',0)
  1220. ->where('type', 2)
  1221. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  1222. ->select('id')
  1223. ->get()->toArray();
  1224. $model2 = Customer::Clear($user,$data);
  1225. $customer = $model2->where('del_time',0)
  1226. ->whereIn('customer_from', array_column($result,'id'))
  1227. ->select('id')
  1228. ->get()->toArray();
  1229. return array_column($customer,'id');
  1230. }
  1231. public function customerImportanceEdit($data,$user){
  1232. if(empty($data['id'])) return [false, '请选择客户'];
  1233. $model = Customer::where('id',$data['id'])->first();
  1234. if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
  1235. if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
  1236. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
  1237. $params = $this->getDataFile($data);
  1238. (new OperationLogService())->setOperationList($params,$user,2);
  1239. try {
  1240. DB::beginTransaction();
  1241. $model = Customer::where('id',$data['id'])->first();
  1242. $model->importance = $data['importance'] ?? '';
  1243. $model->save();
  1244. DB::commit();
  1245. }catch (\Exception $exception){
  1246. DB::rollBack();
  1247. return [false,$exception->getMessage()];
  1248. }
  1249. return [true, ''];
  1250. }
  1251. //发送客户消息
  1252. public function customerSendWx($data, $user){
  1253. list($status,$msg) = $this->customerSendWxRule($data);
  1254. if(! $status) return [false,$msg];
  1255. //发送消息
  1256. $send_data = $msg;
  1257. (new OaService())->sendWxOaCheckMessage($send_data);
  1258. return [true, ''];
  1259. }
  1260. public function customerSendWxRule($data){
  1261. if(empty($data['id'])) return [false,'请选择客户'];
  1262. $customer = Customer::where('del_time',0)
  1263. ->whereIn('id',$data['id'])
  1264. ->get()->toArray();
  1265. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1266. $man = CustomerInfo::where('del_time',0)
  1267. ->whereIn('customer_id',$data['id'])
  1268. ->where('type', CustomerInfo::type_two)
  1269. ->get()->toArray();
  1270. if(empty($man)) return [true, '客户暂无负责人信息,公众号消息发送结束'];
  1271. $customer_map = [];
  1272. foreach ($man as $value){
  1273. $customer_map[$value['data_id']][] = $value['customer_id'];
  1274. }
  1275. $employee_id = array_unique(array_column($man,'data_id'));
  1276. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  1277. ->pluck('openid','employee_id')
  1278. ->toArray();
  1279. $emp = Employee::whereIn('id', $employee_id)
  1280. ->pluck('emp_name','id')
  1281. ->toArray();
  1282. $send_data = [];
  1283. foreach ($employee_id as $value){
  1284. $tmp = $emp[$value] ?? "";
  1285. $open_id = $wx_map[$value] ?? "";
  1286. // if(empty($open_id)) return [false, $tmp . "暂未关注微信公众号,公众号消息发送失败"];
  1287. if(empty($open_id)) continue;
  1288. $customer_id = $customer_map[$value] ?? [];
  1289. $customer_id = implode(',', $customer_id);
  1290. //提醒创建人
  1291. $send_data[] = [
  1292. 'employee_id' => $value,
  1293. 'type' => 2,
  1294. 'state' => 0,
  1295. 'menu_id' => "16|list",
  1296. 'openid' => $open_id,
  1297. 'order_number' => $customer_id,
  1298. 'tmp_data' => [
  1299. time(),
  1300. "客户消息提醒",
  1301. "已发送",
  1302. $tmp,
  1303. date('Y-m-d H:i:s'),
  1304. ],
  1305. ];
  1306. }
  1307. return [true, $send_data];
  1308. }
  1309. }