DeleteService.php 14 KB

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