| package com.zy.asrs.controller; | 
|   | 
| import com.core.annotations.ManagerAuth; | 
| import com.core.common.BaseRes; | 
| import com.core.common.Cools; | 
| import com.core.common.R; | 
| import com.zy.asrs.entity.BasDevp; | 
| import com.zy.asrs.entity.LocDetl; | 
| import com.zy.asrs.entity.OrderDetl; | 
| import com.zy.asrs.service.*; | 
| import com.zy.common.model.LocDto; | 
| import com.zy.common.model.TaskDto; | 
| import com.zy.common.web.BaseController; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.web.bind.annotation.PostMapping; | 
| import org.springframework.web.bind.annotation.RequestBody; | 
| import org.springframework.web.bind.annotation.RequestParam; | 
| import org.springframework.web.bind.annotation.RestController; | 
|   | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| /** | 
|  * Created by vincent on 2022/3/26 | 
|  */ | 
|   | 
| @RestController | 
| public class OutController extends BaseController { | 
|   | 
|     @Autowired | 
|     private OrderService orderService; | 
|     @Autowired | 
|     private OrderDetlService orderDetlService; | 
|     @Autowired | 
|     private LocDetlService locDetlService; | 
|     @Autowired | 
|     private LocMastService locMastService; | 
|     @Autowired | 
|     private StaDescService staDescService; | 
|     @Autowired | 
|     private WorkService workService; | 
|     @Autowired | 
|     private BasDevpService basDevpService; | 
|   | 
|   | 
|     @PostMapping("/out/pakout/orderDetlIds/auth") | 
|     @ManagerAuth | 
|     public R pakoutOrderDetlIds(@RequestParam Long orderId) throws InterruptedException { | 
|         Thread.sleep(200); | 
|         return R.ok().add(orderDetlService.selectByOrderId(orderId).stream().map(OrderDetl::getId).distinct().collect(Collectors.toList())); | 
|     } | 
|   | 
|     @PostMapping("/out/pakout/preview/auth") | 
|     @ManagerAuth | 
|     public R pakoutPreview(@RequestBody List<Long> ids) { | 
| //        if (Cools.isEmpty(ids)) { | 
| //            return R.parse(BaseRes.PARAM); | 
| //        } | 
| //        List<OrderDetl> orderDetls = orderDetlService.selectBatchIds(ids); | 
| //        //用户确认出入库都以pgno为准,那么不考虑数量了 | 
| //        List<LocDto> locDtos = new ArrayList<>(); | 
| //        if (orderDetls != null && !orderDetls.isEmpty()) { | 
| //            List<LocDetl> locDetls = locDetlService.queryStock(orderDetls.get(0).getSPgNO()); | 
| //            for (LocDetl locDetl : locDetls) { | 
| //                LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetls.get(0).getOrderNo(), | 
| //                        locDetl.getAnfme()); | 
| //                List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), 101); | 
| //                locDto.setStaNos(staNos); | 
| //                locDto.setZpallet(locDetl.getZpallet()); | 
| //                locDto.setSPgNO(orderDetls.get(0).getSPgNO()); | 
| //                locDtos.add(locDto); | 
| //            } | 
| //        } | 
| //        return R.ok().add(locDtos); | 
|   | 
|         if (Cools.isEmpty(ids)) { | 
|             return R.parse(BaseRes.PARAM); | 
|         } | 
|         List<OrderDetl> orderDetls = orderDetlService.selectBatchIds(ids); | 
|         List<LocDto> locDtos = new ArrayList<>(); | 
| //        orderDetls = orderDetls.stream() | 
| //                .sorted(Comparator.comparing(OrderDetl::getMatnr).thenComparing(OrderDetl::getBatch)) | 
| //                .collect(Collectors.toList()); | 
| //        Set<String> exist = new HashSet<>(); | 
|   | 
|         for (OrderDetl orderDetl : orderDetls) { | 
|             double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getWorkQty()).orElse(0.0D); | 
|             if (issued <= 0.0D) { | 
|                 continue; | 
|             } | 
|             List<LocDetl> locDetls = locDetlService.queryStock(orderDetl); | 
|   | 
|             for (LocDetl locDetl : locDetls) { | 
|                 if (locDetl.getMatnr().equals(orderDetl.getMatnr())) { | 
|                     if (issued > 0) { | 
|                         LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|                                 issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued); | 
|                         List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), issued >= locDetl.getAnfme() ? 101 : 103); | 
|                         locDto.setStaNos(staNos); | 
|                         locDto.setModel(locDetl.getModel()); | 
|                         locDto.setOutOrderNo(locDetl.getOutOrderNo()); | 
|                         locDto.setLuHao(locDetl.getLuHao()); | 
|                         locDto.setPacking(locDetl.getPacking()); | 
|                         locDto.setProType(locDetl.getProType()); | 
|                         locDto.setSPgNO(locDetl.getSPgNO()); | 
|                         locDto.setColor(locDetl.getColor()); | 
|                         locDtos.add(locDto); | 
|                         //exist.add(locDetl.getLocNo()); | 
|                         // 剩余待出数量递减 | 
|                         issued = issued - locDetl.getAnfme(); | 
|                     } else { | 
|                         break; | 
|                     } | 
|                 } | 
|             } | 
|             if (issued > 0) { | 
|                 LocDto locDto = new LocDto(null, orderDetl.getMatnr(), orderDetl.getMaktx(), orderDetl.getBatch(), orderDetl.getOrderNo(), issued); | 
|                 locDto.setLack(Boolean.TRUE); | 
|                 locDto.setModel(orderDetl.getModel()); | 
|                 locDto.setOutOrderNo(orderDetl.getOutOrderNo()); | 
|                 locDto.setLuHao(orderDetl.getLuHao()); | 
|                 locDto.setPacking(orderDetl.getPacking()); | 
|                 locDto.setProType(orderDetl.getProType()); | 
|                 locDto.setSPgNO(orderDetl.getSPgNO()); | 
|                 locDto.setColor(orderDetl.getColor()); | 
|                 locDtos.add(locDto); | 
|             } | 
|         } | 
|         return R.ok().add(locDtos); | 
|     } | 
|   | 
|     @PostMapping("/out/pakout/auth") | 
|     @ManagerAuth(memo = "订单出库") | 
|     public synchronized R pakout(@RequestBody List<LocDto> locDtos) throws InterruptedException { | 
|         if (Cools.isEmpty(locDtos)) { | 
|             return R.parse(BaseRes.PARAM); | 
|         } | 
|         boolean lack = true; | 
|         for (LocDto locDto : locDtos) { | 
|             if (!locDto.isLack()) { | 
|                 lack = false; | 
|                 break; | 
|             } | 
|         } | 
|         if (lack) { | 
|             return R.error("库存不足"); | 
|         } | 
|   | 
|         Thread.sleep(1000L); | 
|   | 
|         List<TaskDto> taskDtos = new ArrayList<>(); | 
|         // 根据 (库位 & 出库站) 分组; 理想状态:一组为一次出库任务 | 
|         for (LocDto locDto : locDtos) { | 
|             if (locDto.isLack()) { | 
|                 continue; | 
|             } | 
|             TaskDto taskDto = new TaskDto(locDto.getLocNo(), locDto.getStaNo(), locDto); | 
|             if (TaskDto.has(taskDtos, taskDto)) { | 
|                 TaskDto dto = TaskDto.find(taskDtos, taskDto); | 
|                 assert dto != null; | 
|                 dto.getLocDtos().addAll(taskDto.getLocDtos()); | 
|             } else { | 
|                 taskDtos.add(taskDto); | 
|             } | 
|         } | 
|         // ----------------------------------------------------------------------------------------------- | 
|         for (TaskDto taskDto : taskDtos) { | 
|             BasDevp staNo = basDevpService.checkSiteStatus(taskDto.getStaNo()); | 
|             workService.stockOut(staNo, taskDto, getUserId()); | 
|         } | 
|         return R.ok(); | 
|     } | 
|   | 
| } |