| | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.zy.api.controller.params.StopOutTaskParams; |
| | | import com.zy.api.service.WcsApiService; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.core.common.*; |
| | | import com.core.exception.CoolException; |
| | |
| | | import com.zy.common.constant.ArmConstant; |
| | | import com.zy.common.model.DetlDto; |
| | | import com.zy.common.model.LocDetlDto; |
| | | import com.zy.common.model.LocDto; |
| | | import com.zy.common.model.TaskDto; |
| | | import com.zy.common.model.enums.WorkNoType; |
| | | import com.zy.common.service.CommonService; |
| | | import com.zy.common.utils.HttpHandler; |
| | |
| | | |
| | | import javax.rmi.CORBA.Util; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * Created by vincent on 2022/4/9 |
| | |
| | | private WaitPakinService waitPakinService; |
| | | @Autowired |
| | | private WrkMastService wrkMastService; |
| | | @Autowired |
| | | private WcsApiService wcsApiService; |
| | | @Autowired |
| | | private WorkService workService; |
| | | @Autowired |
| | | private BasCrnpService basCrnpService; |
| | | |
| | | @Override |
| | | @Transactional |
| | |
| | | if (!orderDetlService.insert(orderDetl)) { |
| | | throw new CoolException("生成单据明细失败,请联系管理员"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R pakoutOrderPause(OpenOrderPakoutPauseParam param) { |
| | | if (param == null || Cools.isEmpty(param.getOrderId())) { |
| | | throw new CoolException("orderNo不能为空"); |
| | | } |
| | | if (param.getReason() == null) { |
| | | throw new CoolException("reason不能为空"); |
| | | } |
| | | |
| | | List<WrkMast> activeTasks = findActiveOutboundTasks(param.getOrderId()); |
| | | if (Objects.equals(param.getReason(), 1)) { |
| | | // ERP确认立即执行,仅处理待下发的出库任务。 |
| | | List<WrkMast> pendingTasks = activeTasks.stream() |
| | | .filter(wrkMast -> wrkMast != null && Objects.equals(wrkMast.getWrkSts(), 11L)) |
| | | .collect(Collectors.toList()); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("orderNo", param.getOrderId()); |
| | | result.put("taskCount", pendingTasks.size()); |
| | | if (pendingTasks.isEmpty()) { |
| | | result.put("confirmedCount", 0); |
| | | return R.ok("无有效出库任务").add(result); |
| | | } |
| | | |
| | | Date now = new Date(); |
| | | int confirmedCount = 0; |
| | | for (WrkMast wrkMast : pendingTasks) { |
| | | if (wrkMast == null || "Y".equalsIgnoreCase(wrkMast.getPdcType())) { |
| | | continue; |
| | | } |
| | | wrkMast.setPdcType("Y"); |
| | | // wrkMast.setUpdMk("ERP_CONFIRMED"); |
| | | // wrkMast.setManuType("ERP_CONFIRM_OUT"); |
| | | wrkMast.setModiTime(now); |
| | | wrkMast.setModiUser(9527L); |
| | | if (!wrkMastService.updateById(wrkMast)) { |
| | | throw new CoolException("确认执行出库任务失败: " + wrkMast.getWrkNo()); |
| | | } |
| | | confirmedCount++; |
| | | } |
| | | |
| | | result.put("confirmedCount", confirmedCount); |
| | | return R.ok(confirmedCount == 0 ? "任务已确认执行" : "ERP确认执行出库成功").add(result); |
| | | } |
| | | if (Objects.equals(param.getReason(), 2)) { |
| | | // ERP请求取消任务:未下发任务直接本地取消,已下发任务交给WCS判定可取消清单。 |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("orderNo", param.getOrderId()); |
| | | result.put("reason", param.getReason()); |
| | | result.put("taskCount", activeTasks.size()); |
| | | if (activeTasks.isEmpty()) { |
| | | result.put("successList", Collections.emptyList()); |
| | | result.put("failList", Collections.emptyList()); |
| | | result.put("cancelledCount", 0); |
| | | return R.ok("无有效出库任务").add(result); |
| | | } |
| | | |
| | | Map<String, WrkMast> taskIndex = activeTasks.stream() |
| | | .filter(wrkMast -> wrkMast != null && wrkMast.getWrkNo() != null) |
| | | .collect(Collectors.toMap( |
| | | wrkMast -> String.valueOf(wrkMast.getWrkNo()), |
| | | wrkMast -> wrkMast, |
| | | (left, right) -> left, |
| | | LinkedHashMap::new |
| | | )); |
| | | List<WrkMast> directCancelTasks = activeTasks.stream() |
| | | .filter(wrkMast -> wrkMast != null && !needNotifyWcsStop(wrkMast)) |
| | | .collect(Collectors.toList()); |
| | | List<WrkMast> wcsCancelTasks = activeTasks.stream() |
| | | .filter(this::needNotifyWcsStop) |
| | | .collect(Collectors.toList()); |
| | | |
| | | LinkedHashSet<String> successTaskNos = new LinkedHashSet<>(); |
| | | LinkedHashSet<String> failTaskNos = new LinkedHashSet<>(); |
| | | cancelOutboundTasks(directCancelTasks.stream() |
| | | .map(WrkMast::getWrkNo) |
| | | .filter(Objects::nonNull) |
| | | .map(String::valueOf) |
| | | .collect(Collectors.toList()), taskIndex, successTaskNos); |
| | | |
| | | List<String> wcsSuccessList = Collections.emptyList(); |
| | | List<String> wcsFailList = Collections.emptyList(); |
| | | if (!wcsCancelTasks.isEmpty()) { |
| | | R wcsResult = wcsApiService.pauseOutTasks(buildStopOutTaskParams(param, wcsCancelTasks)); |
| | | wcsSuccessList = extractTaskNoList(wcsResult, "successList"); |
| | | wcsFailList = extractTaskNoList(wcsResult, "failList"); |
| | | cancelOutboundTasks(wcsSuccessList, taskIndex, successTaskNos); |
| | | |
| | | LinkedHashSet<String> wcsFailTaskNos = new LinkedHashSet<>(wcsFailList); |
| | | if (wcsFailTaskNos.isEmpty()) { |
| | | for (WrkMast wrkMast : wcsCancelTasks) { |
| | | String taskNo = String.valueOf(wrkMast.getWrkNo()); |
| | | if (!successTaskNos.contains(taskNo)) { |
| | | wcsFailTaskNos.add(taskNo); |
| | | } |
| | | } |
| | | } |
| | | failTaskNos.addAll(wcsFailTaskNos); |
| | | wcsFailList = new ArrayList<>(wcsFailTaskNos); |
| | | } |
| | | |
| | | result.put("wcsTaskCount", wcsCancelTasks.size()); |
| | | result.put("wcsSuccessList", wcsSuccessList); |
| | | result.put("wcsFailList", wcsFailList); |
| | | result.put("successList", new ArrayList<>(successTaskNos)); |
| | | result.put("failList", new ArrayList<>(failTaskNos)); |
| | | result.put("cancelledCount", successTaskNos.size()); |
| | | result.put("failCount", failTaskNos.size()); |
| | | return R.ok(successTaskNos.isEmpty() ? "没有可取消任务" : "取消出库任务成功").add(result); |
| | | } |
| | | throw new CoolException("reason仅支持1或2"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R pakoutOrderExecute(OpenOrderPakoutExecuteParam param) { |
| | | if (param == null || Cools.isEmpty(param.getOrderId())) { |
| | | throw new CoolException("orderId不能为空"); |
| | | } |
| | | if (param.getExecute() == null) { |
| | | throw new CoolException("execute不能为空"); |
| | | } |
| | | if (Objects.equals(param.getExecute(), 1)) { |
| | | return createPakoutTasks(param.getOrderId()); |
| | | } |
| | | if (Objects.equals(param.getExecute(), 2)) { |
| | | OpenOrderPakoutPauseParam pauseParam = new OpenOrderPakoutPauseParam(); |
| | | pauseParam.setOrderId(param.getOrderId()); |
| | | pauseParam.setReason(2); |
| | | return pakoutOrderPause(pauseParam); |
| | | } |
| | | throw new CoolException("execute仅支持1或2"); |
| | | } |
| | | |
| | | private List<WrkMast> findActiveOutboundTasks(String orderNo) { |
| | | List<WrkDetl> wrkDetls = wrkDetlService.selectList(new EntityWrapper<WrkDetl>().eq("order_no", orderNo)); |
| | | if (wrkDetls == null || wrkDetls.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | Set<Integer> wrkNos = wrkDetls.stream() |
| | | .map(WrkDetl::getWrkNo) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| | | if (wrkNos.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>() |
| | | .in("wrk_no", wrkNos) |
| | | .in("io_type", Arrays.asList(101, 103, 104, 107, 108, 110)) |
| | | .in("wrk_sts", Arrays.asList(11L, 12L, 13L))); |
| | | if (wrkMasts == null || wrkMasts.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | wrkMasts.sort(Comparator.comparing(WrkMast::getWrkNo)); |
| | | return wrkMasts; |
| | | } |
| | | |
| | | private R createPakoutTasks(String orderNo) { |
| | | Order order = orderService.selectByNo(orderNo); |
| | | if (order == null) { |
| | | throw new CoolException("order not found: " + orderNo); |
| | | } |
| | | if (order.getSettle() != 1L && order.getSettle() != 2L) { |
| | | throw new CoolException("该订单已处理"); |
| | | } |
| | | |
| | | List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId()); |
| | | if (Cools.isEmpty(orderDetls)) { |
| | | throw new CoolException("订单明细为空"); |
| | | } |
| | | |
| | | Set<String> exist = new HashSet<>(); |
| | | List<LocDto> locDtos = new ArrayList<>(); |
| | | List<String> lackDetails = new ArrayList<>(); |
| | | for (OrderDetl orderDetl : orderDetls) { |
| | | double issued = Optional.ofNullable(orderDetl.getAnfme()).orElse(0.0D) - Optional.ofNullable(orderDetl.getWorkQty()).orElse(0.0D); |
| | | if (issued <= 0.0D) { |
| | | continue; |
| | | } |
| | | List<LocDetl> locDetls = locDetlService.queryStockAll(null, exist, |
| | | orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getBrand(), |
| | | orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), |
| | | orderDetl.getBoxType1(), orderDetl.getBoxType2()); |
| | | for (LocDetl locDetl : locDetls) { |
| | | if (issued <= 0.0D) { |
| | | break; |
| | | } |
| | | LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", locDetl.getLocNo())); |
| | | if (locMast == null) { |
| | | continue; |
| | | } |
| | | BasCrnp basCrnp = basCrnpService.selectOne(new EntityWrapper<BasCrnp>().eq("crn_no", locMast.getCrnNo())); |
| | | if (basCrnp == null || !"Y".equalsIgnoreCase(basCrnp.getOutEnable())) { |
| | | continue; |
| | | } |
| | | |
| | | double allocateQty = issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued; |
| | | LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), allocateQty); |
| | | locDto.setFrozen(locDetl.getFrozen()); |
| | | locDto.setFrozenLoc(locMast.getFrozen()); |
| | | locDto.setBrand(orderDetl.getBrand()); |
| | | locDto.setStandby1(orderDetl.getStandby1()); |
| | | locDto.setStandby2(orderDetl.getStandby2()); |
| | | locDto.setStandby3(orderDetl.getStandby3()); |
| | | locDto.setBoxType1(orderDetl.getBoxType1()); |
| | | locDto.setBoxType2(orderDetl.getBoxType2()); |
| | | locDto.setBoxType3(orderDetl.getBoxType3()); |
| | | locDto.setStaNos(staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), allocateQty >= locDetl.getAnfme() ? 101 : 103)); |
| | | if (Cools.isEmpty(locDto.getStaNos())) { |
| | | continue; |
| | | } |
| | | locDtos.add(locDto); |
| | | exist.add(locDetl.getLocNo()); |
| | | issued -= allocateQty; |
| | | } |
| | | if (issued > 0.0D) { |
| | | lackDetails.add(buildLackDetail(orderDetl, issued)); |
| | | } |
| | | } |
| | | |
| | | List<LocDto> availableLocDtos = new ArrayList<>(); |
| | | for (LocDto locDto : locDtos) { |
| | | if (locDto.getFrozen() != 1 && locDto.getFrozenLoc() != 1) { |
| | | availableLocDtos.add(locDto); |
| | | } |
| | | } |
| | | if (Cools.isEmpty(availableLocDtos)) { |
| | | throw new CoolException(Cools.isEmpty(lackDetails) ? "未生成任何出库任务" : "库存不足"); |
| | | } |
| | | |
| | | List<TaskDto> taskDtos = new ArrayList<>(); |
| | | for (LocDto locDto : availableLocDtos) { |
| | | TaskDto taskDto = new TaskDto(locDto.getLocNo(), locDto.getStaNo(), locDto); |
| | | if (TaskDto.has(taskDtos, taskDto)) { |
| | | TaskDto dto = TaskDto.find(taskDtos, taskDto); |
| | | if (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, 9527L); |
| | | } |
| | | |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("orderId", orderNo); |
| | | result.put("createdTaskCount", taskDtos.size()); |
| | | result.put("allocatedDetailCount", availableLocDtos.size()); |
| | | result.put("lackDetailCount", lackDetails.size()); |
| | | result.put("lackDetails", lackDetails); |
| | | return R.ok("execute out success").add(result); |
| | | } |
| | | |
| | | private String buildLackDetail(OrderDetl orderDetl, double lackQty) { |
| | | return orderDetl.getMatnr() + "|batch=" + (orderDetl.getBatch() == null ? "" : orderDetl.getBatch()) + "|lack=" + lackQty; |
| | | } |
| | | |
| | | private boolean needNotifyWcsStop(WrkMast wrkMast) { |
| | | return wrkMast != null |
| | | && wrkMast.getWrkSts() != null |
| | | && wrkMast.getWrkSts() >= 12L |
| | | && wrkMast.getWrkSts() < 14L; |
| | | } |
| | | |
| | | private StopOutTaskParams buildStopOutTaskParams(OpenOrderPakoutPauseParam param, List<WrkMast> wrkMasts) { |
| | | StopOutTaskParams stopParams = new StopOutTaskParams(); |
| | | // WCS取消接口仅接收 taskList.taskNo。 |
| | | for (WrkMast wrkMast : wrkMasts) { |
| | | StopOutTaskParams.TaskItem item = new StopOutTaskParams.TaskItem(); |
| | | item.setTaskNo(String.valueOf(wrkMast.getWrkNo())); |
| | | stopParams.getTasks().add(item); |
| | | } |
| | | return stopParams; |
| | | } |
| | | |
| | | private List<String> extractTaskNoList(R result, String key) { |
| | | if (result == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<String> taskNos = toTaskNoList(result.get(key)); |
| | | if (!taskNos.isEmpty()) { |
| | | return taskNos; |
| | | } |
| | | Object data = result.get("data"); |
| | | if (data instanceof Map) { |
| | | return toTaskNoList(((Map<?, ?>) data).get(key)); |
| | | } |
| | | if (data instanceof JSONObject) { |
| | | return toTaskNoList(((JSONObject) data).get(key)); |
| | | } |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | private List<String> toTaskNoList(Object value) { |
| | | if (value == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | if (value instanceof Collection) { |
| | | List<String> taskNos = new ArrayList<>(); |
| | | for (Object item : (Collection<?>) value) { |
| | | if (item != null) { |
| | | taskNos.add(String.valueOf(item)); |
| | | } |
| | | } |
| | | return taskNos; |
| | | } |
| | | return JSON.parseArray(JSON.toJSONString(value), String.class); |
| | | } |
| | | |
| | | private void cancelOutboundTasks(List<String> taskNos, Map<String, WrkMast> taskIndex, Set<String> successTaskNos) { |
| | | for (String taskNo : taskNos) { |
| | | if (Cools.isEmpty(taskNo) || successTaskNos.contains(taskNo) || !taskIndex.containsKey(taskNo)) { |
| | | continue; |
| | | } |
| | | workService.cancelWrkMast(taskNo, 9527L); |
| | | successTaskNos.add(taskNo); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | Mat mat = matService.selectByMatnr(param.getMatnr()); |
| | | if (Cools.isEmpty(mat)) { |
| | | if (param.getFull()==1){ |
| | | //满托盘 |
| | | mat = matService.selectByMatnr("1"); |
| | | }else if (param.getFull()==0){ |
| | | //空托盘 |
| | | mat = matService.selectByMatnr("0"); |
| | | } |
| | | |
| | | if (param.getFull() == 1) { |
| | | //满托盘 |
| | | mat = matService.selectByMatnr("emptyPallet"); |
| | | } else if (param.getFull() == 0) { |
| | | //空托盘 |
| | | mat = matService.selectByMatnr("emptyPallet"); |
| | | } |
| | | } |
| | | |
| | | WaitPakin waitPakin = new WaitPakin(); |
| | | waitPakin.sync(mat); |
| | | // waitPakin.setMatnr(param.getMatnr()); |
| | | waitPakin.setBatch(String.valueOf(param.getBatchId())); |
| | | waitPakin.setZpallet(param.getPalletId()); // 托盘码 |
| | | |
| | | waitPakin.setIoStatus("N"); // 入出状态 |
| | | waitPakin.setAnfme(param.getAnfme()); // 数量 |
| | | waitPakin.setStatus("Y"); // 状态 |
| | |
| | | waitPakin.setOrigin(String.valueOf(param.getStorageArea()));//建议入库区域 |
| | | waitPakin.setManu(String.valueOf(param.getLocId()));//mes具体库位编号 |
| | | waitPakin.setThreeCode(param.getBizNo()); |
| | | waitPakin.setBeBatch(param.getPackage1());//是否散货,0 非散货;1 散货;为了管控出货速率,散货可以出慢点。 |
| | | if (!waitPakinService.insert(waitPakin)) { |
| | | throw new CoolException("保存入库通知档失败"); |
| | | } |
| | |
| | | public R outOrder(OutTaskParam param) { |
| | | LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_sts", "F").eq("barcode", param.getPalletId())); |
| | | if (locMast == null) { |
| | | return R.error("没有找到托盘码="+param.getPalletId()+"对应的库位"); |
| | | return R.error("没有找到托盘码=" + param.getPalletId() + "对应的库位"); |
| | | } |
| | | Integer ioType = 101; |
| | | // 获取路径 |
| | |
| | | wrkMast.setExitMk("N"); // 退出 |
| | | wrkMast.setEmptyMk("N"); // 空板 |
| | | wrkMast.setLinkMis("N"); |
| | | wrkMast.setPdcType("N"); |
| | | wrkMast.setUserNo(param.getOrderId());//订单号 |
| | | wrkMast.setPltType(param.getSeq());//出库顺序,从1开始 |
| | | wrkMast.setTakeNone("0"); //0非自动 |
| | |
| | | wrkMast.setModiUser(9995L); |
| | | wrkMast.setModiTime(now); |
| | | if (!wrkMastService.insert(wrkMast)) { |
| | | throw new CoolException("保存工作档失败,出库库位号:"+locMast.getLocNo()); |
| | | throw new CoolException("保存工作档失败,出库库位号:" + locMast.getLocNo()); |
| | | } |
| | | List<LocDetl> locNo = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", locMast.getLocNo())); |
| | | if (locNo == null || locNo.size() == 0) { |
| | | throw new CoolException("查询库存明细失败,出库库位号:"+locMast.getLocNo()); |
| | | throw new CoolException("查询库存明细失败,出库库位号:" + locMast.getLocNo()); |
| | | } |
| | | // 生成工作档明细 |
| | | for (LocDetl locDetl : locNo) { |
| | |
| | | locMast.setModiUser(9995L); |
| | | locMast.setModiTime(now); |
| | | if (!locMastService.updateById(locMast)) { |
| | | throw new CoolException("预约库位状态失败,库位号:"+locMast.getLocNo()); |
| | | throw new CoolException("预约库位状态失败,库位号:" + locMast.getLocNo()); |
| | | } |
| | | } else { |
| | | log.error(locMast.getLocNo() + "库位不是在库状态"); |
| | |
| | | return R.ok(); |
| | | } |
| | | } |
| | | |