CustomerService.php 63 KB

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