CustomerService.php 42 KB

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