CustomerService.php 53 KB

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