CustomerService.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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,$user){
  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. //是否是总社的客户
  336. $is_main = 0;
  337. if($customer['top_depart_id'] == $user['head']['id']) $is_main = 1;
  338. $customer['is_belong_to_main'] = $is_main;
  339. $customer['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $customer['title'];
  340. $product = Product::where('id',$customer['intention_product'])->value('title');
  341. $customer['intention_product_title'] = $product;
  342. $customer['product_category'] = [];
  343. if(! empty($customer['intention_product'])){
  344. $pro = (new ProductService())->getProductDetail([$customer['intention_product']]);
  345. $product_category = $pro[$customer['intention_product']]['product_category'] ?? '';
  346. $customer['product_category'] = $product_category ? json_decode($product_category,true) : [];
  347. }
  348. $address_map = config('address');
  349. $address_str = [];
  350. if(! empty($customer['address1'])) {
  351. $tmp = json_decode($customer['address1'],true);
  352. $this->findLabelsByValue($address_map,$tmp,$address_str);
  353. $customer['address1'] = $tmp;
  354. $tmp = implode(' ',$address_str);
  355. $tmp .= ' ' . $customer['address2'];
  356. $address = $tmp;
  357. }else{
  358. $address = $customer['address2'];
  359. }
  360. $customer['address'] = $address;
  361. $customer['customer_contact'] = $customer['employee_one'] = $customer['employee_two'] = $customer['employee_three'] = $customer['img'] = $customer['file'] = $customer['old_employee_one'] = [];
  362. $array = [
  363. $customer['customer_intention'],
  364. $customer['customer_from'],
  365. $customer['customer_type'],
  366. $customer['car_type'],
  367. $customer['progress_stage'],
  368. $customer['state_type'],
  369. $customer['customer_state'],
  370. ];
  371. $basic_map = BasicType::whereIn('id',$array)
  372. ->pluck('title','id')
  373. ->toArray();
  374. $depart_title = Depart::where('id',$customer['depart_id'])->select('title')->value('title');
  375. $customer = [$customer];
  376. foreach ($customer as $key => $value){
  377. $customer[$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  378. $customer[$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  379. $customer[$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  380. $customer[$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  381. $customer[$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  382. $customer[$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  383. $customer[$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  384. $customer[$key]['depart_title'] = $depart_title ?? '';
  385. }
  386. $customer = $customer[0];
  387. $customer_info = CustomerInfo::where('del_time',0)
  388. ->where('customer_id',$customer['id'])
  389. ->select('id','customer_id','contact_type','contact_info','data_id','file','type','name')
  390. ->get()->toArray();
  391. $emp_id = [];
  392. $emp_id[] = $customer['crt_id'];
  393. foreach ($customer_info as $value){
  394. if(in_array($value['type'], CustomerInfo::$man)){
  395. $emp_id[] = $value['data_id'];
  396. }
  397. }
  398. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  399. ->pluck('emp_name','id')
  400. ->toArray();
  401. $basic_map2 = BasicType::whereIn('id',array_unique(array_column($customer_info,'contact_type')))
  402. ->pluck('title','id')
  403. ->toArray();
  404. foreach ($customer_info as $value){
  405. if($value['type'] == CustomerInfo::type_one){
  406. $tmp = [
  407. 'id' => $value['contact_type'],
  408. 'title' => $basic_map2[$value['contact_type']] ?? '',
  409. 'info' => $value['contact_info']
  410. ];
  411. $customer['customer_contact'][] = $tmp;
  412. }elseif ($value['type'] == CustomerInfo::type_two){
  413. $tmp = [
  414. 'id' => $value['data_id'],
  415. 'name' => $emp_map[$value['data_id']] ?? '',
  416. ];
  417. $customer['employee_one'][] = $tmp;
  418. }elseif ($value['type'] == CustomerInfo::type_three){
  419. $tmp = [
  420. 'id' => $value['data_id'],
  421. 'name' => $emp_map[$value['data_id']] ?? '',
  422. ];
  423. $customer['employee_two'][] = $tmp;
  424. }elseif ($value['type'] == CustomerInfo::type_four){
  425. $tmp = [
  426. 'id' => $value['data_id'],
  427. 'name' => $emp_map[$value['data_id']] ?? '',
  428. ];
  429. $customer['employee_three'][] = $tmp;
  430. }elseif ($value['type'] == CustomerInfo::type_five){
  431. $tmp = [
  432. 'url' => $value['file'],
  433. 'name' => $value['name'],
  434. ];
  435. $customer['img'][] = $tmp;
  436. }elseif ($value['type'] == CustomerInfo::type_six){
  437. $tmp = [
  438. 'url' => $value['file'],
  439. 'name' => $value['name'],
  440. ];
  441. $customer['file'][] = $tmp;
  442. }elseif ($value['type'] == CustomerInfo::type_nine){
  443. $tmp = [
  444. 'id' => $value['data_id'],
  445. 'name' => $emp_map[$value['data_id']] ?? '',
  446. ];
  447. $customer['old_employee_one'][] = $tmp;
  448. }
  449. }
  450. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  451. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  452. //可见范围
  453. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_one);
  454. $customer['depart'] = $return[0] ?? [];
  455. $customer['employee'] = $return[1] ?? [];
  456. return [true, $customer];
  457. }
  458. /**
  459. * 客户列表
  460. * @param $data
  461. * @param $user
  462. * @return array
  463. */
  464. public function customerList($data,$user){
  465. $model = Customer::Clear($user,$data);
  466. $model = $model->where('del_time',0)
  467. ->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')
  468. ->orderby('id', 'desc');
  469. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  470. $id = $this->getCustomerId($data,$user);
  471. $model->whereIn('id',$id);
  472. }
  473. if(! empty($data['pond_state'])) {
  474. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  475. if(empty($search_depart_id)){
  476. $top_depart_id = $user['depart_top'][0] ?? [];
  477. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  478. }else{
  479. //查询 顶级公司
  480. $top_depart_id = $search_depart_id;
  481. }
  482. // 进入公海池的客户 公司下所有人可见
  483. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  484. }
  485. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  486. if(! empty($data['time_type'])) {
  487. if($data['time_type'] == 1) {
  488. $start = strtotime('today');
  489. $end = strtotime('tomorrow') - 1;
  490. }elseif ($data['time_type'] == 2){
  491. $start = strtotime('this week',strtotime('today'));
  492. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  493. }
  494. if(! empty($start) && ! empty($end)) {
  495. $model->where('crt_time','>=',$start);
  496. $model->where('crt_time','<=',$end);
  497. }
  498. }
  499. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  500. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  501. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  502. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  503. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  504. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  505. if(! empty($data['customer_intention_title'])){
  506. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  507. $model->whereIn('customer_intention', $id);
  508. }
  509. if(! empty($data['customer_from_title'])){
  510. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  511. $model->whereIn('customer_from', $id);
  512. }
  513. if(! empty($data['customer_type_title'])) {
  514. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30]);
  515. $model->whereIn('customer_type', $id);
  516. }
  517. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  518. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  519. $model->where('crt_time','>=',$return[0]);
  520. $model->where('crt_time','<=',$return[1]);
  521. }
  522. if(! empty($data['crt_name'])){
  523. $id = (new RangeService())->crtNameSearch($data);
  524. $model->whereIn('crt_id',$id);
  525. }
  526. if(! empty($data['fz'])){
  527. $id = (new RangeService())->customerSearch($data);
  528. $model->whereIn('id',$id);
  529. }
  530. if(! empty($data['last_visit_time'])){
  531. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  532. $model->whereIn('id',$id);
  533. }
  534. if(! empty($data['contact_info'])){
  535. $customer_info = CustomerInfo::where('del_time',0)
  536. ->where("type",CustomerInfo::type_one)
  537. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  538. ->select('customer_id')
  539. ->get()->toArray();
  540. $model->whereIn('id',array_column($customer_info,'customer_id'));
  541. }
  542. $list = $this->limit($model,'',$data);
  543. $list = $this->fillData($list,$data);
  544. return [true, $list];
  545. }
  546. public function customerList2($data,$user){
  547. $model = Customer::Clear($user,$data);
  548. $model = $model->where('del_time',0)
  549. ->where('fp_time','>',0)
  550. ->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')
  551. ->orderby('fp_time', 'desc');
  552. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  553. $id = $this->getCustomerId($data,$user);
  554. $model->whereIn('id',$id);
  555. }
  556. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  557. if(! empty($data['time_type'])) {
  558. if($data['time_type'] == 1) {
  559. $start = strtotime('today');
  560. $end = strtotime('tomorrow') - 1;
  561. }elseif ($data['time_type'] == 2){
  562. $start = strtotime('this week',strtotime('today'));
  563. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  564. }
  565. if(! empty($start) && ! empty($end)) {
  566. $model->where('crt_time','>=',$start);
  567. $model->where('crt_time','<=',$end);
  568. }
  569. }
  570. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  571. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  572. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  573. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  574. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  575. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  576. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  577. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  578. $model->where('crt_time','>=',$return[0]);
  579. $model->where('crt_time','<=',$return[1]);
  580. }
  581. if(! empty($data['crt_name'])){
  582. $id = (new RangeService())->crtNameSearch($data);
  583. $model->whereIn('crt_id',$id);
  584. }
  585. if(! empty($data['fz'])){
  586. $id = (new RangeService())->customerSearch($data);
  587. $model->whereIn('id',$id);
  588. }
  589. if(! empty($data['last_visit_time'])){
  590. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  591. $model->whereIn('id',$id);
  592. }
  593. if(! empty($data['contact_info'])){
  594. $customer_info = CustomerInfo::where('del_time',0)
  595. ->where("type",CustomerInfo::type_one)
  596. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  597. ->select('customer_id')
  598. ->get()->toArray();
  599. $model->whereIn('id',array_column($customer_info,'customer_id'));
  600. }
  601. $list = $this->limit($model,'',$data);
  602. $list = $this->fillData($list,$data);
  603. return [true, $list];
  604. }
  605. public function getCustomerId($data,$user){
  606. $return = [];
  607. if(! empty($data['my_fz'])){
  608. $info = CustomerInfo::where('del_time',0)
  609. ->where('data_id',$user['id'])
  610. ->where('type',CustomerInfo::type_two)
  611. ->select('customer_id')
  612. ->get()->toArray();
  613. $return = array_unique(array_column($info,'customer_id'));
  614. }
  615. if(! empty($data['my_xt'])){
  616. $info = CustomerInfo::where('del_time',0)
  617. ->where('data_id',$user['id'])
  618. ->where('type',CustomerInfo::type_three)
  619. ->select('customer_id')
  620. ->get()->toArray();
  621. $return = array_unique(array_column($info,'customer_id'));
  622. }
  623. return $return;
  624. }
  625. /**
  626. * 客户参数规则
  627. * @param $data
  628. * @param $is_add
  629. * @return array
  630. */
  631. public function customerRule(&$data, $user, $is_add = true){
  632. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  633. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  634. if(empty($data['title'])) return [false,'客户名称不能为空'];
  635. //所属部门 以及 顶级部门
  636. if(empty($data['depart_id'])) {
  637. $data['depart_id'] = $this->getDepart($user);
  638. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  639. }
  640. if($data['model_type'] == Customer::Model_type_one){
  641. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  642. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  643. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  644. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  645. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  646. }else{
  647. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  648. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  649. }
  650. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  651. $bool = BasicType::where('title',$data['car_type_title'])
  652. ->where('top_depart_id',$data['top_depart_id'])
  653. ->where('type',10)
  654. ->where('del_time',0)
  655. ->exists();
  656. if($bool) return [false,'车型名称已存在'];
  657. }
  658. if($is_add){
  659. $bool = Customer::where('del_time',0)
  660. ->where('top_depart_id',$data['top_depart_id'])
  661. ->where('title',$data['title'])
  662. // ->where('model_type',$data['model_type'])
  663. ->exists();
  664. if(! empty($data['customer_contact'])) {
  665. $search = [];
  666. foreach ($data['customer_contact'] as $value){
  667. $search[] = $value['info'];
  668. }
  669. $boolean = CustomerInfo::from('customer_info as a')
  670. ->join('customer as b','b.id','a.customer_id')
  671. ->where('a.del_time',0)
  672. ->where('b.del_time',0)
  673. ->where('b.top_depart_id',$data['top_depart_id'])
  674. ->whereIn('a.contact_info', $search)
  675. ->exists();
  676. if($boolean) return [false,'客户联系内容已存在'];
  677. }
  678. }else{
  679. if(empty($data['id'])) return [false,'ID不能为空'];
  680. $bool = Customer::where('del_time',0)
  681. ->where('id','<>',$data['id'])
  682. ->where('top_depart_id',$data['top_depart_id'])
  683. ->where('title',$data['title'])
  684. // ->where('model_type',$data['model_type'])
  685. ->exists();
  686. if(! empty($data['customer_contact'])) {
  687. $search = [];
  688. foreach ($data['customer_contact'] as $value){
  689. $search[] = $value['info'];
  690. }
  691. $boolean = CustomerInfo::from('customer_info as a')
  692. ->join('customer as b','b.id','a.customer_id')
  693. ->where('b.id','<>',$data['id'])
  694. ->where('a.del_time',0)
  695. ->where('b.del_time',0)
  696. ->where('b.top_depart_id',$data['top_depart_id'])
  697. ->whereIn('a.contact_info', $search)
  698. ->exists();
  699. if($boolean) return [false,'客户联系内容已存在'];
  700. }
  701. }
  702. if($bool) return [false,'客户名称不能重复'];
  703. return [true, ''];
  704. }
  705. /**
  706. * 拼接数据
  707. * @param $data
  708. * @return array
  709. */
  710. public function fillData($data,$ergs){
  711. if(empty($data['data'])) return $data;
  712. $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')));
  713. $basic_map = BasicType::whereIn('id',$array)
  714. ->pluck('title','id')
  715. ->toArray();
  716. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  717. ->pluck('title','id')
  718. ->toArray();
  719. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  720. ->pluck('emp_name','id')
  721. ->toArray();
  722. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  723. ->pluck('title','id')
  724. ->toArray();
  725. //跟进记录
  726. $record = FollowUpRecord::where('del_time',0)
  727. ->where('type',FollowUpRecord::type_one)
  728. ->whereIn('data_id',array_column($data['data'],'id'))
  729. ->select('data_id',DB::raw('max(visit_time) as visit_time'))
  730. ->groupBy('data_id')
  731. ->pluck('visit_time','data_id')->toArray();
  732. $record_array = [];
  733. if(! empty($record)){
  734. $now = time();
  735. foreach ($record as $key => $value){
  736. $record_array[$key] = $this->showTimeAgo($value, $now);
  737. }
  738. }
  739. $customer_info = CustomerInfo::where('del_time',0)
  740. ->whereIn('customer_id',array_column($data['data'],'id'))
  741. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  742. ->select('type','contact_type','contact_info','customer_id','data_id')
  743. ->get()->toArray();
  744. //客户的联系方式 客户的负责人
  745. $customer_info_map = $fz = $customer_info_map2 = [];
  746. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  747. ->pluck('emp_name','id')
  748. ->toArray();
  749. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  750. foreach ($customer_info as $value){
  751. if($value['type'] == CustomerInfo::type_one){
  752. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  753. $customer_info_map[$value['customer_id']][] = $value;
  754. if(! isset($customer_info_map2[$value['customer_id']])){
  755. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  756. }else{
  757. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  758. }
  759. }else{
  760. $tmp = $emp_map[$value['data_id']] ?? "";
  761. if(isset($fz[$value['customer_id']])){
  762. $fz[$value['customer_id']] .= ',' . $tmp;
  763. }else{
  764. $fz[$value['customer_id']] = $tmp;
  765. }
  766. }
  767. }
  768. $array2 = array_unique(array_merge_recursive(array_column($data['data'],'top_depart_id'),array_column($data['data'],'fp_top_depart_id')));
  769. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  770. $address_map = config('address');
  771. foreach ($data['data'] as $key => $value){
  772. $address_str = [];
  773. if(! empty($value['address1'])) {
  774. $tmp = json_decode($value['address1'],true);
  775. $this->findLabelsByValue($address_map,$tmp,$address_str);
  776. $tmp = implode(' ',$address_str);
  777. $tmp .= ' ' . $value['address2'];
  778. $address = $tmp;
  779. }else{
  780. $address = $value['address2'];
  781. }
  782. $data['data'][$key]['address'] = $address;
  783. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  784. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  785. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  786. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  787. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  788. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  789. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  790. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  791. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  792. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  793. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  794. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  795. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  796. $data['data'][$key]['customer_detail'] = $customer_tmp;
  797. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  798. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  799. $record_tmp = $record_array[$value['id']] ?? "";
  800. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  801. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  802. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  803. $data['data'][$key]['top_depart_title'] = $depart[$value['top_depart_id']] ?? "";
  804. $data['data'][$key]['fp_top_depart_title'] = $depart[$value['fp_top_depart_id']] ?? "";
  805. }
  806. return $data;
  807. }
  808. //抢客户
  809. public function customerGrabbing($data, $user){
  810. $key = Customer::$limitKey . $data['customer_id'];
  811. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  812. if(! $status) {
  813. //释放锁
  814. $this->dellimitingSendRequestBackg($key);
  815. return [false,$msg];
  816. }
  817. try {
  818. DB::beginTransaction();
  819. $time = time();
  820. $insert = [];
  821. //负责人获取 改为前负责人
  822. $man = CustomerInfo::where('del_time',0)
  823. ->where('customer_id',$data['customer_id'])
  824. ->where('type', CustomerInfo::type_two)
  825. ->get()->toArray();
  826. foreach ($man as $value){
  827. $insert[] = [
  828. 'customer_id' => $data['customer_id'],
  829. 'data_id' => $value['data_id'],
  830. 'type' => CustomerInfo::type_nine,
  831. 'crt_time' => $time
  832. ];
  833. }
  834. //增加自己为负责人
  835. $insert[] = [
  836. 'customer_id' => $data['customer_id'],
  837. 'data_id' => $user['id'],
  838. 'type' => CustomerInfo::type_two,
  839. 'crt_time' => $time
  840. ];
  841. //人员清空
  842. CustomerInfo::where('del_time',0)
  843. ->where('customer_id',$data['customer_id'])
  844. ->whereIn('type', CustomerInfo::$man2)
  845. ->update(['del_time' => $time]);
  846. //写入最新人员
  847. CustomerInfo::insert($insert);
  848. //更新公海池状态
  849. Customer::where('id',$data['customer_id'])->update([
  850. 'pond_state' => 0
  851. ]);
  852. DB::commit();
  853. }catch (\Exception $exception){
  854. //释放锁
  855. $this->dellimitingSendRequestBackg($key);
  856. DB::rollBack();
  857. return [false,$exception->getMessage()];
  858. }
  859. //释放锁
  860. $this->dellimitingSendRequestBackg($key);
  861. return [true, ''];
  862. }
  863. public function customerGrabbingRule($data, $key){
  864. if(empty($data['customer_id'])) return [false,'请选择客户'];
  865. //上锁
  866. list($status,$msg) = $this->limitingSendRequestBackg($key);
  867. if(! $status) return [false,$msg];
  868. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  869. if(empty($customer)) return [false,'客户不存在或已被删除'];
  870. $customer = $customer->toArray();
  871. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  872. return [true,''];
  873. }
  874. public function searchBy($data,$user){
  875. $model = BasicType::TopClear($user,$data);
  876. $result = $model->where('del_time',0)
  877. ->where('type', 2)
  878. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  879. ->select('id')
  880. ->get()->toArray();
  881. $model2 = Customer::Clear($user,$data);
  882. $customer = $model2->where('del_time',0)
  883. ->whereIn('customer_from', array_column($result,'id'))
  884. ->select('id')
  885. ->get()->toArray();
  886. return array_column($customer,'id');
  887. }
  888. }