CustomerService.php 62 KB

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