CustomerService.php 42 KB

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