CustomerService.php 65 KB

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