CustomerService.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 Illuminate\Support\Facades\DB;
  12. /**
  13. * 客户管理相关
  14. */
  15. class CustomerService extends Service
  16. {
  17. /**
  18. * 客户编辑
  19. * @param $data
  20. * @param $user
  21. * @return array
  22. */
  23. public function customerEdit($data,$user){
  24. list($status,$msg) = $this->customerRule($data,$user, false);
  25. if(!$status) return [$status,$msg];
  26. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $data['title'];
  27. $params = $this->getDataFile($data);
  28. (new OperationLogService())->setOperationList($params,$user,2);
  29. try {
  30. DB::beginTransaction();
  31. //车型
  32. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  33. $model_2 = new BasicType();
  34. $model_2->title = $data['car_type_title'];
  35. $model_2->type = 10;
  36. $model_2->depart_id = $data['depart_id'] ?? 0;
  37. $model_2->top_depart_id = $data['top_depart_id'] ?? 0;
  38. $model_2->crt_id = $user['id'];
  39. $model_2->save();
  40. $data['car_type'] = $model_2->id;
  41. }
  42. $model = Customer::where('id',$data['id'])->first();
  43. $model->title = $data['title'];
  44. $model->code = $data['code'] ?? "";
  45. $model->model_type = $data['model_type'];
  46. $model->customer_intention = $data['customer_intention'] ?? 0;
  47. $model->customer_from = $data['customer_from'] ?? 0;
  48. $model->customer_type = $data['customer_type'] ?? 0;
  49. $model->car_type = $data['car_type'] ?? 0;
  50. $model->consulting_product = $data['consulting_product'] ?? '';
  51. $model->intention_product = $data['intention_product'] ?? 0;
  52. $model->progress_stage = $data['progress_stage'] ?? 0;
  53. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  54. $model->address2 = $data['address2'] ?? '';
  55. $model->mark = $data['mark'] ?? '';
  56. $model->importance = $data['importance'] ?? '';
  57. $model->company = $data['company'] ?? '';
  58. // $model->company_short_name = $data['company_short_name'] ?? '';
  59. $model->state_type = $data['state_type'] ?? 0;
  60. $model->customer_state = $data['customer_state'] ?? 0;
  61. $model->save();
  62. $time = time();
  63. $old = CustomerInfo::where('del_time',0)
  64. ->where('customer_id',$data['id'])
  65. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  66. ->select('file')
  67. ->get()->toArray();
  68. $old = array_column($old,'file');
  69. CustomerInfo::where('del_time',0)
  70. ->where('customer_id',$data['id'])
  71. ->where('type','<>',CustomerInfo::type_two)
  72. ->update(['del_time' => $time]);
  73. if(! empty($data['customer_contact'])){
  74. $insert = [];
  75. foreach ($data['customer_contact'] as $value){
  76. $insert[] = [
  77. 'customer_id' => $model->id,
  78. 'contact_type' => $value['id'],
  79. 'contact_info' => $value['info'],
  80. 'type' => CustomerInfo::type_one,
  81. 'crt_time' => $time,
  82. ];
  83. }
  84. CustomerInfo::insert($insert);
  85. }
  86. if(! empty($data['employee_one'])){
  87. $insert = [];
  88. foreach ($data['employee_one'] as $value){
  89. $insert[] = [
  90. 'customer_id' => $model->id,
  91. 'data_id' => $value,
  92. 'type' => CustomerInfo::type_two,
  93. 'crt_time' => $time,
  94. ];
  95. }
  96. CustomerInfo::insert($insert);
  97. }
  98. if(! empty($data['employee_two'])){
  99. $insert = [];
  100. foreach ($data['employee_two'] as $value){
  101. $insert[] = [
  102. 'customer_id' => $model->id,
  103. 'data_id' => $value,
  104. 'type' => CustomerInfo::type_three,
  105. 'crt_time' => $time,
  106. ];
  107. }
  108. CustomerInfo::insert($insert);
  109. }
  110. if(! empty($data['employee_three'])){
  111. $insert = [];
  112. foreach ($data['employee_three'] as $value){
  113. $insert[] = [
  114. 'customer_id' => $model->id,
  115. 'data_id' => $value,
  116. 'type' => CustomerInfo::type_four,
  117. 'crt_time' => $time,
  118. ];
  119. }
  120. CustomerInfo::insert($insert);
  121. }
  122. $new = [];
  123. if(! empty($data['img'])){
  124. $insert = [];
  125. foreach ($data['img'] as $value){
  126. $insert[] = [
  127. 'customer_id' => $model->id,
  128. 'file' => $value['url'],
  129. 'type' => CustomerInfo::type_five,
  130. 'name' => $value['name'],
  131. 'crt_time' => $time,
  132. ];
  133. $new[] = $value['url'];
  134. }
  135. CustomerInfo::insert($insert);
  136. }
  137. if(! empty($data['file'])){
  138. $insert = [];
  139. foreach ($data['file'] as $value){
  140. $insert[] = [
  141. 'customer_id' => $model->id,
  142. 'file' => $value['url'],
  143. 'type' => CustomerInfo::type_six,
  144. 'name' => $value['name'],
  145. 'crt_time' => $time,
  146. ];
  147. $new[] = $value['url'];
  148. }
  149. CustomerInfo::insert($insert);
  150. }
  151. DB::commit();
  152. }catch (\Exception $exception){
  153. DB::rollBack();
  154. return [false,$exception->getMessage()];
  155. }
  156. $this->delStorageFile($old, $new);
  157. return [true,''];
  158. }
  159. /**
  160. * 客户新增
  161. * @param $data
  162. * @param $user
  163. * @return array
  164. */
  165. public function customerAdd($data,$user){
  166. list($status,$msg) = $this->customerRule($data,$user);
  167. if(!$status) return [$status,$msg];
  168. try {
  169. DB::beginTransaction();
  170. //车型
  171. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  172. $model_2 = new BasicType();
  173. $model_2->title = $data['car_type_title'];
  174. $model_2->type = 10;
  175. $model_2->depart_id = $data['depart_id'] ?? 0;
  176. $model_2->top_depart_id = $data['top_depart_id'] ?? 0;
  177. $model_2->crt_id = $user['id'];
  178. $model_2->save();
  179. $data['car_type'] = $model_2->id;
  180. }
  181. $model = new Customer();
  182. $model->title = $data['title'];
  183. $model->code = $data['code'] ?? "";
  184. $model->model_type = $data['model_type'];
  185. $model->customer_intention = $data['customer_intention'] ?? 0;
  186. $model->customer_from = $data['customer_from'] ?? 0;
  187. $model->customer_type = $data['customer_type'] ?? 0;
  188. $model->car_type = $data['car_type'] ?? 0;
  189. $model->consulting_product = $data['consulting_product'] ?? '';
  190. $model->intention_product = $data['intention_product'] ?? 0;
  191. $model->progress_stage = $data['progress_stage'] ?? 0;
  192. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  193. $model->address2 = $data['address2'] ?? '';
  194. $model->crt_id = $user['id'];
  195. $model->mark = $data['mark'] ?? '';
  196. $model->importance = $data['importance'] ?? '';
  197. $model->company = $data['company'] ?? '';
  198. // $model->company_short_name = $data['company_short_name'] ?? '';
  199. $model->depart_id = $data['depart_id'] ?? 0;
  200. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  201. $model->state_type = $data['state_type'] ?? 0;
  202. $model->customer_state = $data['customer_state'] ?? 0;
  203. $model->save();
  204. $time = time();
  205. if(! empty($data['customer_contact'])){
  206. $insert = [];
  207. foreach ($data['customer_contact'] as $value){
  208. $insert[] = [
  209. 'customer_id' => $model->id,
  210. 'contact_type' => $value['id'],
  211. 'contact_info' => $value['info'],
  212. 'type' => CustomerInfo::type_one,
  213. 'crt_time' => $time,
  214. ];
  215. }
  216. CustomerInfo::insert($insert);
  217. }
  218. if(! empty($data['employee_one'])){
  219. $insert = [];
  220. foreach ($data['employee_one'] as $value){
  221. $insert[] = [
  222. 'customer_id' => $model->id,
  223. 'data_id' => $value,
  224. 'type' => CustomerInfo::type_two,
  225. 'crt_time' => $time,
  226. ];
  227. }
  228. CustomerInfo::insert($insert);
  229. Customer::where('id',$model->id)->update([
  230. 'fp_top_depart_id' => $data['fp_top_depart_id'] ?? 0,
  231. 'fp_time' => $time,
  232. ]);
  233. }
  234. if(! empty($data['employee_two'])){
  235. $insert = [];
  236. foreach ($data['employee_two'] as $value){
  237. $insert[] = [
  238. 'customer_id' => $model->id,
  239. 'data_id' => $value,
  240. 'type' => CustomerInfo::type_three,
  241. 'crt_time' => $time,
  242. ];
  243. }
  244. CustomerInfo::insert($insert);
  245. }
  246. if(! empty($data['employee_three'])){
  247. $insert = [];
  248. foreach ($data['employee_three'] as $value){
  249. $insert[] = [
  250. 'customer_id' => $model->id,
  251. 'data_id' => $value,
  252. 'type' => CustomerInfo::type_four,
  253. 'crt_time' => $time,
  254. ];
  255. }
  256. CustomerInfo::insert($insert);
  257. }
  258. if(! empty($data['img'])){
  259. $insert = [];
  260. foreach ($data['img'] as $value){
  261. $insert[] = [
  262. 'customer_id' => $model->id,
  263. 'file' => $value['url'],
  264. 'type' => CustomerInfo::type_five,
  265. 'name' => $value['name'],
  266. 'crt_time' => $time,
  267. ];
  268. }
  269. CustomerInfo::insert($insert);
  270. }
  271. if(! empty($data['file'])){
  272. $insert = [];
  273. foreach ($data['file'] as $value){
  274. $insert[] = [
  275. 'customer_id' => $model->id,
  276. 'file' => $value['url'],
  277. 'type' => CustomerInfo::type_six,
  278. 'name' => $value['name'],
  279. 'crt_time' => $time,
  280. ];
  281. }
  282. CustomerInfo::insert($insert);
  283. }
  284. DB::commit();
  285. }catch (\Exception $exception){
  286. DB::rollBack();
  287. return [false,$exception->getMessage()];
  288. }
  289. $data['order_number'] = Customer::$order_number . "|" . $model->id . "|" . $data['title'];
  290. (new OperationLogService())->setOperationList($data,$user);
  291. return [true,''];
  292. }
  293. /**
  294. * 客户删除
  295. * @param $data
  296. * @return array
  297. */
  298. public function customerDel($data){
  299. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  300. try {
  301. DB::beginTransaction();
  302. Customer::where('id',$data['id'])->update([
  303. 'del_time'=> time()
  304. ]);
  305. $old = CustomerInfo::where('del_time',0)
  306. ->where('customer_id',$data['id'])
  307. ->whereIn('type',[CustomerInfo::type_five,CustomerInfo::type_six])
  308. ->select('file')
  309. ->get()->toArray();
  310. $old = array_column($old,'file');
  311. CustomerInfo::where('del_time',0)
  312. ->where('customer_id',$data['id'])
  313. ->update(['del_time' => time()]);
  314. (new RangeService())->RangeDelete($data['id'],SeeRange::type_one);
  315. DB::commit();
  316. }catch (\Exception $exception){
  317. DB::rollBack();
  318. return [false,$exception->getMessage()];
  319. }
  320. $this->delStorageFile($old);
  321. return [true,''];
  322. }
  323. /**
  324. * 客户详情
  325. * @param $data
  326. * @return array
  327. */
  328. public function customerDetail($data){
  329. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  330. $customer = Customer::where('del_time',0)
  331. ->where('id',$data['id'])
  332. ->first();
  333. if(empty($customer)) return [false,'客户不存在或已被删除'];
  334. $customer = $customer->toArray();
  335. $customer['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $customer['title'];
  336. $product = Product::where('id',$customer['intention_product'])->value('title');
  337. $customer['intention_product_title'] = $product;
  338. $customer['product_category'] = [];
  339. if(! empty($customer['intention_product'])){
  340. $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);
  341. $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';
  342. $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];
  343. }
  344. $address_map = config('address');
  345. $address_str = [];
  346. if(! empty($customer['address1'])) {
  347. $tmp = json_decode($customer['address1'],true);
  348. $this->findLabelsByValue($address_map,$tmp,$address_str);
  349. $customer['address1'] = $tmp;
  350. $tmp = implode(' ',$address_str);
  351. $tmp .= ' ' . $customer['address2'];
  352. $address = $tmp;
  353. }else{
  354. $address = $customer['address2'];
  355. }
  356. $customer['address'] = $address;
  357. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  358. $array = [
  359. $customer['customer_intention'],
  360. $customer['customer_from'],
  361. $customer['customer_type'],
  362. $customer['car_type'],
  363. $customer['progress_stage'],
  364. $customer['state_type'],
  365. $customer['customer_state'],
  366. ];
  367. $basic_map = BasicType::whereIn('id',$array)
  368. ->pluck('title','id')
  369. ->toArray();
  370. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  371. $customer = [$customer];
  372. foreach ($customer as $key => $value){
  373. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  374. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  375. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  376. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  377. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  378. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  379. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  380. $customer[$key]['depart_title'] = $depart_title ?? '';
  381. }
  382. $customer = $customer[0];
  383. $customer_info = CustomerInfo::where('del_time',0)
  384. ->where('customer_id',$customer['id'])
  385. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  386. ->get()->toArray();
  387. $emp_id = [];
  388. $emp_id[] = $customer['crt_id'];
  389. foreach ($customer_info as $value){
  390. if(in_array($value['type'], CustomerInfo::$man)){
  391. $emp_id[] = $value['data_id'];
  392. }
  393. }
  394. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  395. ->pluck('emp_name','id')
  396. ->toArray();
  397. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  398. ->pluck('title','id')
  399. ->toArray();
  400. foreach ($customer_info as $value){
  401. if($value['type'] == CustomerInfo::type_one){
  402. $tmp = [
  403. 'id' => $value['contact_type'],
  404. 'title' => $basic_map2[$value['contact_type']] ?? '',
  405. 'info' => $value['contact_info']
  406. ];
  407. $customer['customer_contact'][] = $tmp;
  408. }elseif ($value['type'] == CustomerInfo::type_two){
  409. $tmp = [
  410. 'id' => $value['data_id'],
  411. 'name' => $emp_map[$value['data_id']] ?? '',
  412. ];
  413. $customer['employee_one'][] = $tmp;
  414. }elseif ($value['type'] == CustomerInfo::type_three){
  415. $tmp = [
  416. 'id' => $value['data_id'],
  417. 'name' => $emp_map[$value['data_id']] ?? '',
  418. ];
  419. $customer['employee_two'][] = $tmp;
  420. }elseif ($value['type'] == CustomerInfo::type_four){
  421. $tmp = [
  422. 'id' => $value['data_id'],
  423. 'name' => $emp_map[$value['data_id']] ?? '',
  424. ];
  425. $customer['employee_three'][] = $tmp;
  426. }elseif ($value['type'] == CustomerInfo::type_five){
  427. $tmp = [
  428. 'url' => $value['file'],
  429. 'name' => $value['name'],
  430. ];
  431. $customer['img'][] = $tmp;
  432. }elseif ($value['type'] == CustomerInfo::type_six){
  433. $tmp = [
  434. 'url' => $value['file'],
  435. 'name' => $value['name'],
  436. ];
  437. $customer['file'][] = $tmp;
  438. }elseif ($value['type'] == CustomerInfo::type_nine){
  439. $tmp = [
  440. 'id' => $value['data_id'],
  441. 'name' => $emp_map[$value['data_id']] ?? '',
  442. ];
  443. $customer['old_employee_one'][] = $tmp;
  444. }
  445. }
  446. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  447. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  448. //可见范围
  449. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  450. $customer['depart'] = $return[0] ?? [];
  451. $customer['employee'] = $return[1] ?? [];
  452. return [true, $customer];
  453. }
  454. /**
  455. * 客户列表
  456. * @param $data
  457. * @param $user
  458. * @return array
  459. */
  460. public function customerList($data,$user){
  461. $model = Customer::Clear($user,$data);
  462. $model = $model->where('del_time',0)
  463. ->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')
  464. ->orderby('id', 'desc');
  465. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  466. $id = $this->getCustomerId($data,$user);
  467. $model->whereIn('id',$id);
  468. }
  469. if(! empty($data['pond_state'])) {
  470. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  471. if(empty($search_depart_id)){
  472. $top_depart_id = $user['depart_top'][0] ?? [];
  473. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  474. }else{
  475. //查询 顶级公司
  476. $top_depart_id = $search_depart_id;
  477. }
  478. // 进入公海池的客户 公司下所有人可见
  479. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  480. }
  481. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  482. if(! empty($data['time_type'])) {
  483. if($data['time_type'] == 1) {
  484. $start = strtotime('today');
  485. $end = strtotime('tomorrow') - 1;
  486. }elseif ($data['time_type'] == 2){
  487. $start = strtotime('this week',strtotime('today'));
  488. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  489. }
  490. if(! empty($start) && ! empty($end)) {
  491. $model->where('crt_time','>=',$start);
  492. $model->where('crt_time','<=',$end);
  493. }
  494. }
  495. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  496. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  497. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  498. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  499. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  500. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  501. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  502. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  503. $model->where('crt_time','>=',$return[0]);
  504. $model->where('crt_time','<=',$return[1]);
  505. }
  506. if(! empty($data['crt_name'])){
  507. $id = (new RangeService())->crtNameSearch($data);
  508. $model->whereIn('crt_id',$id);
  509. }
  510. if(! empty($data['fz'])){
  511. $id = (new RangeService())->customerSearch($data);
  512. $model->whereIn('id',$id);
  513. }
  514. if(! empty($data['last_visit_time'])){
  515. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  516. $model->whereIn('id',$id);
  517. }
  518. if(! empty($data['contact_info'])){
  519. $customer_info = CustomerInfo::where('del_time',0)
  520. ->where("type",CustomerInfo::type_one)
  521. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  522. ->select('customer_id')
  523. ->get()->toArray();
  524. $model->whereIn('id',array_column($customer_info,'customer_id'));
  525. }
  526. $list = $this->limit($model,'',$data);
  527. $list = $this->fillData($list,$data);
  528. return [true, $list];
  529. }
  530. public function customerList2($data,$user){
  531. $model = Customer::Clear($user,$data);
  532. $model = $model->where('del_time',0)
  533. ->where('fp_time','>',0)
  534. ->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')
  535. ->orderby('id', 'desc')
  536. ->orderby('fp_time', 'desc');
  537. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  538. $id = $this->getCustomerId($data,$user);
  539. $model->whereIn('id',$id);
  540. }
  541. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  542. if(! empty($data['time_type'])) {
  543. if($data['time_type'] == 1) {
  544. $start = strtotime('today');
  545. $end = strtotime('tomorrow') - 1;
  546. }elseif ($data['time_type'] == 2){
  547. $start = strtotime('this week',strtotime('today'));
  548. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  549. }
  550. if(! empty($start) && ! empty($end)) {
  551. $model->where('crt_time','>=',$start);
  552. $model->where('crt_time','<=',$end);
  553. }
  554. }
  555. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  556. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  557. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  558. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  559. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  560. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  561. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  562. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  563. $model->where('crt_time','>=',$return[0]);
  564. $model->where('crt_time','<=',$return[1]);
  565. }
  566. if(! empty($data['crt_name'])){
  567. $id = (new RangeService())->crtNameSearch($data);
  568. $model->whereIn('crt_id',$id);
  569. }
  570. if(! empty($data['fz'])){
  571. $id = (new RangeService())->customerSearch($data);
  572. $model->whereIn('id',$id);
  573. }
  574. if(! empty($data['last_visit_time'])){
  575. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  576. $model->whereIn('id',$id);
  577. }
  578. if(! empty($data['contact_info'])){
  579. $customer_info = CustomerInfo::where('del_time',0)
  580. ->where("type",CustomerInfo::type_one)
  581. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  582. ->select('customer_id')
  583. ->get()->toArray();
  584. $model->whereIn('id',array_column($customer_info,'customer_id'));
  585. }
  586. $list = $this->limit($model,'',$data);
  587. $list = $this->fillData($list,$data);
  588. return [true, $list];
  589. }
  590. public function getCustomerId($data,$user){
  591. $return = [];
  592. if(! empty($data['my_fz'])){
  593. $info = CustomerInfo::where('del_time',0)
  594. ->where('data_id',$user['id'])
  595. ->where('type',CustomerInfo::type_two)
  596. ->select('customer_id')
  597. ->get()->toArray();
  598. $return = array_unique(array_column($info,'customer_id'));
  599. }
  600. if(! empty($data['my_xt'])){
  601. $info = CustomerInfo::where('del_time',0)
  602. ->where('data_id',$user['id'])
  603. ->where('type',CustomerInfo::type_three)
  604. ->select('customer_id')
  605. ->get()->toArray();
  606. $return = array_unique(array_column($info,'customer_id'));
  607. }
  608. return $return;
  609. }
  610. /**
  611. * 客户参数规则
  612. * @param $data
  613. * @param $is_add
  614. * @return array
  615. */
  616. public function customerRule(&$data, $user, $is_add = true){
  617. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  618. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  619. if(empty($data['title'])) return [false,'客户名称不能为空'];
  620. //所属部门 以及 顶级部门
  621. if(empty($data['depart_id'])) {
  622. $data['depart_id'] = $this->getDepart($user);
  623. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  624. }
  625. if($data['model_type'] == Customer::Model_type_one){
  626. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  627. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  628. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  629. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  630. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  631. }else{
  632. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  633. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  634. }
  635. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  636. $bool = BasicType::where('title',$data['car_type_title'])
  637. ->where('top_depart_id',$data['top_depart_id'])
  638. ->where('type',10)
  639. ->where('del_time',0)
  640. ->exists();
  641. if($bool) return [false,'车型名称已存在'];
  642. }
  643. if($is_add){
  644. $bool = Customer::where('del_time',0)
  645. ->where('top_depart_id',$data['top_depart_id'])
  646. ->where('title',$data['title'])
  647. // ->where('model_type',$data['model_type'])
  648. ->exists();
  649. if(! empty($data['customer_contact'])) {
  650. $search = [];
  651. foreach ($data['customer_contact'] as $value){
  652. $search[] = $value['info'];
  653. }
  654. $boolean = CustomerInfo::from('customer_info as a')
  655. ->join('customer as b','b.id','a.customer_id')
  656. ->where('a.del_time',0)
  657. ->where('b.del_time',0)
  658. ->where('b.top_depart_id',$data['top_depart_id'])
  659. ->whereIn('a.contact_info', $search)
  660. ->exists();
  661. if($boolean) return [false,'客户联系内容已存在'];
  662. }
  663. }else{
  664. if(empty($data['id'])) return [false,'ID不能为空'];
  665. $bool = Customer::where('del_time',0)
  666. ->where('id','<>',$data['id'])
  667. ->where('top_depart_id',$data['top_depart_id'])
  668. ->where('title',$data['title'])
  669. // ->where('model_type',$data['model_type'])
  670. ->exists();
  671. if(! empty($data['customer_contact'])) {
  672. $search = [];
  673. foreach ($data['customer_contact'] as $value){
  674. $search[] = $value['info'];
  675. }
  676. $boolean = CustomerInfo::from('customer_info as a')
  677. ->join('customer as b','b.id','a.customer_id')
  678. ->where('b.id','<>',$data['id'])
  679. ->where('a.del_time',0)
  680. ->where('b.del_time',0)
  681. ->where('b.top_depart_id',$data['top_depart_id'])
  682. ->whereIn('a.contact_info', $search)
  683. ->exists();
  684. if($boolean) return [false,'客户联系内容已存在'];
  685. }
  686. }
  687. if($bool) return [false,'客户名称不能重复'];
  688. return [true, ''];
  689. }
  690. /**
  691. * 拼接数据
  692. * @param $data
  693. * @return array
  694. */
  695. public function fillData($data,$ergs){
  696. if(empty($data['data'])) return $data;
  697. $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')));
  698. $basic_map = BasicType::whereIn('id',$array)
  699. ->pluck('title','id')
  700. ->toArray();
  701. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  702. ->pluck('title','id')
  703. ->toArray();
  704. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  705. ->pluck('emp_name','id')
  706. ->toArray();
  707. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  708. ->pluck('title','id')
  709. ->toArray();
  710. //跟进记录
  711. $record = FollowUpRecord::where('del_time',0)
  712. ->where('type',FollowUpRecord::type_one)
  713. ->whereIn('data_id',array_column($data['data'],'id'))
  714. ->select('data_id',DB::raw('max(visit_time) as visit_time'))
  715. ->groupBy('data_id')
  716. ->pluck('visit_time','data_id')->toArray();
  717. $record_array = [];
  718. if(! empty($record)){
  719. $now = time();
  720. foreach ($record as $key => $value){
  721. $record_array[$key] = $this->showTimeAgo($value, $now);
  722. }
  723. }
  724. $customer_info = CustomerInfo::where('del_time',0)
  725. ->whereIn('customer_id',array_column($data['data'],'id'))
  726. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  727. ->select('type','contact_type','contact_info','customer_id','data_id')
  728. ->get()->toArray();
  729. //客户的联系方式 客户的负责人
  730. $customer_info_map = $fz = $customer_info_map2 = [];
  731. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  732. ->pluck('emp_name','id')
  733. ->toArray();
  734. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  735. foreach ($customer_info as $value){
  736. if($value['type'] == CustomerInfo::type_one){
  737. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  738. $customer_info_map[$value['customer_id']][] = $value;
  739. if(! isset($customer_info_map2[$value['customer_id']])){
  740. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  741. }else{
  742. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  743. }
  744. }else{
  745. $tmp = $emp_map[$value['data_id']] ?? "";
  746. if(isset($fz[$value['customer_id']])){
  747. $fz[$value['customer_id']] .= ',' . $tmp;
  748. }else{
  749. $fz[$value['customer_id']] = $tmp;
  750. }
  751. }
  752. }
  753. $array2 = array_unique(array_merge_recursive(array_column($data['data'],'top_depart_id'),array_column($data['data'],'fp_top_depart_id')));
  754. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  755. $address_map = config('address');
  756. foreach ($data['data'] as $key => $value){
  757. $address_str = [];
  758. if(! empty($value['address1'])) {
  759. $tmp = json_decode($value['address1'],true);
  760. $this->findLabelsByValue($address_map,$tmp,$address_str);
  761. $tmp = implode(' ',$address_str);
  762. $tmp .= ' ' . $value['address2'];
  763. $address = $tmp;
  764. }else{
  765. $address = $value['address2'];
  766. }
  767. $data['data'][$key]['address'] = $address;
  768. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  769. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  770. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  771. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  772. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  773. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  774. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  775. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  776. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  777. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  778. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  779. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  780. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  781. $data['data'][$key]['customer_detail'] = $customer_tmp;
  782. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  783. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  784. $record_tmp = $record_array[$value['id']] ?? "";
  785. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  786. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  787. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  788. $data['data'][$key]['top_depart_title'] = $depart[$value['top_depart_id']] ?? "";
  789. $data['data'][$key]['fp_top_depart_title'] = $depart[$value['fp_top_depart_id']] ?? "";
  790. }
  791. return $data;
  792. }
  793. //抢客户
  794. public function customerGrabbing($data, $user){
  795. $key = Customer::$limitKey . $data['customer_id'];
  796. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  797. if(! $status) {
  798. //释放锁
  799. $this->dellimitingSendRequestBackg($key);
  800. return [false,$msg];
  801. }
  802. try {
  803. DB::beginTransaction();
  804. $time = time();
  805. $insert = [];
  806. //负责人获取 改为前负责人
  807. $man = CustomerInfo::where('del_time',0)
  808. ->where('customer_id',$data['customer_id'])
  809. ->where('type', CustomerInfo::type_two)
  810. ->get()->toArray();
  811. foreach ($man as $value){
  812. $insert[] = [
  813. 'customer_id' => $data['customer_id'],
  814. 'data_id' => $value['data_id'],
  815. 'type' => CustomerInfo::type_nine,
  816. 'crt_time' => $time
  817. ];
  818. }
  819. //增加自己为负责人
  820. $insert[] = [
  821. 'customer_id' => $data['customer_id'],
  822. 'data_id' => $user['id'],
  823. 'type' => CustomerInfo::type_two,
  824. 'crt_time' => $time
  825. ];
  826. //人员清空
  827. CustomerInfo::where('del_time',0)
  828. ->where('customer_id',$data['customer_id'])
  829. ->whereIn('type', CustomerInfo::$man2)
  830. ->update(['del_time' => $time]);
  831. //写入最新人员
  832. CustomerInfo::insert($insert);
  833. //更新公海池状态
  834. Customer::where('id',$data['customer_id'])->update([
  835. 'pond_state' => 0
  836. ]);
  837. DB::commit();
  838. }catch (\Exception $exception){
  839. //释放锁
  840. $this->dellimitingSendRequestBackg($key);
  841. DB::rollBack();
  842. return [false,$exception->getMessage()];
  843. }
  844. //释放锁
  845. $this->dellimitingSendRequestBackg($key);
  846. return [true, ''];
  847. }
  848. public function customerGrabbingRule($data, $key){
  849. if(empty($data['customer_id'])) return [false,'请选择客户'];
  850. //上锁
  851. list($status,$msg) = $this->limitingSendRequestBackg($key);
  852. if(! $status) return [false,$msg];
  853. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  854. if(empty($customer)) return [false,'客户不存在或已被删除'];
  855. $customer = $customer->toArray();
  856. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  857. return [true,''];
  858. }
  859. public function searchBy($data,$user){
  860. $model = BasicType::TopClear($user,$data);
  861. $result = $model->where('del_time',0)
  862. ->where('type', 2)
  863. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  864. ->select('id')
  865. ->get()->toArray();
  866. $model2 = Customer::Clear($user,$data);
  867. $customer = $model2->where('del_time',0)
  868. ->whereIn('customer_from', array_column($result,'id'))
  869. ->select('id')
  870. ->get()->toArray();
  871. return array_column($customer,'id');
  872. }
  873. }