自动化立体仓库 - WMS系统
Junjie
2023-05-12 fd1bfa1163d438ec6cabfdd6e01e632d31189c80
src/main/java/com/zy/common/service/CommonService.java
@@ -28,11 +28,18 @@
public class CommonService {
    public static final List<Integer> FIRST_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(1);add(2);add(3);add(4);
        add(1);add(2);add(3);add(4);add(5);add(6);add(7);add(8);add(9);add(10);
    }};
    public static final List<Integer> FIRST_GROUP_ROW_LIST_SHORT = new ArrayList<Integer>() {{
        add(8);add(9);add(10);
    }};
    public static final List<Integer> SECOND_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(5);add(6);add(7);add(8);add(9);add(10);add(11);
        add(12);add(13);add(14);add(15);add(16);add(17);add(18);
    }};
    public static final List<Integer> THIRD_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(20);add(21);add(22);add(23);
    }};
    @Autowired
    private WrkMastService wrkMastService;
@@ -52,6 +59,8 @@
    private LocDetlService locDetlService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private LocRuleService locRuleService;
    /**
     * 生成工作号
@@ -94,48 +103,163 @@
    }
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int times) {
        LocTypeDto oldLocType = locTypeDto.clone();
        return getLocNo(whsType, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
        return getLocNo(staDescId, sourceStaNo, matNos, locTypeDto, times);
    }
    /**
     * 检索库位号
     * @param whsType 类型 1:双深式货架
     * @param staDescId 路径ID
     * @param sourceStaNo 源站
     * @param matNos 物料号集合
     * @return locNo 检索到的库位号
     */
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, LocTypeDto oldLocType, int times) {
        StartupDto startupDto = new StartupDto();
        whsType = 1;
        // 生成工作号
        int workNo = getWorkNo(0);
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员");
        }
        int curRow = rowLastno.getCurrentRow();  // 当前列
        int sRow = rowLastno.getsRow();          // 起始列
        int eRow = rowLastno.geteRow();          // 终止列
        int crn_qty = rowLastno.getCrnQty();     // 堆垛机数量(巷道数量)
        int rowCount = eRow - sRow + 1;          // 库排总数
        // 目标堆垛机号
        int crnNo = 0;
    public StartupDto getLocNo(Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto,int times) {
        // 目标库位
        LocMast locMast = null;
        // 靠近摆放规则 --- 同天同规格物料
        if (!Cools.isEmpty(matNos) && matNos.size() == 1) {
            List<String> locNos = locDetlService.getSameDetl(matNos.get(0), sRow, eRow);
            for (String locNo : locNos) {
                LocMast locMast0 = locMastService.findOutMost(locNo);
        //混载找库位
        locMast = getLocNoStep1(matNos, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        //库位规则
        locMast = getLocNoStep2(matNos, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        // 靠近摆放规则 --- 相同订单号, 同天同规格物料
        locMast = getLocNoStep3(staDescId, matNos, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        // 靠近摆放规则 --- 空托
        locMast = getLocNoStep4(staDescId, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        // 如果没有相近物料,则按规则轮询货架
        // 开始查找库位 ==============================>>
        locMast = getLocNoStep5(locTypeDto, times);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        //找不到库位,抛出异常
        throw new CoolException("没有空库位");
    }
    /**
     * 混载找库位
     */
    private LocMast getLocNoStep1(List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        //判断当前货物是否为混载货物
        String firstMatNos = matNos.get(0);
        boolean mixed = false;//默认不是混载货物
        for (String matNo : matNos) {
            if (!firstMatNos.equals(matNo)) {
                mixed = true;//混载货物
                break;
            }
        }
        if (!mixed) {
            return locMast;//不是混载货物,直接跳出当前任务
        }
        // 找混载库位规则
        LocRule locRule = locRuleService.findMixed();
        if (locRule == null) {
            return locMast;//没有混载规则,跳出当前任务
        }
        //将库位规则组转换为库位组
        List<LocMast> locMasts = locRuleService.locRuleToLocNos(locRule);
        if (locMasts == null || locMasts.size() == 0) {
            if (locRule.getKeepGo() == 0) {
                //找不到空库位,且禁止继续寻找其他非混载区域库位
                //找不到库位,抛出异常
                throw new CoolException("混载区域没有空库位");
            }
            return locMast;//没有混载规则,跳出当前任务
        }
        for (LocMast one : locMasts) {
            LocMast locMast0 = locMastService.findOutMost(one.getLocNo());
            if (null != locMast0) {
                // 浅库位符合尺寸检测
                if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                    // 因库位移转、需预留空库位
                    if (locMastService.checkEmptyCount(locMast0, 10)) {
                        locMast = locMast0;
                        break;
                    }
                }
            }
        }
        return locMast;
    }
    /**
     * 库位规则
     */
    private LocMast getLocNoStep2(List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        // 库区锁定
        LocRule locRule = locRuleService.find(Cools.isEmpty(matNos) ? null : matNos.get(0));
        if (!Cools.isEmpty(locRule)) {
            List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1(), locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd());
            for (LocMast one : locMasts) {
                LocMast locMast0 = locMastService.findOutMost(one.getLocNo());
                if (null != locMast0) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                        // 浅库位对应堆垛机必须可用且无异常
                        if (basCrnpService.checkSiteError(locMast0.getCrnNo(), true)) {
                            crnNo = locMast0.getCrnNo();
                        // 因库位移转、需预留空库位
                        if (locMastService.checkEmptyCount(locMast0, 10)) {
                            locMast = locMast0;
                            break;
                        }
                    }
                }
            }
        }
        return locMast;
    }
    // 靠近摆放规则 --- 相同订单号, 同天同规格物料
    private LocMast getLocNoStep3(Integer staDescId, List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        if (!Cools.isEmpty(matNos) && matNos.size() == 1 && staDescId == 1) {
            //先判断工作档,查找主档IOType=1, wrkSts < 10的工作明细,料号相同的明细库位
            List<WrkMast> wrkMasts = wrkMastService.selectSameMatnrs(matNos.get(0));
            for (WrkMast wrkMast : wrkMasts) {
                LocMast locMast0 = locMastService.findNearloc(wrkMast.getLocNo());
                if (null != locMast0) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                        locMast = locMast0;
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                //再判断库存明细档,料号相同的明细库位
                List<String> locNos = locDetlService.getSameDetl(matNos.get(0));
                for (String locNo : locNos) {
                    LocMast locMast0 = locMastService.findNearloc(locNo);
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                            locMast = locMast0;
                            break;
                        }
@@ -144,19 +268,35 @@
            }
        }
        return locMast;
    }
    // 靠近摆放规则 --- 空托
    private LocMast getLocNoStep4(Integer staDescId, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("loc_sts", "D").ge("row1", sRow).le("row1", eRow));
            if (locMasts.size() > 0) {
                for (LocMast loc : locMasts) {
                    LocMast locMast0 = locMastService.findOutMost(loc.getLocNo());
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                            // 浅库位对应堆垛机必须可用且无异常
                            if (basCrnpService.checkSiteError(locMast0.getCrnNo(), true)) {
                                crnNo = locMast0.getCrnNo();
            //先判断工作档,查找主档IOType=1, wrkSts < 10的工作明细,料号相同的明细库位
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("io_type",10));
            for (WrkMast wrkMast : wrkMasts) {
                LocMast locMast0 = locMastService.findNearloc(wrkMast.getLocNo());
                if (null != locMast0) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                        locMast = locMast0;
                        break;
                    }
                }
            }
            if (Cools.isEmpty(locMast)){
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                        .eq("loc_sts", "D"));
                if (locMasts.size() > 0) {
                    for (LocMast loc : locMasts) {
                        LocMast locMast0 = locMastService.findNearloc(loc.getLocNo());
                        if (null != locMast0) {
                            // 浅库位符合尺寸检测
                            if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                                locMast = locMast0;
                                break;
                            }
@@ -166,148 +306,62 @@
            }
        }
        // 如果没有相近物料,则按规则轮询货架
        if (null == locMast) {
            if (whsType == 1) {
                if (curRow == 2) {
                    curRow = 30;
                } else if (curRow == 30) {
                    curRow = 2;
                }
            }
            if (basCrnpService.checkSiteError(dualCrnNo, true)) {
                crnNo = dualCrnNo;
        return locMast;
    }
    // 如果没有相近物料,则按规则轮询货架
    private LocMast getLocNoStep5(LocTypeDto locTypeDto, int times) {
        LocMast locMast = null;
        // 1.当检索库排为浅库位排时,优先寻找当前库排的深库位排
        List<LocMast> peakLocs = locMastService.selectAllPeakLoc();
        for (LocMast peakLoc : peakLocs) {
            List<String> groupInsideLoc = Utils.getGroupInsideLoc(peakLoc.getLocNo());
            if (!Cools.isEmpty(groupInsideLoc)) {
                if (!locMastService.checkAllLocEmpty(groupInsideLoc)) continue;
                locMast = peakLoc;
                break;
            } else {
                locMast = peakLoc;
                break;
            }
        }
        if (crnNo == 0) {
            throw new CoolException("没有可用的堆垛机");
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
            log.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
        }
        return locMast;
    }
    //返回dto
    private StartupDto getLocNoStep6(Integer staDescId, Integer sourceStaNo, LocMast locMast) {
        StartupDto startupDto = new StartupDto();
        // 获取目标站
        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                .eq("type_no", staDescId)
                .eq("stn_no", sourceStaNo)
                .eq("crn_no", crnNo);
                .eq("stn_no", sourceStaNo);
        StaDesc staDesc = staDescService.selectOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            log.error("入库路径不存在, staDescId={}, sourceStaNo={}, crnNo={}", staDescId, sourceStaNo, crnNo);
            log.error("入库路径不存在, staDescId={}, sourceStaNo={}", staDescId, sourceStaNo);
            throw new CoolException("入库路径不存在");
        }
        // 检测目标站
        BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
        if (!staNo.getAutoing().equals("Y")) {
            throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
            throw new CoolException("目标站" + staDesc.getCrnStn() + "不可用");
        }
        // 更新库位排号
        rowLastno.setCurrentRow(curRow);
        rowLastnoService.updateById(rowLastno);
        // 开始查找库位 ==============================>>
        // 1.当检索库排为浅库位排时,优先寻找当前库排的深库位排
        if (locMast == null) {
            if (whsType == 1) {
                List<Integer> rows = Utils.getGroupLoc(curRow);
//                Iterator<Integer> iterator = rows.iterator();
//                while (iterator.hasNext()) {
//                    Integer next = iterator.next();
//                    if (next.equals(17) || next.equals(18)) {
//                        iterator.remove();
//                    }
//                }
                List<LocMast> locMasts = locMastService.queryFreeLocMast(rows, rows.size(), locTypeDto.getLocType1());
                if (!Cools.isEmpty(locMasts)) {
                    Integer innermostRow = Utils.getOutLayerRow(locMasts.get(0).getLocNo(), false);
                    for (LocMast one : locMasts) {
                        if (one.getRow1().equals(innermostRow)) {
                            locMast = one;
                            break;
                        }
                    }
                }
                // 因库位移转、需预留空库位 todo:luxiaotao
//            if (!locMastService.checkEmptyCount(locMast)) {
//                locMast = null;
//            }
            } else {
                if (Utils.isShallowLoc(slaveProperties, curRow)) {
                    Integer deepRow = Utils.getDeepRow(slaveProperties, curRow);
                    locMast = locMastService.queryFreeLocMast0(deepRow, locTypeDto.getLocType1());
                    // 因库位移转、需预留空库位
                    if (locMast !=null && !locMastService.checkEmptyCount(locMast)) {
                        locMast = null;
                    }
                }
                if (Cools.isEmpty(locMast)) {
                    locMast = locMastService.queryFreeLocMast0(curRow, locTypeDto.getLocType1());
                    // 因库位移转、需预留空库位
                    if (!locMastService.checkEmptyCount(locMast)) {
                        locMast = null;
                    }
                    // 目标库位 ===>> 浅库位, 则校验其深库位是否为 F D X
                    if (null != locMast && Utils.isShallowLoc(slaveProperties, locMast.getLocNo())) {
                        LocMast deepLoc = locMastService.selectById(Utils.getDeepLoc(slaveProperties, locMast.getLocNo()));
                        if (!deepLoc.getLocSts().equals("F") && !deepLoc.getLocSts().equals("D") && !deepLoc.getLocSts().equals("X")) {
                            locMast = null;
                        }
                    }
                    // 目标库位 ===>> 深库位, 则校验其浅库位是否为 O
                    if (null != locMast && Utils.isDeepLoc(slaveProperties, locMast.getLocNo())) {
                        LocMast shallowLoc = locMastService.selectById(Utils.getShallowLoc(slaveProperties, locMast.getLocNo()));
                        if (!shallowLoc.getLocSts().equals("O")) {
                            locMast = null;
                        }
                    }
                }
            }
        }
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归
            if (times < rowCount) {
                times = times + 1;
                return getLocNo(whsType, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
            } else {
                times = 0;
            }
            // 货物检索低库位仓失败,兼容高库位仓后继续执行
            if (locTypeDto.getLocType1() == 1) {
                locTypeDto.setLocType1((short) 2);
                return getLocNo(1, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
            }
            log.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
            throw new CoolException("没有空库位");
        }
        String locNo = locMast.getLocNo();
        // 生成工作号
        int workNo = getWorkNo(0);
        // 返回dto
        startupDto.setWorkNo(workNo);
        startupDto.setCrnNo(crnNo);
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setStaNo(staNo.getDevNo());
        startupDto.setLocNo(locNo);
        startupDto.setLocNo(locMast.getLocNo());
        return startupDto;
    }
    public static String zerofill(String msg, Integer count) {
        if (msg.length() == count) {
            return msg;
        } else if (msg.length() > count) {
            return msg.substring(0, 16);
        } else {
            StringBuilder msgBuilder = new StringBuilder(msg);
            for(int i = 0; i < count - msg.length(); ++i) {
                msgBuilder.insert(0, "0");
            }
            return msgBuilder.toString();
        }
    }
}