CustomerService.php 65 KB

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