DeleteService.php 15 KB

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