DeleteService.php 15 KB

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