|  |  | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
 |  |  | import com.baomidou.mybatisplus.mapper.Wrapper; | 
 |  |  | import com.core.common.BaseRes; | 
 |  |  | import com.core.common.Cools; | 
 |  |  | import com.core.common.DateUtils; | 
 |  |  | import com.core.common.SnowflakeIdWorker; | 
 |  |  | 
 |  |  |  | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.concurrent.TimeUnit; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * Created by vincent on 2020/6/11 | 
 |  |  | 
 |  |  |     @Transactional | 
 |  |  |     public String startupFullPutStore(FullStoreParam param, Long userId) { | 
 |  |  |         // 参数非空判断 | 
 |  |  |         if (Cools.isEmpty(param.getDevpNo(), param.getList())) { | 
 |  |  |             throw new CoolException(BaseRes.PARAM); | 
 |  |  |         } | 
 |  |  |         Date now = new Date(); | 
 |  |  |         // 源站点状态检测 | 
 |  |  |         BasDevp sourceStaNo = basDevpService.checkSiteStatus(param.getDevpNo(), true); | 
 |  |  |         // 检索库位 | 
 |  |  |         LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo); | 
 |  |  |         List<String> matnrs = param.getList().stream().map(FullStoreParam.MatCodeStore::getMatnr).distinct().collect(Collectors.toList()); | 
 |  |  |         List<String> batchs = param.getList().stream().map(FullStoreParam.MatCodeStore::getBatch).distinct().collect(Collectors.toList()); | 
 |  |  |         StartupDto dto = commonService.getLocNo(DEFAULT_ROW_NO_TYPE, 1, param.getDevpNo(), matnrs.get(0), batchs.get(0), null, locTypeDto); | 
 |  |  |         // 生成工作号 | 
 |  |  |         int workNo = dto.getWorkNo(); | 
 |  |  |         // 生成工作档 | 
 |  |  |         WrkMast wrkMast = new WrkMast(); | 
 |  |  |         wrkMast.setWrkNo(workNo); | 
 |  |  |         wrkMast.setIoTime(now); | 
 |  |  |         wrkMast.setWrkSts(1L); // 工作状态:生成入库ID | 
 |  |  |         wrkMast.setIoType(1); // 入出库状态:1.入库 | 
 |  |  |         wrkMast.setIoPri(13D); // 优先级:13 | 
 |  |  |         wrkMast.setCrnNo(dto.getCrnNo()); | 
 |  |  |         wrkMast.setSourceStaNo(dto.getSourceStaNo()); | 
 |  |  |         wrkMast.setStaNo(dto.getStaNo()); | 
 |  |  |         wrkMast.setLocNo(dto.getLocNo()); | 
 |  |  |         wrkMast.setBarcode(param.getBarcode()); // 托盘码 | 
 |  |  |         wrkMast.setFullPlt("Y"); // 满板:Y | 
 |  |  |         wrkMast.setPicking("N"); // 拣料 | 
 |  |  |         wrkMast.setExitMk("N"); // 退出 | 
 |  |  |         wrkMast.setEmptyMk("N"); // 空板 | 
 |  |  |         wrkMast.setLinkMis("N"); | 
 |  |  |         wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型 | 
 |  |  |         wrkMast.setAppeUser(userId); | 
 |  |  |         wrkMast.setAppeTime(now); | 
 |  |  |         wrkMast.setModiUser(userId); | 
 |  |  |         wrkMast.setModiTime(now); | 
 |  |  |         if (!wrkMastService.insert(wrkMast)) { | 
 |  |  |             throw new CoolException("保存工作档失败"); | 
 |  |  |         } | 
 |  |  |         // 生成工作档明细 | 
 |  |  |         List<DetlDto> detlDtos = new ArrayList<>(); | 
 |  |  |         param.getList().forEach(elem -> { | 
 |  |  |             DetlDto detlDto = new DetlDto(elem.getMatnr(), elem.getBatch(), elem.getAnfme()); | 
 |  |  |             if (DetlDto.has(detlDtos, detlDto)) { | 
 |  |  |                 DetlDto detlDto1 = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  |                 assert detlDto1 != null; | 
 |  |  |                 detlDto1.setAnfme(detlDto1.getAnfme() + detlDto.getAnfme()); | 
 |  |  |             } else { | 
 |  |  |                 detlDtos.add(detlDto); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |         wrkDetlService.createWorkDetail(workNo, detlDtos, param.getBarcode(), userId, now); | 
 |  |  |         // 更新源站点信息 | 
 |  |  |         sourceStaNo.setWrkNo(workNo); | 
 |  |  |         sourceStaNo.setModiUser(userId); | 
 |  |  |         sourceStaNo.setModiTime(now); | 
 |  |  |         if (!basDevpService.updateById(sourceStaNo)) { | 
 |  |  |             throw new CoolException("更新源站失败"); | 
 |  |  |         } | 
 |  |  |         // 更新目标库位状态 | 
 |  |  |         LocMast locMast = locMastService.selectById(dto.getLocNo()); | 
 |  |  |         if (locMast.getLocSts().equals("O")) { | 
 |  |  |             locMast.setLocSts("S"); // S.入库预约 | 
 |  |  |             locMast.setModiUser(userId); | 
 |  |  |             locMast.setModiTime(now); | 
 |  |  |             if (!locMastService.updateById(locMast)) { | 
 |  |  |                 throw new CoolException("改变库位状态失败"); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             throw new CoolException(dto.getLocNo() + "目标库位已被占用"); | 
 |  |  |         } | 
 |  |  |         return dto.getLocNo(); | 
 |  |  | //        if (Cools.isEmpty(param.getDevpNo(), param.getList())) { | 
 |  |  | //            throw new CoolException(BaseRes.PARAM); | 
 |  |  | //        } | 
 |  |  | //        Date now = new Date(); | 
 |  |  | //        // 源站点状态检测 | 
 |  |  | //        BasDevp sourceStaNo = basDevpService.checkSiteStatus(param.getDevpNo(), true); | 
 |  |  | //        // 检索库位 | 
 |  |  | //        LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo); | 
 |  |  | //        List<String> matnrs = param.getList().stream().map(FullStoreParam.MatCodeStore::getMatnr).distinct().collect(Collectors.toList()); | 
 |  |  | //        List<String> batchs = param.getList().stream().map(FullStoreParam.MatCodeStore::getBatch).distinct().collect(Collectors.toList()); | 
 |  |  | //        StartupDto dto = commonService.getLocNo(DEFAULT_ROW_NO_TYPE, 1, param.getDevpNo(), matnrs.get(0), batchs.get(0), null, locTypeDto); | 
 |  |  | //        // 生成工作号 | 
 |  |  | //        int workNo = dto.getWorkNo(); | 
 |  |  | //        // 生成工作档 | 
 |  |  | //        WrkMast wrkMast = new WrkMast(); | 
 |  |  | //        wrkMast.setWrkNo(workNo); | 
 |  |  | //        wrkMast.setIoTime(now); | 
 |  |  | //        wrkMast.setWrkSts(1L); // 工作状态:生成入库ID | 
 |  |  | //        wrkMast.setIoType(1); // 入出库状态:1.入库 | 
 |  |  | //        wrkMast.setIoPri(13D); // 优先级:13 | 
 |  |  | //        wrkMast.setCrnNo(dto.getCrnNo()); | 
 |  |  | //        wrkMast.setSourceStaNo(dto.getSourceStaNo()); | 
 |  |  | //        wrkMast.setStaNo(dto.getStaNo()); | 
 |  |  | //        wrkMast.setLocNo(dto.getLocNo()); | 
 |  |  | //        wrkMast.setBarcode(param.getBarcode()); // 托盘码 | 
 |  |  | //        wrkMast.setFullPlt("Y"); // 满板:Y | 
 |  |  | //        wrkMast.setPicking("N"); // 拣料 | 
 |  |  | //        wrkMast.setExitMk("N"); // 退出 | 
 |  |  | //        wrkMast.setEmptyMk("N"); // 空板 | 
 |  |  | //        wrkMast.setLinkMis("N"); | 
 |  |  | //        wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型 | 
 |  |  | //        wrkMast.setAppeUser(userId); | 
 |  |  | //        wrkMast.setAppeTime(now); | 
 |  |  | //        wrkMast.setModiUser(userId); | 
 |  |  | //        wrkMast.setModiTime(now); | 
 |  |  | //        if (!wrkMastService.insert(wrkMast)) { | 
 |  |  | //            throw new CoolException("保存工作档失败"); | 
 |  |  | //        } | 
 |  |  | //        // 生成工作档明细 | 
 |  |  | //        List<DetlDto> detlDtos = new ArrayList<>(); | 
 |  |  | //        param.getList().forEach(elem -> { | 
 |  |  | //            DetlDto detlDto = new DetlDto(elem); | 
 |  |  | //            if (DetlDto.has(detlDtos, detlDto)) { | 
 |  |  | //                DetlDto detlDto1 = DetlDto.find(detlDtos, detlDto.getMatnr(), detlDto.getBatch()); | 
 |  |  | //                assert detlDto1 != null; | 
 |  |  | //                detlDto1.setAnfme(detlDto1.getAnfme() + detlDto.getAnfme()); | 
 |  |  | //            } else { | 
 |  |  | //                detlDtos.add(detlDto); | 
 |  |  | //            } | 
 |  |  | //        }); | 
 |  |  | //        wrkDetlService.createWorkDetail(workNo, detlDtos, param.getBarcode(), userId, now); | 
 |  |  | //        // 更新源站点信息 | 
 |  |  | //        sourceStaNo.setWrkNo(workNo); | 
 |  |  | //        sourceStaNo.setModiUser(userId); | 
 |  |  | //        sourceStaNo.setModiTime(now); | 
 |  |  | //        if (!basDevpService.updateById(sourceStaNo)) { | 
 |  |  | //            throw new CoolException("更新源站失败"); | 
 |  |  | //        } | 
 |  |  | //        // 更新目标库位状态 | 
 |  |  | //        LocMast locMast = locMastService.selectById(dto.getLocNo()); | 
 |  |  | //        if (locMast.getLocSts().equals("O")) { | 
 |  |  | //            locMast.setLocSts("S"); // S.入库预约 | 
 |  |  | //            locMast.setModiUser(userId); | 
 |  |  | //            locMast.setModiTime(now); | 
 |  |  | //            if (!locMastService.updateById(locMast)) { | 
 |  |  | //                throw new CoolException("改变库位状态失败"); | 
 |  |  | //            } | 
 |  |  | //        } else { | 
 |  |  | //            throw new CoolException(dto.getLocNo() + "目标库位已被占用"); | 
 |  |  | //        } | 
 |  |  | //        return dto.getLocNo(); | 
 |  |  |         return null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |         List<LocDetlDto> locDetlDtos = new ArrayList<>(); | 
 |  |  |         for (StockOutParam.LocDetl paramLocDetl : param.getLocDetls()) { | 
 |  |  |             if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) { | 
 |  |  |                 LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch()); | 
 |  |  |                 LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl); | 
 |  |  |                 if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount())); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             log.info("工作档明细:{},{},{},{}", locDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch(),locDto.getAnfme()); | 
 |  |  |             OrderDetl orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch(), locDto.getAnfme()); | 
 |  |  |             if (orderDetl == null) { | 
 |  |  |                 orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), null, locDto.getAnfme()); | 
 |  |  |             OrderDetl orderDetl = orderDetlService.selectItem(locDto); | 
 |  |  |  | 
 |  |  |             WrkDetl wrkDetl = new WrkDetl(); | 
 |  |  |             wrkDetl.sync(orderDetl); | 
 |  |  |             wrkDetl.setZpallet(wrkMast.getBarcode()); | 
 |  |  |             wrkDetl.setIoTime(now); | 
 |  |  |             wrkDetl.setWrkNo(workNo); | 
 |  |  |             //wrkDetl.setBatch(locDto.getBatch()); | 
 |  |  |             wrkDetl.setOrderNo(locDto.getOrderNo()); | 
 |  |  |             wrkDetl.setAnfme(locDto.getAnfme()); // 数量 | 
 |  |  |             wrkDetl.setAppeTime(now); | 
 |  |  |             wrkDetl.setAppeUser(userId); | 
 |  |  |             wrkDetl.setModiTime(now); | 
 |  |  |             wrkDetl.setModiUser(userId); | 
 |  |  |             if (!wrkDetlService.insert(wrkDetl)) { | 
 |  |  |                 throw new CoolException("保存工作档明细失败"); | 
 |  |  |             } | 
 |  |  |             // 修改订单明细 | 
 |  |  |             if (!orderDetlService.increaseWorkQty(orderDetl, locDto.getAnfme())) { | 
 |  |  |                 throw new CoolException("修改订单明细数量失败"); | 
 |  |  |             } | 
 |  |  |             orderService.updateSettle(orderDetl.getOrderId(), 2L, userId); | 
 |  |  |         } | 
 |  |  |         // 修改库位状态:   F.在库 ====>>> R.出库预约/P.拣料/盘点/并板出库中 | 
 |  |  |         locMast = locMastService.selectById(taskDto.getLocNo()); | 
 |  |  |         if (locMast.getLocSts().equals("F")) { | 
 |  |  |             locMast.setLocSts(ioType == 101 ? "R" : "P"); | 
 |  |  |             locMast.setModiUser(userId); | 
 |  |  |             locMast.setModiTime(now); | 
 |  |  |             if (!locMastService.updateById(locMast)) { | 
 |  |  |                 throw new CoolException("预约库位状态失败,库位号:" + taskDto.getLocNo()); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             throw new CoolException(taskDto.getLocNo() + "库位不是在库状态"); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void stockOut(BasDevp staNo, TaskDto taskDto, Long userId, String endLocation) { | 
 |  |  |         Date now = new Date(); | 
 |  |  |         List<LocDto> locDtos = taskDto.getLocDtos(); | 
 |  |  |         for (LocDto locDto : locDtos) { | 
 |  |  |             if (!taskDto.getLocNo().equals(locDto.getLocNo()) && !taskDto.getStaNo().equals(locDto.getStaNo())) { | 
 |  |  |                 throw new CoolException("订单出库异常,请联系管理员"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         // 获取库位 | 
 |  |  |         LocMast locMast = locMastService.selectById(taskDto.getLocNo()); | 
 |  |  |         // 获取路径 | 
 |  |  |         int ioType = taskDto.isAll() ? 101 : 103; | 
 |  |  |         StaDesc staDesc = staDescService.queryCrnStnAuto(ioType, locMast.getCrnNo(), staNo.getDevNo()); | 
 |  |  |         // 生成工作号 | 
 |  |  |         int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType)); | 
 |  |  |         // 生成工作档 | 
 |  |  |         WrkMast wrkMast = new WrkMast(); | 
 |  |  |         wrkMast.setWrkNo(workNo); | 
 |  |  |         wrkMast.setInvWh(endLocation); // 地码 | 
 |  |  |         wrkMast.setIoTime(now); | 
 |  |  |         wrkMast.setWrkSts(11L); // 工作状态:11.生成出库ID | 
 |  |  |         wrkMast.setIoType(ioType); // 入出库状态 | 
 |  |  |         wrkMast.setIoPri(13D); // 优先级:13 | 
 |  |  |         wrkMast.setCrnNo(locMast.getCrnNo()); | 
 |  |  |         wrkMast.setSourceStaNo(staDesc.getCrnStn()); // 源站 | 
 |  |  |         wrkMast.setStaNo(staDesc.getStnNo()); // 目标站 | 
 |  |  |         wrkMast.setSourceLocNo(taskDto.getLocNo()); // 源库位 | 
 |  |  |         wrkMast.setFullPlt("Y"); // 满板:Y | 
 |  |  |         wrkMast.setPicking("N"); // 拣料 | 
 |  |  |         wrkMast.setExitMk("N"); // 退出 | 
 |  |  |         wrkMast.setEmptyMk("N"); // 空板 | 
 |  |  |         wrkMast.setLinkMis("N"); | 
 |  |  |         wrkMast.setBarcode(locMast.getBarcode()); | 
 |  |  |         wrkMast.setAppeUser(userId); // 操作人员数据 | 
 |  |  |         wrkMast.setAppeTime(now); | 
 |  |  |         wrkMast.setModiUser(userId); | 
 |  |  |         wrkMast.setModiTime(now); | 
 |  |  |         if (!wrkMastService.insert(wrkMast)) { | 
 |  |  |             throw new CoolException("保存工作档失败,出库库位号:" + taskDto.getLocNo()); | 
 |  |  |         } | 
 |  |  |         // 生成工作档明细 | 
 |  |  |         for (LocDto locDto : taskDto.getLocDtos()) { | 
 |  |  |             if (locDto.getAnfme() == null || locDto.getAnfme() <= 0.0D) { | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             log.info("工作档明细:{},{},{},{}", locDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch(),locDto.getAnfme()); | 
 |  |  |             OrderDetl orderDetl = orderDetlService.selectItem(locDto); | 
 |  |  |  | 
 |  |  |             WrkDetl wrkDetl = new WrkDetl(); | 
 |  |  |             wrkDetl.sync(orderDetl); | 
 |  |  |             wrkDetl.setZpallet(wrkMast.getBarcode()); | 
 |  |  | 
 |  |  |         List<LocDetlDto> locDetlDtos = new ArrayList<>(); | 
 |  |  |         for (StockOutParam.LocDetl paramLocDetl : param.getLocDetls()) { | 
 |  |  |             if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount())) { | 
 |  |  |                 LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch()); | 
 |  |  |                 LocDetl one = locDetlService.selectItem(paramLocDetl.getLocNo(), paramLocDetl); | 
 |  |  |                 if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount())); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  | 
 |  |  |         if (Cools.isEmpty(wrkMast)) { | 
 |  |  |             throw new CoolException(workNo + "工作档不存在"); | 
 |  |  |         } | 
 |  |  |         if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 14) { | 
 |  |  |         if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() >= 14) { | 
 |  |  |             throw new CoolException("当前工作档已完成"); | 
 |  |  |         } | 
 |  |  |         // 入库 + 库位转移 | 
 |  |  | 
 |  |  |                     if (!locDetl.getAnfme().equals(adjust.getCount())) { | 
 |  |  |                         // todo 盘点记录 | 
 |  |  |                         // 修改库存 | 
 |  |  |                         if (!locDetlService.updateAnfme(adjust.getCount(), locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch())) { | 
 |  |  |                         if (!locDetlService.updateAnfme(adjust.getCount(), locDetl.getLocNo(), locDetl)) { | 
 |  |  |                             throw new CoolException(locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "批号修改数量失败"); | 
 |  |  |                         } | 
 |  |  |                         // 保存调整记录 | 
 |  |  | 
 |  |  |         // 删除库存 | 
 |  |  |         for (LocDetl locDetl : locDetls) { | 
 |  |  |             // todo 盘点记录 | 
 |  |  |             if (!locDetlService.updateAnfme(-1.0D, locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch())) { | 
 |  |  |             if (!locDetlService.updateAnfme(-1.0D, locDetl.getLocNo(), locDetl)) { | 
 |  |  |                 throw new CoolException("删除" + locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "批号库存明细失败"); | 
 |  |  |             } | 
 |  |  |             // 保存调整记录 |