CustomerService.php 55 KB

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