CustomerService.php 64 KB

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