CustomerService.php 64 KB

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