CustomerService.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  505. $id = (new RangeService())->crtContactSearch($data);
  506. $model->where(function ($q) use ($cleanTitle, $data, $id) {
  507. $q->whereRaw(
  508. "REPLACE(title, ' ', '') LIKE ?",
  509. ['%' . str_replace(' ', '', $cleanTitle) . '%']
  510. )
  511. ->orWhere('consulting_product', 'LIKE', '%' . $data['title_t'] . '%')
  512. ->orWhereIn('id', $id);
  513. });
  514. }
  515. if(! empty($data['id'])) $model->where('id', $data['id']);
  516. if(! empty($data['customer_id'])){
  517. $customer_id = explode(',',$data['customer_id']);
  518. $model->whereIn('id', $customer_id);
  519. }
  520. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  521. $id = $this->getCustomerId($data,$user);
  522. $model->whereIn('id',$id);
  523. }
  524. if(! empty($data['pond_state'])) {
  525. $search_depart_id = $data['top_depart_id'] ?? 0; //顶级公司
  526. if(empty($search_depart_id)){
  527. $top_depart_id = $user['depart_top'][0] ?? [];
  528. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  529. }else{
  530. //查询 顶级公司
  531. $top_depart_id = $search_depart_id;
  532. }
  533. // 进入公海池的客户 公司下所有人可见
  534. $model->where('pond_state', '>',0)->where('top_depart_id',$top_depart_id);
  535. }
  536. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  537. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  538. if(! empty($data['time_type'])) {
  539. if($data['time_type'] == 1) {
  540. $start = strtotime('today');
  541. $end = strtotime('tomorrow') - 1;
  542. }elseif ($data['time_type'] == 2){
  543. $start = strtotime('this week',strtotime('today'));
  544. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  545. }
  546. if(! empty($start) && ! empty($end)) {
  547. $model->where('crt_time','>=',$start);
  548. $model->where('crt_time','<=',$end);
  549. }
  550. }
  551. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  552. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  553. if(! empty($data['consulting_product_new'])) $model->where('consulting_product_new',$data['consulting_product_new']);
  554. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  555. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  556. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  557. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  558. if(! empty($data['customer_intention_title'])){
  559. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  560. $model->whereIn('customer_intention', $id);
  561. }
  562. if(! empty($data['consulting_product_new_title'])) {
  563. $id = (new RangeService())->customerBasicTypeAllUseSearch($data['customer_from_title'],[BasicTypeAllUse::type_one]);
  564. $model->whereIn('consulting_product_new', $id);
  565. }
  566. if(! empty($data['customer_from_title'])){
  567. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  568. $model->whereIn('customer_from', $id);
  569. }
  570. if(! empty($data['customer_type_title'])) {
  571. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  572. $model->whereIn('customer_type', $id);
  573. }
  574. if(! empty($data['progress_stage_title'])){
  575. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  576. $model->whereIn('progress_stage', $id);
  577. }
  578. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  579. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  580. $model->where('crt_time','>=',$return[0]);
  581. $model->where('crt_time','<=',$return[1]);
  582. }
  583. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  584. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  585. $model->where('enter_time','>=',$return[0]);
  586. $model->where('enter_time','<=',$return[1]);
  587. }
  588. if(! empty($data['crt_name'])){
  589. $id = (new RangeService())->crtNameSearch($data);
  590. $model->whereIn('crt_id',$id);
  591. }
  592. if(! empty($data['fz'])){
  593. $id = (new RangeService())->customerSearch($data);
  594. $model->whereIn('id',$id);
  595. }
  596. if(! empty($data['last_visit_time'])){
  597. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  598. $model->whereIn('id',$id);
  599. }
  600. if(! empty($data['contact_info'])){
  601. $customer_id = $this->contactSearch($data['contact_info']);
  602. $model->whereIn('id', $customer_id);
  603. }
  604. if(! empty($data['no_fp_time'])) $model->where('fp_time', 0);
  605. $is_fp = -1;
  606. if(isset($data['is_fp'])) {
  607. if($data['is_fp']){
  608. $model->where('fp_time', '>', 0);
  609. $is_fp = 1;
  610. } else{
  611. $model->where('fp_time', 0);
  612. $is_fp = 0;
  613. }
  614. }
  615. if(! empty($data['belong_top_depart_title'])){
  616. list($id, $depart_id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  617. if($is_fp < 0){
  618. $model->where(function ($query) use ($id, $depart_id) {
  619. $query->whereIn('id', $id)
  620. ->orWhereIn('top_depart_id', $depart_id);
  621. });
  622. }elseif ($is_fp > 0){
  623. $model->whereIn('id', $id);
  624. }else{
  625. $model->where('top_depart_id', $depart_id);
  626. }
  627. }
  628. if(! empty($data['smart_search'])){
  629. $customer_id = $this->contactSearch($data['smart_search']);
  630. $progress_stage_id = (new RangeService())->customerBasicTypeSearch($data['smart_search'],[5]);
  631. $model->where(function (Builder $query) use ($data, $customer_id,$progress_stage_id) {
  632. $query->where('title', 'LIKE', '%'.$data['smart_search'].'%')
  633. ->orWhereIn('id', $customer_id)
  634. ->orWhereIn('progress_stage', $progress_stage_id);
  635. });
  636. }
  637. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  638. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  639. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  640. }
  641. return $model;
  642. }
  643. public function contactSearch($contact_info){
  644. $customer_info = CustomerInfo::where('del_time',0)
  645. ->where("type",CustomerInfo::type_one)
  646. ->where('contact_info','LIKE', '%'.$contact_info.'%')
  647. ->select('customer_id')
  648. ->get()->toArray();
  649. return array_column($customer_info,'customer_id');
  650. }
  651. public function titleSearch($user, $data, $message){
  652. $model2 = Customer::Clear($user,$data);
  653. $customer = $model2->where('del_time',0)
  654. ->where('title', 'LIKE', '%'. $message .'%')
  655. ->select('id')
  656. ->get()->toArray();
  657. return array_column($customer,'id');
  658. }
  659. /**
  660. * 客户列表
  661. * @param $data
  662. * @param $user
  663. * @return array
  664. */
  665. public function customerWxList($data,$user){
  666. $model = $this->customerCommonSearch($data,$user);
  667. $list = $this->limit($model,'',$data);
  668. $list = $this->fillData($list,$data, $user);
  669. //微信数据
  670. $count = $this->countData("crt_time", $data, $user);
  671. $list['today'] = $count[0] ?? 0;
  672. $list['yesterday'] = $count[1] ?? 0;
  673. return [true, $list];
  674. }
  675. /**
  676. * 客户列表
  677. * @param $data
  678. * @param $user
  679. * @return array
  680. */
  681. public function customerList($data,$user){
  682. $model = $this->customerCommonSearch($data,$user);
  683. $list = $this->limit($model,'',$data);
  684. $list = $this->fillData($list,$data, $user);
  685. return [true, $list];
  686. }
  687. public function customer2CommonSearch($data,$user){
  688. $model = Customer::Clear($user,$data);
  689. $model = $model->where('del_time',0)
  690. ->where('fp_time','>',0)
  691. ->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')
  692. ->orderby('fp_time', 'desc');
  693. if(! empty($data['customer_id'])){
  694. $customer_id = explode(',',$data['customer_id']);
  695. $model->whereIn('id', $customer_id);
  696. }
  697. if(! empty($data['my_fz']) || ! empty($data['my_xt'])){
  698. $id = $this->getCustomerId($data,$user);
  699. $model->whereIn('id',$id);
  700. }
  701. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  702. if(! empty($data['time_type'])) {
  703. if($data['time_type'] == 1) {
  704. $start = strtotime('today');
  705. $end = strtotime('tomorrow') - 1;
  706. }elseif ($data['time_type'] == 2){
  707. $start = strtotime('this week',strtotime('today'));
  708. $end = strtotime('this week +6 days 23:59:59', strtotime('today'));
  709. }
  710. if(! empty($start) && ! empty($end)) {
  711. $model->where('crt_time','>=',$start);
  712. $model->where('crt_time','<=',$end);
  713. }
  714. }
  715. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  716. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  717. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  718. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  719. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  720. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  721. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  722. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  723. $model->where('crt_time','>=',$return[0]);
  724. $model->where('crt_time','<=',$return[1]);
  725. }
  726. if(! empty($data['crt_name'])){
  727. $id = (new RangeService())->crtNameSearch($data);
  728. $model->whereIn('crt_id',$id);
  729. }
  730. if(! empty($data['fz'])){
  731. $id = (new RangeService())->customerSearch($data);
  732. $model->whereIn('id',$id);
  733. }
  734. if(! empty($data['last_visit_time'])){
  735. $id = (new FollowUpRecordService())->getLastVisitData($data['last_visit_time']);
  736. $model->whereIn('id',$id);
  737. }
  738. if(! empty($data['contact_info'])){
  739. $customer_info = CustomerInfo::where('del_time',0)
  740. ->where("type",CustomerInfo::type_one)
  741. ->where('contact_info','LIKE', '%'.$data['contact_info'].'%')
  742. ->select('customer_id')
  743. ->get()->toArray();
  744. $model->whereIn('id',array_column($customer_info,'customer_id'));
  745. }
  746. if(! empty($data['fp_time'][0]) && ! empty($data['fp_time'][1])){
  747. $return = $this->changeDateToTimeStampAboutRange($data['fp_time']);
  748. $model->where('fp_time','>=',$return[0]);
  749. $model->where('fp_time','<=',$return[1]);
  750. }
  751. if(! empty($data['smart_search'])){
  752. $customer_id = $this->contactSearch($data['smart_search']);
  753. $progress_stage_id = (new RangeService())->customerBasicTypeSearch($data['smart_search'],[5]);
  754. $model->where('title', 'LIKE', '%'.$data['smart_search'].'%')
  755. ->orWhereIn('id', $customer_id)
  756. ->orWhereIn('progress_stage', $progress_stage_id);
  757. }
  758. if(! empty($data['belong_top_depart_title'])){
  759. list($id) = $this->searchCustomerDepart($data['belong_top_depart_title']);
  760. $model->whereIn('id', $id);
  761. }
  762. if(! empty($data['wx_fp_time'][0]) && ! empty($data['wx_fp_time'][1])) {
  763. $model->where('fp_time','>=',$data['wx_fp_time'][0]);
  764. $model->where('fp_time','<=',$data['wx_fp_time'][1]);
  765. }
  766. return $model;
  767. }
  768. public function customerList2($data,$user){
  769. $model = $this->customer2CommonSearch($data, $user);
  770. $list = $this->limit($model,'',$data);
  771. $list = $this->fillData($list,$data,$user);
  772. //微信数据
  773. $count = $this->countData2("fp_time", $data, $user);
  774. $list['today'] = $count[0] ?? 0;
  775. $list['yesterday'] = $count[1] ?? 0;
  776. return [true, $list];
  777. }
  778. /**
  779. * 客户重复
  780. * @param $data
  781. * @param $user
  782. * @return array
  783. */
  784. public function customerRepeatList($data,$user){
  785. $model = $this->customerRepeatCommonSearch($data,$user);
  786. $list = $this->limit($model,'',$data);
  787. $list = $this->fillRepeatData($list,$data, $user);
  788. return [true, $list];
  789. }
  790. public function customerRepeatCommonSearch($data,$user, $field = []){
  791. if(empty($field)){
  792. $field = ['title','id','model_type','customer_from','car_type','address2','crt_id','crt_time','enter_time','contact_type','contact','mark'];
  793. }
  794. $model = CustomerRepeat::Clear($user,$data);
  795. $model = $model->where('del_time',0)
  796. ->select($field)
  797. ->orderby('id', 'desc');
  798. if(! empty($data['customer_id'])){
  799. $customer_id = explode(',',$data['customer_id']);
  800. $model->whereIn('id', $customer_id);
  801. }
  802. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  803. if(! empty($data['address2'])) $model->where('address2', 'LIKE', '%'.$data['address2'].'%');
  804. if(! empty($data['model_type'])) $model->where('model_type',$data['model_type']);
  805. if(! empty($data['consulting_product'])) $model->where('consulting_product','LIKE', '%'.$data['consulting_product'].'%');
  806. if(! empty($data['mark'])) $model->where('mark','LIKE', '%'.$data['mark'].'%');
  807. if(! empty($data['customer_intention'])) $model->where('customer_intention',$data['customer_intention']);
  808. if(! empty($data['customer_from'])) $model->where('customer_from',$data['customer_from']);
  809. if(! empty($data['customer_type'])) $model->where('customer_type',$data['customer_type']);
  810. if(! empty($data['customer_intention_title'])){
  811. $id = (new RangeService())->customerBasicTypeSearch($data['customer_intention_title'],[1]);
  812. $model->whereIn('customer_intention', $id);
  813. }
  814. if(! empty($data['customer_from_title'])){
  815. $id = (new RangeService())->customerBasicTypeSearch($data['customer_from_title'],[2]);
  816. $model->whereIn('customer_from', $id);
  817. }
  818. if(! empty($data['customer_type_title'])) {
  819. $id = (new RangeService())->customerBasicTypeSearch($data['customer_type_title'],[3,30,31]);
  820. $model->whereIn('customer_type', $id);
  821. }
  822. if(! empty($data['progress_stage_title'])){
  823. $id = (new RangeService())->customerBasicTypeSearch($data['progress_stage_title'],[5]);
  824. $model->whereIn('progress_stage', $id);
  825. }
  826. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  827. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  828. $model->where('crt_time','>=',$return[0]);
  829. $model->where('crt_time','<=',$return[1]);
  830. }
  831. if(! empty($data['enter_time'][0]) && ! empty($data['enter_time'][1])) {
  832. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  833. $model->where('enter_time','>=',$return[0]);
  834. $model->where('enter_time','<=',$return[1]);
  835. }
  836. if(! empty($data['crt_name'])){
  837. $id = (new RangeService())->crtNameSearch($data);
  838. $model->whereIn('crt_id',$id);
  839. }
  840. if(! empty($data['fz'])){
  841. $emp_id = Employee::where('del_time',0)
  842. ->where('emp_name','LIKE', '%'.$data['fz'].'%')
  843. ->select('id')->get()->toArray();
  844. $emp_id = array_column($emp_id,'id');
  845. $model->whereIn('fz_man', $emp_id);
  846. }
  847. if(! empty($data['contact_info'])) $model->where('contact', 'LIKE', '%'.$data['contact_info'].'%');
  848. return $model;
  849. }
  850. /**
  851. * 拼接数据
  852. * @param $data
  853. * @return array
  854. */
  855. public function fillRepeatData($data,$ergs,$user){
  856. if(empty($data['data'])) return $data;
  857. $array = array_unique(array_merge_recursive(array_column($data['data'],'customer_from'),array_column($data['data'],'car_type')));
  858. $basic_map = BasicType::whereIn('id',$array)
  859. ->pluck('title','id')
  860. ->toArray();
  861. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  862. ->pluck('emp_name','id')
  863. ->toArray();
  864. foreach ($data['data'] as $key => $value){
  865. $data['data'][$key]['address'] = $value['address2'];
  866. //客户来源
  867. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  868. // $this->isTopDispatch($user, $data['data'][$key]);
  869. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  870. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  871. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  872. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  873. }
  874. return $data;
  875. }
  876. public function getCustomerId($data,$user){
  877. $return = [];
  878. if(! empty($data['my_fz'])){
  879. $info = CustomerInfo::where('del_time',0)
  880. ->where('data_id',$user['id'])
  881. ->where('type',CustomerInfo::type_two)
  882. ->select('customer_id')
  883. ->get()->toArray();
  884. $return = array_unique(array_column($info,'customer_id'));
  885. }
  886. if(! empty($data['my_xt'])){
  887. $info = CustomerInfo::where('del_time',0)
  888. ->where('data_id',$user['id'])
  889. ->where('type',CustomerInfo::type_three)
  890. ->select('customer_id')
  891. ->get()->toArray();
  892. $return = array_unique(array_column($info,'customer_id'));
  893. }
  894. return $return;
  895. }
  896. /**
  897. * 客户参数规则
  898. * @param $data
  899. * @param $is_add
  900. * @return array
  901. */
  902. public function customerRule(&$data, $user, $is_add = true){
  903. if(empty($data['model_type'])) return [false,'客户模板类型不能为空'];
  904. if(! in_array($data['model_type'],Customer::$model_type)) return [false,'客户模板类型错误'];
  905. if(empty($data['title'])) return [false,'客户名称不能为空'];
  906. if(empty($data['enter_time'])) return [false, '录入系统日期不能为空'];
  907. $data['enter_time'] = $this->changeDateToDateMin($data['enter_time']);
  908. //所属部门 以及 顶级部门
  909. if(empty($data['depart_id'])) {
  910. $data['depart_id'] = $this->getDepart($user);
  911. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  912. }
  913. if($data['model_type'] == Customer::Model_type_one){
  914. // if(empty($data['customer_from'])) return [false,'客户来源不能为空'];
  915. // if(empty($data['customer_type'])) return [false,'客户类别不能为空'];
  916. // if(empty($data['consulting_product'])) return [false,'咨询产品不能为空'];
  917. // if(empty($data['progress_stage'])) return [false,'进展阶段不能为空'];
  918. // if(empty($data['employee_one'])) return [false,'销售SA不能为空'];
  919. }else{
  920. // if(empty($data['car_type'])) return [false,'车型不能为空'];
  921. // if(empty($data['customer_contact'])) return [false,'客户联系方式不能为空'];
  922. }
  923. list($status,$msg) = $this->limitingSendRequestBackgExpire($data['top_depart_id'] . $data['title'],3);
  924. if(! $status) return [false, $msg];
  925. if(empty($data['car_type']) && ! empty($data['car_type_title'])){
  926. $bool = BasicType::where('title',$data['car_type_title'])
  927. ->where('top_depart_id',$data['top_depart_id'])
  928. ->where('type',BasicType::type_10)
  929. ->where('del_time',0)
  930. ->exists();
  931. if($bool) return [false,'车型名称已存在'];
  932. }
  933. if($is_add){
  934. // $bool = Customer::where('del_time',0)
  935. // ->where('top_depart_id',$data['top_depart_id'])
  936. // ->where('title',$data['title'])
  937. //// ->where('model_type',$data['model_type'])
  938. // ->exists();
  939. // if(! empty($data['customer_contact'])) {
  940. // $search = [];
  941. // foreach ($data['customer_contact'] as $value){
  942. // $search[] = $value['info'];
  943. // }
  944. // $boolean = CustomerInfo::from('customer_info as a')
  945. // ->join('customer as b','b.id','a.customer_id')
  946. // ->where('a.del_time',0)
  947. // ->where('b.del_time',0)
  948. // ->where('b.top_depart_id',$data['top_depart_id'])
  949. // ->whereIn('a.contact_info', $search)
  950. // ->exists();
  951. // if($boolean) return [false,'客户联系内容已存在'];
  952. // }
  953. }else{
  954. if(empty($data['id'])) return [false,'ID不能为空'];
  955. $bool = Customer::where('del_time',0)
  956. ->where('id',$data['id'])
  957. ->exists();
  958. if(! $bool) return [false, '客户不存在或已被删除'];
  959. // $bool = Customer::where('del_time',0)
  960. // ->where('id','<>',$data['id'])
  961. // ->where('top_depart_id',$data['top_depart_id'])
  962. // ->where('title',$data['title'])
  963. //// ->where('model_type',$data['model_type'])
  964. // ->exists();
  965. // if(! empty($data['customer_contact'])) {
  966. // $search = [];
  967. // foreach ($data['customer_contact'] as $value){
  968. // $search[] = $value['info'];
  969. // }
  970. // $boolean = CustomerInfo::from('customer_info as a')
  971. // ->join('customer as b','b.id','a.customer_id')
  972. // ->where('b.id','<>',$data['id'])
  973. // ->where('a.del_time',0)
  974. // ->where('b.del_time',0)
  975. // ->where('b.top_depart_id',$data['top_depart_id'])
  976. // ->whereIn('a.contact_info', $search)
  977. // ->exists();
  978. // if($boolean) return [false,'客户联系内容已存在'];
  979. // }
  980. }
  981. // if($bool) return [false,'客户名称不能重复'];
  982. return [true, ''];
  983. }
  984. /**
  985. * 拼接数据
  986. * @param $data
  987. * @return array
  988. */
  989. public function fillData($data,$ergs,$user){
  990. if(empty($data['data'])) return $data;
  991. $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')));
  992. $basic_map = BasicType::whereIn('id',$array)
  993. ->pluck('title','id')
  994. ->toArray();
  995. $basic_all_map = BasicTypeAllUse::where('type', BasicTypeAllUse::type_one)
  996. ->whereIn('id',array_unique(array_column($data['data'],'consulting_product_new')))
  997. ->pluck('title','id')
  998. ->toArray();
  999. $depart_title = Depart::where('id',array_unique(array_column($data['data'],'depart_id')))
  1000. ->pluck('title','id')
  1001. ->toArray();
  1002. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  1003. ->pluck('emp_name','id')
  1004. ->toArray();
  1005. $product = Product::whereIn('id',array_unique(array_column($data['data'],'intention_product')))
  1006. ->pluck('title','id')
  1007. ->toArray();
  1008. $customer_id = array_column($data['data'],'id');
  1009. //跟进记录
  1010. $record_array = (new FollowUpRecordService())->getVisitDataOfTime($customer_id, FollowUpRecord::type_one);
  1011. $customer_info = CustomerInfo::where('del_time',0)
  1012. ->whereIn('customer_id',$customer_id)
  1013. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two,CustomerInfo::type_three])
  1014. ->select('type','contact_type','contact_info','customer_id','data_id')
  1015. ->get()->toArray();
  1016. //客户的联系方式 客户的负责人 协同人
  1017. $customer_info_map = $fz = $customer_info_map2 = $xt = [];
  1018. $emp_map = Employee::whereIn('id',array_filter(array_column($customer_info,'data_id')))
  1019. ->pluck('emp_name','id')
  1020. ->toArray();
  1021. $basic_maps = BasicType::whereIn('id',array_unique(array_filter(array_column($customer_info,'contact_type'))))->pluck('title','id')->toArray();
  1022. foreach ($customer_info as $value){
  1023. if($value['type'] == CustomerInfo::type_one){
  1024. if(! $value['contact_info']) continue;
  1025. $value['contact_type_title'] = $basic_maps[$value['contact_type']] ?? "";
  1026. $customer_info_map[$value['customer_id']][] = $value;
  1027. // if(! isset($customer_info_map2[$value['customer_id']])){
  1028. // $customer_info_map2[$value['customer_id']] = $value['contact_type_title'] . ": " . $value['contact_info'];
  1029. // }else{
  1030. // $customer_info_map2[$value['customer_id']] .= ',' . $value['contact_type_title'] . ": " . $value['contact_info'];
  1031. // }
  1032. if(! isset($customer_info_map2[$value['customer_id']])){
  1033. $customer_info_map2[$value['customer_id']] = $value['contact_info'];
  1034. }else{
  1035. $customer_info_map2[$value['customer_id']] .= '|' . $value['contact_info'];
  1036. }
  1037. }elseif($value['type'] == CustomerInfo::type_two){
  1038. $tmp = $emp_map[$value['data_id']] ?? "";
  1039. if(isset($fz[$value['customer_id']])){
  1040. $fz[$value['customer_id']] .= ',' . $tmp;
  1041. }else{
  1042. $fz[$value['customer_id']] = $tmp;
  1043. }
  1044. }else{
  1045. $tmp = $emp_map[$value['data_id']] ?? "";
  1046. if(isset($xt[$value['customer_id']])){
  1047. $xt[$value['customer_id']] .= ',' . $tmp;
  1048. }else{
  1049. $xt[$value['customer_id']] = $tmp;
  1050. }
  1051. }
  1052. }
  1053. $array2 = array_unique(array_column($data['data'],'top_depart_id'));
  1054. $depart = Depart::whereIn('id',$array2)->pluck('title','id')->toArray();
  1055. $depart2_map = $this->getCustomerDepart($customer_id);
  1056. //地址
  1057. $address_map = config('address');
  1058. foreach ($data['data'] as $key => $value){
  1059. $address_str = [];
  1060. if(! empty($value['address1'])) {
  1061. $tmp = json_decode($value['address1'],true);
  1062. $this->findLabelsByValue($address_map,$tmp,$address_str);
  1063. $tmp = implode(' ',$address_str);
  1064. $tmp .= ' ' . $value['address2'];
  1065. $address = $tmp;
  1066. }else{
  1067. $address = $value['address2'];
  1068. }
  1069. $data['data'][$key]['address'] = $address;
  1070. $data['data'][$key]['customer_intention_title'] = $basic_map[$value['customer_intention']] ?? '';
  1071. //客户来源
  1072. $data['data'][$key]['customer_from_title'] = $basic_map[$value['customer_from']] ?? '';
  1073. $this->isTopDispatch($user, $data['data'][$key]);
  1074. $data['data'][$key]['customer_type_title'] = $basic_map[$value['customer_type']] ?? '';
  1075. $data['data'][$key]['car_type_title'] = $basic_map[$value['car_type']] ?? '';
  1076. $data['data'][$key]['intention_product_title'] = $product[$value['intention_product']] ?? '';
  1077. $data['data'][$key]['consulting_product_new_title'] = $basic_all_map[$value['consulting_product_new']] ?? '';
  1078. $data['data'][$key]['progress_stage_title'] = $basic_map[$value['progress_stage']] ?? '';
  1079. $data['data'][$key]['state_type_title'] = $basic_map[$value['state_type']] ?? '';
  1080. $data['data'][$key]['customer_state_title'] = $basic_map[$value['customer_state']] ?? '';
  1081. $data['data'][$key]['depart_title'] = $depart_title[$value['depart_id']] ?? '';
  1082. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  1083. $data['data'][$key]['enter_time'] = $value['enter_time'] ? date('Y-m-d H:i:s',$value['enter_time']) : '';
  1084. $data['data'][$key]['fp_time'] = $value['fp_time'] ? date('Y-m-d H:i:s',$value['fp_time']) : '';
  1085. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  1086. $customer_tmp = $customer_info_map[$value['id']] ?? [];
  1087. $data['data'][$key]['customer_detail'] = $customer_tmp;
  1088. $data['data'][$key]['customer_detail2'] = $customer_info_map2[$value['id']] ?? "";
  1089. $data['data'][$key]['fz'] = $fz[$value['id']] ?? [];
  1090. $record_tmp = $record_array[$value['id']] ?? "";
  1091. $data['data'][$key]['has_record'] = $record_tmp ? "查看" : "无记录";
  1092. $data['data'][$key]['follow_record'] = $record_array[$value['id']] ?? "";
  1093. $data['data'][$key]['order_number'] = Customer::$order_number . "|" . $value['id'] . "|" . $value['title'];
  1094. $top_depart_title = $depart[$value['top_depart_id']] ?? "";
  1095. $data['data'][$key]['top_depart_title'] = $top_depart_title;
  1096. //分配门店
  1097. $fp_top_depart_title = $depart2_map[$value['id']] ?? "";
  1098. if($value['fp_time'] > 0) {
  1099. //分配过所属门店是分配门店
  1100. $data['data'][$key]['belong_top_depart_title'] = $fp_top_depart_title;
  1101. }else{
  1102. //没分配过所属门店是创建门店
  1103. $data['data'][$key]['belong_top_depart_title'] = $top_depart_title;
  1104. }
  1105. $data['data'][$key]['is_dispatch_title'] = $value['fp_time'] ? "已分配或移交" : "未分配或移交";
  1106. $data['data'][$key]['xt'] = $xt[$value['id']] ?? "";
  1107. }
  1108. return $data;
  1109. }
  1110. private function isTopDispatch($user, &$value){
  1111. //特殊功能按钮
  1112. $special_button = $user['special_button'] ?? [];
  1113. //总社
  1114. $head = $user['head']['id'] ?? 0;
  1115. //当前门店
  1116. $top_depart_id = $user['depart_top'][0] ?? [];
  1117. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  1118. //分社账号
  1119. if($top_depart_id != $head){
  1120. //总社创建分配给分社的客户
  1121. if($value['top_depart_id'] == $head && $value['fp_time'] > 0 && $value['fp_top_depart_id'] != $head){
  1122. if(! in_array(RoleMenuButton::special_nine,$special_button)) $value['customer_from_title'] = "";
  1123. }
  1124. }
  1125. }
  1126. public function countData($column = "", $data, $user){
  1127. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1128. // 获取今天的开始和结束时间戳
  1129. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1130. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1131. // 获取昨天的开始和结束时间戳
  1132. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1133. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1134. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1135. $model = $this->customerCommonSearch($data, $user);
  1136. // 查询今天的数据条数
  1137. $todayCount = $model->count();
  1138. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1139. $model = $this->customerCommonSearch($data, $user);
  1140. // 查询昨天的数据条数
  1141. $yesterdayCount = $model->count();
  1142. return [$todayCount, $yesterdayCount];
  1143. }
  1144. public function countData2($column = "", $data, $user){
  1145. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  1146. // 获取今天的开始和结束时间戳
  1147. $todayStart = Carbon::today()->startOfDay()->timestamp;
  1148. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  1149. // 获取昨天的开始和结束时间戳
  1150. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  1151. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  1152. $data['wx_' . $column] = [$todayStart, $todayEnd];
  1153. $model = $this->customer2CommonSearch($data, $user);
  1154. // 查询今天的数据条数
  1155. $todayCount = $model->count();
  1156. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  1157. $model = $this->customer2CommonSearch($data, $user);
  1158. // 查询昨天的数据条数
  1159. $yesterdayCount = $model->count();
  1160. return [$todayCount, $yesterdayCount];
  1161. }
  1162. //获取客资门店
  1163. public function getCustomerDepart($customer_id = []){
  1164. if(empty($customer_id)) return [];
  1165. $result = SeeRange::where('del_time',0)
  1166. ->whereIn('data_id', $customer_id)
  1167. ->where('data_type',SeeRange::type_one)
  1168. ->where('type',SeeRange::data_three)
  1169. ->select('data_id as customer_id','param_id as top_depart_id')
  1170. ->get()->toArray();
  1171. $depart_map = Depart::whereIn('id',array_unique(array_column($result,'top_depart_id')))->pluck('title','id')->toArray();
  1172. $return = [];
  1173. foreach ($result as $value){
  1174. $tmp = $depart_map[$value['top_depart_id']] ?? "";
  1175. if(! $tmp) continue;
  1176. if(isset($return[$value['customer_id']])){
  1177. $return[$value['customer_id']] .= ',' . $tmp;
  1178. }else{
  1179. $return[$value['customer_id']] = $tmp;
  1180. }
  1181. }
  1182. return $return;
  1183. }
  1184. //客资门店搜索
  1185. public function searchCustomerDepart($depart_title = ""){
  1186. $customer_id = [];
  1187. if(empty($depart_title)) return $customer_id;
  1188. $depart_id = Depart::where('del_time', 0)
  1189. ->where('parent_id', 0)
  1190. ->where('title', 'LIKE', '%'. $depart_title .'%')
  1191. ->select('id')->get()->toArray();
  1192. $depart_id = array_column($depart_id, 'id');
  1193. if(empty($depart_id)) return [$customer_id, $depart_id];
  1194. $customer_id = SeeRange::where('del_time',0)
  1195. ->whereIn('param_id', $depart_id)
  1196. ->where('data_type',SeeRange::type_one)
  1197. ->where('type',SeeRange::data_three)
  1198. ->select('data_id as customer_id')
  1199. ->get()->toArray();
  1200. $customer_id = array_column($customer_id, 'customer_id');
  1201. return [$customer_id, $depart_id];
  1202. }
  1203. //抢客户
  1204. public function customerGrabbing($data, $user){
  1205. $key = Customer::$limitKey . $data['customer_id'];
  1206. list($status,$msg) = $this->customerGrabbingRule($data,$key);
  1207. if(! $status) {
  1208. //释放锁
  1209. $this->dellimitingSendRequestBackg($key);
  1210. return [false,$msg];
  1211. }
  1212. try {
  1213. DB::beginTransaction();
  1214. $time = time();
  1215. $insert = [];
  1216. //负责人获取 改为前负责人
  1217. $man = CustomerInfo::where('del_time',0)
  1218. ->where('customer_id',$data['customer_id'])
  1219. ->where('type', CustomerInfo::type_two)
  1220. ->get()->toArray();
  1221. foreach ($man as $value){
  1222. $insert[] = [
  1223. 'customer_id' => $data['customer_id'],
  1224. 'data_id' => $value['data_id'],
  1225. 'type' => CustomerInfo::type_nine,
  1226. 'crt_time' => $time
  1227. ];
  1228. }
  1229. //增加自己为负责人
  1230. $insert[] = [
  1231. 'customer_id' => $data['customer_id'],
  1232. 'data_id' => $user['id'],
  1233. 'type' => CustomerInfo::type_two,
  1234. 'crt_time' => $time
  1235. ];
  1236. //人员清空
  1237. CustomerInfo::where('del_time',0)
  1238. ->where('customer_id',$data['customer_id'])
  1239. ->whereIn('type', CustomerInfo::$man2)
  1240. ->update(['del_time' => $time]);
  1241. //写入最新人员
  1242. CustomerInfo::insert($insert);
  1243. //更新公海池状态
  1244. Customer::where('id',$data['customer_id'])->update([
  1245. 'pond_state' => 0
  1246. ]);
  1247. DB::commit();
  1248. }catch (\Exception $exception){
  1249. //释放锁
  1250. $this->dellimitingSendRequestBackg($key);
  1251. DB::rollBack();
  1252. return [false,$exception->getMessage()];
  1253. }
  1254. //释放锁
  1255. $this->dellimitingSendRequestBackg($key);
  1256. return [true, ''];
  1257. }
  1258. public function customerGrabbingRule($data, $key){
  1259. if(empty($data['customer_id'])) return [false,'请选择客户'];
  1260. //上锁
  1261. list($status,$msg) = $this->limitingSendRequestBackg($key);
  1262. if(! $status) return [false,$msg];
  1263. $customer = Customer::where('del_time',0)->where('id',$data['customer_id'])->first();
  1264. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1265. $customer = $customer->toArray();
  1266. if(empty($customer['pond_state'])) return [false,'客户已退出公海池'];
  1267. return [true,''];
  1268. }
  1269. public function searchBy($data,$user){
  1270. $model = BasicType::TopClear($user,$data);
  1271. $result = $model->where('del_time',0)
  1272. ->where('type', 2)
  1273. ->where('title', 'LIKE', '%'.$data['customer_from'].'%')
  1274. ->select('id')
  1275. ->get()->toArray();
  1276. $model2 = Customer::Clear($user,$data);
  1277. $customer = $model2->where('del_time',0)
  1278. ->whereIn('customer_from', array_column($result,'id'))
  1279. ->select('id')
  1280. ->get()->toArray();
  1281. return array_column($customer,'id');
  1282. }
  1283. public function customerImportanceEdit($data,$user){
  1284. if(empty($data['id'])) return [false, '请选择客户'];
  1285. $model = Customer::where('id',$data['id'])->first();
  1286. if(empty($model) || $model->del_time > 0) return [false, '客户不存在或已被删除'];
  1287. if(! isset($data['importance'])) return [false, '客户重要程度不存在'];
  1288. $data['order_number'] = Customer::$order_number . "|" . $data['id'] . "|" . $model->title;
  1289. $params = $this->getDataFile($data);
  1290. (new OperationLogService())->setOperationList($params,$user,2);
  1291. try {
  1292. DB::beginTransaction();
  1293. $model = Customer::where('id',$data['id'])->first();
  1294. $model->importance = $data['importance'] ?? '';
  1295. $model->save();
  1296. DB::commit();
  1297. }catch (\Exception $exception){
  1298. DB::rollBack();
  1299. return [false,$exception->getMessage()];
  1300. }
  1301. return [true, ''];
  1302. }
  1303. //发送客户消息
  1304. public function customerSendWx($data, $user){
  1305. list($status,$msg) = $this->customerSendWxRule($data);
  1306. if(! $status) return [false,$msg];
  1307. //发送消息
  1308. $send_data = $msg;
  1309. (new OaService())->sendWxOaCheckMessage($send_data);
  1310. return [true, ''];
  1311. }
  1312. public function customerSendWxRule($data){
  1313. if(empty($data['id'])) return [false,'请选择客户'];
  1314. $customer = Customer::where('del_time',0)
  1315. ->whereIn('id',$data['id'])
  1316. ->get()->toArray();
  1317. if(empty($customer)) return [false,'客户不存在或已被删除'];
  1318. $man = CustomerInfo::where('del_time',0)
  1319. ->whereIn('customer_id',$data['id'])
  1320. ->where('type', CustomerInfo::type_two)
  1321. ->get()->toArray();
  1322. if(empty($man)) return [false, '客户暂无负责人信息,公众号消息发送结束'];
  1323. $customer_map = [];
  1324. foreach ($man as $value){
  1325. $customer_map[$value['data_id']][] = $value['customer_id'];
  1326. }
  1327. $employee_id = array_unique(array_column($man,'data_id'));
  1328. $wx_map = WxEmployeeOfficial::whereIn('employee_id', $employee_id)
  1329. ->pluck('openid','employee_id')
  1330. ->toArray();
  1331. $emp = Employee::whereIn('id', $employee_id)
  1332. ->pluck('emp_name','id')
  1333. ->toArray();
  1334. $send_data = [];
  1335. foreach ($employee_id as $value){
  1336. $tmp = $emp[$value] ?? "";
  1337. $open_id = $wx_map[$value] ?? "";
  1338. // if(empty($open_id)) return [false, $tmp . "暂未关注微信公众号,公众号消息发送失败"];
  1339. if(empty($open_id)) continue;
  1340. $customer_id = $customer_map[$value] ?? [];
  1341. $customer_id = implode(',', $customer_id);
  1342. //提醒创建人
  1343. $send_data[] = [
  1344. 'employee_id' => $value,
  1345. 'type' => 2,
  1346. 'state' => 0,
  1347. 'menu_id' => "16|list",
  1348. 'openid' => $open_id,
  1349. 'order_number' => $customer_id,
  1350. 'tmp_data' => [
  1351. time(),
  1352. "客户消息提醒",
  1353. "已发送",
  1354. $tmp,
  1355. date('Y-m-d H:i:s'),
  1356. ],
  1357. ];
  1358. }
  1359. return [true, $send_data];
  1360. }
  1361. }