CustomerService.php 46 KB

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