CustomerService.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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, $field = []){
  479. if(empty($field)){
  480. $field = ['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'];
  481. }
  482. $model = Customer::Clear($user,$data);
  483. $model = $model->where('del_time',0)
  484. ->select($field)
  485. ->orderby('id', 'desc');
  486. if(! empty($data['title_t'])) {
  487. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  488. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  489. $id = (new RangeService())->crtContactSearch($data);
  490. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  491. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  492. ->orWhere('consulting_product', 'LIKE', '%'.$data['title_t'].'%')
  493. ->orWhereIn('id', $id);
  494. }
  495. if(! empty($data['id'])) $model->where('id', $data['id']);
  496. if(! empty($data['customer_id'])){
  497. $customer_id = explode(',',$data['customer_id']);
  498. $model->whereIn('id', $customer_id);
  499. }
  500. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  501. $id = $this->getCustomerId($data,$user);
  502. $model->whereIn('id',$id);
  503. }
  504. if(! empty($data['pond_state'])) {
  505. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  506. if(empty($search_depart_id)){
  507. $top_depart_id = $user['depart_top'][0] ?? [];
  508. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  509. }else{
  510. //查询 顶级公司
  511. $top_depart_id = $search_depart_id;
  512. }
  513. // 进入公海池的客户 公司下所有人可见
  514. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  515. }
  516. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  517. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  518. if(! empty($data['time_type'])) {
  519. if($data['time_type'] == 1) {
  520. $start = strtotime('today');
  521. $end = strtotime('tomorrow') - 1;
  522. }elseif ($data['time_type'] == 2){
  523. $start = strtotime('this week',strtotime('today'));
  524. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  525. }
  526. if(! empty($start) && ! empty($end)) {
  527. $model->where('crt_time','>=',$start);
  528. $model->where('crt_time','<=',$end);
  529. }
  530. }
  531. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  532. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  533. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  534. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  535. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  536. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  537. if(! empty($data['customer_intention_title'])){
  538. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  539. $model->whereIn('customer_intention', $id);
  540. }
  541. if(! empty($data['customer_from_title'])){
  542. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  543. $model->whereIn('customer_from', $id);
  544. }
  545. if(! empty($data['customer_type_title'])) {
  546. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  547. $model->whereIn('customer_type', $id);
  548. }
  549. if(! empty($data['progress_stage_title'])){
  550. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  551. $model->whereIn('progress_stage', $id);
  552. }
  553. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  554. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  555. $model->where('crt_time','>=',$return[0]);
  556. $model->where('crt_time','<=',$return[1]);
  557. }
  558. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  559. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  560. $model->where('enter_time','>=',$return[0]);
  561. $model->where('enter_time','<=',$return[1]);
  562. }
  563. if(! empty($data['crt_name'])){
  564. $id = (new RangeService())->crtNameSearch($data);
  565. $model->whereIn('crt_id',$id);
  566. }
  567. if(! empty($data['fz'])){
  568. $id = (new RangeService())->customerSearch($data);
  569. $model->whereIn('id',$id);
  570. }
  571. if(! empty($data['last_visit_time'])){
  572. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  573. $model->whereIn('id',$id);
  574. }
  575. if(! empty($data['contact_info'])){
  576. $customer_info = CustomerInfo::where('del_time',0)
  577. ->where("type",CustomerInfo::type_one)
  578. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  579. ->select('customer_id')
  580. ->get()->toArray();
  581. $model->whereIn('id',array_column($customer_info,'customer_id'));
  582. }
  583. if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
  584. $is_fp = -1;
  585. if(isset($data['is_fp'])) {
  586. if($data['is_fp']){
  587. $model->where('fp_time', '>', 0);
  588. $is_fp = 1;
  589. } else{
  590. $model->where('fp_time', 0);
  591. $is_fp = 0;
  592. }
  593. }
  594. if(! empty($data['belong_top_depart_title'])){
  595. list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  596. if($is_fp < 0){
  597. $model->whereIn('id', $id)
  598. ->orWhereIn('top_depart_id', $depart_id);
  599. }elseif ($is_fp > 0){
  600. $model->whereIn('id', $id);
  601. }else{
  602. $model->where('top_depart_id', $depart_id);
  603. }
  604. }
  605. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  606. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  607. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  608. }
  609. return $model;
  610. }
  611. /**
  612. * 客户列表
  613. * @param $data
  614. * @param $user
  615. * @return array
  616. */
  617. public function customerList($data,$user){
  618. $model = $this->customerCommonSearch($data,$user);
  619. $list = $this->limit($model,'',$data);
  620. $list = $this->fillData($list,$data);
  621. //微信数据
  622. $count = $this->countData("crt_time", $data, $user);
  623. $list['today'] = $count[0] ?? 0;
  624. $list['yesterday'] = $count[1] ?? 0;
  625. return [true, $list];
  626. }
  627. public function customer2CommonSearch($data,$user){
  628. $model = Customer::Clear($user,$data);
  629. $model = $model->where('del_time',0)
  630. ->where('fp_time','>',0)
  631. ->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')
  632. ->orderby('fp_time', 'desc');
  633. if(! empty($data['customer_id'])){
  634. $customer_id = explode(',',$data['customer_id']);
  635. $model->whereIn('id', $customer_id);
  636. }
  637. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  638. $id = $this->getCustomerId($data,$user);
  639. $model->whereIn('id',$id);
  640. }
  641. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  642. if(! empty($data['time_type'])) {
  643. if($data['time_type'] == 1) {
  644. $start = strtotime('today');
  645. $end = strtotime('tomorrow') - 1;
  646. }elseif ($data['time_type'] == 2){
  647. $start = strtotime('this week',strtotime('today'));
  648. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  649. }
  650. if(! empty($start) && ! empty($end)) {
  651. $model->where('crt_time','>=',$start);
  652. $model->where('crt_time','<=',$end);
  653. }
  654. }
  655. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  656. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  657. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  658. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  659. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  660. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  661. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  662. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  663. $model->where('crt_time','>=',$return[0]);
  664. $model->where('crt_time','<=',$return[1]);
  665. }
  666. if(! empty($data['crt_name'])){
  667. $id = (new RangeService())->crtNameSearch($data);
  668. $model->whereIn('crt_id',$id);
  669. }
  670. if(! empty($data['fz'])){
  671. $id = (new RangeService())->customerSearch($data);
  672. $model->whereIn('id',$id);
  673. }
  674. if(! empty($data['last_visit_time'])){
  675. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  676. $model->whereIn('id',$id);
  677. }
  678. if(! empty($data['contact_info'])){
  679. $customer_info = CustomerInfo::where('del_time',0)
  680. ->where("type",CustomerInfo::type_one)
  681. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  682. ->select('customer_id')
  683. ->get()->toArray();
  684. $model->whereIn('id',array_column($customer_info,'customer_id'));
  685. }
  686. if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){
  687. $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);
  688. $model->where('fp_time','>=',$return[0]);
  689. $model->where('fp_time','<=',$return[1]);
  690. }
  691. if(! empty($data['belong_top_depart_title'])){
  692. list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  693. $model->whereIn('id', $id);
  694. }
  695. if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
  696. $model->where('fp_time','>=',$data['wx_fp_time'][0]);
  697. $model->where('fp_time','<=',$data['wx_fp_time'][1]);
  698. }
  699. return $model;
  700. }
  701. public function customerList2($data,$user){
  702. $model = $this->customer2CommonSearch($data, $user);
  703. $list = $this->limit($model,'',$data);
  704. $list = $this->fillData($list,$data);
  705. //微信数据
  706. $count = $this->countData2("fp_time", $data, $user);
  707. $list['today'] = $count[0] ?? 0;
  708. $list['yesterday'] = $count[1] ?? 0;
  709. return [true, $list];
  710. }
  711. public function getCustomerId($data,$user){
  712. $return = [];
  713. if(! empty($data['my_fz'])){
  714. $info = CustomerInfo::where('del_time',0)
  715. ->where('data_id',$user['id'])
  716. ->where('type',CustomerInfo::type_two)
  717. ->select('customer_id')
  718. ->get()->toArray();
  719. $return = array_unique(array_column($info,'customer_id'));
  720. }
  721. if(! empty($data['my_xt'])){
  722. $info = CustomerInfo::where('del_time',0)
  723. ->where('data_id',$user['id'])
  724. ->where('type',CustomerInfo::type_three)
  725. ->select('customer_id')
  726. ->get()->toArray();
  727. $return = array_unique(array_column($info,'customer_id'));
  728. }
  729. return $return;
  730. }
  731. /**
  732. * 客户参数规则
  733. * @param $data
  734. * @param $is_add
  735. * @return array
  736. */
  737. public function customerRule(&$data, $user, $is_add = true){
  738. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  739. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  740. if(empty($data['title'])) return [false,'客户名称不能为空'];
  741. if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
  742. $data['enter_time'] = $this->changeDateToDateMin($data['enter_time']);
  743. //所属部门 以及 顶级部门
  744. if(empty($data['depart_id'])) {
  745. $data['depart_id'] = $this->getDepart($user);
  746. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  747. }
  748. if($data['model_type'] == Customer::Model_type_one){
  749. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  750. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  751. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  752. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  753. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  754. }else{
  755. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  756. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  757. }
  758. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  759. $bool = BasicType::where('title',$data['car_type_title'])
  760. ->where('top_depart_id',$data['top_depart_id'])
  761. ->where('type',10)
  762. ->where('del_time',0)
  763. ->exists();
  764. if($bool) return [false,'车型名称已存在'];
  765. }
  766. if($is_add){
  767. $bool = Customer::where('del_time',0)
  768. ->where('top_depart_id',$data['top_depart_id'])
  769. ->where('title',$data['title'])
  770. // ->where('model_type',$data['model_type'])
  771. ->exists();
  772. if(! empty($data['customer_contact'])) {
  773. $search = [];
  774. foreach ($data['customer_contact'] as $value){
  775. $search[] = $value['info'];
  776. }
  777. $boolean = CustomerInfo::from('customer_info as a')
  778. ->join('customer as b','b.id','a.customer_id')
  779. ->where('a.del_time',0)
  780. ->where('b.del_time',0)
  781. ->where('b.top_depart_id',$data['top_depart_id'])
  782. ->whereIn('a.contact_info', $search)
  783. ->exists();
  784. if($boolean) return [false,'客户联系内容已存在'];
  785. }
  786. }else{
  787. if(empty($data['id'])) return [false,'ID不能为空'];
  788. $bool = Customer::where('del_time',0)
  789. ->where('id','<>',$data['id'])
  790. ->where('top_depart_id',$data['top_depart_id'])
  791. ->where('title',$data['title'])
  792. // ->where('model_type',$data['model_type'])
  793. ->exists();
  794. if(! empty($data['customer_contact'])) {
  795. $search = [];
  796. foreach ($data['customer_contact'] as $value){
  797. $search[] = $value['info'];
  798. }
  799. $boolean = CustomerInfo::from('customer_info as a')
  800. ->join('customer as b','b.id','a.customer_id')
  801. ->where('b.id','<>',$data['id'])
  802. ->where('a.del_time',0)
  803. ->where('b.del_time',0)
  804. ->where('b.top_depart_id',$data['top_depart_id'])
  805. ->whereIn('a.contact_info', $search)
  806. ->exists();
  807. if($boolean) return [false,'客户联系内容已存在'];
  808. }
  809. }
  810. if($bool) return [false,'客户名称不能重复'];
  811. return [true, ''];
  812. }
  813. /**
  814. * 拼接数据
  815. * @param $data
  816. * @return array
  817. */
  818. public function fillData($data,$ergs){
  819. if(empty($data['data'])) return $data;
  820. $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')));
  821. $basic_map = BasicType::whereIn('id',$array)
  822. ->pluck('title','id')
  823. ->toArray();
  824. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  825. ->pluck('title','id')
  826. ->toArray();
  827. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  828. ->pluck('emp_name','id')
  829. ->toArray();
  830. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  831. ->pluck('title','id')
  832. ->toArray();
  833. $customer_id = array_column($data['data'],'id');
  834. //跟进记录
  835. $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);
  836. $customer_info = CustomerInfo::where('del_time',0)
  837. ->whereIn('customer_id',$customer_id)
  838. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])
  839. ->select('type','contact_type','contact_info','customer_id','data_id')
  840. ->get()->toArray();
  841. //客户的联系方式 客户的负责人 协同人
  842. $customer_info_map = $fz = $customer_info_map2 = $xt = [];
  843. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  844. ->pluck('emp_name','id')
  845. ->toArray();
  846. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  847. foreach ($customer_info as $value){
  848. if($value['type'] == CustomerInfo::type_one){
  849. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  850. $customer_info_map[$value['customer_id']][] = $value;
  851. if(! isset($customer_info_map2[$value['customer_id']])){
  852. $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  853. }else{
  854. $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  855. }
  856. }elseif($value['type'] == CustomerInfo::type_two){
  857. $tmp = $emp_map[$value['data_id']] ?? "";
  858. if(isset($fz[$value['customer_id']])){
  859. $fz[$value['customer_id']] .= ',' . $tmp;
  860. }else{
  861. $fz[$value['customer_id']] = $tmp;
  862. }
  863. }else{
  864. $tmp = $emp_map[$value['data_id']] ?? "";
  865. if(isset($xt[$value['customer_id']])){
  866. $xt[$value['customer_id']] .= ',' . $tmp;
  867. }else{
  868. $xt[$value['customer_id']] = $tmp;
  869. }
  870. }
  871. }
  872. $array2 = array_unique(array_column($data['data'],'top_depart_id'));
  873. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  874. $depart2_map = $this->getCustomerDepart($customer_id);
  875. $address_map = config('address');
  876. foreach ($data['data'] as $key => $value){
  877. $address_str = [];
  878. if(! empty($value['address1'])) {
  879. $tmp = json_decode($value['address1'],true);
  880. $this->findLabelsByValue($address_map,$tmp,$address_str);
  881. $tmp = implode(' ',$address_str);
  882. $tmp .= ' ' . $value['address2'];
  883. $address = $tmp;
  884. }else{
  885. $address = $value['address2'];
  886. }
  887. $data['data'][$key]['address'] = $address;
  888. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  889. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  890. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  891. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  892. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  893. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  894. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  895. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  896. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  897. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  898. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  899. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  900. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  901. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  902. $data['data'][$key]['customer_detail'] = $customer_tmp;
  903. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  904. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  905. $record_tmp = $record_array[$value['id']] ?? "";
  906. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  907. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  908. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  909. $top_depart_title = $depart[$value['top_depart_id']] ?? "";
  910. $data['data'][$key]['top_depart_title'] = $top_depart_title;
  911. //分配门店
  912. $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
  913. if($value['fp_time'] > 0) {
  914. //分配过所属门店是分配门店
  915. $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;
  916. }else{
  917. //没分配过所属门店是创建门店
  918. $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;
  919. }
  920. $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
  921. $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
  922. }
  923. return $data;
  924. }
  925. public function countData($column = "", $data, $user){
  926. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  927. // 获取今天的开始和结束时间戳
  928. $todayStart = Carbon::today()->startOfDay()->timestamp;
  929. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  930. // 获取昨天的开始和结束时间戳
  931. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  932. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  933. $data['wx_' . $column] = [$todayStart, $todayEnd];
  934. $model = $this->customerCommonSearch($data, $user);
  935. // 查询今天的数据条数
  936. $todayCount = $model->count();
  937. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  938. $model = $this->customerCommonSearch($data, $user);
  939. // 查询昨天的数据条数
  940. $yesterdayCount = $model->count();
  941. return [$todayCount, $yesterdayCount];
  942. }
  943. public function countData2($column = "", $data, $user){
  944. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  945. // 获取今天的开始和结束时间戳
  946. $todayStart = Carbon::today()->startOfDay()->timestamp;
  947. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  948. // 获取昨天的开始和结束时间戳
  949. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  950. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  951. $data['wx_' . $column] = [$todayStart, $todayEnd];
  952. $model = $this->customer2CommonSearch($data, $user);
  953. // 查询今天的数据条数
  954. $todayCount = $model->count();
  955. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  956. $model = $this->customer2CommonSearch($data, $user);
  957. // 查询昨天的数据条数
  958. $yesterdayCount = $model->count();
  959. return [$todayCount, $yesterdayCount];
  960. }
  961. //获取客资门店
  962. public function getCustomerDepart($customer_id = []){
  963. if(empty($customer_id)) return [];
  964. $result = SeeRange::where('del_time',0)
  965. ->whereIn('data_id', $customer_id)
  966. ->where('data_type',SeeRange::type_one)
  967. ->where('type',SeeRange::data_three)
  968. ->select('data_id as customer_id','param_id as top_depart_id')
  969. ->get()->toArray();
  970. $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();
  971. $return = [];
  972. foreach ($result as $value){
  973. $tmp = $depart_map[$value['top_depart_id']] ?? "";
  974. if(! $tmp) continue;
  975. if(isset($return[$value['customer_id']])){
  976. $return[$value['customer_id']] .= ',' . $tmp;
  977. }else{
  978. $return[$value['customer_id']] = $tmp;
  979. }
  980. }
  981. return $return;
  982. }
  983. //客资门店搜索
  984. public function searchCustomerDepart($depart_title = ""){
  985. $customer_id = [];
  986. if(empty($depart_title)) return $customer_id;
  987. $depart_id = Depart::where('del_time', 0)
  988. ->where('parent_id', 0)
  989. ->where('title', 'LIKE', '%'. $depart_title .'%')
  990. ->select('id')->get()->toArray();
  991. $depart_id = array_column($depart_id, 'id');
  992. if(empty($depart_id)) return [$customer_id, $depart_id];
  993. $customer_id = SeeRange::where('del_time',0)
  994. ->whereIn('param_id', $depart_id)
  995. ->where('data_type',SeeRange::type_one)
  996. ->where('type',SeeRange::data_three)
  997. ->select('data_id as customer_id')
  998. ->get()->toArray();
  999. $customer_id = array_column($customer_id, 'customer_id');
  1000. return [$customer_id, $depart_id];
  1001. }
  1002. //抢客户
  1003. public function customerGrabbing($data, $user){
  1004. $key = Customer::$limitKey . $data['customer_id'];
  1005. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  1006. if(! $status) {
  1007. //释放锁
  1008. $this->dellimitingSendRequestBackg($key);
  1009. return [false,$msg];
  1010. }
  1011. try {
  1012. DB::beginTransaction();
  1013. $time = time();
  1014. $insert = [];
  1015. //负责人获取 改为前负责人
  1016. $man = CustomerInfo::where('del_time',0)
  1017. ->where('customer_id',$data['customer_id'])
  1018. ->where('type', CustomerInfo::type_two)
  1019. ->get()->toArray();
  1020. foreach ($man as $value){
  1021. $insert[] = [
  1022. 'customer_id' => $data['customer_id'],
  1023. 'data_id' => $value['data_id'],
  1024. 'type' => CustomerInfo::type_nine,
  1025. 'crt_time' => $time
  1026. ];
  1027. }
  1028. //增加自己为负责人
  1029. $insert[] = [
  1030. 'customer_id' => $data['customer_id'],
  1031. 'data_id' => $user['id'],
  1032. 'type' => CustomerInfo::type_two,
  1033. 'crt_time' => $time
  1034. ];
  1035. //人员清空
  1036. CustomerInfo::where('del_time',0)
  1037. ->where('customer_id',$data['customer_id'])
  1038. ->whereIn('type', CustomerInfo::$man2)
  1039. ->update(['del_time' => $time]);
  1040. //写入最新人员
  1041. CustomerInfo::insert($insert);
  1042. //更新公海池状态
  1043. Customer::where('id',$data['customer_id'])->update([
  1044. 'pond_state' => 0
  1045. ]);
  1046. DB::commit();
  1047. }catch (\Exception $exception){
  1048. //释放锁
  1049. $this->dellimitingSendRequestBackg($key);
  1050. DB::rollBack();
  1051. return [false,$exception->getMessage()];
  1052. }
  1053. //释放锁
  1054. $this->dellimitingSendRequestBackg($key);
  1055. return [true, ''];
  1056. }
  1057. public function customerGrabbingRule($data, $key){
  1058. if(empty($data['customer_id'])) return [false,'请选择客户'];
  1059. //上锁
  1060. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1061. if(! $status) return [false,$msg];
  1062. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  1063. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1064. $customer = $customer->toArray();
  1065. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  1066. return [true,''];
  1067. }
  1068. public function searchBy($data,$user){
  1069. $model = BasicType::TopClear($user,$data);
  1070. $result = $model->where('del_time',0)
  1071. ->where('type', 2)
  1072. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  1073. ->select('id')
  1074. ->get()->toArray();
  1075. $model2 = Customer::Clear($user,$data);
  1076. $customer = $model2->where('del_time',0)
  1077. ->whereIn('customer_from', array_column($result,'id'))
  1078. ->select('id')
  1079. ->get()->toArray();
  1080. return array_column($customer,'id');
  1081. }
  1082. public function customerImportanceEdit($data,$user){
  1083. if(empty($data['id'])) return [false, '请选择客户'];
  1084. $model = Customer::where('id',$data['id'])->first();
  1085. if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
  1086. if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
  1087. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
  1088. $params = $this->getDataFile($data);
  1089. (new OperationLogService())->setOperationList($params,$user,2);
  1090. try {
  1091. DB::beginTransaction();
  1092. $model = Customer::where('id',$data['id'])->first();
  1093. $model->importance = $data['importance'] ?? '';
  1094. $model->save();
  1095. DB::commit();
  1096. }catch (\Exception $exception){
  1097. DB::rollBack();
  1098. return [false,$exception->getMessage()];
  1099. }
  1100. return [true, ''];
  1101. }
  1102. //发送客户消息
  1103. public function customerSendWx($data, $user){
  1104. list($status,$msg) = $this->customerSendWxRule($data);
  1105. if(! $status) return [false,$msg];
  1106. //发送消息
  1107. $send_data = $msg;
  1108. (new OaService())->sendWxOaCheckMessage($send_data);
  1109. return [true, ''];
  1110. }
  1111. public function customerSendWxRule($data){
  1112. if(empty($data['id'])) return [false,'请选择客户'];
  1113. $customer = Customer::where('del_time',0)
  1114. ->whereIn('id',$data['id'])
  1115. ->get()->toArray();
  1116. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1117. $man = CustomerInfo::where('del_time',0)
  1118. ->whereIn('customer_id',$data['id'])
  1119. ->where('type', CustomerInfo::type_two)
  1120. ->get()->toArray();
  1121. if(empty($man)) return [true, '客户暂无负责人信息,公众号消息发送结束'];
  1122. $customer_map = [];
  1123. foreach ($man as $value){
  1124. $customer_map[$value['data_id']][] = $value['customer_id'];
  1125. }
  1126. $employee_id = array_unique(array_column($man,'data_id'));
  1127. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  1128. ->pluck('openid','employee_id')
  1129. ->toArray();
  1130. $emp = Employee::whereIn('id', $employee_id)
  1131. ->pluck('emp_name','id')
  1132. ->toArray();
  1133. $send_data = [];
  1134. foreach ($employee_id as $value){
  1135. $tmp = $emp[$value] ?? "";
  1136. $open_id = $wx_map[$value] ?? "";
  1137. // if(empty($open_id)) return [false, $tmp . "暂未关注微信公众号,公众号消息发送失败"];
  1138. if(empty($open_id)) continue;
  1139. $customer_id = $customer_map[$value] ?? [];
  1140. $customer_id = implode(',', $customer_id);
  1141. //提醒创建人
  1142. $send_data[] = [
  1143. 'employee_id' => $value,
  1144. 'type' => 2,
  1145. 'state' => 0,
  1146. 'menu_id' => "16|list",
  1147. 'openid' => $open_id,
  1148. 'order_number' => $customer_id,
  1149. 'tmp_data' => [
  1150. time(),
  1151. "客户消息提醒",
  1152. "已发送",
  1153. $tmp,
  1154. date('Y-m-d H:i:s'),
  1155. ],
  1156. ];
  1157. }
  1158. return [true, $send_data];
  1159. }
  1160. }