自动化立体仓库 - WMS系统
#
Junjie
2023-10-02 ce5f3c4a6a9bed1c9f2317c45698fe49e63e5d36
src/main/java/com/zy/common/service/CommonService.java
@@ -28,13 +28,30 @@
public class CommonService {
    public static final List<Integer> FIRST_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(2);add(3);add(4);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(1);
    }};
    public static final List<Integer> SECOND_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(18);add(19);add(20);
        add(21);add(22);add(23);add(24);add(25);add(26);add(27);add(28);add(29);add(30);
        add(3);add(4);
    }};
    public static final List<Integer> THIRD_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(6);add(7);
    }};
    public static final List<Integer> FOURTH_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(8);
    }};
    public static final List<Integer> FIFTH_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(10);add(11);
    }};
    public static final List<Integer> SIXTH_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(13);add(14);add(15);add(16);
    }};
    public static final List<Integer> SEVENTH_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(13);add(14);
    }};
    public static final List<Integer> EIGHTH_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(16);
    }};
    @Autowired
    private WrkMastService wrkMastService;
@@ -54,6 +71,8 @@
    private LocDetlService locDetlService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private LocRuleService locRuleService;
    /**
     * 生成工作号
@@ -96,62 +115,121 @@
    }
    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();
        switch (sourceStaNo) {
            case 103:
                whsType = 1;
                break;
            case 111:
            case 211:
                whsType = 2;
                break;
            case 208:
                whsType = 3;
                break;
            default:
                throw new CoolException("检索库位失败,请联系管理员");
        }
        // 生成工作号
        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)) {
            List<String> locNos = locDetlService.getSameDetl(matNos.get(0), sRow, eRow);
            for (String locNo : locNos) {
                List<String> groupLoc = Utils.getGroupLoc(locNo);
                locMast = locMastService.findOutMost(groupLoc);
                if (null != locMast) {
        // 靠近摆放规则 --- 空托
        locMast = getLocNoStep4(staDescId, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        //库位规则
        locMast = getLocNoStepRule(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 = getLocNoStepSingle(matNos, locTypeDto);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        //搜索混放
        locMast = getLocNoStepMixed(matNos, locTypeDto, times);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
        //找不到库位,抛出异常
        throw new CoolException("没有空库位");
    }
    /**
     * 库位规则
     */
    private LocMast getLocNoStepRule(List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        //库位规则只能在单品货物中生效
        if (matNos.size() == 1) {
            // 库区锁定
            List<LocRule> locRules = locRuleService.find(Cools.isEmpty(matNos) ? null : matNos.get(0));
            if (locRules == null || locRules.isEmpty()) {
                return null;//没有库位规则,跳出当前任务
            }
            for (LocRule locRule : locRules) {
                if (locRule == null) {
                    continue;
                }
                List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1(), locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd());
                for (LocMast locMast0 : locMasts) {
                    //预留空库位
                    if (locMastService.checkEmptyCount(locMast0, 10)) {
                        return locMast0;
                    }
                }
            }
            if (locRules.get(0).getKeepGo() == 0) {
                //找不到空库位,且禁止继续寻找其他非混载区域库位
                //找不到库位,抛出异常
                throw new CoolException("规则区域没有空库位");
            }
        }
        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(locMast, locTypeDto)) {
                        // 浅库位对应堆垛机必须可用且无异常
                        if (basCrnpService.checkSiteError(locMast.getCrnNo(), true)) {
                            crnNo = locMast.getCrnNo();
                    if (VersionUtils.checkLocType(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.checkLocType(locMast0, locTypeDto)) {
                            locMast = locMast0;
                            break;
                        }
                    }
@@ -159,19 +237,36 @@
            }
        }
        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) {
                    List<String> groupLoc = Utils.getGroupLoc(loc.getLocNo());
                    locMast = locMastService.findOutMost(groupLoc);
                    if (null != locMast) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.locMoveCheckLocType(locMast, locTypeDto)) {
                            // 浅库位对应堆垛机必须可用且无异常
                            if (basCrnpService.checkSiteError(locMast.getCrnNo(), true)) {
                                crnNo = locMast.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.checkLocType(locMast0, locTypeDto)) {
                        locMast = locMast0;
                        break;
                    }
                }
            }
            if (Cools.isEmpty(locMast)){
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                        .eq("loc_sts", "O"));
                if (locMasts.size() > 0) {
                    for (LocMast loc : locMasts) {
                        LocMast locMast0 = locMastService.findNearloc(loc.getLocNo());
                        if (null != locMast0) {
                            // 浅库位符合尺寸检测
                            if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
                                locMast = locMast0;
                                break;
                            }
                        }
@@ -180,118 +275,101 @@
            }
        }
        // 如果没有相近物料,则按规则轮询货架
        if (null == locMast) {
            switch (whsType) {
                case 1:
                    if (curRow == 1) {
                        curRow = 7;
                    } else if (curRow == 7) {
                        curRow = 1;
                    }
                    crnNo = 1;
        return locMast;
    }
    // 搜索混放
    private LocMast getLocNoStepMixed(List<String> matNos, LocTypeDto locTypeDto, int times) {
        LocMast locMast = null;
        if (matNos.size() > 1) {
            List<LocMast> locMasts = locMastService.selectAreaEmpty(locTypeDto.getLocType1(), 1);//搜索混放库位
            //混放区域直接取第一个即可
            for (LocMast mast : locMasts) {
                //预留空库位
                if (locMastService.checkEmptyCount(mast, 10)) {
                    locMast = mast;
                    break;
                case 2:
                    if (curRow == 8) {
                        curRow = 14;
                    } else if (curRow == 14) {
                        curRow = 8;
                    }
                    crnNo = 2;
                    break;
                case 3:
                    if (curRow == 15) {
                        curRow = 21;
                    } else if (curRow == 21) {
                        curRow = 15;
                    }
                    crnNo = 3;
                    break;
                default:
                    throw new CoolException("检索库位失败,请联系管理员");
                }
            }
        }
        if (!basCrnpService.checkSiteError(crnNo, true)) {
            throw new CoolException("没有可用的堆垛机");
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
            log.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
        }
        if (crnNo == 0) {
            throw new CoolException("没有可用的堆垛机");
        return locMast;
    }
    // 搜索单品
    private LocMast getLocNoStepSingle(List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        // 1.先搜索单品
        if (matNos.size() == 1) {
            //单品
            List<LocMast> locMasts = locMastService.selectAreaEmpty(locTypeDto.getLocType1(), 2);//搜索单品库位
            for (LocMast mast : locMasts) {
                List<String> groupLoc = Utils.getGroupLoc(mast.getLocNo());
                if (!locMastService.checkAllLocEmpty(groupLoc)) {
                    continue;
                }
                LocMast tmp = null;
                for (String loc : groupLoc) {
                    LocMast locMast1 = locMastService.selectByLoc(loc);
                    if (locMast1 == null) {
                        continue;
                    }
                    if (!locMast1.getLocSts().equals("O")) {
                        continue;
                    }
                    tmp = locMast1;
                    break;
                }
                //预留空库位
                if (tmp != null && locMastService.checkEmptyCount(mast, 10)) {
                    locMast = tmp;
                    break;
                }
            }
        }
        return locMast;
    }
    //返回dto
    private StartupDto getLocNoStep6(Integer staDescId, Integer sourceStaNo, LocMast locMast) {
        StartupDto startupDto = new StartupDto();
        String locNo = locMast.getLocNo();
        // 获取目标站
        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                .eq("type_no", staDescId)
                .eq("stn_no", sourceStaNo)
                .eq("crn_no", crnNo);
                .eq("crn_no",Utils.stnNoToLiftNo(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) {
            List<Integer> rows = Utils.getGroupLoc(curRow);
            locMast = locMastService.queryFreeLocMast(null, rows, locTypeDto.getLocType1());
            // 因库位移转、需预留空库位
//            if (!locMastService.checkEmptyCount(locMast)) {
//                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();
        }
    }
}