CustomerService.php 55 KB

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