CustomerService.php 54 KB

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