CustomerService.php 40 KB

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