CustomerService.php 50 KB

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