DeleteService.php 19 KB

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