自动化立体仓库 - WMS系统
lty
3 天以前 d907a822a940c7ec0fb7de0ca845472bda52229b
src/main/java/com/zy/asrs/service/impl/WorkServiceImpl.java
@@ -11,6 +11,7 @@
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.param.*;
import com.zy.asrs.entity.result.FindLocNoAttributeVo;
import com.zy.asrs.entity.result.WrkCancel;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
@@ -31,6 +32,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/**
@@ -106,7 +108,7 @@
//        List<String> batchs = param.getList().stream().map(FullStoreParam.MatCodeStore::getBatch).distinct().collect(Collectors.toList());
        StartupDto dto = commonService.getLocNo(1, param.getDevpNo(), matnrs.get(0), null, null,locTypeDto);
        if (Cools.isEmpty(dto)){
            throw new CoolException("查询库位失败!!==》startupFullPutStore ==》 commonService.getLocNo");
            throw new CoolException("response.query_loc_failed");
        }
        // 生成工作号
        int workNo = dto.getWorkNo();
@@ -133,7 +135,7 @@
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败");
            throw new CoolException("response.save_work_master_failed");
        }
        // 生成工作档明细
        List<DetlDto> detlDtos = new ArrayList<>();
@@ -153,7 +155,7 @@
        sourceStaNo.setModiUser(userId);
        sourceStaNo.setModiTime(now);
        if (!basDevpService.updateById(sourceStaNo)){
            throw new CoolException("更新源站失败");
            throw new CoolException("response.update_source_station_failed");
        }
        // 更新目标库位状态
        LocMast locMast = locMastService.selectById(dto.getLocNo());
@@ -162,10 +164,10 @@
            locMast.setModiUser(userId);
            locMast.setModiTime(now);
            if (!locMastService.updateById(locMast)){
                throw new CoolException("改变库位状态失败");
                throw new CoolException("response.change_loc_status_failed");
            }
        } else {
            throw new CoolException(dto.getLocNo()+"目标库位已被占用");
            throw new CoolException("response.target_loc_occupied");
        }
        return dto.getLocNo();
    }
@@ -178,8 +180,8 @@
        // 获取库位明细
        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());
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount(),paramLocDetl.getBarcode())) {
                LocDetl one = locDetlService.selectItemCarton(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch(),paramLocDetl.getBarcode());
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
@@ -187,7 +189,7 @@
            // 启动出库开始 101.出库
            stockOut(staNo, locDetlDtos, null, userId);
        } else {
            throw new CoolException("库存不存在");
            throw new CoolException("response.stock_not_exist");
        }
    }
    /**
@@ -204,7 +206,7 @@
        LocGroupOrder currentGroup = locGroupAscOrder.stream()
                .filter(g -> g.getRowList().contains(locMast.getRow1()))
                .findFirst()
                .orElseThrow(() -> new CoolException("当前排不在任何出库优先分组中,无法进行深库位检查: row=" + locMast.getRow1()));
                .orElseThrow(() -> new CoolException("response.current_row_not_in_priority_group"));
        List<Integer> orderedRows = currentGroup.getRowList();
@@ -233,7 +235,7 @@
            // 前方有货(F状态)
            if ("F".equals(prevLoc.getLocSts())) {
                throw new CoolException(locNo + " 的前方位置 " + prevLoc.getLocNo() + " 有货,禁止出库");
                throw new CoolException("response.front_loc_has_goods_forbid_out");
            }
            // 前方有入库任务(假设 io_type=100 为入库,根据实际调整)
@@ -246,24 +248,114 @@
            );
            if (frontTask != null) {
                throw new CoolException(locNo + " 的前方位置 " + prevLoc.getLocNo() + " 存在入库任务,禁止出库");
                throw new CoolException("response.front_loc_has_in_task_forbid_out");
            }
        }
        if (!foundSelf) {
            throw new CoolException("分组内未找到目标库位,请检查数据一致性: " + locNo);
            throw new CoolException("response.target_loc_not_found_in_group");
        }
    }
    private boolean isInNormalRule(LocMast lm) {
        List<LocGroupOrder> locGroupAscOrder = slaveProperties.getLocGroupAscOrder();
        if (locGroupAscOrder == null || locGroupAscOrder.isEmpty()) {
            return false;
        }
        Integer row = lm.getRow1();
        if (row == null) return false;
        return locGroupAscOrder.stream()
                .anyMatch(group ->
                        group.getRowList() != null &&
                                group.getRowList().contains(row)
                );
    }
    /**
     * 检查正常库位前方是否堵塞(深库位规则:前方排是否有货或入库任务)
     *
     * 判断依据:
     * 1. 只检查属于出库分组规则内的库位(通过分组的 rowList 确定范围)
     * 2. 前方 = 分组中索引大于当前库位的排(假设分组 rowList 已按出库顺序排列,大索引 = 前方更深)
     * 3. 堵塞条件:前方有货(F状态)或有入库任务
     *
     * @param normalMasts 需要检查的正常库位列表(已通过 isInNormalRule 过滤)
     * @return true = 前方有堵塞(需要补齐),false = 前方清空或无需检查
     */
    private boolean checkDeepLocationBlocked(List<LocMast> normalMasts) {
        if (normalMasts == null || normalMasts.isEmpty()) {
            return false;
        }
        // 获取出库分组配置
        List<LocGroupOrder> locGroupAscOrder = slaveProperties.getLocGroupAscOrder();
        if (locGroupAscOrder == null || locGroupAscOrder.isEmpty()) {
            return false; // 无配置时默认不检查
        }
        // 假设所有 normalMasts 在同一个分组(常见情况,若多分组可循环处理)
        LocMast representative = normalMasts.get(0);
        LocGroupOrder group = locGroupAscOrder.stream()
                .filter(g -> g.getRowList() != null && g.getRowList().contains(representative.getRow1()))
                .findFirst()
                .orElse(null);
        if (group == null || group.getRowList() == null || group.getRowList().isEmpty()) {
            return false; // 不在任何分组,不检查
        }
        List<Integer> fullRows = group.getRowList(); // 分组内所有排号列表(按出库顺序排列)
        // 遍历每个正常库位
        for (LocMast lm : normalMasts) {
            Integer currentRow = lm.getRow1();
            Integer bay1 = lm.getBay1();
            Integer lev1 = lm.getLev1();
            if (currentRow == null || bay1 == null || lev1 == null) {
                continue;
            }
            // 在分组 rowList 中找到当前排的索引
            int currentIndex = fullRows.indexOf(currentRow);
            if (currentIndex < 0) {
                continue; // 当前排不在分组内,跳过
            }
            // 前方 = 分组中索引更大的位置(假设分组 rowList 从前到后排列,大索引 = 更深)
            // 如果你的分组列表是倒序的(从后到前),则需改为 currentIndex - 1 到 0
            for (int i = currentIndex + 1; i < fullRows.size(); i++) {
                Integer frontRow = fullRows.get(i);
                LocMast front = getLocMastByRow(frontRow, bay1, lev1);
                if (front == null) {
                    continue;
                }
                // 有货(F状态) → 堵塞
                if ("F".equals(front.getLocSts())) {
                    return true;
                }
                WrkMast frontTask = wrkMastService.selectOne(
                        new EntityWrapper<WrkMast>()
                                .eq("source_loc_no", front.getLocNo())
                                .in("loc_sts", Arrays.asList("S", "Q")) // 支持 loc_sts 为 S 或 Q
                );
                if (frontTask != null) {
                    return true;
                }
            }
        }
        // 所有正常库位前方都清空(或无前方)
        return false;
    }
    @Override
    @Transactional
    public void stockOut(BasDevp staNo, List<LocDetlDto> locDetlDtos, IoWorkType ioWorkType, Long userId) {
        Date now = new Date();
        // 从配置获取分组(和空板出库保持一致)
        List<LocGroupOrder> locGroupAscOrder = slaveProperties.getLocGroupAscOrder();
        // 合并同类项(相同 locNo 的明细合并)
        // 保留:合并同类项(同一库位合并明细)
        Set<String> locNos = new HashSet<>();
        List<OutLocDto> dtos = new ArrayList<>();
        for (LocDetlDto locDetlDto : locDetlDtos) {
@@ -280,67 +372,106 @@
                dtos.add(new OutLocDto(locNo, locDetlDto));
            }
        }
        // 使用出库专用分组配置
        List<LocGroupOrder> locGroupAscOrder = slaveProperties.getLocGroupAscOrder();
        // 确定正常出库类型(和原来保持类似)
        int ioTypeNormal = 101; // 默认整托出库
        if (ioWorkType != null && ioWorkType.equals(IoWorkType.CHECK_OUT)) {
            ioTypeNormal = 107;
        } else if (!dtos.isEmpty() && !dtos.get(0).isAll()) {
            ioTypeNormal = 103; // 部分出库
        }
        Integer ioType = null;
        // 1. 查询所有选中的库位主信息
        List<LocMast> allSelectedMasts = locMastService.selectList(
                new EntityWrapper<LocMast>().in("loc_no", locNos)
        );
        // 按列和层分组库位(bay + lev)
        Map<String, List<OutLocDto>> locGroups = dtos.stream()
                .collect(Collectors.groupingBy(dto -> {
                    String column = dto.getLocNo().substring(3, 6); // 列号
                    String level = dto.getLocNo().substring(6);     // 层号
                    return column + "-" + level;
                }));
        if (allSelectedMasts.size() != locNos.size()) {
            throw new CoolException("response.selected_loc_not_exist_or_error");
        }
        // 遍历每组
        for (Map.Entry<String, List<OutLocDto>> entry : locGroups.entrySet()) {
            List<OutLocDto> groupDtos = entry.getValue();
        // 2. 区分正常库位(需要严格深库位检查)与补充库位
        List<LocMast> normalMasts = new ArrayList<>();
        List<LocMast> supplementMasts = new ArrayList<>();
            // 组内优先级重新从100开始(原有逻辑)
            double priority = 100;
        // 假设我们有某种“分组规则”(如按列层或排范围),这里简化用一个示例判断
        // 你可以替换成实际的规则(如 getLocGroupOrderOut() 或其他)
        for (LocMast lm : allSelectedMasts) {
            boolean isNormal = isInNormalRule(lm); // ← 你需要实现这个判断方法
            if (isNormal) {
                normalMasts.add(lm);
            } else {
                supplementMasts.add(lm);
            }
        }
            // 排序组内库位(排号倒序,原有逻辑)
            groupDtos.sort(Comparator.comparing((OutLocDto dto) -> Integer.valueOf(dto.getLocNo().substring(0, 2))).reversed());
        // 3. 对正常库位进行深库位前方检查(类似之前的连续段 + 清空)
        AtomicReference<Boolean> isLeftSideSupplement = new AtomicReference<>(false);
            for (OutLocDto dto : groupDtos) {
                String locNo = dto.getLocNo();
        if (!normalMasts.isEmpty()) {
            // 这里模拟深库位前方检查(你可以替换成你实际的检查方法)
            boolean hasBlockage = checkDeepLocationBlocked(normalMasts);
                // 获取库位信息(提前获取,用于检查和后续使用)
                LocMast locMast = locMastService.selectById(locNo);
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException(locNo + "库位不存在");
            if (hasBlockage) {
                // 前方堵塞 → 自动补充最少一侧
                supplementBothSidesBlocked(normalMasts, locGroupAscOrder, supplementMasts, isLeftSideSupplement);
            }
        }
        // 4. 合并所有要出库的库位
        List<LocMast> allMasts = new ArrayList<>();
        allMasts.addAll(normalMasts);
        allMasts.addAll(supplementMasts);
        if (allMasts.isEmpty()) {
            throw new CoolException("response.no_valid_out_loc");
        }
        // 5. 统一按排号(row)倒序排序(高排先出)
        List<LocMast> sortedAll = allMasts.stream()
                .sorted(Comparator.comparing(LocMast::getRow1, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        // 6. 生成任务(从后往前遍历)
        double basePriority = 100.0;
        for (int index = sortedAll.size() - 1; index >= 0; index--) {
            LocMast locMast = sortedAll.get(index);
            String locNo = locMast.getLocNo();
            boolean isSupplement = supplementMasts.contains(locMast);
            int ioType = isSupplement ? 11 : ioTypeNormal;
            // 优先级计算
            double priority;
            if (isSupplement) {
                if (Boolean.TRUE.equals(isLeftSideSupplement.get())) {
                    // 左侧补充 → 晚出
                    priority = basePriority - index * 1.0;
                } else {
                    // 右侧补充 → 早出
                    priority = basePriority + index * 1.0;
                }
                if (!locMast.getLocSts().equals("F")) {
                    throw new CoolException(locNo + "托盘非在库状态");
                }
                // =====================================
                // 深库位前方堵塞检查(新逻辑,按分组顺序)
                checkDeepPositionBlocking(locMast, locNo, locGroupAscOrder);
                // =====================================
                // 计算优先级(原有逻辑)
                dto.setPriority(priority);
                priority--;
                // 判断入出库类型
                if (ioWorkType == null) {
                    ioType = dto.isAll() ? 101 : 103;
                } else if (ioWorkType.equals(IoWorkType.CHECK_OUT)) {
                    ioType = 107;
                }
                assert ioType != null;
                Integer outSta = staNo.getDevNo();
                // 获取路径
                StaDesc staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), outSta);
                // 生成工作号
                int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
                String pick = ioType == 101 ? "N" : "Y";
            } else {
                priority = basePriority - index * 1.0;
            }
            OutLocDto dto;
            Integer outSta = staNo.getDevNo();
            StaDesc staDesc = null;
            if(ioType != 11){
                staDesc = staDescService.queryCrnStn(ioType, locMast.getCrnNo(), outSta);
            }
            LocMast locMastNew = null;
            WrkMast wrkMast = new WrkMast();
            String locSts;
            int workNo = commonService.getWorkNo(WorkNoType.getWorkNoType(ioType));
            String pick = (ioType == 101) ? "N" : "Y";
            if((ioType == 101 || ioType == 103 || ioType == 107) && staDesc != null) {
                // 找到对应的 OutLocDto
                dto = dtos.stream()
                        .filter(d -> d.getLocNo().equals(locNo))
                        .findFirst()
                        .orElseThrow(() -> new CoolException("response.out_detail_not_found"));
                // 生成工作档
                WrkMast wrkMast = new WrkMast();
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(0L);
@@ -360,18 +491,14 @@
                wrkMast.setAppeTime(now);
                wrkMast.setModiUser(userId);
                wrkMast.setModiTime(now);
                if (!wrkMastService.insert(wrkMast)) {
                    throw new CoolException("保存工作档失败,出库库位号:" + locNo);
                }
                // 生成工作档明细
                locSts = ioType != 101? "P" : "R";
                // 生成工作档明细(保留原逻辑)
                for (LocDetlDto detlDto : dto.getLocDetlDtos()) {
                    if (detlDto.getCount() == null || detlDto.getCount() <= 0.0D) {
                        continue;
                    }
                    WrkDetl wrkDetl = new WrkDetl();
                    wrkDetl.sync(detlDto.getLocDetl());
                    wrkDetl.setOrderNo("");
                    wrkDetl.setWrkNo(workNo);
                    wrkDetl.setIoTime(now);
                    Double anfme = ioType == 101 ? detlDto.getLocDetl().getAnfme() : detlDto.getCount();
@@ -381,23 +508,85 @@
                    wrkDetl.setModiTime(now);
                    wrkDetl.setModiUser(userId);
                    if (!wrkDetlService.insert(wrkDetl)) {
                        throw new CoolException("保存工作档明细失败");
                    }
            throw new CoolException("response.save_work_detail_failed");
        }
                }
            }else{
                List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no",locNo));
                FindLocNoAttributeVo findLocNoAttributeVo = new FindLocNoAttributeVo();
                findLocNoAttributeVo.setMatnr(locDetls.get(0).getMatnr());
                findLocNoAttributeVo.setBatch(locDetls.get(0).getBatch());
                LocTypeDto locTypeDto = new LocTypeDto();
                locTypeDto.setLocType1(locMast.getLocType1());
                locMastNew = commonService.searchMaxPallet(findLocNoAttributeVo,locTypeDto);
                // 生成工作档
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(0L);
                wrkMast.setIoType(ioType);
                wrkMast.setIoPri(priority);
                wrkMast.setCrnNo(locMast.getCrnNo());
                wrkMast.setSourceStaNo(null);
                wrkMast.setStaNo(null);
                wrkMast.setSourceLocNo(locNo);
                wrkMast.setLocNo(locMastNew.getLocNo());
                wrkMast.setFullPlt("Y");
                wrkMast.setPicking(pick);
                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);
                locSts = "R";
                // 生成工作档明细(保留原逻辑)
                if(locDetls != null && locDetls.size() > 0) {
                    for (LocDetl locDetl : locDetls) {
                        WrkDetl wrkDetl = new WrkDetl();
                        wrkDetl.sync(locDetl);
                        wrkDetl.setWrkNo(workNo);
                        wrkDetl.setIoTime(now);
                // 修改库位状态
                locMast = locMastService.selectById(locNo);  // 重新获取最新状态
                if (locMast.getLocSts().equals("F")) {
                    locMast.setLocSts(ioType == 101 ? "R" : "P");
                    locMast.setModiUser(userId);
                    locMast.setModiTime(now);
                    if (!locMastService.updateById(locMast)) {
                        throw new CoolException("预约库位状态失败,库位号:" + locNo);
                        wrkDetl.setAppeTime(now);
                        wrkDetl.setAppeUser(userId);
                        wrkDetl.setModiTime(now);
                        wrkDetl.setModiUser(userId);
                        if (!wrkDetlService.insert(wrkDetl)) {
                            throw new CoolException("response.save_work_detail_failed");
                        }
                    }
                } else {
                    throw new CoolException(locNo + "库位不是在库状态");
                }
            }
            if (!wrkMastService.insert(wrkMast)) {
                throw new CoolException("response.save_work_master_failed_loc|" + locNo);
            }
            if(locMastNew != null){
                if ("O".equals(locMastNew.getLocSts())) {
                    locMastNew.setLocSts("S");
                    locMastNew.setModiUser(userId);
                    locMastNew.setModiTime(now);
                    if (!locMastService.updateById(locMastNew)) {
                        throw new CoolException("response.update_loc_status_failed_loc|" + locNo);
                    }
                } else {
                    throw new CoolException("response.loc_status_error_not_empty|" + locNo);
                }
            }
            // 更新库位状态
            locMast = locMastService.selectById(locNo);
            if ("F".equals(locMast.getLocSts())) {
                locMast.setLocSts(locSts);
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                throw new CoolException("response.reserve_loc_status_failed|" + locNo);
            }
        } else {
            throw new CoolException("response.loc_not_in_store|" + locNo);
        }
        }
    }
@@ -408,7 +597,7 @@
        List<LocDto> locDtos = taskDto.getLocDtos();
        for (LocDto locDto : locDtos) {
            if (!taskDto.getLocNo().equals(locDto.getLocNo()) && !taskDto.getStaNo().equals(locDto.getStaNo())) {
                throw new CoolException("订单出库异常,请联系管理员");
                throw new CoolException("response.order_out_error_contact_admin");
            }
        }
        // 获取库位
@@ -440,14 +629,15 @@
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        if (!wrkMastService.insert(wrkMast)) {
            throw new CoolException("保存工作档失败,出库库位号:"+taskDto.getLocNo());
            throw new CoolException("response.save_work_master_failed_out_loc|" + taskDto.getLocNo());
        }
        // 生成工作档明细
        for (LocDto locDto : taskDto.getLocDtos()) {
            if (locDto.getAnfme()==null || locDto.getAnfme() <= 0.0D) { continue; }
            OrderDetl orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), locDto.getBatch());
            Order order = orderService.selectByNo(locDto.getOrderNo());
            OrderDetl orderDetl = orderDetlService.selectItem(order.getId(), locDto.getMatnr(), locDto.getBatch(),locDto.getBarcode());
            if (orderDetl == null) {
                orderDetl = orderDetlService.selectItem(locDto.getOrderNo(), locDto.getMatnr(), null);
                orderDetl = orderDetlService.selectItem(order.getId(), locDto.getMatnr(), null,locDto.getBarcode());
            }
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(orderDetl);
@@ -462,11 +652,11 @@
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
                throw new CoolException("response.save_work_detail_failed");
            }
            // 修改订单明细
            if (!orderDetlService.increaseWorkQty(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), locDto.getAnfme())) {
                throw new CoolException("修改订单明细数量失败");
            if (!orderDetlService.increaseWorkQty(orderDetl.getOrderId(), orderDetl.getMatnr(), orderDetl.getBatch(), locDto.getAnfme(),locDto.getBarcode())) {
                throw new CoolException("response.update_order_detail_qty_failed");
            }
            orderService.updateSettle(orderDetl.getOrderId(), 2L, userId);
        }
@@ -477,10 +667,10 @@
            locMast.setModiUser(userId);
            locMast.setModiTime(now);
            if (!locMastService.updateById(locMast)) {
                throw new CoolException("预约库位状态失败,库位号:"+taskDto.getLocNo());
                throw new CoolException("response.reserve_loc_status_failed_loc|" + taskDto.getLocNo());
            }
        } else {
            throw new CoolException(taskDto.getLocNo() + "库位不是在库状态");
            throw new CoolException("response.loc_not_in_store_status|" + taskDto.getLocNo());
        }
    }
@@ -518,14 +708,14 @@
        wrkMast.setModiTime(now);
        boolean res = wrkMastService.insert(wrkMast);
        if (!res) {
            throw new CoolException("保存工作档失败");
            throw new CoolException("response.save_work_master_failed");
        }
        // 更新源站点信息
        sourceStaNo.setWrkNo(workNo);
        sourceStaNo.setModiUser(userId);
        sourceStaNo.setModiTime(now);
        if (!basDevpService.updateById(sourceStaNo)){
            throw new CoolException("更新源站失败");
            throw new CoolException("response.update_source_station_failed");
        }
        // 更新目标库位状态
        LocMast locMast = locMastService.selectById(dto.getLocNo());
@@ -534,10 +724,10 @@
            locMast.setModiUser(userId);
            locMast.setModiTime(now);
            if (!locMastService.updateById(locMast)){
                throw new CoolException("改变库位状态失败");
                throw new CoolException("response.change_loc_status_failed");
            }
        } else {
            throw new CoolException(dto.getLocNo()+"目标库位已被占用");
            throw new CoolException("response.target_loc_occupied|" + dto.getLocNo());
        }
        return dto.getLocNo();
    }
@@ -562,7 +752,7 @@
        );
        if (selectedMasts.size() != selectedLocNos.size()) {
            return LockingCheckResultParam.fail("部分选中库位不存在或数据异常");
            return LockingCheckResultParam.fail("response.selected_loc_invalid_or_missing");
        }
        // 2. 按分组聚合选中的库位(支持多分组)
@@ -571,7 +761,7 @@
            LocGroupOrder group = locGroupAscOrder.stream()
                    .filter(g -> g.getRowList().contains(lm.getRow1()))
                    .findFirst()
                    .orElseThrow(() -> new CoolException("排不在出库分组配置中: row=" + lm.getRow1()));
                    .orElseThrow(() -> new CoolException("response.row_not_in_group_config|" + lm.getRow1()));
            groupSelected.computeIfAbsent(group, k -> new ArrayList<>()).add(lm);
        }
@@ -595,7 +785,7 @@
            // 检查是否重复或无效
            if (selectedRows.size() != selected.size()) {
                return LockingCheckResultParam.fail("选中库位存在重复或无效排");
                return LockingCheckResultParam.fail("response.selected_loc_duplicate_or_invalid_row");
            }
            int minIndex = fullRows.indexOf(selectedRows.get(0));
@@ -604,7 +794,7 @@
            // 1. 必须是连续段(无缺口)
            if (maxIndex - minIndex + 1 != selectedRows.size()) {
                return LockingCheckResultParam.fail(
                        "选中排必须连续,无缺口。从 " + fullRows.get(minIndex) + " 到 " + fullRows.get(maxIndex)
                        "response.selected_rows_must_be_continuous|" + fullRows.get(minIndex) + "|" + fullRows.get(maxIndex)
                );
            }
@@ -628,18 +818,17 @@
                }
            }
            // 4. 至少有一侧清空才允许出库
            // 4. 至少有一侧清空才允许出库(修改:放宽,如果两侧都堵,返回特定错误码或继续)
            if (!leftClear && !rightClear) {
                return LockingCheckResultParam.fail(
                        "选中段 " + fullRows.get(minIndex) + "~" + fullRows.get(maxIndex) +
                                " 两侧前方都有空板/故障,无法出库(正序或倒序方向都堵塞)"
                        "response.both_sides_blocked|" + fullRows.get(minIndex) + "|" + fullRows.get(maxIndex)
                );
            }
            // 5. 选中段内所有库位必须是 D 状态
            for (LocMast lm : selected) {
                if (!"D".equals(lm.getLocSts())) {
                    return LockingCheckResultParam.fail("选中库位非空板状态: " + lm.getLocNo());
                    return LockingCheckResultParam.fail("response.selected_loc_not_empty_status|" + lm.getLocNo());
                }
            }
@@ -674,37 +863,167 @@
                .eq("lev1", lev1)
                .eq("row1", row));
    }
    /**
     * 当选中段两侧前方都堵塞时,判断补哪一侧需要的空板最少,并把那些库位加入补充列表
     */
    private void supplementBothSidesBlocked(
            List<LocMast> normalMasts,
            List<LocGroupOrder> locGroupAscOrder,
            List<LocMast> supplementMasts,
    AtomicReference<Boolean> isLeftSideSupplement) {
        // 假设所有 normalMasts 在同一个分组(如果多分组,可循环处理每个分组)
        LocGroupOrder group = locGroupAscOrder.stream()
                .filter(g -> g.getRowList().contains(normalMasts.get(0).getRow1()))
                .findFirst()
                .orElseThrow(() -> new CoolException("response.group_exception"));
        List<Integer> fullRows = group.getRowList();
        // 取选中段的 min/max row
        Set<Integer> selectedRowSet = normalMasts.stream()
                .map(LocMast::getRow1)
                .collect(Collectors.toSet());
        int minRow = Collections.min(selectedRowSet);
        int maxRow = Collections.max(selectedRowSet);
        int minIndex = fullRows.indexOf(minRow);
        int maxIndex = fullRows.indexOf(maxRow);
        // 假设所有库位在同 bay 和 lev
        Integer bay1 = normalMasts.get(0).getBay1();
        Integer lev1 = normalMasts.get(0).getLev1();
        // 计算左侧前方需要补充的 D 状态库位数量(从 0 到 minIndex-1 的 D 状态库位)
        int leftSupplementCount = 0;
        List<LocMast> leftSupplementLocs = new ArrayList<>();
        for (int i = 0; i < minIndex; i++) {
            LocMast loc = getLocMastByRow(fullRows.get(i), bay1, lev1);
            if (loc != null && "D".equals(loc.getLocSts())) {
                leftSupplementCount++;
                leftSupplementLocs.add(loc);
            }
        }
        // 计算右侧前方需要补充的 D 状态库位数量(从 maxIndex+1 到 end 的 D 状态库位)
        int rightSupplementCount = 0;
        List<LocMast> rightSupplementLocs = new ArrayList<>();
        for (int i = maxIndex + 1; i < fullRows.size(); i++) {
            LocMast loc = getLocMastByRow(fullRows.get(i), bay1, lev1);
            if (loc != null && "D".equals(loc.getLocSts())) {
                rightSupplementCount++;
                rightSupplementLocs.add(loc);
            }
        }
        // 选择需要补充最少的一侧(如果相等,优先左侧)
        List<LocMast> chosenSupplementLocs;
        boolean isLeft = false;
        if (leftSupplementCount <= rightSupplementCount) {
            chosenSupplementLocs = leftSupplementLocs;
            isLeft = true;
            log.info("选择补充左侧前方,共 {} 个库位", leftSupplementCount);
        } else {
            chosenSupplementLocs = rightSupplementLocs;
            isLeft = false;
            log.info("选择补充右侧前方,共 {} 个库位", rightSupplementCount);
        }
        // 记录选择的侧
        isLeftSideSupplement.set(isLeft);
        // 添加到 supplementMasts(避免重复添加)
        for (LocMast supp : chosenSupplementLocs) {
            if (!supplementMasts.contains(supp) && !normalMasts.contains(supp)) {
                supplementMasts.add(supp);
            }
        }
    }
    @Override
    @Transactional
    public void emptyPlateOut(EmptyPlateOutParam param, Long userId) {
        if (Cools.isEmpty(param.getOutSite())) {
            throw new CoolException("站点不存在");
            throw new CoolException("response.site_not_exist");
        }
        // 使用新的出库专用分组配置
        // 使用出库专用分组配置
        List<LocGroupOrder> locGroupAscOrder = slaveProperties.getLocGroupAscOrderOut();
        // 1. 先进行整体阻塞检查(包含连续性 + 双向前方清空校验)
        LockingCheckResultParam checkResult = checkEmptyPlateBlocking(param.getLocNos(), locGroupAscOrder);
        // 1. 查询所有选中的库位信息
        List<LocMast> selectedMasts = locMastService.selectList(
                new EntityWrapper<LocMast>().in("loc_no", param.getLocNos())
        );
        if (!checkResult.isSuccess()) {
            throw new CoolException(checkResult.getErrorMessage());
        if (selectedMasts.size() != param.getLocNos().size()) {
            throw new CoolException("response.selected_loc_abnormal");
        }
        // 2. 获取按出库方向排序好的库位列表
        List<LocMast> orderedLocMasts = checkResult.getSortedSelected();
        // 2. 区分正常分组内的库位 和 需要补充的库位(规则外的)
        List<LocMast> normalMasts = new ArrayList<>();
        List<LocMast> supplementMasts = new ArrayList<>();
        for (LocMast lm : selectedMasts) {
            boolean inAnyGroup = locGroupAscOrder.stream()
                    .anyMatch(g -> g.getRowList().contains(lm.getRow1()));
            if (inAnyGroup) {
                normalMasts.add(lm);
            } else {
                supplementMasts.add(lm);
            }
        }
        // 新增:记录是否因为左侧前方被补充
        AtomicReference<Boolean> isLeftSideSupplement = new AtomicReference<>(false);
        // 3. 对正常分组内的库位进行检查(放宽两侧堵塞规则)
        if (!normalMasts.isEmpty()) {
            List<String> normalLocNos = normalMasts.stream()
                    .map(LocMast::getLocNo)
                    .collect(Collectors.toList());
        // 3. 优先级基础参数(数值越大优先级越高)
        double BASE_PRI = 10.0;          // 最低优先级组基准(数值最大)
        double GROUP_STEP = 100.0;       // 组间差距
        double IN_GROUP_STEP = 1.0;      // 组内位置差距
            LockingCheckResultParam checkResult = checkEmptyPlateBlocking(
                    normalLocNos,
                    locGroupAscOrder
            );
            if (!checkResult.isSuccess()) {
                String errMsg = checkResult.getErrorMessage();
                if (errMsg.contains("response.both_sides_blocked")) {
                    // 两侧都堵 → 进入补齐逻辑
                    supplementBothSidesBlocked(normalMasts, locGroupAscOrder, supplementMasts, isLeftSideSupplement);
                } else {
                    // 其他错误(如不连续、非D状态)抛出
                    throw new CoolException(errMsg);
                }
            }
            // 如果有一侧清空,则正常继续
        }
        // 4. 合并所有库位(正常 + 补充的,包括规则外的和前方补的)
        List<LocMast> allMasts = new ArrayList<>();
        allMasts.addAll(normalMasts);
        allMasts.addAll(supplementMasts);
        if (allMasts.isEmpty()) {
            throw new CoolException("response.no_valid_empty_pallet_loc");
        }
        // 5. 统一按 row → bay → lev 排序(从小到大)
        List<LocMast> sortedAll = allMasts.stream()
                .sorted(Comparator.comparing(LocMast::getRow1)
                        .thenComparing(LocMast::getBay1)
                        .thenComparing(LocMast::getLev1))
                .collect(Collectors.toList());
        Date now = new Date();
        // 4. 逐个处理排序后的库位,生成出库任务
        for (int index = orderedLocMasts.size(); index > 0; index--) {
            LocMast locMast = orderedLocMasts.get(index);
        // 6. 按排序后的顺序生成出库任务(从后往前生成)
        for (int index = 0; index <sortedAll.size() ; index ++) {
            LocMast locMast = sortedAll.get(index);
            String locNo = locMast.getLocNo();
            // 判断是否为补充库位(规则外的 或 前方补的)
            boolean isSupplement = supplementMasts.contains(locMast);
            int ioType = isSupplement ? 11 : 110;
            // 获取工作号
            int workNo = commonService.getWorkNo(WorkNoType.PAKOUT.type);
@@ -715,67 +1034,95 @@
                    .eq("stn_no", param.getOutSite())
                    .eq("crn_no", locMast.getCrnNo());
            StaDesc staDesc = staDescService.selectOne(wrapper);
            Integer sourceStaNo = staDesc.getCrnStn();
            Integer sourceStaNo = staDesc != null ? staDesc.getCrnStn() : null;
            if (Cools.isEmpty(sourceStaNo)) {
                throw new CoolException("检索源站失败,库位:" + locNo);
                throw new CoolException("response.query_source_station_failed_loc|" + locNo);
            }
            // 计算动态优先级(组优先级 + 组内顺序偏移)
            // 这里假设所有选中的库位在同一个分组(多分组可再细分)
            int groupIndex = locGroupAscOrder.indexOf(
                    locGroupAscOrder.stream()
                            .filter(g -> g.getRowList().contains(locMast.getRow1()))
                            .findFirst()
                            .orElse(null)
            );
            if (groupIndex == -1) {
                throw new CoolException("分组索引异常: " + locNo);
            }
            double groupBasePri = BASE_PRI + (locGroupAscOrder.size() + 1 - groupIndex) * GROUP_STEP;
            // 组内偏移:按出库顺序(index越小越先出,优先级越高 → 数值越大)
            // 如果是 DESC 方向,index 已经是倒序的,所以直接用
            double inGroupOffset = index * IN_GROUP_STEP;
            double ioPri = groupBasePri + inGroupOffset;  // 数值越大优先级越高
            // 生成工作档
            // 计算优先级(示例:补充的优先级稍低)
            double BASE_PRI = 200.0;
            double ioPri;
            WrkMast wrkMast = new WrkMast();
            wrkMast.setWrkNo(workNo);
            wrkMast.setIoTime(now);
            wrkMast.setWrkSts(0L);           // 待发送
            wrkMast.setIoType(110);          // 空板出库
            wrkMast.setIoPri(ioPri);
            wrkMast.setSourceStaNo(sourceStaNo);
            wrkMast.setStaNo(param.getOutSite());
            wrkMast.setCrnNo(locMast.getCrnNo());
            wrkMast.setSourceLocNo(locNo);
            wrkMast.setFullPlt("N");
            wrkMast.setPicking("N");
            wrkMast.setExitMk("N");
            wrkMast.setEmptyMk("Y");
            wrkMast.setLinkMis("N");
            wrkMast.setAppeUser(userId);
            wrkMast.setAppeTime(now);
            wrkMast.setModiUser(userId);
            wrkMast.setModiTime(now);
            LocMast locMastNew = null;
            if(isSupplement){
                // 补充的库位:根据是左侧还是右侧补充,决定优先级方向
                if (Boolean.TRUE.equals(isLeftSideSupplement.get())) {
                    // 左侧补充 → 优先级较低(晚出)
                    ioPri = BASE_PRI - index * 1.0;
                } else {
                    // 右侧补充 → 优先级较高(早出)
                    ioPri = BASE_PRI + index * 1.0;
                }
                locMastNew = commonService.searchEmptyPallet(null);
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(0L);
                wrkMast.setIoType(ioType);
                wrkMast.setIoPri(ioPri);
                wrkMast.setSourceStaNo(null);
                wrkMast.setLocNo(locMastNew.getLocNo());
                wrkMast.setStaNo(null);
                wrkMast.setCrnNo(locMast.getCrnNo());
                wrkMast.setSourceLocNo(locNo);
                wrkMast.setFullPlt("N");
                wrkMast.setPicking("N");
                wrkMast.setExitMk("N");
                wrkMast.setEmptyMk("Y");
                wrkMast.setLinkMis("N");
                wrkMast.setAppeUser(userId);
                wrkMast.setAppeTime(now);
                wrkMast.setModiUser(userId);
                wrkMast.setModiTime(now);
            }else{
                ioPri = BASE_PRI + index * 1.0;
                wrkMast.setWrkNo(workNo);
                wrkMast.setIoTime(now);
                wrkMast.setWrkSts(0L);
                wrkMast.setIoType(ioType);
                wrkMast.setIoPri(ioPri);
                wrkMast.setSourceStaNo(sourceStaNo);
                wrkMast.setStaNo(param.getOutSite());
                wrkMast.setCrnNo(locMast.getCrnNo());
                wrkMast.setSourceLocNo(locNo);
                wrkMast.setFullPlt("N");
                wrkMast.setPicking("N");
                wrkMast.setExitMk("N");
                wrkMast.setEmptyMk("Y");
                wrkMast.setLinkMis("N");
                wrkMast.setAppeUser(userId);
                wrkMast.setAppeTime(now);
                wrkMast.setModiUser(userId);
                wrkMast.setModiTime(now);
            }
            if (!wrkMastService.insert(wrkMast)) {
                throw new CoolException("保存工作档失败,库位:" + locNo);
                throw new CoolException("response.save_work_master_failed_loc|" + locNo);
            }
            // 更新库位状态 D → R(出库预约)
            if(locMastNew != null){
                if ("O".equals(locMastNew.getLocSts())) {
                    locMastNew.setLocSts("S");
                    locMastNew.setModiUser(userId);
                    locMastNew.setModiTime(now);
                    if (!locMastService.updateById(locMastNew)) {
                        throw new CoolException("response.update_loc_status_failed_loc|" + locNo);
                    }
                } else {
                    throw new CoolException("response.loc_status_error_not_empty|" + locNo);
                }
            }
            // 更新库位状态 D → R
            if ("D".equals(locMast.getLocSts())) {
                locMast.setLocSts("R");
                locMast.setModiUser(userId);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("更新库位状态失败,库位:" + locNo);
                    throw new CoolException("response.update_loc_status_failed_loc|" + locNo);
                }
            } else {
                throw new CoolException("库位状态异常,非空板状态:" + locNo);
                throw new CoolException("response.loc_status_error_not_empty|" + locNo);
            }
        }
    }
@@ -784,7 +1131,7 @@
    public WrkMast emptyPlateOut(EmptyPlateOutParam param) {
        WrkMast wrkMast = new WrkMast();
        if (Cools.isEmpty(param.getOutSite())) {
            throw new CoolException("站点不存在");
            throw new CoolException("response.site_not_exist");
        }
        for (String locNo : param.getLocNos()) {
            // 获取工作号
@@ -792,7 +1139,7 @@
            // 获取库位
            LocMast locMast = locMastService.selectById(locNo);
            if (Cools.isEmpty(locMast)) {
                throw new CoolException(locNo+"库位不存在");
                throw new CoolException("response.loc_not_exist|" + locNo);
            }
            // 获取源站
            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
@@ -802,7 +1149,7 @@
            StaDesc staDesc = staDescService.selectOne(wrapper);
            Integer sourceStaNo = staDesc.getCrnStn();
            if (Cools.isEmpty(sourceStaNo)) {
                throw new CoolException("检索源站失败");
                throw new CoolException("response.query_source_station_failed");
            }
            Date now = new Date();
            // 保存工作档
@@ -825,10 +1172,10 @@
            wrkMast.setAppeTime(now);
            wrkMast.setModiUser(1L);
            wrkMast.setModiTime(now);
            wrkMast.setMemo("生成自动空板出库");
            wrkMast.setMemo("response.memo_auto_empty_pallet_out");
            boolean res = wrkMastService.insert(wrkMast);
            if (!res) {
                throw new CoolException("保存工作档失败");
                throw new CoolException("response.save_work_master_failed");
            }
            // 更新库位状态 D.空板 -> R.出库预约
            if (locMast.getLocSts().equals("D")){
@@ -836,7 +1183,7 @@
                locMast.setModiUser(1L);
                locMast.setModiTime(now);
                if (!locMastService.updateById(locMast)) {
                    throw new CoolException("更新库位状态失败");
                    throw new CoolException("response.update_loc_status_failed_simple");
                }
            }
        }
@@ -851,8 +1198,8 @@
        // 获取库位明细
        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());
            if (!Cools.isEmpty(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getCount(),paramLocDetl.getBarcode())) {
                LocDetl one = locDetlService.selectItemCarton(paramLocDetl.getLocNo(), paramLocDetl.getMatnr(), paramLocDetl.getBatch(),paramLocDetl.getBarcode());
                if (null != one) locDetlDtos.add(new LocDetlDto(one, paramLocDetl.getCount()));
            }
        }
@@ -862,10 +1209,10 @@
                // 启动出库开始 107.盘点出库
                stockOut(staNo, locDetlDtos, IoWorkType.CHECK_OUT, userId);
            }else {
                throw new CoolException("所选库位存在状态不为F的库位,库位号:"+locMast.getLocNo()+" 、当前状态:"+locMast.getLocSts()+"-"+locMast.getLocSts$());
                throw new CoolException("response.selected_loc_status_error|" + locMast.getLocNo() + "," + locMast.getLocSts() + "-" + locMast.getLocSts$());
            }
        } else {
            throw new CoolException("库位物料不存在");
            throw new CoolException("response.loc_material_not_exist");
        }
    }
@@ -875,17 +1222,17 @@
        LocMast sourceLoc = locMastService.selectById(sourceLocNo);
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", sourceLocNo));
        if (Cools.isEmpty(sourceLoc)){
            throw new CoolException("未找到库位");
            throw new CoolException("response.loc_not_found");
        }
        LocMast loc = locMastService.selectById(locNo);
        if (Cools.isEmpty(loc)){
            throw new CoolException("未找到库位");
            throw new CoolException("response.loc_not_found");
        }
        if (!loc.getLocSts().equals("O") || (!sourceLoc.getLocSts().equals("F") && !sourceLoc.getLocSts().equals("D"))){
            throw new CoolException("库位状态已改变");
            throw new CoolException("response.loc_status_changed");
        }
        if (!sourceLoc.getCrnNo().equals(loc.getCrnNo())) {
            throw new CoolException("移转库位属于不同堆垛机");
            throw new CoolException("response.transfer_loc_diff_crn");
        }
        Date now = new Date();
        // 获取工作号
@@ -912,7 +1259,7 @@
        wrkMast.setModiTime(now);
        boolean res = wrkMastService.insert(wrkMast);
        if (!res) {
            throw new CoolException("保存工作档失败");
            throw new CoolException("response.save_work_master_failed_simple");
        }
        // 工作档明细保存
        for (LocDetl locDetl : locDetls) {
@@ -926,7 +1273,7 @@
            wrkDetl.setModiTime(now);
            wrkDetl.setModiUser(userId);
            if (!wrkDetlService.insert(wrkDetl)) {
                throw new CoolException("保存工作档明细失败");
                throw new CoolException("response.save_work_detail_failed");
            }
        }
        // 修改源库位状态
@@ -935,10 +1282,10 @@
            sourceLoc.setModiUser(userId);
            sourceLoc.setModiTime(now);
            if (!locMastService.updateById(sourceLoc)){
                throw new CoolException("更新源库位状态失败");
                throw new CoolException("response.update_source_loc_status_failed");
            }
        } else {
            throw new CoolException("源库位出库失败,状态:"+sourceLoc.getLocSts$());
            throw new CoolException("response.source_loc_out_failed_status|" + sourceLoc.getLocSts$());
        }
        // 修改目标库位状态
        if (loc.getLocSts().equals("O")) {
@@ -946,10 +1293,10 @@
            loc.setModiTime(now);
            loc.setModiUser(userId);
            if (!locMastService.updateById(loc)) {
                throw new CoolException("更新目标库位状态失败");
                throw new CoolException("response.update_target_loc_status_failed");
            }
        } else {
            throw new CoolException("移转失败,目标库位状态:"+loc.getLocSts$());
            throw new CoolException("response.transfer_failed_target_loc_status|" + loc.getLocSts$());
        }
    }
@@ -958,10 +1305,10 @@
    public void completeWrkMast(String workNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
            throw new CoolException("response.work_master_not_exist|" + workNo);
        }
        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 14) {
            throw new CoolException("当前工作档已完成");
            throw new CoolException("response.work_master_completed");
        }
        // 出库
        if (wrkMast.getIoType() > 100) {
@@ -977,9 +1324,9 @@
        wrkMast.setModiTime(now);
        wrkMast.setModiUser(userId);
        // 完成操作人员记录
        wrkMast.setManuType("手动完成");
        wrkMast.setManuType("response.manu_complete");
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("修改工作档失败");
            throw new CoolException("response.update_work_master_failed");
        }
    }
@@ -989,10 +1336,10 @@
        param.integrate();
        LocMast locMast = locMastService.selectById(param.getLocNo());
        if (Cools.isEmpty(locMast)) {
            throw new CoolException("库位不存在");
            throw new CoolException("response.loc_not_exist_simple");
        }
        if (!(locMast.getLocSts().equals("F") || locMast.getLocSts().equals("D") || locMast.getLocSts().equals("O"))) {
            throw new CoolException("当前库位不可调整!库位状态:" + locMast.getLocSts$());
            throw new CoolException("response.loc_not_adjustable|" + locMast.getLocSts$());
        }
        Date now = new Date();
@@ -1013,8 +1360,8 @@
                    if (!locDetl.getAnfme().equals(adjust.getCount())) {
                        // todo 盘点记录
                        // 修改库存
                        if (!locDetlService.updateAnfme(adjust.getCount(), locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch())) {
                            throw new CoolException(locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "批号修改数量失败");
                        if (!locDetlService.updateAnfme(adjust.getCount(), locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(),locDetl.getBarcode())) {
                            throw new CoolException("response.update_stock_qty_failed|" + locDetl.getLocNo() + "," + locDetl.getMatnr() + "," + locDetl.getBatch());
                        }
                        // 保存调整记录
                        AdjDetl adjDetl = new AdjDetl();
@@ -1038,8 +1385,8 @@
        // 删除库存
        for (LocDetl locDetl : locDetls) {
            // todo 盘点记录
            if (!locDetlService.updateAnfme(-1.0D, locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch())) {
                throw new CoolException("删除" + locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "批号库存明细失败");
            if (!locDetlService.updateAnfme(-1.0D, locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(),locDetl.getBarcode())) {
                throw new CoolException("response.delete_stock_detail_failed|" + locDetl.getLocNo() + "," + locDetl.getMatnr() + "," + locDetl.getBatch());
            }
            // 保存调整记录
            AdjDetl adjDetl = new AdjDetl();
@@ -1069,7 +1416,7 @@
            locDetl.setAppeUser(userId);
            locDetl.setAppeTime(now);
            if (!locDetlService.insert(locDetl)) {
                throw new CoolException("添加" + locDetl.getLocNo() + "库位," + locDetl.getMatnr() + "商品," + locDetl.getBatch() + "批号库存明细失败");
                throw new CoolException("response.add_stock_detail_failed|" + locDetl.getLocNo() + "," + locDetl.getMatnr() + "," + locDetl.getBatch());
            }
            // 保存调整记录
            AdjDetl adjDetl = new AdjDetl();
@@ -1099,7 +1446,7 @@
        locMast.setModiUser(userId);
        locMast.setModiTime(now);
        if (!locMastService.updateById(locMast)) {
            throw new CoolException("更新库位状态失败");
            throw new CoolException("response.update_loc_status_failed");
        }
    }
@@ -1110,7 +1457,7 @@
        Date now = new Date();
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
            throw new CoolException("response.work_master_not_exist|" + workNo);
        }
        String locNo = ""; // 待修改目标库位
        String locSts = ""; // 待修改目标库位状态
@@ -1124,7 +1471,7 @@
                // 库位转移:源库位
                LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,源库位不存在:"+ wrkMast.getSourceLocNo());
                    throw new CoolException("response.cancel_transfer_failed_source_not_exist|" + wrkMast.getSourceLocNo());
                }
                locMast.setLocSts(wrkMast.getFullPlt().equalsIgnoreCase("N")?"D":"F");
                locMast.setModiTime(now);
@@ -1135,7 +1482,7 @@
        } else if (wrkMast.getIoType() > 100 && wrkMast.getWrkSts() != 14) {
            locNo = wrkMast.getSourceLocNo();
            // 出库 ===>> F.在库
            if (wrkMast.getIoType() == 101 || wrkMast.getIoType() == 103) {
            if (wrkMast.getIoType() == 101 || wrkMast.getIoType() == 103 || wrkMast.getIoType() == 107) {
                locSts = "F";
            // 空板出库 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 110) {
@@ -1146,7 +1493,7 @@
                // 库位转移:目标库位
                LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,目标库位不存在:"+ wrkMast.getSourceLocNo());
                    throw new CoolException("response.cancel_transfer_failed_target_not_exist|" + wrkMast.getLocNo());
                }
                locMast.setLocSts("O");
                locMast.setModiTime(now);
@@ -1154,7 +1501,7 @@
                locMastService.updateById(locMast);
            }
        } else {
            throw new CoolException("当前工作状态无法取消");
            throw new CoolException("response.work_status_cannot_cancel");
        }
        //取消入库工作档时,查询组托表,如果有将状态改为待处理
@@ -1178,7 +1525,7 @@
            for (WrkDetl wrkDetl : wrkDetls) {
                if (!Cools.isEmpty(wrkDetl.getOrderNo())) {
                    if (!orderDetlService.decrease(wrkDetl.getOrderNo(), wrkDetl.getMatnr(), wrkDetl.getBatch(), wrkDetl.getAnfme())) {
                        throw new CoolException("订单数据回滚失败");
                        throw new CoolException("response.order_data_rollback_failed");
                    }
                    //修改订单主表状态,没有作业数量时才可以修改
@@ -1197,7 +1544,7 @@
                            order.setUpdateTime(now);
                        }
                        if(!orderService.update(order,new EntityWrapper<Order>().eq("order_no",wrkDetl.getOrderNo()))){
                            throw new CoolException("修改订单状态失败");
                            throw new CoolException("response.update_order_status_failed");
                        }
                    }
                }
@@ -1218,15 +1565,15 @@
//        }
        // 取消操作人员记录
        wrkMast.setManuType("手动取消");
        wrkMast.setManuType("response.manu_cancel");
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("取消工作档失败");
            throw new CoolException("response.cancel_work_master_failed");
        }
        // 保存工作主档历史档
        if (!wrkMastLogService.save(wrkMast.getWrkNo())) {
            throw new CoolException("保存工作历史档失败, workNo = " + wrkMast.getWrkNo());
            throw new CoolException("response.save_work_log_failed|" + wrkMast.getWrkNo());
        }
        // 删除工作主档
        boolean wrkMastRes = wrkMastService.deleteById(wrkMast);
@@ -1243,14 +1590,14 @@
        // 修改库位状态
        LocMast locMast = locMastService.selectById(locNo);
        if (Cools.isEmpty(locMast)) {
            throw new CoolException("取消工作档失败,库位不存在:"+ locNo);
            throw new CoolException("response.cancel_work_master_failed_loc_not_exist|" + locNo);
        }
        locMast.setLocSts(locSts);
        locMast.setModiTime(now);
        locMast.setModiUser(userId);
        boolean locMastRes = locMastService.updateById(locMast);
        if (!wrkMastRes || !locMastRes) {
            throw new CoolException("保存数据失败");
            throw new CoolException("response.save_data_failed");
        }
        //wms取消任务 同时调用wcs任务取消接口通知wcs
@@ -1275,7 +1622,7 @@
                flag = true;
            }else {
                log.error("wms取消任务下发wcs失败--->url:{};request:{};response:{}", url+"/"+cancel, JSON.toJSONString(wrkCancel), response);
                throw new CoolException("wms取消任务下发wcs失败");
                throw new CoolException("response.wms_cancel_task_wcs_failed");
            }
        }catch (Exception e){
            log.error("fail", e);
@@ -1304,15 +1651,15 @@
    public void pickWrkMast(String workNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)){
            throw new CoolException(workNo+"工作档不存在");
            throw new CoolException("response.work_master_not_exist|" + workNo);
        }
        // 入出库类型判断
        if (wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107) {
            throw new CoolException("当前入出库类型无法进行操作");
            throw new CoolException("response.io_type_cannot_operate");
        }
        // 工作状态判断
        if (wrkMast.getWrkSts() < 11 || wrkMast.getWrkSts() == 15) {
            throw new CoolException("当前工作状态无法进行操作");
            throw new CoolException("response.work_status_cannot_operate");
        }
        // 保存工作明细档历史档
//        if (!wrkDetlLogService.save(wrkMast.getWrkNo())) {
@@ -1320,7 +1667,7 @@
//        }
        // 保存工作主档历史档
        if (!wrkMastLogService.save(wrkMast.getWrkNo())) {
            throw new CoolException("保存工作主档历史档失败");
            throw new CoolException("response.save_work_master_log_failed");
        }
        // 获取目标站
        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
@@ -1329,7 +1676,7 @@
                .eq("crn_no", wrkMast.getCrnNo()); // 堆垛机号
        StaDesc staDesc = staDescService.selectOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            throw new CoolException("入库路径不存在");
            throw new CoolException("response.in_route_not_exist");
        }
        Date now = new Date();
        // 堆垛机站点(目标站)
@@ -1344,7 +1691,7 @@
        wrkMast.setModiTime(now);
        wrkMast.setModiUser(userId);
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("更新工作档数据状态失败");
            throw new CoolException("response.update_work_master_status_failed");
        }
        // 修改库位状态 Q.拣料/盘点/并板再入库
        LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
@@ -1352,14 +1699,14 @@
        locMast.setModiTime(now);
        locMast.setModiUser(userId);
        if (!locMastService.updateById(locMast)) {
            throw new CoolException("修改库位状态失败");
            throw new CoolException("response.update_loc_status_failed");
        }
    }
    @Override
    public StartupDto createWaitPainWrkMastStart(List<WaitPakin> list, Long userId) {
        if (Cools.isEmpty(list)) {
            throw new CoolException("入库通知档不能为空");
            throw new CoolException("response.wait_pakin_empty");
        }
        LocTypeDto locTypeDto = new LocTypeDto();
        locTypeDto.setLocType1((short) 1);
@@ -1371,7 +1718,7 @@
    public String dealPreHaveStart(Integer wrkNo, Long userId) {
        WrkMast wrkMast = wrkMastService.selectById(wrkNo);
        if (wrkMast == null) {
            throw new CoolException("任务已失效");
            throw new CoolException("response.task_invalid");
        }
        String locNo = wrkMast.getLocNo();
        LocMast locMast = locMastService.selectById(locNo);
@@ -1421,14 +1768,14 @@
        }
        if (targetLoc == null) {
            throw new CoolException("操作失败,当前仓库找不到空库位");
            throw new CoolException("response.no_empty_loc_found");
        }
        Date now = new Date();
        // 修改工作档
        StaDesc staDesc = staDescService.queryCrnStn(targetLoc.getCrnNo());
        if (Cools.isEmpty(staDesc)) {
            throw new CoolException("入库路径不存在");
            throw new CoolException("response.in_route_not_exist");
        }
        wrkMast.setWrkSts(2L);
        wrkMast.setLocNo(targetLoc.getLocNo());
@@ -1438,7 +1785,7 @@
        wrkMast.setModiUser(userId);
        wrkMast.setPreHave("N");
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("修改工作档失败");
            throw new CoolException("response.update_work_master_failed");
        }
        // 修改库位状态 O ===>>> S
        if (targetLoc.getLocSts().equals("O")){
@@ -1446,10 +1793,10 @@
            targetLoc.setModiUser(userId);
            targetLoc.setModiTime(now);
            if (!locMastService.updateById(targetLoc)){
                throw new CoolException("改变库位状态失败");
                throw new CoolException("response.update_loc_status_failed");
            }
        } else {
            throw new CoolException(targetLoc.getLocNo()+"目标库位已被占用");
            throw new CoolException("response.target_loc_occupied|" + targetLoc.getLocNo());
        }
        // 禁用异常库位
//        locMast.setLocSts("X"); // X.禁用
@@ -1466,16 +1813,16 @@
    public void turnMatLocDetl(EmptyPlateOutParam param, Long userId) {
        Mat mat = matService.selectOne(new EntityWrapper<Mat>().eq("id", param.getMatId()));
        if (Cools.isEmpty(mat)){
            throw new CoolException("目标库位商品编码有误!");
            throw new CoolException("response.target_loc_mat_error");
        }
        List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("matnr", param.getLocDetls().get(0).getMatnr()));
        if (Cools.isEmpty(locDetls) || locDetls.size()<1){
            throw new CoolException("待修改商品无库存,无需修改!  品号:"+param.getLocDetls().get(0).getMatnr());
            throw new CoolException("response.mat_no_stock_no_need_update|" + param.getLocDetls().get(0).getMatnr());
        }
        try {
            locDetlService.updateMatTurn(param.getLocDetls().get(0).getMatnr(),mat.getMatnr());
        }catch (Exception e){
            throw new CoolException("对数据库修改出错!");
            throw new CoolException("response.db_update_error");
        }
        for (LocDetl locDetl:locDetls){
            // 保存调整记录