CustomerService.php 54 KB

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