CustomerService.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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')
  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['crt_name'])){
  550. $id = (new RangeService())->crtNameSearch($data);
  551. $model->whereIn('crt_id',$id);
  552. }
  553. if(! empty($data['fz'])){
  554. $id = (new RangeService())->customerSearch($data);
  555. $model->whereIn('id',$id);
  556. }
  557. if(! empty($data['last_visit_time'])){
  558. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  559. $model->whereIn('id',$id);
  560. }
  561. if(! empty($data['contact_info'])){
  562. $customer_info = CustomerInfo::where('del_time',0)
  563. ->where("type",CustomerInfo::type_one)
  564. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  565. ->select('customer_id')
  566. ->get()->toArray();
  567. $model->whereIn('id',array_column($customer_info,'customer_id'));
  568. }
  569. if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
  570. $is_fp = -1;
  571. if(isset($data['is_fp'])) {
  572. if($data['is_fp']){
  573. $model->where('fp_time', '>', 0);
  574. $is_fp = 1;
  575. } else{
  576. $model->where('fp_time', 0);
  577. $is_fp = 0;
  578. }
  579. }
  580. if(! empty($data['belong_top_depart_title'])){
  581. list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  582. if($is_fp < 0){
  583. $model->whereIn('id', $id)
  584. ->orWhereIn('top_depart_id', $depart_id);
  585. }elseif ($is_fp > 0){
  586. $model->whereIn('id', $id);
  587. }else{
  588. $model->where('top_depart_id', $depart_id);
  589. }
  590. }
  591. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  592. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  593. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  594. }
  595. return $model;
  596. }
  597. /**
  598. * 客户列表
  599. * @param $data
  600. * @param $user
  601. * @return array
  602. */
  603. public function customerList($data,$user){
  604. $model = $this->customerCommonSearch($data,$user);
  605. $list = $this->limit($model,'',$data);
  606. $list = $this->fillData($list,$data);
  607. //微信数据
  608. $count = $this->countData("crt_time", $data, $user);
  609. $list['today'] = $count[0] ?? 0;
  610. $list['yesterday'] = $count[1] ?? 0;
  611. return [true, $list];
  612. }
  613. public function customer2CommonSearch($data,$user){
  614. $model = Customer::Clear($user,$data);
  615. $model = $model->where('del_time',0)
  616. ->where('fp_time','>',0)
  617. ->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')
  618. ->orderby('fp_time', 'desc');
  619. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  620. $id = $this->getCustomerId($data,$user);
  621. $model->whereIn('id',$id);
  622. }
  623. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  624. if(! empty($data['time_type'])) {
  625. if($data['time_type'] == 1) {
  626. $start = strtotime('today');
  627. $end = strtotime('tomorrow') - 1;
  628. }elseif ($data['time_type'] == 2){
  629. $start = strtotime('this week',strtotime('today'));
  630. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  631. }
  632. if(! empty($start) && ! empty($end)) {
  633. $model->where('crt_time','>=',$start);
  634. $model->where('crt_time','<=',$end);
  635. }
  636. }
  637. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  638. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  639. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  640. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  641. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  642. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  643. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  644. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  645. $model->where('crt_time','>=',$return[0]);
  646. $model->where('crt_time','<=',$return[1]);
  647. }
  648. if(! empty($data['crt_name'])){
  649. $id = (new RangeService())->crtNameSearch($data);
  650. $model->whereIn('crt_id',$id);
  651. }
  652. if(! empty($data['fz'])){
  653. $id = (new RangeService())->customerSearch($data);
  654. $model->whereIn('id',$id);
  655. }
  656. if(! empty($data['last_visit_time'])){
  657. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  658. $model->whereIn('id',$id);
  659. }
  660. if(! empty($data['contact_info'])){
  661. $customer_info = CustomerInfo::where('del_time',0)
  662. ->where("type",CustomerInfo::type_one)
  663. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  664. ->select('customer_id')
  665. ->get()->toArray();
  666. $model->whereIn('id',array_column($customer_info,'customer_id'));
  667. }
  668. if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){
  669. $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);
  670. $model->where('fp_time','>=',$return[0]);
  671. $model->where('fp_time','<=',$return[1]);
  672. }
  673. if(! empty($data['belong_top_depart_title'])){
  674. list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  675. $model->whereIn('id', $id);
  676. }
  677. if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
  678. $model->where('fp_time','>=',$data['wx_fp_time'][0]);
  679. $model->where('fp_time','<=',$data['wx_fp_time'][1]);
  680. }
  681. return $model;
  682. }
  683. public function customerList2($data,$user){
  684. $model = $this->customer2CommonSearch($data, $user);
  685. $list = $this->limit($model,'',$data);
  686. $list = $this->fillData($list,$data);
  687. //微信数据
  688. $count = $this->countData2("fp_time", $data, $user);
  689. $list['today'] = $count[0] ?? 0;
  690. $list['yesterday'] = $count[1] ?? 0;
  691. return [true, $list];
  692. }
  693. public function getCustomerId($data,$user){
  694. $return = [];
  695. if(! empty($data['my_fz'])){
  696. $info = CustomerInfo::where('del_time',0)
  697. ->where('data_id',$user['id'])
  698. ->where('type',CustomerInfo::type_two)
  699. ->select('customer_id')
  700. ->get()->toArray();
  701. $return = array_unique(array_column($info,'customer_id'));
  702. }
  703. if(! empty($data['my_xt'])){
  704. $info = CustomerInfo::where('del_time',0)
  705. ->where('data_id',$user['id'])
  706. ->where('type',CustomerInfo::type_three)
  707. ->select('customer_id')
  708. ->get()->toArray();
  709. $return = array_unique(array_column($info,'customer_id'));
  710. }
  711. return $return;
  712. }
  713. /**
  714. * 客户参数规则
  715. * @param $data
  716. * @param $is_add
  717. * @return array
  718. */
  719. public function customerRule(&$data, $user, $is_add = true){
  720. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  721. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  722. if(empty($data['title'])) return [false,'客户名称不能为空'];
  723. if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
  724. $data['enter_time'] = $this->changeDateToDate($data['enter_time']);
  725. //所属部门 以及 顶级部门
  726. if(empty($data['depart_id'])) {
  727. $data['depart_id'] = $this->getDepart($user);
  728. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  729. }
  730. if($data['model_type'] == Customer::Model_type_one){
  731. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  732. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  733. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  734. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  735. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  736. }else{
  737. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  738. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  739. }
  740. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  741. $bool = BasicType::where('title',$data['car_type_title'])
  742. ->where('top_depart_id',$data['top_depart_id'])
  743. ->where('type',10)
  744. ->where('del_time',0)
  745. ->exists();
  746. if($bool) return [false,'车型名称已存在'];
  747. }
  748. if($is_add){
  749. $bool = Customer::where('del_time',0)
  750. ->where('top_depart_id',$data['top_depart_id'])
  751. ->where('title',$data['title'])
  752. // ->where('model_type',$data['model_type'])
  753. ->exists();
  754. if(! empty($data['customer_contact'])) {
  755. $search = [];
  756. foreach ($data['customer_contact'] as $value){
  757. $search[] = $value['info'];
  758. }
  759. $boolean = CustomerInfo::from('customer_info as a')
  760. ->join('customer as b','b.id','a.customer_id')
  761. ->where('a.del_time',0)
  762. ->where('b.del_time',0)
  763. ->where('b.top_depart_id',$data['top_depart_id'])
  764. ->whereIn('a.contact_info', $search)
  765. ->exists();
  766. if($boolean) return [false,'客户联系内容已存在'];
  767. }
  768. }else{
  769. if(empty($data['id'])) return [false,'ID不能为空'];
  770. $bool = Customer::where('del_time',0)
  771. ->where('id','<>',$data['id'])
  772. ->where('top_depart_id',$data['top_depart_id'])
  773. ->where('title',$data['title'])
  774. // ->where('model_type',$data['model_type'])
  775. ->exists();
  776. if(! empty($data['customer_contact'])) {
  777. $search = [];
  778. foreach ($data['customer_contact'] as $value){
  779. $search[] = $value['info'];
  780. }
  781. $boolean = CustomerInfo::from('customer_info as a')
  782. ->join('customer as b','b.id','a.customer_id')
  783. ->where('b.id','<>',$data['id'])
  784. ->where('a.del_time',0)
  785. ->where('b.del_time',0)
  786. ->where('b.top_depart_id',$data['top_depart_id'])
  787. ->whereIn('a.contact_info', $search)
  788. ->exists();
  789. if($boolean) return [false,'客户联系内容已存在'];
  790. }
  791. }
  792. if($bool) return [false,'客户名称不能重复'];
  793. return [true, ''];
  794. }
  795. /**
  796. * 拼接数据
  797. * @param $data
  798. * @return array
  799. */
  800. public function fillData($data,$ergs){
  801. if(empty($data['data'])) return $data;
  802. $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')));
  803. $basic_map = BasicType::whereIn('id',$array)
  804. ->pluck('title','id')
  805. ->toArray();
  806. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  807. ->pluck('title','id')
  808. ->toArray();
  809. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  810. ->pluck('emp_name','id')
  811. ->toArray();
  812. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  813. ->pluck('title','id')
  814. ->toArray();
  815. $customer_id = array_column($data['data'],'id');
  816. //跟进记录
  817. $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);
  818. $customer_info = CustomerInfo::where('del_time',0)
  819. ->whereIn('customer_id',$customer_id)
  820. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])
  821. ->select('type','contact_type','contact_info','customer_id','data_id')
  822. ->get()->toArray();
  823. //客户的联系方式 客户的负责人 协同人
  824. $customer_info_map = $fz = $customer_info_map2 = $xt = [];
  825. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  826. ->pluck('emp_name','id')
  827. ->toArray();
  828. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  829. foreach ($customer_info as $value){
  830. if($value['type'] == CustomerInfo::type_one){
  831. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  832. $customer_info_map[$value['customer_id']][] = $value;
  833. if(! isset($customer_info_map2[$value['customer_id']])){
  834. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  835. }else{
  836. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  837. }
  838. }elseif($value['type'] == CustomerInfo::type_two){
  839. $tmp = $emp_map[$value['data_id']] ?? "";
  840. if(isset($fz[$value['customer_id']])){
  841. $fz[$value['customer_id']] .= ',' . $tmp;
  842. }else{
  843. $fz[$value['customer_id']] = $tmp;
  844. }
  845. }else{
  846. $tmp = $emp_map[$value['data_id']] ?? "";
  847. if(isset($xt[$value['customer_id']])){
  848. $xt[$value['customer_id']] .= ',' . $tmp;
  849. }else{
  850. $xt[$value['customer_id']] = $tmp;
  851. }
  852. }
  853. }
  854. $array2 = array_unique(array_column($data['data'],'top_depart_id'));
  855. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  856. $depart2_map = $this->getCustomerDepart($customer_id);
  857. $address_map = config('address');
  858. foreach ($data['data'] as $key => $value){
  859. $address_str = [];
  860. if(! empty($value['address1'])) {
  861. $tmp = json_decode($value['address1'],true);
  862. $this->findLabelsByValue($address_map,$tmp,$address_str);
  863. $tmp = implode(' ',$address_str);
  864. $tmp .= ' ' . $value['address2'];
  865. $address = $tmp;
  866. }else{
  867. $address = $value['address2'];
  868. }
  869. $data['data'][$key]['address'] = $address;
  870. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  871. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  872. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  873. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  874. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  875. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  876. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  877. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  878. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  879. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  880. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  881. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  882. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  883. $data['data'][$key]['customer_detail'] = $customer_tmp;
  884. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  885. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  886. $record_tmp = $record_array[$value['id']] ?? "";
  887. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  888. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  889. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  890. $top_depart_title = $depart[$value['top_depart_id']] ?? "";
  891. $data['data'][$key]['top_depart_title'] = $top_depart_title;
  892. //分配门店
  893. $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
  894. if($value['fp_time'] > 0) {
  895. //分配过所属门店是分配门店
  896. $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;
  897. }else{
  898. //没分配过所属门店是创建门店
  899. $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;
  900. }
  901. $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
  902. $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
  903. }
  904. return $data;
  905. }
  906. public function countData($column = "", $data, $user){
  907. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  908. // 获取今天的开始和结束时间戳
  909. $todayStart = Carbon::today()->startOfDay()->timestamp;
  910. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  911. // 获取昨天的开始和结束时间戳
  912. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  913. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  914. $data['wx_' . $column] = [$todayStart, $todayEnd];
  915. $model = $this->customerCommonSearch($data, $user);
  916. // 查询今天的数据条数
  917. $todayCount = $model->count();
  918. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  919. $model = $this->customerCommonSearch($data, $user);
  920. // 查询昨天的数据条数
  921. $yesterdayCount = $model->count();
  922. return [$todayCount, $yesterdayCount];
  923. }
  924. public function countData2($column = "", $data, $user){
  925. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  926. // 获取今天的开始和结束时间戳
  927. $todayStart = Carbon::today()->startOfDay()->timestamp;
  928. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  929. // 获取昨天的开始和结束时间戳
  930. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  931. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  932. $data['wx_' . $column] = [$todayStart, $todayEnd];
  933. $model = $this->customer2CommonSearch($data, $user);
  934. // 查询今天的数据条数
  935. $todayCount = $model->count();
  936. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  937. $model = $this->customer2CommonSearch($data, $user);
  938. // 查询昨天的数据条数
  939. $yesterdayCount = $model->count();
  940. return [$todayCount, $yesterdayCount];
  941. }
  942. //获取客资门店
  943. public function getCustomerDepart($customer_id = []){
  944. if(empty($customer_id)) return [];
  945. $result = SeeRange::where('del_time',0)
  946. ->whereIn('data_id', $customer_id)
  947. ->where('data_type',SeeRange::type_one)
  948. ->where('type',SeeRange::data_three)
  949. ->select('data_id as customer_id','param_id as top_depart_id')
  950. ->get()->toArray();
  951. $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();
  952. $return = [];
  953. foreach ($result as $value){
  954. $tmp = $depart_map[$value['top_depart_id']] ?? "";
  955. if(! $tmp) continue;
  956. if(isset($return[$value['customer_id']])){
  957. $return[$value['customer_id']] .= ',' . $tmp;
  958. }else{
  959. $return[$value['customer_id']] = $tmp;
  960. }
  961. }
  962. return $return;
  963. }
  964. //客资门店搜索
  965. public function searchCustomerDepart($depart_title = ""){
  966. $customer_id = [];
  967. if(empty($depart_title)) return $customer_id;
  968. $depart_id = Depart::where('del_time', 0)
  969. ->where('parent_id', 0)
  970. ->where('title', 'LIKE', '%'. $depart_title .'%')
  971. ->select('id')->get()->toArray();
  972. $depart_id = array_column($depart_id, 'id');
  973. if(empty($depart_id)) return [$customer_id, $depart_id];
  974. $customer_id = SeeRange::where('del_time',0)
  975. ->whereIn('param_id', $depart_id)
  976. ->where('data_type',SeeRange::type_one)
  977. ->where('type',SeeRange::data_three)
  978. ->select('data_id as customer_id')
  979. ->get()->toArray();
  980. $customer_id = array_column($customer_id, 'customer_id');
  981. return [$customer_id, $depart_id];
  982. }
  983. //抢客户
  984. public function customerGrabbing($data, $user){
  985. $key = Customer::$limitKey . $data['customer_id'];
  986. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  987. if(! $status) {
  988. //释放锁
  989. $this->dellimitingSendRequestBackg($key);
  990. return [false,$msg];
  991. }
  992. try {
  993. DB::beginTransaction();
  994. $time = time();
  995. $insert = [];
  996. //负责人获取 改为前负责人
  997. $man = CustomerInfo::where('del_time',0)
  998. ->where('customer_id',$data['customer_id'])
  999. ->where('type', CustomerInfo::type_two)
  1000. ->get()->toArray();
  1001. foreach ($man as $value){
  1002. $insert[] = [
  1003. 'customer_id' => $data['customer_id'],
  1004. 'data_id' => $value['data_id'],
  1005. 'type' => CustomerInfo::type_nine,
  1006. 'crt_time' => $time
  1007. ];
  1008. }
  1009. //增加自己为负责人
  1010. $insert[] = [
  1011. 'customer_id' => $data['customer_id'],
  1012. 'data_id' => $user['id'],
  1013. 'type' => CustomerInfo::type_two,
  1014. 'crt_time' => $time
  1015. ];
  1016. //人员清空
  1017. CustomerInfo::where('del_time',0)
  1018. ->where('customer_id',$data['customer_id'])
  1019. ->whereIn('type', CustomerInfo::$man2)
  1020. ->update(['del_time' => $time]);
  1021. //写入最新人员
  1022. CustomerInfo::insert($insert);
  1023. //更新公海池状态
  1024. Customer::where('id',$data['customer_id'])->update([
  1025. 'pond_state' => 0
  1026. ]);
  1027. DB::commit();
  1028. }catch (\Exception $exception){
  1029. //释放锁
  1030. $this->dellimitingSendRequestBackg($key);
  1031. DB::rollBack();
  1032. return [false,$exception->getMessage()];
  1033. }
  1034. //释放锁
  1035. $this->dellimitingSendRequestBackg($key);
  1036. return [true, ''];
  1037. }
  1038. public function customerGrabbingRule($data, $key){
  1039. if(empty($data['customer_id'])) return [false,'请选择客户'];
  1040. //上锁
  1041. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1042. if(! $status) return [false,$msg];
  1043. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  1044. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1045. $customer = $customer->toArray();
  1046. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  1047. return [true,''];
  1048. }
  1049. public function searchBy($data,$user){
  1050. $model = BasicType::TopClear($user,$data);
  1051. $result = $model->where('del_time',0)
  1052. ->where('type', 2)
  1053. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  1054. ->select('id')
  1055. ->get()->toArray();
  1056. $model2 = Customer::Clear($user,$data);
  1057. $customer = $model2->where('del_time',0)
  1058. ->whereIn('customer_from', array_column($result,'id'))
  1059. ->select('id')
  1060. ->get()->toArray();
  1061. return array_column($customer,'id');
  1062. }
  1063. public function customerImportanceEdit($data,$user){
  1064. if(empty($data['id'])) return [false, '请选择客户'];
  1065. $model = Customer::where('id',$data['id'])->first();
  1066. if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
  1067. if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
  1068. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
  1069. $params = $this->getDataFile($data);
  1070. (new OperationLogService())->setOperationList($params,$user,2);
  1071. try {
  1072. DB::beginTransaction();
  1073. $model = Customer::where('id',$data['id'])->first();
  1074. $model->importance = $data['importance'] ?? '';
  1075. $model->save();
  1076. DB::commit();
  1077. }catch (\Exception $exception){
  1078. DB::rollBack();
  1079. return [false,$exception->getMessage()];
  1080. }
  1081. return [true, ''];
  1082. }
  1083. }