自动化立体仓库 - WMS系统
whycq
2024-11-25 bcb19e8c93c3cabf0d8eb3ee34ed819b3c5a10ef
src/main/java/com/zy/common/service/CommonService.java
@@ -1,5 +1,6 @@
package com.zy.common.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
@@ -7,22 +8,50 @@
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.asrs.utils.VersionUtils;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.Shelves;
import com.zy.common.model.StartupDto;
import com.zy.common.properties.SlaveProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
 * 货架核心功能
 * Created by vincent on 2020/6/11
 */
@Slf4j
@Service
public class CommonService {
    public static final List<Integer> FIRST_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(1);
    }};
    public static final List<Integer> SECOND_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        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;
@@ -42,10 +71,12 @@
    private LocDetlService locDetlService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private LocRuleService locRuleService;
    /**
     * 生成工作号
     * @param wrkMk 0:入库 1 - 3000 ; 1:拣料/并板/盘点 3001 - 6000 ; 2: 出库 6001 -9000 ; 3:其他 9001 -9999
     * @param wrkMk 0:入出库 ; 1:其他
     * @return workNo(工作号)
     */
    public int getWorkNo(Integer wrkMk) {
@@ -53,11 +84,14 @@
        if (Cools.isEmpty(wrkLastno)) {
            throw new CoolException("数据异常,请联系管理员");
        }
        int workNo = wrkLastno.getWrkNo();
        int workNo = 0;
        // 入出库类型
        workNo = wrkLastno.getWrkNo();
        int sNo = wrkLastno.getSNo();
        int eNo = wrkLastno.getENo();
        workNo = workNo>=eNo ? sNo : workNo+1;
        while (true) {
            WrkMast wrkMast = wrkMastService.selectById(workNo);
            if (null != wrkMast) {
@@ -66,12 +100,10 @@
                break;
            }
        }
        // 修改序号记录
        if (workNo > 0){
            wrkLastno.setWrkNo(workNo);
            wrkLastnoService.updateById(wrkLastno);
        }
        // 检验
        if (workNo == 0) {
            throw new CoolException("生成工作号失败,请联系管理员");
        } else {
@@ -82,199 +114,604 @@
        return workNo;
    }
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int times) {
        return getLocNo(staDescId, sourceStaNo, matNos, locTypeDto, times);
    }
    /**
     * 检索库位号
     * @param whsType 类型 1:双深式货架
     * @param staDescId 路径工作类型
     *
     * @param staDescId   路径ID
     * @param sourceStaNo 源站
     * @param matNos 物料号集合
     * @param matNos      物料号集合
     * @return locNo 检索到的库位号
     */
    @Transactional
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto) {
        StartupDto startupDto = new StartupDto();
        // 生成工作号
        int workNo = getWorkNo(0);
        switch (sourceStaNo) {
            case 202:
                whsType = 2;
                break;
            case 205:
                whsType = 1;
                break;
            case 126:
                whsType = 3;
                break;
            default:
                throw new CoolException("库位排号分配错误, 源站号:" + sourceStaNo);
        }
        startupDto.setWorkNo(workNo);
        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.getSameDetlToday(matNos.get(0));
            for (String locNo : locNos) {
                if (Utils.isShallowLoc(slaveProperties, locNo)) {
        // 靠近摆放规则 --- 空托
        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 = getLocNoCodeStepRule(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
        }
        if(locTypeDto.getLocType1()==1){
            locTypeDto.setLocType1((short)2);
            return getLocNo(staDescId, 1, sourceStaNo, matNos, locTypeDto,0);
        }else if(locTypeDto.getLocType1()==2){
            locTypeDto.setLocType1((short)3);
            return getLocNo(staDescId, 1, sourceStaNo, matNos, locTypeDto,0);
        }
        //找不到库位,抛出异常
        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;
                }
                String shallowLocNo = Utils.getShallowLoc(slaveProperties, locNo);
                // 检测目标库位是否为空库位
                LocMast shallowLoc = locMastService.selectById(shallowLocNo);
                if (shallowLoc != null && shallowLoc.getLocSts().equals("O")) {
                    locMast = shallowLoc;
                    crnNo = locMast.getCrnNo();
                List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1(), locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd());
                locMast = locFilter(locMasts, matNos,locTypeDto);
            }
            if (locRules.get(0).getKeepGo() == 0) {
                //找不到空库位,且禁止继续寻找其他非混载区域库位
                //找不到库位,抛出异常
                throw new CoolException("规则区域没有空库位");
            }
        }
        return locMast;
    }
    /**
     * 库位编码规则
     */
    private LocMast getLocNoCodeStepRule(List<String> matNos, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        //库位编码规则只能在相同编码货物中生效
        String matnr = "";
        for(String matNo : matNos){
            if("".equals(matnr)){
                matnr = matNo.substring(0, 4);
            }
            if(!matnr.equals(matNo.substring(0, 4))){
                return null;
            }
        }
        // 库区锁定
        List<LocRule> locRules = locRuleService.findCode(Cools.isEmpty(matNos) ? null : matNos.get(0),null);
        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());
            locMast = locFilter(locMasts, matNos,locTypeDto);
        }
//            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.checkLocType(locMast0, locTypeDto)) {
                        locMast = locMast0;
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                //再判断库存明细档,料号相同的明细库位
                List<String> locNos = locDetlService.getSameDetl(matNos.get(0));
                for (String locNo : locNos) {
                    LocMast locMast0 = locMastService.findInnerLoc(locNo);
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
                            locMast = locMast0;
                            break;
                        }
                    }
                }
            }
        }
        return locMast;
    }
    // 靠近摆放规则 --- 空托
    private LocMast getLocNoStep4(Integer staDescId, LocTypeDto locTypeDto) {
        LocMast locMast = null;
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            //先判断工作档,查找主档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;
                            }
                        }
                    }
                }
            }
        }
        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;
                }
            }
        }
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("loc_sts", "D"));
            if (locMasts.size() > 0) {
                for (LocMast loc : locMasts) {
                    if (Utils.isShallowLoc(slaveProperties, loc.getLocNo())) {
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
            log.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
        }
        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;
                    }
                    String shallowLocNo = Utils.getShallowLoc(slaveProperties,  loc.getLocNo());
                    // 检测目标库位是否为空库位
                    LocMast shallowLoc = locMastService.selectById(shallowLocNo);
                    if (shallowLoc != null && shallowLoc.getLocSts().equals("O")) {
                        locMast = shallowLoc;
                        crnNo = locMast.getCrnNo();
                    if (!locMast1.getLocSts().equals("O")) {
                        continue;
                    }
                    tmp = locMast1;
                    break;
                }
                //预留空库位
                if (tmp != null && locMastService.checkEmptyCount(mast, 10)) {
                    locMast = tmp;
                    break;
                }
            }
            if (locMast == null) {
                //单品找不到库位,允许尝试从混放区域中进行搜索
                List<LocMast> locMasts2 = locMastService.selectAreaEmpty(locTypeDto.getLocType1(), 1);//搜索单品库位
                //混放区域直接取第一个即可
                for (LocMast mast : locMasts2) {
                    //预留空库位
                    if (locMastService.checkEmptyCount(mast, 10)) {
                        locMast = mast;
                        break;
                    }
                }
            }
        }
        // 如果没有相近物料,则按规则轮询货架
        if (null == locMast) {
            // 1-4排
            if (whsType == 1) {
                // 获取目标站所在货架排号
                Shelves shelves = new Shelves(rowCount, crn_qty);
                curRow = shelves.start(curRow);
                if (curRow < 0) {
                    throw new CoolException("检索库位失败,请联系管理员");
                }
                for (List<Integer> node : shelves.nodes){
                    if (node.contains(curRow)) {
                        crnNo = shelves.nodes.indexOf(node) + 1;
                        break;
        return locMast;
    }
    /*
    库存规则搜索到的库位确认
     */
    private LocMast locFilter(List<LocMast> locMasts,List<String> matNos,LocTypeDto locTypeDto){
        for (LocMast locMast0 : locMasts) {
            if(!VersionUtils.checkLocType(locMast0,locTypeDto)){
                continue;
            }
            //单品物料
            if (matNos.size() == 1) {
                //如果不是深浅,则直接返回当前库位
                if(Cools.isEmpty(Utils.getGroupDeepLoc(locMast0.getLocNo()))){
                    return locMast0;
                }else{
                    List<String> groupInnerLoc = Utils.getGroupInnerLoc(locMast0.getLocNo());
                    //如果是深浅库位,但是内测无库位的情况下,则直接返回当前库位
                    if (Cools.isEmpty(groupInnerLoc)) {
                        return locMast0;
                    }else {
                        //获取最深的库位
                        String loc = groupInnerLoc.get(groupInnerLoc.size() - 1);
                        LocMast locMast1 = locMastService.selectByLoc(loc);
                        //如果最深测库位为空库位,则直接返回最深库位
                        if("O".equals(locMast1.getLocSts())){
                            return locMast1;
                        }else {
                            //如果最深库位状态不为F或者O,则跳出当前的深浅库位查找
                            if(!"F".equals(locMast1.getLocSts())){
                                continue;
                            }
                            List<LocDetl> locDetls = locDetlService.selectByLocNo(loc);
                            //如果当前深库位物料与入库物料不相符,则跳出当前深浅库位查找
                            if(!Cools.eq(locDetls.get(0).getMatnr(),matNos.get(0))){
                                continue;
                            }
                            //从里往外依次遍历该组深浅库位
                            for (int i=groupInnerLoc.size()-1; i>=0 ;i--){
                                LocMast locMast2 = locMastService.selectByLoc(groupInnerLoc.get(i));
                                if("O".equals(locMast2.getLocSts())){
                                    //如果深库位中含有0状态的库位,则直接返回该库位
                                    return locMast2;
                                }else if("F".equals(locMast2.getLocSts())){
                                    //如果对于locaMast0最近的一个库位也为F状态,则返回locaMastO
                                    if(i==0){
                                        return locMast0;
                                    }else {
                                        //如果该组深浅库位中含有既不为F也不为O的库位,则跳出当前深浅库位查找
                                        continue;
                                    }
                                }else {
                                    break;
                                }
                            }
                        }
                    }
                }
                // 5-8排
            } else if (whsType == 2) {
                // 获取目标站所在货架排号
                Shelves shelves = new Shelves(rowCount, crn_qty);
                curRow = shelves.start(curRow - 4);
                if (curRow < 0) {
                    throw new CoolException("检索库位失败,请联系管理员");
                }
                for (List<Integer> node : shelves.nodes){
                    if (node.contains(curRow)) {
                        crnNo = shelves.nodes.indexOf(node) + 1;
                        break;
                    }
                }
                // 偏移量补偿
                curRow = curRow + 4;
                crnNo = crnNo + 1;
                // 126空板入 1-8排
            } else {
                // 获取目标站所在货架排号
                Shelves shelves = new Shelves(rowCount, crn_qty);
                curRow = shelves.start(curRow);
                if (curRow < 0) {
                    throw new CoolException("检索库位失败,请联系管理员");
                }
                for (List<Integer> node : shelves.nodes){
                    if (node.contains(curRow)) {
                        crnNo = shelves.nodes.indexOf(node) + 1;
                        break;
                    }
            }else {
                if(Cools.isEmpty(Utils.getGroupDeepLoc(locMast0.getLocNo()))){
                    return locMast0;
                }
            }
        }
        basCrnpService.checkSiteStatus(crnNo);
        }
        return null;
    }
    //返回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={}", staDescId, sourceStaNo);
            throw new CoolException("入库路径不存在");
        }
        // 检测目标站
        BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
        int inQty = staNo.getInQty()==null?0:staNo.getInQty();
        if (staNo.getAutoing().equals("Y") && inQty<2) {
            // 查找库位
            if (locMast == null) {
                if (Utils.isShallowLoc(slaveProperties, curRow)) {
                    Integer deepRow = Utils.getDeepRow(slaveProperties, curRow);
                    locMast = locMastService.queryFreeLocMast(deepRow, locTypeDto.getLocType1());
                }
                if (Cools.isEmpty(locMast)) {
                    locMast = locMastService.queryFreeLocMast(curRow, locTypeDto.getLocType1());
                }
            }
            if (Cools.isEmpty(locMast)) {
                // 轻货物找轻库位为空时,可以去找重库位仓
                if (locTypeDto.getLocType1() == 1) {
                    locTypeDto.setLocType1((short) 2);
                    return getLocNo(null, staDescId, sourceStaNo, matNos, locTypeDto);
                }
                throw new CoolException("没有空库位");
            }
            String locNo = locMast.getLocNo();
            // 更新库位排号
            rowLastno.setCurrentRow(curRow);
            rowLastnoService.updateById(rowLastno);
            // 返回dto
            startupDto.setCrnNo(crnNo);
            startupDto.setSourceStaNo(sourceStaNo);
            startupDto.setStaNo(staNo.getDevNo());
            startupDto.setLocNo(locNo);
        } else {
            throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
        if (!staNo.getAutoing().equals("Y")) {
            throw new CoolException("目标站" + staDesc.getCrnStn() + "不可用");
        }
        // 生成工作号
        int workNo = getWorkNo(0);
        // 返回dto
        startupDto.setWorkNo(workNo);
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setStaNo(staNo.getDevNo());
        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();
        }
    /**
     * 四向库获取库位
     */
    public StartupDto getShuttleLocNo(Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto) {
        RowLastno rowLastno = rowLastnoService.selectOne(new EntityWrapper<RowLastno>().eq("whs_type", 1));
        //起始层
        Integer startLev = rowLastno.getCurrentRow();
        return getShuttleLocNo(staDescId, sourceStaNo, matNos, locTypeDto, startLev, 0);
    }
}
    public StartupDto getShuttleLocNo(Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int startLev, int times) {
        RowLastno rowLastno = rowLastnoService.selectOne(new EntityWrapper<RowLastno>().eq("whs_type", 1));
        //起始层
        Integer currentLev = rowLastno.getCurrentRow();
        if (times > 0 && startLev == currentLev) {
            throw new CoolException("未找到可用库位");
        }
        rowLastno.setCurrentRow(currentLev >= rowLastno.geteRow() ? rowLastno.getsRow() : currentLev + 1);//更新楼层
        rowLastnoService.updateById(rowLastno);
        // 目标库位
        LocMast locMast = null;
        // 靠近摆放规则 --- 空托
        locMast = getShuttleEmptyLocNo(staDescId, locTypeDto, currentLev);
        if (locMast != null) {
            //找到库位,返回dto
            return getShuttleLocStartupDto(staDescId, sourceStaNo, locMast);//返回dto
        }
        //单品物料 靠近摆放规则 --- 相同订单号, 同天同规格物料
        locMast = getShuttleSingleLocNo(matNos, locTypeDto, currentLev);
        if (locMast != null) {
            //找到库位,返回dto
            return getShuttleLocStartupDto(staDescId, sourceStaNo, locMast);//返回dto
        }
        // 靠近摆放规则 --- 相同订单号, 同天同规格物料
        locMast = getShuttleMixedLocNo(matNos, locTypeDto, currentLev);
        if (locMast != null) {
            //找到库位,返回dto
            return getShuttleLocStartupDto(staDescId, sourceStaNo, locMast);//返回dto
        }
        return getShuttleLocNo(staDescId, sourceStaNo, matNos, locTypeDto, startLev, times + 1);
    }
    //搜索空货架库位
    private LocMast getShuttleEmptyLocNo(Integer staDescId, LocTypeDto locTypeDto, int lev) {
        LocMast locMast = null;
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            //先判断工作档,查找主档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").eq("lev1", lev).orderBy("row1,bay1,lev1"));
                if (!locMasts.isEmpty()) {
                    for (LocMast loc : locMasts) {
                        LocMast locMast0 = locMastService.findNearloc(loc.getLocNo());
                        if (null != locMast0) {
                            // 浅库位符合尺寸检测
                            if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
                                locMast = locMast0;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return locMast;
    }
    //混放物料
    private LocMast getShuttleMixedLocNo(List<String> matNos, LocTypeDto locTypeDto, Integer lev) {
        LocMast locMast = null;
        if (matNos.size() > 1) {
            List<LocMast> locMasts = locMastService.selectAreaEmptyByLev(locTypeDto.getLocType1(), 1, lev);//搜索混放库位
            //混放区域直接取第一个即可
            for (LocMast mast : locMasts) {
                // 浅库位符合尺寸检测
                if (VersionUtils.checkLocType(mast, locTypeDto)) {
                    locMast = mast;
                    break;
                }
            }
        }
        return locMast;
    }
    //单品物料 靠近摆放规则 --- 相同订单号, 同天同规格物料
    private LocMast getShuttleSingleLocNo(List<String> matNos, LocTypeDto locTypeDto, int lev) {
        LocMast locMast = null;
        if (matNos.size() == 1) {
            String matNo = matNos.get(0);//物料号
            //先判断工作档,查找主档IOType=1, wrkSts < 10的工作明细,料号相同的明细库位
            List<WrkMast> wrkMasts = wrkMastService.selectSameLevMatnrs(matNo, lev);
            for (WrkMast wrkMast : wrkMasts) {
                LocMast locMast0 = locMastService.findNearloc(wrkMast.getLocNo());
                if (null != locMast0) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
                        locMast = locMast0;
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                //再判断库存明细档,料号相同的明细库位
                List<String> locNos = locDetlService.getSameLevDetl(matNo, lev);
                for (String locNo : locNos) {
                    LocMast locMast0 = locMastService.findInnerLoc(locNo);
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
                            locMast = locMast0;
                            break;
                        }
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                //找新库位
                List<LocMast> locMasts = locMastService.selectAreaEmptyByLev(locTypeDto.getLocType1(), 2, lev);//搜索单品库位
                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;
                        }
                        // 浅库位符合尺寸检测
                        if (VersionUtils.checkLocType(locMast1, locTypeDto)) {
                            tmp = locMast1;
                            break;
                        }
                    }
                    if (!Cools.isEmpty(tmp)) {
                        locMast = tmp;
                        break;
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                //单品找不到库位,允许尝试从混放区域中进行搜索
                List<LocMast> locMasts2 = locMastService.selectAreaEmptyByLev(locTypeDto.getLocType1(), 1, lev);//搜索混放库位
                //混放区域直接取第一个即可
                for (LocMast mast : locMasts2) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.checkLocType(mast, locTypeDto)) {
                        locMast = mast;
                        break;
                    }
                }
            }
        }
        return locMast;
    }
    //返回dto
    private StartupDto getShuttleLocStartupDto(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",Utils.stnNoToLiftNo(sourceStaNo));//借用堆垛机号字段充当提升机号
        StaDesc staDesc = staDescService.selectOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            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() + "不可用");
        }
        // 生成工作号
        int workNo = getWorkNo(0);
        // 返回dto
        startupDto.setWorkNo(workNo);
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setStaNo(staNo.getDevNo());
        startupDto.setLocNo(locMast.getLocNo());
        return startupDto;
    }
}