DeleteService.php 17 KB

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