DeleteService.php 15 KB

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