|  |  |  | 
|---|
|  |  |  | 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.OrderDetl; | 
|---|
|  |  |  | import com.zy.asrs.service.LocDetlService; | 
|---|
|  |  |  | import com.zy.asrs.service.OrderDetlService; | 
|---|
|  |  |  | import com.zy.asrs.service.OrderService; | 
|---|
|  |  |  | import com.zy.common.model.DetlDto; | 
|---|
|  |  |  | import com.zy.asrs.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.service.*; | 
|---|
|  |  |  | import com.zy.common.model.LocDto; | 
|---|
|  |  |  | import com.zy.common.model.OrderDto; | 
|---|
|  |  |  | import com.zy.common.model.TaskDto; | 
|---|
|  |  |  | import com.zy.common.web.BaseController; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | 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.HashSet; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 2022/3/26 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | Set<DetlDto> detlDtos = new HashSet<>(); | 
|---|
|  |  |  | List<LocDto> locDtos = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Set<String> exist = new HashSet<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (OrderDetl orderDetl : orderDetls) { | 
|---|
|  |  |  | if (DetlDto.hasList(detlDtos, orderDetl)) { | 
|---|
|  |  |  | DetlDto detlDto = DetlDto.find(detlDtos, orderDetl.getMatnr(), orderDetl.getBatch()); | 
|---|
|  |  |  | assert detlDto != null; | 
|---|
|  |  |  | detlDto.setAnfme(detlDto.getAnfme() + orderDetl.getAnfme()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | detlDtos.add(new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getAnfme())); | 
|---|
|  |  |  | double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getQty()).orElse(0.0D); | 
|---|
|  |  |  | if (issued <= 0.0D) { continue; } | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), null, exist); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getOrderNo()); | 
|---|
|  |  |  | return R.ok().add(locDtos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok().add(orderDetls); | 
|---|
|  |  |  | @PostMapping("/out/pakout/auth") | 
|---|
|  |  |  | @ManagerAuth(memo = "订单出库") | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | 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(500L); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 订单预校验  ===>> 1.订单状态; 2.订单带出数量 | 
|---|
|  |  |  | List<OrderDto> orderDtos = new ArrayList<>(); | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | if (Cools.isEmpty(locDto.getOrderNo())) { continue; } | 
|---|
|  |  |  | OrderDto orderDto = new OrderDto(locDto.getOrderNo(), locDto.getMatnr(), locDto.getAnfme()); | 
|---|
|  |  |  | if (OrderDto.has(orderDtos, orderDto)) { | 
|---|
|  |  |  | OrderDto dto = OrderDto.find(orderDtos, orderDto); | 
|---|
|  |  |  | assert dto != null; | 
|---|
|  |  |  | dto.setAnfme(dto.getAnfme() + orderDto.getAnfme()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | orderDtos.add(orderDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (OrderDto orderDto : orderDtos) { | 
|---|
|  |  |  | Order order = orderService.selectByNo(orderDto.getOrderNo()); | 
|---|
|  |  |  | if (order.getSettle() > 2) { | 
|---|
|  |  |  | return R.error(orderDto.getOrderNo() + "订单已失效,请及时刷新页面"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | OrderDetl orderDetl = orderDetlService.selectItem(orderDto.getOrderNo(), orderDto.getMatnr(), null); | 
|---|
|  |  |  | if (orderDetl.getAnfme() - orderDetl.getQty() < orderDto.getAnfme()) { | 
|---|
|  |  |  | return R.ok(orderDto.getOrderNo() + "订单已作业,请及时刷新页面"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskDto> taskDtos = new ArrayList<>(); | 
|---|
|  |  |  | // 根据 (库位 & 出库站) 分组; 理想状态:一组为一次出库任务 | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | if (locDto.isLack()) { continue; } | 
|---|
|  |  |  | // 防止前端页面提取库位信息后,在其他地方对该库位生成了出库任务(库位状态非F状态) | 
|---|
|  |  |  | LocMast locMast = locMastService.selectById(locDto.getLocNo()); | 
|---|
|  |  |  | if(!Cools.isEmpty(locMast) && !locMast.getLocSts().equals("F")){ | 
|---|
|  |  |  | return R.error("库位号非在库状态,请重新选择出库库位===>>" + locDto.getLocNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // ----------------------------------------------------------------------------------------------- | 
|---|
|  |  |  | List<String> excludeLocNos = taskDtos.stream().map(TaskDto::getLocNo).distinct().collect(Collectors.toList()); | 
|---|
|  |  |  | for (TaskDto taskDto : taskDtos) { | 
|---|
|  |  |  | BasDevp staNo = basDevpService.checkSiteStatus(taskDto.getStaNo()); | 
|---|
|  |  |  | workService.stockOut(staNo, taskDto, getUserId()); | 
|---|
|  |  |  | locMastService.breakUp(taskDto.getLocNo(), excludeLocNos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|