DeleteService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. //负责人清除
  131. CustomerInfo::where('del_time',0)
  132. ->whereIn('customer_id',$data['id'])
  133. ->where('type', CustomerInfo::type_two)
  134. ->update(['del_time' => $time]);
  135. foreach ($data['man'] as $value){
  136. foreach ($data['id'] as $data_id){
  137. //负责人累加
  138. $insert[] = [
  139. 'customer_id' => $data_id,
  140. 'data_id' => $value,
  141. 'type' => CustomerInfo::type_two,
  142. 'crt_time' => $time,
  143. ];
  144. if(! in_array($data_id, $xt)){
  145. //协同人累加
  146. $insert[] = [
  147. 'customer_id' =>$data_id,
  148. 'data_id' => $user['id'],
  149. 'type' => CustomerInfo::type_three,
  150. 'crt_time' => $time,
  151. ];
  152. }
  153. }
  154. }
  155. CustomerInfo::insert($insert);
  156. $fp_top_depart_id = $data['fp_top_depart_id'] ?? 0;
  157. if(! is_array($data['id'])){
  158. $title = Customer::where('id',$data['id'])->value('title') ?? "";
  159. (new OrderOperationService())->add([
  160. 'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,
  161. 'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",
  162. 'type' => OrderOperation::twenty_three
  163. ],$user);
  164. Customer::where('id',$data['id'])->update(['fp_time' => $time, 'fp_top_depart_id' => $fp_top_depart_id]);
  165. }else{
  166. $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();
  167. foreach($data['id'] as $value){
  168. $t_title = $title[$value] ?? "";
  169. (new OrderOperationService())->add([
  170. 'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,
  171. 'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",
  172. 'type' => OrderOperation::twenty_three
  173. ],$user);
  174. }
  175. Customer::whereIn('id',$data['id'])->update(['fp_time' => $time,'fp_top_depart_id' => $fp_top_depart_id]);
  176. }
  177. // foreach ($data['man'] as $value){
  178. // $tmp_data = [
  179. // "测试",
  180. // '测试',
  181. // '审核通过',
  182. // '测试',
  183. // date('Y-m-d H:i:s'),
  184. // ];
  185. // (new OaService())->sendWxMsg($value,2,0,16,$tmp_data);
  186. // }
  187. }
  188. }
  189. public function fpConstructionMan($data,$user){
  190. $time = time();
  191. //负责人清除
  192. ConstructionInfo::where('del_time',0)
  193. ->where('construction_id',$data['id'])
  194. ->where('type', ConstructionInfo::type_three)
  195. ->update(['del_time' => $time]);
  196. if(! empty($data['man'])){
  197. $insert = [];
  198. foreach ($data['man'] as $value){
  199. $insert[] = [
  200. 'construction_id' => $data['id'],
  201. 'employee_id' => $value,
  202. 'type' => ConstructionInfo::type_three,
  203. 'crt_time' => $time,
  204. ];
  205. }
  206. ConstructionInfo::insert($insert);
  207. $order = Construction::where('id',$data['id'])->first();
  208. $order = $order->toArray();
  209. (new OrderOperationService())->add([
  210. 'order_number' => $order['order_number'],
  211. 'msg' => OrderOperation::$type[OrderOperation::twenty_six] ?? "",
  212. 'type' => OrderOperation::twenty_six
  213. ],$user);
  214. }
  215. }
  216. public function yj($data,$user){
  217. if(empty($data['id']) || empty($data['type']) || empty($data['man'])) return [false, '必填参数不能为空!'];
  218. // $key = "fpyj".$data['id'].$data['type'];
  219. // list($status,$msg) = $this->limitingSendRequestBackgExpire($key);
  220. // if(! $status) return [false,$msg];
  221. try {
  222. DB::beginTransaction();
  223. if($data['type'] == 1){
  224. $this->yjSaleOrderMan($data,$user);
  225. }elseif ($data['type'] == 2){
  226. $this->yjCustomerMan($data,$user);
  227. }
  228. DB::commit();
  229. }catch (\Exception $exception){
  230. DB::rollBack();
  231. return [false,$exception->getMessage()];
  232. }
  233. return [true,''];
  234. }
  235. public function yjSaleOrderMan($data,$user){
  236. $time = time();
  237. SalesOrderInfo::where('del_time',0)
  238. ->where('sales_order_id',$data['id'])
  239. ->where('type', SalesOrderInfo::type_two)
  240. ->where('data_id', $user['id'])
  241. ->update(['del_time' => $time]);
  242. if(! empty($data['man'])){
  243. $insert = [];
  244. foreach ($data['man'] as $value){
  245. $insert[] = [
  246. 'sales_order_id' => $data['id'],
  247. 'data_id' => $value,
  248. 'type' => SalesOrderInfo::type_two,
  249. 'crt_time' => $time,
  250. ];
  251. }
  252. SalesOrderInfo::insert($insert);
  253. $order = SalesOrder::where('id',$data['id'])->first();
  254. $order = $order->toArray();
  255. (new OrderOperationService())->add([
  256. 'order_number' => $order['order_number'],
  257. 'msg' => OrderOperation::$type[OrderOperation::nineteen] ?? "",
  258. 'type' => OrderOperation::nineteen
  259. ],$user);
  260. }
  261. }
  262. public function yjCustomerMan($data,$user){
  263. $time = time();
  264. if(! is_array($data['id'])) $data['id'] = [$data['id']];
  265. CustomerInfo::where('del_time',0)
  266. ->whereIn('customer_id',$data['id'])
  267. ->where('type', CustomerInfo::type_two)
  268. ->update(['del_time' => $time]);
  269. if(! empty($data['man'])){
  270. $insert = [];
  271. foreach ($data['man'] as $value){
  272. if(! is_array($data['id'])){
  273. $insert[] = [
  274. 'customer_id' => $data['id'],
  275. 'data_id' => $value,
  276. 'type' => CustomerInfo::type_two,
  277. 'crt_time' => $time,
  278. ];
  279. }else{
  280. foreach ($data['id'] as $data_id){
  281. $insert[] = [
  282. 'customer_id' => $data_id,
  283. 'data_id' => $value,
  284. 'type' => CustomerInfo::type_two,
  285. 'crt_time' => $time,
  286. ];
  287. }
  288. }
  289. }
  290. CustomerInfo::insert($insert);
  291. $fp_top_depart_id = $data['fp_top_depart_id'] ?? 0;
  292. if(! is_array($data['id'])){
  293. $title = Customer::where('id',$data['id'])->value('title') ?? "";
  294. (new OrderOperationService())->add([
  295. 'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,
  296. 'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",
  297. 'type' => OrderOperation::twenty_four
  298. ],$user);
  299. Customer::where('id',$data['id'])->update(['fp_time' => $time, 'fp_top_depart_id' => $fp_top_depart_id]);
  300. }else{
  301. $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();
  302. foreach($data['id'] as $value){
  303. $t_title = $title[$value] ?? "";
  304. (new OrderOperationService())->add([
  305. 'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,
  306. 'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",
  307. 'type' => OrderOperation::twenty_four
  308. ],$user);
  309. }
  310. Customer::whereIn('id',$data['id'])->update(['fp_time' => $time,'fp_top_depart_id' => $fp_top_depart_id]);
  311. }
  312. }
  313. }
  314. public function pq($data,$user){
  315. list($status,$msg) = $this->pqRule($data,$user);
  316. if(! $status) return [false,$msg];
  317. try {
  318. DB::beginTransaction();
  319. if(! empty($msg['schedule_id']) && ! empty($msg['day_start_stamp']) && ! empty($msg['day_end_stamp'])){
  320. //如果已经设置过排期 将子表占用数据释放
  321. $schedule = ScheduleInfo::where('del_time',0)
  322. ->where('schedule_id',$msg['schedule_id'])
  323. ->where('start_time',$msg['day_start_stamp'])
  324. ->where('end_time',$msg['day_end_stamp'])
  325. ->where('is_use','>', ScheduleInfo::not_use)
  326. ->first();
  327. if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);
  328. }
  329. Construction::where('id',$data['id'])->update([
  330. 'schedule_id' => $data['schedule_id'],
  331. 'day_stamp' => $data['day_stamp'],
  332. 'day_start_stamp' => $data['day_start_stamp'],
  333. 'day_end_stamp' => $data['day_end_stamp'],
  334. 'pq_state' => Construction::STATE_ONE
  335. ]);
  336. if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);
  337. DB::commit();
  338. }catch (\Exception $exception){
  339. $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  340. DB::rollBack();
  341. return [false,$exception->getMessage()];
  342. }
  343. $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  344. return [true,''];
  345. }
  346. public function pqRule(&$data,$user){
  347. if(empty($data['id'])) return [false, '施工单不能为空!'];
  348. $construction = Construction::where('id',$data['id'])->where('del_time',0)->first();
  349. if(empty($construction)) return [false,'施工单不存在或已被删除'];
  350. $construction = $construction->toArray();
  351. if($construction['state'] != Construction::STATE_TWO) return [false,'施工单未确认无法排期!'];
  352. if(empty($data['schedule_id']) ||empty($data['day_stamp']) || empty($data['day_start_stamp']) || empty($data['day_end_stamp'])) return [false,'排班时间信息不能为空'];
  353. //day_stamp 日期的时间戳(0点的) day_start_stamp 日期加上开始时分的时间戳 day_end_stamp 日期加上结束时分的时间戳
  354. if($construction['day_stamp'] != $data['day_stamp'] || $construction['day_start_stamp'] != $data['day_start_stamp'] && $construction['day_end_stamp'] != $data['day_end_stamp']) {
  355. $schedule_info = ScheduleInfo::where('del_time',0)
  356. ->where('day',$data['day_stamp'])
  357. ->where('start_time',$data['day_start_stamp'])
  358. ->where('end_time',$data['day_end_stamp'])
  359. ->where('is_use',ScheduleInfo::not_use)
  360. ->first();
  361. if(empty($schedule_info)) return [false,'该时间段排班已满或不存在!'];
  362. $data['schedule_info_id'] = $schedule_info->id;
  363. list($status,$msg) = $this->limitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);
  364. if(! $status) return [false,'操作频繁,请稍等!'];
  365. }else{
  366. return [false,'排期未做更新!'];
  367. }
  368. return [true, $construction];
  369. }
  370. }