CustomerService.php 66 KB

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