DeleteService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Construction;
  4. use App\Model\ConstructionInfo;
  5. use App\Model\Customer;
  6. use App\Model\CustomerInfo;
  7. use App\Model\Depart;
  8. use App\Model\Employee;
  9. use App\Model\EmployeeDepartPermission;
  10. use App\Model\OrderOperation;
  11. use App\Model\SalesOrder;
  12. use App\Model\SalesOrderInfo;
  13. use App\Model\ScheduleInfo;
  14. use App\Model\SeeRange;
  15. use Illuminate\Support\Facades\DB;
  16. class DeleteService extends Service
  17. {
  18. public function getMan($data,$user){
  19. if(empty($data['id']) || empty($data['type']) || empty($data['man_type'])) return [false, '必填参数不能为空!'];
  20. $return = [];
  21. if($data['type'] == 1){
  22. $return = $this->getSaleOrderMan($data);
  23. }
  24. return [true, $return];
  25. }
  26. public function delete($data,$user){
  27. if(empty($data['id']) || empty($data['type']) || empty($data['man_type']) || empty($data['man'])) return [false, '必填参数不能为空!'];
  28. try {
  29. DB::beginTransaction();
  30. if($data['type'] == 1){
  31. $this->delSaleOrderMan($data,$user);
  32. }
  33. DB::commit();
  34. }catch (\Exception $exception){
  35. DB::rollBack();
  36. return [false,$exception->getMessage()];
  37. }
  38. return [true,''];
  39. }
  40. public function getSaleOrderMan($data){
  41. $man_id = SalesOrderInfo::where('del_time',0)
  42. ->where('sales_order_id',$data['id'])
  43. ->where('type',$data['man_type'])
  44. ->get('data_id')->toArray();
  45. $man_id = array_column($man_id,'data_id');
  46. return Employee::whereIn('id',$man_id)->select('id', 'emp_name')->get()->toArray();
  47. }
  48. public function delSaleOrderMan($data,$user){
  49. $time = time();
  50. SalesOrderInfo::where('del_time',0)
  51. ->where('sales_order_id',$data['id'])
  52. ->where('type',$data['man_type'])
  53. ->update(['del_time' => $time]);
  54. if(! empty($data['man'])){
  55. $insert = [];
  56. foreach ($data['man'] as $value){
  57. $insert[] = [
  58. 'sales_order_id' => $data['id'],
  59. 'data_id' => $value,
  60. 'type' => $data['man_type'],
  61. 'crt_time' => $time,
  62. ];
  63. }
  64. SalesOrderInfo::insert($insert);
  65. $type = 0;
  66. if($data['man_type'] == SalesOrderInfo::type_two) $type = OrderOperation::sixty;
  67. if($data['man_type'] == SalesOrderInfo::type_three) $type = OrderOperation::seventeen;
  68. $order = SalesOrder::where('id',$data['id'])->first();
  69. $order = $order->toArray();
  70. if($type){
  71. (new OrderOperationService())->add([
  72. 'order_number' => $order['order_number'],
  73. 'msg' => OrderOperation::$type[$type] ?? "",
  74. 'type' => $type
  75. ],$user);
  76. }
  77. }
  78. }
  79. public function fp($data,$user){
  80. if(empty($data['id']) || empty($data['type']) || empty($data['man'])) return [false, '必填参数不能为空!'];
  81. try {
  82. DB::beginTransaction();
  83. if($data['type'] == 1){
  84. list($status, $msg) = $this->fpSaleOrderMan($data,$user);
  85. }elseif ($data['type'] == 2){
  86. list($status, $msg) = $this->fpCustomerMan($data,$user);
  87. }elseif($data['type'] == 3){
  88. list($status, $msg) = $this->fpConstructionMan($data,$user);
  89. }else{
  90. return [false, '非法操作'];
  91. }
  92. if(! $status) {
  93. DB::rollBack();
  94. return [false, $msg];
  95. }
  96. DB::commit();
  97. }catch (\Exception $exception){
  98. DB::rollBack();
  99. return [false,$exception->getMessage()];
  100. }
  101. return [true,''];
  102. }
  103. public function fpSaleOrderMan($data,$user){
  104. $time = time();
  105. if(! empty($data['man'])){
  106. $insert = [];
  107. foreach ($data['man'] as $value){
  108. $insert[] = [
  109. 'sales_order_id' => $data['id'],
  110. 'data_id' => $value,
  111. 'type' => SalesOrderInfo::type_two,
  112. 'crt_time' => $time,
  113. ];
  114. }
  115. SalesOrderInfo::insert($insert);
  116. $order = SalesOrder::where('id',$data['id'])->first();
  117. $order = $order->toArray();
  118. (new OrderOperationService())->add([
  119. 'order_number' => $order['order_number'],
  120. 'msg' => OrderOperation::$type[OrderOperation::eighteen] ?? "",
  121. 'type' => OrderOperation::eighteen
  122. ],$user);
  123. }
  124. return [true, ''];
  125. }
  126. public function fpCustomerMan($data,$user){
  127. if(! is_array($data['id'])) $data_id = [$data['id']];
  128. else $data_id = $data['id'];
  129. $time = time();
  130. if(! empty($data['man'])){
  131. $insert = [];
  132. //协同人
  133. $xt = CustomerInfo::where('del_time',0)
  134. ->whereIn('customer_id',$data_id)
  135. ->where('type',CustomerInfo::type_three)
  136. ->where('data_id',$user['id'])
  137. ->select('customer_id')->get()->toArray();
  138. $xt = array_column($xt,'customer_id');
  139. $customer_map = Customer::whereIn('id', $data_id)
  140. ->pluck('title','id')
  141. ->toArray();
  142. $emp_map = Employee::whereIn('id',$data['man'])
  143. ->pluck('emp_name','id')
  144. ->toArray();
  145. //负责人清除
  146. CustomerInfo::where('del_time',0)
  147. ->whereIn('customer_id',$data_id)
  148. ->where('type', CustomerInfo::type_two)
  149. ->update(['del_time' => $time]);
  150. $send_data = [];
  151. foreach ($data['man'] as $value){
  152. $emp_tmp = $emp_map[$value] ?? "";
  153. foreach ($data_id as $c){
  154. //负责人累加
  155. $insert[] = [
  156. 'customer_id' => $c,
  157. 'data_id' => $value,
  158. 'type' => CustomerInfo::type_two,
  159. 'crt_time' => $time,
  160. ];
  161. if(! in_array($c, $xt)){
  162. //协同人累加
  163. $insert[] = [
  164. 'customer_id' => $c,
  165. 'data_id' => $user['id'],
  166. 'type' => CustomerInfo::type_three,
  167. 'crt_time' => $time,
  168. ];
  169. }
  170. $customer_tmp = $customer_map[$c] ?? "";
  171. $send_data[] = [
  172. 'employee_id' => $value,
  173. 'type' => 2,
  174. 'state' => 0,
  175. 'menu_id' => 16,
  176. 'order_number' => $customer_tmp,
  177. 'tmp_data' => [
  178. $customer_tmp,
  179. $emp_tmp,
  180. date('Y-m-d H:i:s'),
  181. ],
  182. ];
  183. }
  184. }
  185. CustomerInfo::insert($insert);
  186. if(! is_array($data['id'])){
  187. $title = Customer::where('id',$data['id'])->value('title') ?? "";
  188. (new OrderOperationService())->add([
  189. 'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,
  190. 'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",
  191. 'type' => OrderOperation::twenty_three
  192. ],$user);
  193. Customer::where('id',$data['id'])->update(['fp_time' => $time]);
  194. }else{
  195. $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();
  196. foreach($data['id'] as $value){
  197. $t_title = $title[$value] ?? "";
  198. (new OrderOperationService())->add([
  199. 'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,
  200. 'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",
  201. 'type' => OrderOperation::twenty_three
  202. ],$user);
  203. }
  204. Customer::whereIn('id',$data['id'])->update(['fp_time' => $time]);
  205. }
  206. (new OaService())->sendWxOaCheckMessage($send_data);
  207. }
  208. //客户被指派的门店 先清空
  209. SeeRange::where('del_time',0)
  210. ->whereIn('data_id', $data_id)
  211. ->where('data_type',SeeRange::type_one)
  212. ->where('type',SeeRange::data_three)
  213. ->update(['del_time' => $time]);
  214. if(! empty($data['fp_top_depart_id'])){
  215. if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];
  216. else $fp_top_depart_id = $data['fp_top_depart_id'];
  217. $man = $data['man'] ?? [];
  218. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  219. list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);
  220. if(! $status) return [false, $msg];
  221. $insert = [];
  222. foreach ($data_id as $id){
  223. foreach ($fp_top_depart_id as $value){
  224. $insert[] = [
  225. 'data_id' => $id, //客户id
  226. 'data_type' => SeeRange::type_one,
  227. 'param_id' => $value, //门店id
  228. 'type' => SeeRange::data_three,
  229. 'crt_time' => $time,
  230. ];
  231. }
  232. }
  233. if(! empty($insert)) SeeRange::insert($insert);
  234. }
  235. return [true, ''];
  236. }
  237. public function fpConstructionMan($data,$user){
  238. $time = time();
  239. //负责人清除
  240. ConstructionInfo::where('del_time',0)
  241. ->where('construction_id',$data['id'])
  242. ->where('type', ConstructionInfo::type_three)
  243. ->update(['del_time' => $time]);
  244. if(! empty($data['man'])){
  245. $insert = [];
  246. foreach ($data['man'] as $value){
  247. $insert[] = [
  248. 'construction_id' => $data['id'],
  249. 'employee_id' => $value,
  250. 'type' => ConstructionInfo::type_three,
  251. 'crt_time' => $time,
  252. 'opt_id' => $user['id'],
  253. ];
  254. }
  255. ConstructionInfo::insert($insert);
  256. $order = Construction::where('id',$data['id'])->first();
  257. $order = $order->toArray();
  258. (new OrderOperationService())->add([
  259. 'order_number' => $order['order_number'],
  260. 'msg' => OrderOperation::$type[OrderOperation::twenty_six] ?? "",
  261. 'type' => OrderOperation::twenty_six
  262. ],$user);
  263. }
  264. return [true, ''];
  265. }
  266. public function yj($data,$user){
  267. if(empty($data['id']) || empty($data['type']) || empty($data['man'])) return [false, '必填参数不能为空!'];
  268. // $key = "fpyj".$data['id'].$data['type'];
  269. // list($status,$msg) = $this->limitingSendRequestBackgExpire($key);
  270. // if(! $status) return [false,$msg];
  271. try {
  272. DB::beginTransaction();
  273. if($data['type'] == 1){
  274. list($status, $msg) = $this->yjSaleOrderMan($data,$user);
  275. }elseif ($data['type'] == 2){
  276. list($status, $msg) = $this->yjCustomerMan($data,$user);
  277. }else{
  278. return [false, '非法操作'];
  279. }
  280. if(! $status) {
  281. DB::rollBack();
  282. return [false, $msg];
  283. }
  284. DB::commit();
  285. }catch (\Exception $exception){
  286. DB::rollBack();
  287. return [false,$exception->getMessage()];
  288. }
  289. return [true,''];
  290. }
  291. public function yjSaleOrderMan($data,$user){
  292. $time = time();
  293. SalesOrderInfo::where('del_time',0)
  294. ->where('sales_order_id',$data['id'])
  295. ->where('type', SalesOrderInfo::type_two)
  296. ->where('data_id', $user['id'])
  297. ->update(['del_time' => $time]);
  298. if(! empty($data['man'])){
  299. $insert = [];
  300. foreach ($data['man'] as $value){
  301. $insert[] = [
  302. 'sales_order_id' => $data['id'],
  303. 'data_id' => $value,
  304. 'type' => SalesOrderInfo::type_two,
  305. 'crt_time' => $time,
  306. ];
  307. }
  308. SalesOrderInfo::insert($insert);
  309. $order = SalesOrder::where('id',$data['id'])->first();
  310. $order = $order->toArray();
  311. (new OrderOperationService())->add([
  312. 'order_number' => $order['order_number'],
  313. 'msg' => OrderOperation::$type[OrderOperation::nineteen] ?? "",
  314. 'type' => OrderOperation::nineteen
  315. ],$user);
  316. }
  317. return [true, ''];
  318. }
  319. public function yjCustomerMan($data,$user){
  320. $time = time();
  321. if(! is_array($data['id'])) $data_id = [$data['id']];
  322. else $data_id = $data['id'];
  323. CustomerInfo::where('del_time',0)
  324. ->whereIn('customer_id', $data_id)
  325. ->where('type', CustomerInfo::type_two)
  326. ->update(['del_time' => $time]);
  327. if(! empty($data['man'])){
  328. $insert = [];
  329. foreach ($data['man'] as $value){
  330. foreach ($data_id as $c){
  331. $insert[] = [
  332. 'customer_id' => $c,
  333. 'data_id' => $value,
  334. 'type' => CustomerInfo::type_two,
  335. 'crt_time' => $time,
  336. ];
  337. }
  338. }
  339. CustomerInfo::insert($insert);
  340. if(! is_array($data['id'])){
  341. $title = Customer::where('id',$data['id'])->value('title') ?? "";
  342. (new OrderOperationService())->add([
  343. 'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,
  344. 'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",
  345. 'type' => OrderOperation::twenty_four
  346. ],$user);
  347. Customer::where('id',$data['id'])->update(['fp_time' => $time]);
  348. }else{
  349. $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();
  350. foreach($data['id'] as $value){
  351. $t_title = $title[$value] ?? "";
  352. (new OrderOperationService())->add([
  353. 'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,
  354. 'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",
  355. 'type' => OrderOperation::twenty_four
  356. ],$user);
  357. }
  358. Customer::whereIn('id',$data['id'])->update(['fp_time' => $time]);
  359. }
  360. }
  361. //客户被移交的门店 先清空
  362. SeeRange::where('del_time',0)
  363. ->whereIn('data_id', $data_id)
  364. ->where('data_type',SeeRange::type_one)
  365. ->where('type',SeeRange::data_three)
  366. ->update(['del_time' => $time]);
  367. if(! empty($data['fp_top_depart_id'])){
  368. if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];
  369. else $fp_top_depart_id = $data['fp_top_depart_id'];
  370. $man = $data['man'] ?? [];
  371. $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
  372. list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);
  373. if(! $status) return [false, $msg];
  374. $insert = [];
  375. foreach ($data_id as $id){
  376. foreach ($fp_top_depart_id as $value){
  377. $insert[] = [
  378. 'data_id' => $id, //客户id
  379. 'data_type' => SeeRange::type_one,
  380. 'param_id' => $value, //门店id
  381. 'type' => SeeRange::data_three,
  382. 'crt_time' => $time,
  383. ];
  384. }
  385. }
  386. if(! empty($insert)) SeeRange::insert($insert);
  387. }
  388. return [true, ''];
  389. }
  390. public function checkRule($man_id = [], $top_depart_id = [], $depart_list){
  391. $depart = EmployeeDepartPermission::whereIn('employee_id', $man_id)
  392. ->select('employee_id','depart_id')
  393. ->get()->toArray();
  394. $depart_has = [];
  395. foreach ($depart as $value){
  396. $p = $this->findTopLevelId($depart_list, $value['depart_id']);
  397. if($p > 0) $depart_has[$value['employee_id']][] = $p;
  398. }
  399. if(empty($depart_has)) return [false, '无相关信息能获取到门店数据,校验失败'];
  400. if(count($depart_has) == 1){
  401. $intersection = array_values($depart_has);
  402. $intersection = $intersection[0];
  403. }else{
  404. // 提取所有的子数组
  405. $subArrays = array_map(function($item) {
  406. return $item;
  407. }, $depart_has);
  408. // 使用 array_intersect 计算交集
  409. $intersection = call_user_func_array('array_intersect', $subArrays);
  410. if(empty($intersection)) return [false, '非法数据,请刷新网页重新操作'];
  411. }
  412. foreach ($top_depart_id as $value){
  413. if(! in_array($value, $intersection)) return [false, '非法数据,请刷新网页重新操作'];
  414. }
  415. return [true, ''];
  416. }
  417. public function pq($data,$user){
  418. list($status,$msg) = $this->pqRule($data,$user);
  419. if(! $status) return [false,$msg];
  420. try {
  421. DB::beginTransaction();
  422. if(! empty($msg['schedule_id']) && ! empty($msg['day_start_stamp']) && ! empty($msg['day_end_stamp'])){
  423. //如果已经设置过排期 将子表占用数据释放
  424. $schedule = ScheduleInfo::where('del_time',0)
  425. ->where('schedule_id',$msg['schedule_id'])
  426. ->where('start_time',$msg['day_start_stamp'])
  427. ->where('end_time',$msg['day_end_stamp'])
  428. ->where('is_use','>', ScheduleInfo::not_use)
  429. ->first();
  430. if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  431. }
  432. Construction::where('id',$data['id'])->update([
  433. 'schedule_id' => $data['schedule_id'],
  434. 'day_stamp' => $data['day_stamp'],
  435. 'day_start_stamp' => $data['day_start_stamp'],
  436. 'day_end_stamp' => $data['day_end_stamp'],
  437. 'pq_state' => Construction::STATE_ONE
  438. ]);
  439. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  440. DB::commit();
  441. }catch (\Exception $exception){
  442. $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  443. DB::rollBack();
  444. return [false,$exception->getMessage()];
  445. }
  446. $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  447. return [true,''];
  448. }
  449. public function pqRule(&$data,$user){
  450. if(empty($data['id'])) return [false, '施工单不能为空!'];
  451. $construction = Construction::where('id',$data['id'])->where('del_time',0)->first();
  452. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  453. $construction = $construction->toArray();
  454. if($construction['state'] != Construction::STATE_TWO) return [false,'施工单未确认无法排期!'];
  455. if(empty($data['schedule_id']) ||empty($data['day_stamp']) || empty($data['day_start_stamp']) || empty($data['day_end_stamp'])) return [false,'排班时间信息不能为空'];
  456. //day_stamp 日期的时间戳(0点的) day_start_stamp 日期加上开始时分的时间戳 day_end_stamp 日期加上结束时分的时间戳
  457. if($construction['day_stamp'] != $data['day_stamp'] || $construction['day_start_stamp'] != $data['day_start_stamp'] && $construction['day_end_stamp'] != $data['day_end_stamp']) {
  458. $schedule_info = ScheduleInfo::where('del_time',0)
  459. ->where('day',$data['day_stamp'])
  460. ->where('start_time',$data['day_start_stamp'])
  461. ->where('end_time',$data['day_end_stamp'])
  462. ->where('is_use',ScheduleInfo::not_use)
  463. ->first();
  464. if(empty($schedule_info)) return [false,'该时间段排班已满或不存在!'];
  465. $data['schedule_info_id'] = $schedule_info->id;
  466. list($status,$msg) = $this->limitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  467. if(! $status) return [false,'操作频繁,请稍等!'];
  468. }else{
  469. return [false,'排期未做更新!'];
  470. }
  471. return [true, $construction];
  472. }
  473. }