自动化立体仓库 - WMS系统
pang.jiabao
6 天以前 b6e0bec71de45cad64e9de14977770053be7e010
src/main/java/com/zy/common/service/CommonService.java
@@ -3,23 +3,28 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Arith;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.entity.result.FindLocNoAttributeVo;
import com.zy.asrs.mapper.LocMastMapper;
import com.zy.asrs.mapper.WrkMastMapper;
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 com.zy.common.web.param.SearchLocParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 货架核心功能
@@ -36,6 +41,8 @@
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private RowLastnoTypeService rowLastnoTypeService;
    @Autowired
    private BasCrnpService basCrnpService;
    @Autowired
    private StaDescService staDescService;
@@ -47,10 +54,19 @@
    private LocDetlService locDetlService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Resource
    private WrkMastMapper wrkMastMapper;
    @Resource
    private LocMastMapper locMastMapper;
    /**
     * 生成工作号
     * @param wrkMk 0:入库 1 - 3000 ; 1:拣料/并板/盘点 3001 - 6000 ; 2: 出库 6001 -9000 ; 3:其他 9001 -9999
     *
     * @param wrkMk
     * @return workNo(工作号)
     */
    public int getWorkNo(Integer wrkMk) {
@@ -62,17 +78,17 @@
        int workNo = wrkLastno.getWrkNo();
        int sNo = wrkLastno.getSNo();
        int eNo = wrkLastno.getENo();
        workNo = workNo>=eNo ? sNo : workNo+1;
        workNo = workNo >= eNo ? sNo : workNo + 1;
        while (true) {
            WrkMast wrkMast = wrkMastService.selectById(workNo);
            if (null != wrkMast) {
                workNo = workNo>=eNo ? sNo : workNo+1;
                workNo = workNo >= eNo ? sNo : workNo + 1;
            } else {
                break;
            }
        }
        // 修改序号记录
        if (workNo > 0){
        if (workNo > 0) {
            wrkLastno.setWrkNo(workNo);
            wrkLastnoService.updateById(wrkLastno);
        }
@@ -80,46 +96,425 @@
        if (workNo == 0) {
            throw new CoolException("生成工作号失败,请联系管理员");
        } else {
            if (wrkMastService.selectById(workNo)!=null) {
            if (wrkMastService.selectById(workNo) != null) {
                throw new CoolException("生成工作号" + workNo + "在工作档中已存在");
            }
        }
        return workNo;
    }
    //拆盘机处空板扫码,驱动托盘向码垛位,不入库
    @Transactional
    public StartupDto getScanBarcodeEmptyBoard() {
        StartupDto startupDto = new StartupDto();
        Integer staNo = 0;
        if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 201).eq("staNo", 216)) < 2) {
            staNo = 216;
        }
        if (wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("io_type", 201).eq("staNo", 220)) < 2) {
            staNo = 220;
        }
        startupDto.setStaNo(staNo);
        return startupDto;
    }
    /**
     * 检索库位号
     * @param whsType 类型 1:双深式货架
     * @param staDescId 路径工作类型
     * @param sourceStaNo 源站
     * @param matNos 商品编号集合
     *
     * @param staDescId            路径ID
     * @param sourceStaNo          源站
     * @param findLocNoAttributeVo 属性
     * @param locTypeDto           类型
     * @return locNo 检索到的库位号
     */
    @Transactional
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int times) {
        whsType = getWhsType(sourceStaNo, times);
    public StartupDto getLocNo(Integer staDescId, Integer sourceStaNo, FindLocNoAttributeVo findLocNoAttributeVo, LocTypeDto locTypeDto) {
        try {
            Integer whsType = Utils.GetWhsType(sourceStaNo);
            RowLastno rowLastno = rowLastnoService.selectById(whsType);
            RowLastnoType rowLastnoType = rowLastnoTypeService.selectById(rowLastno.getTypeId());
            /**
             * 库型 1: 标准堆垛机库  2: 平库  3: 穿梭板  4: 四向车  5: AGV  0: 未知
             */
            switch (rowLastnoType.getType()) {
                case 1:
                    return getLocNoRun(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, 0, locTypeDto, 0);
                case 2:
                    log.error("站点={} 未查询到对应的规则", sourceStaNo);
                    break;
                case 3:
                    log.error("站点={} 未查询到对应的规则", sourceStaNo);
                    break;
                case 4:
                    return getLocNoRun4(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, 4, locTypeDto, 0);
                case 5:
                    return getLocNoRun5(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, 0, locTypeDto, 0);
                default:
                    log.error("站点={} 未查询到对应的规则", sourceStaNo);
                    break;
            }
        } catch (Exception e) {
            log.error("站点={} 未查询到对应的规则", sourceStaNo);
        }
        return null;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public StartupDto getLocNoNew(Integer staDescId, Integer sourceStaNo, FindLocNoAttributeVo findLocNoAttributeVo, int locArea) {
        // 目标库位
        LocMast locMast;
        // 入空桶库
        if (staDescId == 1 && locArea == 2) {
            List<WrkMast> wrkMastList = wrkMastMapper.selectLastInEmptyLoc(); // 前面入空桶库的任务(未执行堆垛机入库)
            if (wrkMastList.size() == 0) {
                // 取新库位组的第一个库位
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("crn_no", 4).eq("loc_sts", "O"));
                List<LocMast> collect = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 9 || locMast1.getRow1() == 11).collect(Collectors.toList());
                List<LocMast> collect1 = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 10 || locMast1.getRow1() == 12).collect(Collectors.toList()); // 第一个先放这两排
                if (collect.size() == 0 || collect1.size() == 0) {
                    log.error("-----空桶库无空库位----");
                    throw new CoolException("空桶库无空库位");
                }
                // 可用库位组
                List<LocMast> locMastList = new ArrayList<>();
                for(LocMast locMast1:collect) {
                    Optional<LocMast> first = collect1.stream().filter(locMast2 -> locMast1.getRow1() + 1 == locMast2.getRow1() && Objects.equals(locMast1.getLev1(), locMast2.getLev1())
                            && Objects.equals(locMast1.getBay1(), locMast2.getBay1())).findFirst();
                    first.ifPresent(locMastList::add);
                }
                if (locMastList.size() == 0) {
                    log.error("-----空桶库无对应空空桶组----");
                    throw new CoolException("空桶库无对应空空桶组");
                }
                String model = findLocNoAttributeVo.getModel(); // 当前桶类型
                if (model.equals("208L") && locMastList.size() <= 40 * 2 * 2) { // 最上面一层只能放208L,库位剩余量不多时,如果是208L桶入库,则先从最顶层放
                    locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).reversed().thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                } else {
                    locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                }
                locMast = locMastList.get(0);
            } else {
                WrkMast wrkMast = wrkMastList.get(0); // 最后一个入空桶库任务
                // 前一个任务桶类型
                String oldModel = wrkMast.getPdcType();
                // 前一个任务尾托标识
                Integer oldCtnType = wrkMast.getCtnType();
                // 当前桶类型
                String matnr = findLocNoAttributeVo.getMatnr();
                if(findLocNoAttributeVo.getMatnr().equals(wrkMast.getPdcType())) { // 当前桶与前桶是一种类型
                    // 当前桶类型的任务有几个
                    long count = wrkMastList.stream().filter(wrkMast1 -> wrkMast1.getPdcType().equals(matnr)).count();
                    if (count % 2  == 0) { // 偶数,取一组新库位
                        // 取新库位组的第一个库位
                        List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("crn_no", 4).eq("loc_sts", "O"));
                        List<LocMast> collect = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 9 || locMast1.getRow1() == 11).collect(Collectors.toList());
                        List<LocMast> collect1 = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 10 || locMast1.getRow1() == 12).collect(Collectors.toList()); // 第一个先放这两排
                        if (collect.size() == 0 || collect1.size() == 0) {
                            log.error("-----空桶库无空库位----");
                            throw new CoolException("空桶库无空库位");
                        }
                        // 可用库位组
                        List<LocMast> locMastList = new ArrayList<>();
                        for(LocMast locMast1:collect) {
                            Optional<LocMast> first = collect1.stream().filter(locMast2 -> locMast1.getRow1() + 1 == locMast2.getRow1() && Objects.equals(locMast1.getLev1(), locMast2.getLev1())
                                    && Objects.equals(locMast1.getBay1(), locMast2.getBay1())).findFirst();
                            first.ifPresent(locMastList::add);
                        }
                        if (locMastList.size() == 0) {
                            log.error("-----空桶库无对应空空桶组----");
                            throw new CoolException("空桶库无对应空空桶组");
                        }
                        String model = findLocNoAttributeVo.getModel(); // 当前桶类型
                        if (model.equals("208L") && locMastList.size() <= 40 * 2 * 2) { // 最上面一层只能放208L,库位剩余量不多时,如果是208L桶入库,则先从最顶层放
                            locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).reversed().thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                        } else {
                            locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                        }
                        locMast = locMastList.get(0);
                    } else { // 奇数,取前一库位对应的空库位
                        String locNo = wrkMast.getLocNo();
                        int row = Integer.parseInt(locNo.substring(0, 2));
                        if(row == 10 || row == 12) {
                            String newLocNo =(row == 10 ? "09" : "11") + locNo.substring(2);
                            locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", newLocNo).eq("loc_sts","O"));
                            if (locMast == null) {
                                log.error("-----与前面桶类型一样,前面该桶类型任务数是奇数,10,12排,但是对应库位不为空----");
                                throw new CoolException("与前面桶类型一样,前面该桶类型任务数是奇数,是10,12排,但是对应库位不为空");
                            }
                        } else {
                            log.error("-----与前面桶类型一样,前面该桶类型任务数是奇数,但是不是10,12排----");
                            throw new CoolException("与前面桶类型一样,前面该桶类型任务数是奇数,但是不是10,12排");
                        }
                    }
                } else { // 当前桶与前桶不是一种类型
                      if(oldCtnType == 1) { // 前面是尾桶
                          // 取新库位组的第一个库位
                          List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("crn_no", 4).eq("loc_sts", "O"));
                          List<LocMast> collect = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 9 || locMast1.getRow1() == 11).collect(Collectors.toList());
                          List<LocMast> collect1 = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 10 || locMast1.getRow1() == 12).collect(Collectors.toList()); // 第一个先放这两排
                          if (collect.size() == 0 || collect1.size() == 0) {
                              log.error("-----空桶库无空库位----");
                              throw new CoolException("空桶库无空库位");
                          }
                          // 可用库位组
                          List<LocMast> locMastList = new ArrayList<>();
                          for(LocMast locMast1:collect) {
                              Optional<LocMast> first = collect1.stream().filter(locMast2 -> locMast1.getRow1() + 1 == locMast2.getRow1() && Objects.equals(locMast1.getLev1(), locMast2.getLev1())
                                      && Objects.equals(locMast1.getBay1(), locMast2.getBay1())).findFirst();
                              first.ifPresent(locMastList::add);
                          }
                          if (locMastList.size() == 0) {
                              log.error("-----空桶库无对应空空桶组----");
                              throw new CoolException("空桶库无对应空空桶组");
                          }
                          String model = findLocNoAttributeVo.getModel(); // 当前桶类型
                          if (model.equals("208L") && locMastList.size() <= 40 * 2 * 2) { // 最上面一层只能放208L,库位剩余量不多时,如果是208L桶入库,则先从最顶层放
                              locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).reversed().thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                          } else {
                              locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                          }
                          locMast = locMastList.get(0);
                      } else { // 前面不是尾桶
                          // 判断前面入空桶库任务数量(未执行堆垛机入库)
                          long count = wrkMastList.stream().filter(wrkMast1 -> wrkMast1.getPdcType().equals(oldModel)).count();
                          if (count % 2 == 0) { // 偶数,可以入库
                              // 取新库位组的第一个库位
                              List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("crn_no", 4).eq("loc_sts", "O"));
                              List<LocMast> collect = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 9 || locMast1.getRow1() == 11).collect(Collectors.toList());
                              List<LocMast> collect1 = locMasts.stream().filter(locMast1 -> locMast1.getRow1() == 10 || locMast1.getRow1() == 12).collect(Collectors.toList()); // 第一个先放这两排
                              if (collect.size() == 0 || collect1.size() == 0) {
                                  log.error("-----空桶库无空库位----");
                                  throw new CoolException("空桶库无空库位");
                              }
                              // 可用库位组
                              List<LocMast> locMastList = new ArrayList<>();
                              for(LocMast locMast1:collect) {
                                  Optional<LocMast> first = collect1.stream().filter(locMast2 -> locMast1.getRow1() + 1 == locMast2.getRow1() && Objects.equals(locMast1.getLev1(), locMast2.getLev1())
                                          && Objects.equals(locMast1.getBay1(), locMast2.getBay1())).findFirst();
                                  first.ifPresent(locMastList::add);
                              }
                              if (locMastList.size() == 0) {
                                  log.error("-----空桶库无对应空空桶组----");
                                  throw new CoolException("空桶库无对应空空桶组");
                              }
                              String model = findLocNoAttributeVo.getModel(); // 当前桶类型
                              if (model.equals("208L") && locMastList.size() <= 40 * 2 * 2) { // 最上面一层只能放208L,库位剩余量不多时,如果是208L桶入库,则先从最顶层放
                                  locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).reversed().thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                              } else {
                                  locMastList = locMastList.stream().sorted(Comparator.comparing(LocMast::getLev1).thenComparing(LocMast::getBay1).thenComparing(LocMast::getRow1)).collect(Collectors.toList());
                              }
                              locMast = locMastList.get(0);
                          } else { // 奇数,不可以入库
                              log.error("-----与前面桶类型不一样,前面桶不是尾桶,前面该桶类型任务数不是偶数,不能入库----");
                              throw new CoolException("与前面桶类型不一样,前面桶不是尾桶,前面该桶类型任务数不是偶数,不能入库");
                          }
                      }
                }
            }
        } else if ((staDescId == 1 && locArea == 1) || staDescId == 10) { // 满板入成品库或者空板入成品库
            // 入成品库摆放规则 判断可用堆垛机自动-无报警 按层列排顺序从下往上,从前往后排 先深库位在浅库位 最上面一层只能放208L桶(剩余库位达到一个阈值208L就先从最上面开始放,下面留给其他货物-后续优化)
            List<BasCrnp> basCrnps = basCrnpService.selectList(new EntityWrapper<BasCrnp>().eq("crn_sts", 3).eq("crn_err", 0).in("crn_no",1,2,3));
            if (basCrnps.size() == 0) {
                log.error("入库请求库位失败,没有联机堆垛机或堆垛机异常");
                throw new CoolException("入库请求库位失败,没有联机堆垛机或堆垛机异常");
            }
            List<Integer> crnList = basCrnps.stream().map(BasCrnp::getCrnNo).collect(Collectors.toList());
            // 入库桶型
            String model = findLocNoAttributeVo.getModel();
            Wrapper<LocMast> wrapper = new EntityWrapper<LocMast>().eq("loc_sts", 'O')
                    .in("crn_no", crnList).orderBy("lev1").orderBy("bay1").orderBy("row1");
            // 入库桶型是不是208L
            if (model == null || !model.equals("208L")) {
                wrapper.ne("lev1", 11);
            }
            locMast = locMastService.selectOne(wrapper.in("row1", 1, 4, 5, 6, 7, 8));
            // 深库位没有了则获取一个浅库位
            if (locMast == null) {
                locMast = locMastService.selectOne(wrapper.in("row1", 2, 3));
                if (locMast == null) {
                    log.error("-----库位不足----");
                    throw new CoolException("库位不足");
                } else {
                    int count = locMastService.selectCount(wrapper.in("row1", 2, 3));
                    if (count <= 10) {
                        log.error("-----库位不足,预留10个库位用来库位转移----");
                        throw new CoolException("库位不足,预留10个库位用来库位转移");
                    }
                }
            }
        } else {
            log.error("入库类型错误,staDescId={}", staDescId);
            throw new CoolException("入库类型错误,staDescId=" + staDescId);
        }
        // 返回dto
        StartupDto startupDto = new StartupDto();
        // 生成工作号
        int workNo = getWorkNo(0);
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员");
        StaDesc staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                .eq("type_no", staDescId)
                .eq("stn_no", sourceStaNo)
                .eq("crn_no", locMast.getCrnNo()));
        if (Cools.isEmpty(staDesc)) {
            log.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, locMast.getCrnNo());
            throw new CoolException("入库路径不存在");
        } else {
            BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
            if (!staNo.getAutoing().equals("Y")) {
                log.error("目标站" + staDesc.getCrnStn() + "不可用");
                throw new CoolException("目标站" + staDesc.getCrnStn() + "不可用");
            }
            startupDto.setStaNo(staNo.getDevNo());
        }
        // ===============>>>> 开始执行
        int curRow = rowLastno.getCurrentRow();
        // 生成工作号
        int workNo = getWorkNo(0);
        startupDto.setWorkNo(workNo);
        startupDto.setCrnNo(locMast.getCrnNo());
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setLocNo(locMast.getLocNo());
        return startupDto;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public StartupDto getLocNo(SearchLocParam param, Integer staNo) {
        // 返回dto
        StartupDto startupDto = new StartupDto();
        startupDto.setWorkNo(getWorkNo(0)); // 工作号
        startupDto.setSourceStaNo(param.getSourceStaNo()); // 源站点
        startupDto.setStaNo(staNo); // 目标站点
        return startupDto;
    }
    /**
     * 检索库位号
     *
     * @param whsType              类型 1:双深式货架
     * @param staDescId            路径ID
     * @param sourceStaNo          源站
     * @param findLocNoAttributeVo 属性
     * @param moveCrnNo            源
     * @param locTypeDto           类型
     * @param times                轮询次数
     * @return locNo 检索到的库位号
     */
    @Transactional
    public StartupDto getLocNoRun(Integer whsType, Integer staDescId, Integer sourceStaNo, FindLocNoAttributeVo findLocNoAttributeVo, Integer moveCrnNo, LocTypeDto locTypeDto, int times) {
        // 初始化参数
        int crnNo = 0;      //堆垛机号
        int nearRow = 0;    //最浅库位排
        int curRow = 0;     //最深库位排
        int rowCount = 0;   //轮询轮次
        LocMast locMast = null;     // 目标库位
        StartupDto startupDto = new StartupDto();
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员===>库位规则未知");
        }
        RowLastnoType rowLastnoType = rowLastnoTypeService.selectById(rowLastno.getTypeId());
        if (Cools.isEmpty(rowLastnoType)) {
            throw new CoolException("数据异常,请联系管理员===》库位规则类型未知");
        }
        int sRow = rowLastno.getsRow();
        int eRow = rowLastno.geteRow();
        int crn_qty = rowLastno.getCrnQty();
        int rowCount = eRow - sRow + 1;
        // 目标堆垛机号
        int crnNo = 0;
        // 目标库位
        LocMast locMast = null;
        int crnNumber = rowLastno.getCrnQty();
        // 靠近摆放规则 --- 同天同规格物料
        if (!Cools.isEmpty(matNos)) {
            List<String> locNos = locDetlService.getSameDetlToday(matNos.get(0), sRow, eRow);
        // ===============>>>> 开始执行
        curRow = rowLastno.getCurrentRow();
        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo != 0) {
            crnNumber = moveCrnNo;
            if (times == 0) {
                curRow = moveCrnNo * 4 - 1;
            } else {
                curRow = moveCrnNo * 4 - 2;
            }
        }
        //此程序用于优化堆垛机异常时的运行时间
        for (int i = times; i < crnNumber * 2; i++) {
            int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRow, crnNumber);
            curRow = locNecessaryParameters[1];
            crnNo = locNecessaryParameters[2];
            if (basCrnpService.checkSiteError(crnNo, true)) {
                rowCount = locNecessaryParameters[0];
                nearRow = locNecessaryParameters[3];
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                        .eq("row1", nearRow).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
                int crnCountO = wrkMastService.selectCount(new EntityWrapper<WrkMast>().eq("crn_no", crnNo).le("io_type", 100));
                if (locMasts.size() - crnCountO <= 2) {
                    log.error(crnNo + "号堆垛机没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
                    nearRow = 0;
                    times++;
                    continue;
                }
                break;
            } else {
                times++;
            }
        }
        if (nearRow == 0) {
            throw new CoolException("无可用堆垛机");
        }
        boolean signRule1 = false;
        boolean signRule2 = false;
        if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
            // 靠近摆放规则 --- 同天同规格物料 //分离版
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && staDescId == 1) {
                signRule1 = true;
            }
            // 靠近摆放规则 --- 同天同规格物料 //互通版
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && staDescId == 1) {
                signRule2 = true;
            }
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && (staDescId == 11 || staDescId == 111)) {
                signRule1 = true;
            }
        }
        if (signRule1) {
            if (nearRow != curRow) {
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                        .eq("row1", nearRow).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
                for (LocMast locMast1 : locMasts) {
                    //获取巷道
//                    List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,locMast1.getLocNo(), curRow>nearRow);
//                    LocMast locMastGro = locMastService.selectById(wrkMast.getLocNo());
                    //获取目标库位所在巷道最浅非空库位
                    LocMast locMastF = locMastService.selectLocByLocStsPakInF(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                    if (!Cools.isEmpty(locMastF) && locMastF.getLocSts().equals("F")) {
                        LocDetl locDetl = locDetlService.selectOne(new EntityWrapper<LocDetl>().eq("loc_no", locMastF.getLocNo()));
                        if (!Cools.isEmpty(locDetl) && findLocNoAttributeVo.beSimilar(locDetl)) {
                            //获取目标库位所在巷道最深空库位
                            locMast = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                            break;
                        }
                    }
                }
            }
        } else if (signRule2) {
            List<String> locNos = locDetlService.getSameDetlToday(findLocNoAttributeVo.getMatnr(), sRow, eRow);
            for (String locNo : locNos) {
                if (Utils.isShallowLoc(slaveProperties, locNo)) {
                    continue;
@@ -128,7 +523,7 @@
                // 检测目标库位是否为空库位
                LocMast shallowLoc = locMastService.selectById(shallowLocNo);
                if (shallowLoc != null && shallowLoc.getLocSts().equals("O")) {
                    if (VersionUtils.locMoveCheckLocType(shallowLoc, locTypeDto)) {
                    if (VersionUtils.locMoveCheckLocTypeComplete(shallowLoc, locTypeDto)) {
                        if (basCrnpService.checkSiteError(shallowLoc.getCrnNo(), true)) {
                            locMast = shallowLoc;
                            crnNo = locMast.getCrnNo();
@@ -139,21 +534,136 @@
            }
        }
        // 靠近摆放规则 --- 空托
        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) {
//        // 靠近摆放规则 --- 空托  //分离版
//        if (staDescId == 10 && Utils.BooleanWhsTypeStaIoType(whsType)) {
//            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow).eq("loc_sts", "O"));
//            for (LocMast locMast1:locMasts){
//                if (VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
//                    continue;
//                }
//                String shallowLoc = Utils.getDeepLoc(slaveProperties,locMast1.getLocNo());
//                LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no",shallowLoc));
//                if (!Cools.isEmpty(locMast2) && locMast2.getLocSts().equals("D")){
//                    locMast = locMast1;
//                    break;
//                }
//            }
//        }
        // 靠近摆放规则 --- 空托 //互通版
        if (staDescId == 10 && Utils.BooleanWhsTypeStaIoType(rowLastno)) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("loc_sts", "D").ge("row1", sRow).le("row1", eRow).eq("whs_type", rowLastnoType.getType().longValue()));
            if (!locMasts.isEmpty()) {
                for (LocMast loc : locMasts) {
                    if (Utils.isShallowLoc(slaveProperties, loc.getLocNo())) {
                        continue;
                    }
                    String shallowLocNo = Utils.getShallowLoc(slaveProperties,  loc.getLocNo());
                    String shallowLocNo = Utils.getShallowLoc(slaveProperties, loc.getLocNo());
                    // 检测目标库位是否为空库位
                    LocMast shallowLoc = locMastService.selectById(shallowLocNo);
                    if (shallowLoc != null && shallowLoc.getLocSts().equals("O")) {
                        if (basCrnpService.checkSiteError(shallowLoc.getCrnNo(), true)) {
                            locMast = shallowLoc;
                            crnNo = locMast.getCrnNo();
                        if (VersionUtils.locMoveCheckLocTypeComplete(shallowLoc, locTypeDto)) {
                            if (basCrnpService.checkSiteError(shallowLoc.getCrnNo(), true)) {
                                locMast = shallowLoc;
                                crnNo = locMast.getCrnNo();
                                break;
                            }
                        }
                    }
                }
            }
        }
        Wrapper<StaDesc> wrapper = null;
        StaDesc staDesc = null;
        BasDevp staNo = null;
        if (Utils.BooleanWhsTypeSta(rowLastno, staDescId)) {
            // 获取目标站
            wrapper = new EntityWrapper<StaDesc>()
                    .eq("type_no", staDescId)
                    .eq("stn_no", sourceStaNo)
                    .eq("crn_no", crnNo);
            staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
                log.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo);
//                throw new CoolException("入库路径不存在");
                crnNo = 0;
            } else {
                staNo = basDevpService.selectById(staDesc.getCrnStn());
                if (!staNo.getAutoing().equals("Y")) {
                    log.error("目标站" + staDesc.getCrnStn() + "不可用");
//                throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
                    crnNo = 0;
                }
                startupDto.setStaNo(staNo.getDevNo());
            }
            // 更新库位排号
            if (Cools.isEmpty(locMast)) {
                rowLastno.setCurrentRow(curRow);
                rowLastnoService.updateById(rowLastno);
            }
        }
        // 开始查找库位 ==============================>>
        // 1.按规则查找库位
        if (Cools.isEmpty(locMast) && crnNo != 0) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("row1", nearRow)
                    .eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue())
                    .orderBy("lev1", true).orderBy("bay1", true));
            for (LocMast locMast1 : locMasts) {
                if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                    continue;
                }
                if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                    String shallowLoc = Utils.getDeepLoc(slaveProperties, locMast1.getLocNo());
                    LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>()
                            .eq("loc_no", shallowLoc).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
                    if (!Cools.isEmpty(locMast2)) {
                        locMast = locMast2;
                        break;
                    }
                } else {
                    if (!Cools.isEmpty(locMast1)) {
                        locMast = locMast1;
                        break;
                    }
                }
            }
            if (Cools.isEmpty(locMast) && Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                for (LocMast locMast1 : locMasts) {
                    if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                        continue;
                    }
                    if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                        String shallowLoc = Utils.getDeepLoc(slaveProperties, locMast1.getLocNo());
                        LocMast locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>()
                                .eq("loc_no", shallowLoc).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
                        if (!Cools.isEmpty(locMast2)) {
                            locMast = locMast2;
                            break;
                        } else {
                            locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>()
                                    .eq("loc_no", shallowLoc).eq("loc_sts", "F").eq("whs_type", rowLastnoType.getType().longValue()));
                            if (!Cools.isEmpty(locMast2)) {
                                locMast = locMast1;
                                break;
                            } else {
                                locMast2 = locMastService.selectOne(new EntityWrapper<LocMast>()
                                        .eq("loc_no", shallowLoc).eq("loc_sts", "D").eq("whs_type", rowLastnoType.getType().longValue()));
                                if (!Cools.isEmpty(locMast2)) {
                                    locMast = locMast1;
                                    break;
                                }
                            }
                        }
                    } else {
                        if (!Cools.isEmpty(locMast1)) {
                            locMast = locMast1;
                            break;
                        }
                    }
@@ -161,126 +671,595 @@
            }
        }
        // 如果没有相近物料,则按规则轮询货架
        if (null == locMast) {
            Shelves shelves = new Shelves(rowCount, crn_qty);
            if (whsType != 4) {
                int divides = (int) Arith.divides(1, curRow - 1, 16);
                curRow = (int) Arith.remainder(curRow, 16);
                if (curRow == 0) {
                    curRow = 16;
        if (!Cools.isEmpty(locMast) && !basCrnpService.checkSiteError(crnNo, true)) {
            locMast = null;
        }
        // 递归查询
        if (Cools.isEmpty(locMast) || !locMast.getLocSts().equals("O")) {
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归
            if (times < rowCount * 2) {
                times = times + 1;
                return getLocNoRun(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, moveCrnNo, locTypeDto, times);
            }
//            // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
//            if (locTypeDto.getLocType1() < 2) {
//                int i = locTypeDto.getLocType1() + 1;
//                locTypeDto.setLocType1((short)i);
//                return getLocNo(1, staDescId, sourceStaNo, matnr,batch,grade, locTypeDto, 0);
//            }
            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.setLocNo(locNo);
        return startupDto;
    }
    public StartupDto getLocNoRun4(Integer whsType, Integer staDescId, Integer sourceStaNo, FindLocNoAttributeVo findLocNoAttributeVo, Integer moveCrnNo, LocTypeDto locTypeDto, int times) {
        // 初始化参数
        int crnNo = 0;      //堆垛机号
        int nearRow = 0;    //最浅库位排
        int curRow = 0;     //最深库位排
        int rowCount = 0;   //轮询轮次
        LocMast locMast = null;     // 目标库位
        StartupDto startupDto = new StartupDto();
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员===>库位规则未知");
        }
        RowLastnoType rowLastnoType = rowLastnoTypeService.selectById(rowLastno.getTypeId());
        if (Cools.isEmpty(rowLastnoType)) {
            throw new CoolException("数据异常,请联系管理员===》库位规则类型未知");
        }
        int sRow = rowLastno.getsRow();
        int eRow = rowLastno.geteRow();
        int crnNumber = rowLastno.getCrnQty();
        // ===============>>>> 开始执行
        curRow = rowLastno.getCurrentRow();
        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo != 0) {
            crnNumber = moveCrnNo;
//            if (times==0){
//                curRow = moveCrnNo*4-1;
//            }else {
//                curRow = moveCrnNo*4-2;
//            }
        }
        //此程序用于优化堆垛机异常时的运行时间
        for (int i = times; i < crnNumber * 2; i++) {
            int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRow, crnNumber);
            rowCount = locNecessaryParameters[0];
            curRow = locNecessaryParameters[1];
            crnNo = locNecessaryParameters[2];
            nearRow = locNecessaryParameters[3];
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("crn_no", crnNo).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
            if (locMasts.size() <= 5) {
                nearRow = 0;
                times++;
                continue;
            }
            break;
        }
        if (crnNo == 0) {
            throw new CoolException("无可用库位");
        }
        // 相似工作档案 --- 同天同规格物料
        if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && (staDescId == 1 || staDescId == 11 || staDescId == 111)) {
            //查询相似工作档案
            List<WrkMast> wrkMasts = wrkMastService.selectWrkMastWrkDetl(staDescId, findLocNoAttributeVo, crnNo);
            int nearbay = 0;    //相似工作档案 目标库位列
            int nearlev = 0;    //相似工作档案 目标库位层
            for (WrkMast wrkMast : wrkMasts) {
                int curRowW = curRow;    //相似工作档案 最深库位排
                int nearRowW = nearRow;    //相似工作档案 最浅库位排
                if (Cools.isEmpty(wrkMast.getLocNo())) {
                    continue;
                }
                for (int i = 0; i < shelves.group; i ++) {
                    curRow = shelves.start(curRow);
                    if (curRow < 0) {
                        throw new CoolException("检索库位失败,请联系管理员");
                    }
                    Integer crnNo1 = shelves.get(curRow);
                    crnNo1 = crnNo1 + divides*4;
                    if (basCrnpService.checkSiteError(crnNo1, true)) {
                        crnNo = crnNo1;
                //目标排为最外层排
                if (Utils.getRow(wrkMast.getLocNo()) == nearRow) {
                    continue;
                }
                //起始站不一致
                if (!wrkMast.getSourceStaNo().equals(sourceStaNo)) {
                    continue;
                }
                //相同列、层过滤
                if (Utils.getBay(wrkMast.getLocNo()) == nearbay && Utils.getLev(wrkMast.getLocNo()) == nearlev) {
                    continue;
                } else {
                    nearbay = Utils.getBay(wrkMast.getLocNo());
                    nearlev = Utils.getLev(wrkMast.getLocNo());
                }
                //获取目标库位所在巷道并排序
//                List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,wrkMast.getLocNo(), curRow>nearRow);
                LocMast locMastGro = locMastService.selectById(wrkMast.getLocNo());
                for (int i = 0; i < crnNumber * 2; i++) {
                    if (!(Utils.getRow(locMastGro.getLocNo()) > nearRowW && Utils.getRow(locMastGro.getLocNo()) <= curRowW) && !(Utils.getRow(locMastGro.getLocNo()) < nearRowW && Utils.getRow(locMastGro.getLocNo()) >= curRowW)) {
                        int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRowW, crnNumber);
                        curRowW = locNecessaryParameters[1];
                        nearRowW = locNecessaryParameters[3];
                    } else {
                        break;
                    }
                }
            } else {
                for (int i = 0; i < shelves.group; i ++) {
                    curRow = shelves.start(curRow);
                    if (curRow < 0) {
                        throw new CoolException("检索库位失败,请联系管理员");
                    }
                    Integer crnNo1 = shelves.get(curRow);
                    if (basCrnpService.checkSiteError(crnNo1, true)) {
                        crnNo = crnNo1;
                //获取目标库位所在巷道最浅非空库位
                LocMast locMast2 = locMastService.selectLocByLocStsPakInF(curRowW, nearRowW, locMastGro, rowLastnoType.getType().longValue());
                //目标库位所在巷道最浅非空库位存在&&非最外侧库位&&入库状态
                if (!Cools.isEmpty(locMast2) && Utils.getRow(locMast2.getLocNo()) != nearRowW && (locMast2.getLocSts().equals("S") || locMast2.getLocSts().equals("Q"))) {
                    //获取库存明细
                    WrkDetl wrkDetl = wrkDetlService.selectOne(new EntityWrapper<WrkDetl>().eq("wrk_no", wrkMast.getWrkNo()));
                    //判断同规格物料
                    if (!Cools.isEmpty(wrkDetl) && findLocNoAttributeVo.beSimilar(wrkDetl)) {
                        int row2 = 0;
                        if (Utils.getRow(locMast2.getLocNo()) > nearRowW) {
                            row2 = Utils.getRow(locMast2.getLocNo()) - 1;
                        } else {
                            row2 = Utils.getRow(locMast2.getLocNo()) + 1;
                        }
                        String targetLocNo = zerofill(String.valueOf(row2), 2) + locMast2.getLocNo().substring(2);
                        locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", targetLocNo).eq("loc_sts", "O"));
                        if (Cools.isEmpty(locMast)) {
                            continue;
                        }
                        break;
                    }
                }
            }
        }
        if (crnNo == 0) {
            throw new CoolException("没有可用的堆垛机");
        // 相似工作档 --- 空托
        if (Cools.isEmpty(locMast) && staDescId == 10) {
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("io_type", 10).eq("crn_no", crnNo).eq("whs_type", rowLastnoType.getType().longValue()));
            int nearbay = 0;
            int nearlev = 0;
            for (WrkMast wrkMast : wrkMasts) {
                int curRowW = curRow;    //相似工作档案 最深库位排
                int nearRowW = nearRow;    //相似工作档案 最浅库位排
                if (Cools.isEmpty(wrkMast.getLocNo())) {
                    continue;
                }
                if (Utils.getRow(wrkMast.getLocNo()) == nearRow) {
                    continue;
                }
                //起始站不一致
                if (!wrkMast.getSourceStaNo().equals(sourceStaNo)) {
                    continue;
                }
                if (Utils.getBay(wrkMast.getLocNo()) == nearbay && Utils.getLev(wrkMast.getLocNo()) == nearlev) {
                    continue;
                } else {
                    nearbay = Utils.getBay(wrkMast.getLocNo());
                    nearlev = Utils.getLev(wrkMast.getLocNo());
                }
//                List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,wrkMast.getLocNo(), curRow>nearRow);
                LocMast locMastGro = locMastService.selectById(wrkMast.getLocNo());
                for (int i = 0; i < crnNumber * 2; i++) {
                    if (!(Utils.getRow(locMastGro.getLocNo()) > nearRowW && Utils.getRow(locMastGro.getLocNo()) <= curRowW) && !(Utils.getRow(locMastGro.getLocNo()) < nearRowW && Utils.getRow(locMastGro.getLocNo()) >= curRowW)) {
                        int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRowW, crnNumber);
                        curRowW = locNecessaryParameters[1];
                        nearRowW = locNecessaryParameters[3];
                    } else {
                        break;
                    }
                }
                LocMast locMast2 = locMastService.selectLocByLocStsPakInF(curRowW, nearRowW, locMastGro, rowLastnoType.getType().longValue());
                if (!Cools.isEmpty(locMast2) && Utils.getRow(locMast2.getLocNo()) != nearRowW && locMast2.getLocSts().equals("S")) {
                    int row2 = 0;
                    if (Utils.getRow(locMast2.getLocNo()) > nearRowW) {
                        row2 = Utils.getRow(locMast2.getLocNo()) - 1;
                    } else {
                        row2 = Utils.getRow(locMast2.getLocNo()) + 1;
                    }
                    String targetLocNo = zerofill(String.valueOf(row2), 2) + locMast2.getLocNo().substring(2);
                    locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", targetLocNo).eq("loc_sts", "O"));
                    if (Cools.isEmpty(locMast)) {
                        continue;
                    }
                    break;
                }
            }
        }
        // 获取目标站
        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
                .eq("type_no", staDescId)
                .eq("stn_no", sourceStaNo)
                .eq("crn_no", crnNo);
        StaDesc staDesc = staDescService.selectOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            log.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo);
            throw new CoolException("入库路径不存在");
        boolean signRule1 = false;
        boolean signRule2 = false;
        if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
            // 靠近摆放规则 --- 同天同规格物料 //分离版
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && staDescId == 1) {
//                signRule1 = true;
            }
            // 靠近摆放规则 --- 同天同规格物料 //互通版
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && staDescId == 1) {
                signRule2 = true;
            }
            if (!Cools.isEmpty(findLocNoAttributeVo.getMatnr()) && (staDescId == 11 || staDescId == 111)) {
                signRule1 = true;
            }
        }
        BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
        if (!staNo.getAutoing().equals("Y")) {
            throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
        if (signRule1) {
            if (nearRow != curRow) {
                List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                        .eq("row1", nearRow).eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue()));
                for (LocMast locMast1 : locMasts) {
                    //获取巷道
//                    List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,locMast1.getLocNo(), curRow>nearRow);
                    //获取目标库位所在巷道最浅非空库位
                    LocMast locMastF = locMastService.selectLocByLocStsPakInF(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                    if (!Cools.isEmpty(locMastF) && locMastF.getLocSts().equals("F")) {
                        LocDetl locDetl = locDetlService.selectOne(new EntityWrapper<LocDetl>().eq("loc_no", locMastF.getLocNo()));
                        if (!Cools.isEmpty(locDetl) && findLocNoAttributeVo.beSimilar(locDetl)) {
                            //获取目标库位所在巷道最深空库位
                            locMast = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                            break;
                        }
                    }
                }
            }
        } else if (signRule2) {
            List<String> locNos = locDetlService.getSameDetlToday(findLocNoAttributeVo.getMatnr(), sRow, eRow);
            int nearbay = 0;
            int nearlev = 0;
            for (String locNo : locNos) {
                int curRowW = curRow;    //相似工作档案 最深库位排
                int nearRowW = nearRow;    //相似工作档案 最浅库位排
                if (Cools.isEmpty(locNo)) {
                    continue;
                }
                if (Utils.getRow(locNo) == nearRow) {
                    continue;
                }
                if (Utils.getBay(locNo) == nearbay && Utils.getLev(locNo) == nearlev) {
                    continue;
                } else {
                    nearbay = Utils.getBay(locNo);
                    nearlev = Utils.getLev(locNo);
                }
//                List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,wrkMast.getLocNo(), curRow>nearRow);
                LocMast locMastGro = locMastService.selectById(locNo);
                for (int i = 0; i < crnNumber * 2; i++) {
                    if (!(Utils.getRow(locMastGro.getLocNo()) > nearRowW && Utils.getRow(locMastGro.getLocNo()) <= curRowW) && !(Utils.getRow(locMastGro.getLocNo()) < nearRowW && Utils.getRow(locMastGro.getLocNo()) >= curRowW)) {
                        int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRowW, crnNumber);
                        curRowW = locNecessaryParameters[1];
                        nearRowW = locNecessaryParameters[3];
                    } else {
                        break;
                    }
                }
                LocMast locMast2 = locMastService.selectLocByLocStsPakInF(curRowW, nearRowW, locMastGro, rowLastnoType.getType().longValue());
                if (!Cools.isEmpty(locMast2) && Utils.getRow(locMast2.getLocNo()) != nearRowW && locMast2.getLocSts().equals("S")) {
                    int row2 = 0;
                    if (Utils.getRow(locMast2.getLocNo()) > nearRowW) {
                        row2 = Utils.getRow(locMast2.getLocNo()) - 1;
                    } else {
                        row2 = Utils.getRow(locMast2.getLocNo()) + 1;
                    }
                    String targetLocNo = zerofill(String.valueOf(row2), 2) + locMast2.getLocNo().substring(2);
                    locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", targetLocNo).eq("loc_sts", "O"));
                    if (Cools.isEmpty(locMast)) {
                        continue;
                    }
                    break;
                }
            }
        }
//        // 靠近摆放规则 --- 空托  //分离版
//        if (staDescId == 10 && Utils.BooleanWhsTypeStaIoType(rowLastno)) {
//            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("row1", nearRow).eq("loc_sts", "O"));
//            for (LocMast locMast1:locMasts){
//                //获取巷道
////                List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,locMast1.getLocNo(), curRow>nearRow);
//                //获取目标库位所在巷道最浅非空库位
//                LocMast locMastF = locMastService.selectLocByLocStsPakInF(curRow,nearRow,locMast1,rowLastnoType.getType().longValue());
//                if (!Cools.isEmpty(locMastF) && locMastF.getLocSts().equals("D")){
//                    //获取目标库位所在巷道最浅非空库位
//                    locMast = locMastService.selectLocByLocStsPakInO(curRow,nearRow,locMast1,rowLastnoType.getType().longValue());
//                    break;
//                }
//            }
//        }
        // 靠近摆放规则 --- 空托 //互通版
        if (staDescId == 10 && Utils.BooleanWhsTypeStaIoType(rowLastno)) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("loc_sts", "D").ge("row1", sRow).le("row1", eRow));
            int nearbay = 0;
            int nearlev = 0;
            for (LocMast locMastSign : locMasts) {
                int curRowW = curRow;    //相似工作档案 最深库位排
                int nearRowW = nearRow;    //相似工作档案 最浅库位排
                if (Cools.isEmpty(locMastSign.getLocNo())) {
                    continue;
                }
                if (Utils.getRow(locMastSign.getLocNo()) == nearRow) {
                    continue;
                }
                if (Utils.getBay(locMastSign.getLocNo()) == nearbay && Utils.getLev(locMastSign.getLocNo()) == nearlev) {
                    continue;
                } else {
                    nearbay = Utils.getBay(locMastSign.getLocNo());
                    nearlev = Utils.getLev(locMastSign.getLocNo());
                }
//                List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,wrkMast.getLocNo(), curRow>nearRow);
                LocMast locMastGro = locMastService.selectById(locMastSign.getLocNo());
                for (int i = 0; i < crnNumber * 2; i++) {
                    if (!(Utils.getRow(locMastGro.getLocNo()) > nearRowW && Utils.getRow(locMastGro.getLocNo()) <= curRowW) && !(Utils.getRow(locMastGro.getLocNo()) < nearRowW && Utils.getRow(locMastGro.getLocNo()) >= curRowW)) {
                        int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRowW, crnNumber);
                        curRowW = locNecessaryParameters[1];
                        nearRowW = locNecessaryParameters[3];
                    } else {
                        break;
                    }
                }
                LocMast locMast2 = locMastService.selectLocByLocStsPakInF(curRowW, nearRowW, locMastGro, rowLastnoType.getType().longValue());
                if (!Cools.isEmpty(locMast2) && Utils.getRow(locMast2.getLocNo()) != nearRowW && locMast2.getLocSts().equals("S")) {
                    int row2 = 0;
                    if (Utils.getRow(locMast2.getLocNo()) > nearRowW) {
                        row2 = Utils.getRow(locMast2.getLocNo()) - 1;
                    } else {
                        row2 = Utils.getRow(locMast2.getLocNo()) + 1;
                    }
                    String targetLocNo = zerofill(String.valueOf(row2), 2) + locMast2.getLocNo().substring(2);
                    locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", targetLocNo).eq("loc_sts", "O"));
                    if (Cools.isEmpty(locMast)) {
                        continue;
                    }
                    break;
                }
            }
        }
        Wrapper<StaDesc> wrapper = null;
        StaDesc staDesc = null;
        BasDevp staNo = null;
        if (Utils.BooleanWhsTypeSta(rowLastno, staDescId)) {
            // 获取目标站
            wrapper = new EntityWrapper<StaDesc>()
                    .eq("type_no", staDescId)
                    .eq("stn_no", sourceStaNo)
                    .eq("crn_no", crnNo);
            staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
                log.error("入库路径不存在:type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo);
                crnNo = 0;
            } else {
                staNo = basDevpService.selectById(staDesc.getCrnStn());
                if (!staNo.getAutoing().equals("Y")) {
                    log.error("目标站" + staDesc.getCrnStn() + "不可用");
                    crnNo = 0;
                }
                startupDto.setStaNo(staNo.getDevNo());
            }
        }
        // 更新库位排号
        rowLastno.setCurrentRow(curRow);
        rowLastnoService.updateById(rowLastno);
        if (Utils.BooleanWhsTypeSta(rowLastno, staDescId) && Cools.isEmpty(locMast)) {
            rowLastno.setCurrentRow(curRow);
            rowLastnoService.updateById(rowLastno);
        }
        // 开始查找库位 ==============================>>
        // 1.当检索库排为浅库位排时,优先寻找当前库排的深库位排
        if (locMast == null) {
            if (Utils.isShallowLoc(slaveProperties, curRow)) {
                Integer deepRow = Utils.getDeepRow(slaveProperties, curRow);
                locMast = locMastService.queryFreeLocMast(deepRow, locTypeDto.getLocType1());
                // todo:luxiaotao 如果用浅排找到的深库位,那么则需要判断这个深库位对应的浅库位是否有货(F、X、D)
                // 因库位移转、需预留空库位
                if (!locMastService.checkEmptyCount(locMast)) {
                    locMast = null;
        // 1.按规则查找库位
        if (Cools.isEmpty(locMast) && crnNo != 0) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("row1", nearRow)
                    .eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue())
                    .orderBy("lev1", true).orderBy("bay1", true));//最浅库位
            for (LocMast locMast1 : locMasts) {
                if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                    continue;
                }
                if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                    //获取目标库位所在巷道最深空库位
                    LocMast locMast2 = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                    if (!Cools.isEmpty(locMast2) && locMast2.getBay1() == curRow) {
                        locMast = locMast2;
                        break;
                    }
                }
            }
            if (Cools.isEmpty(locMast)) {
                locMast = locMastService.queryFreeLocMast(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;
            //未找到  允许混料
            if (Cools.isEmpty(locMast) && Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                for (LocMast locMast1 : locMasts) {
                    if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                        continue;
                    }
                }
                // 目标库位 ===>> 深库位, 则校验其浅库位是否为 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;
                    if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                        //获取目标库位所在巷道并排序
//                        List<String> groupOutsideLocCrn = Utils.getGroupOutLocCrn(curRow,nearRow,locMast1.getLocNo(), curRow>nearRow);
                        //获取目标库位所在巷道最浅非空库位
                        LocMast locMast2 = locMastService.selectLocByLocStsPakInF(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                        if (Cools.isEmpty(locMast2)) {
                            LocMast locMast3 = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                            if (!Cools.isEmpty(locMast3)) {
                                locMast = locMast3;
                                break;
                            }
                        } else {
                            if ((locMast2.getLocSts().equals("F") && staDescId == 1) || (locMast2.getLocSts().equals("D") && staDescId == 10)) {
                                LocMast locMast3 = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                                if (!Cools.isEmpty(locMast3)) {
                                    locMast = locMast3;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
        // 递归查询
        if (Cools.isEmpty(locMast) || !locMast.getLocSts().equals("O")) {
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归
            if (times < rowCount) {
            if (times < rowCount * 2) {
                times = times + 1;
                return getLocNo(1, staDescId, sourceStaNo, matNos, locTypeDto, times);
            }
            // 轻货物找轻库位为空时,可以去找重库位仓
            if (locTypeDto.getLocType1() == 1) {
                locTypeDto.setLocType1((short) 2);
                return getLocNo(1, staDescId, sourceStaNo, matNos, locTypeDto, times);
                return getLocNoRun4(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, moveCrnNo, locTypeDto, 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);
        return startupDto;
    }
    public StartupDto getLocNoRun5(Integer whsType, Integer staDescId, Integer sourceStaNo, FindLocNoAttributeVo findLocNoAttributeVo, Integer moveCrnNo, LocTypeDto locTypeDto, int times) {
        // 初始化参数
        int crnNo = 0;      //堆垛机号
        int nearRow = 0;    //最浅库位排
        int curRow = 0;     //最深库位排
        int rowCount = 0;   //轮询轮次
        LocMast locMast = null;     // 目标库位
        StartupDto startupDto = new StartupDto();
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员===>库位规则未知");
        }
        RowLastnoType rowLastnoType = rowLastnoTypeService.selectById(rowLastno.getTypeId());
        if (Cools.isEmpty(rowLastnoType)) {
            throw new CoolException("数据异常,请联系管理员===》库位规则类型未知");
        }
        int sRow = rowLastno.getsRow();
        int eRow = rowLastno.geteRow();
        int crnNumber = eRow - sRow + 1;
        // ===============>>>> 开始执行
        curRow = rowLastno.getCurrentRow();
        if (!Cools.isEmpty(moveCrnNo) && moveCrnNo != 0) {
            crnNumber = moveCrnNo;
        }
        //此程序用于优化堆垛机异常时的运行时间
        int[] locNecessaryParameters = Utils.LocNecessaryParameters(rowLastno, curRow, crnNumber);
        curRow = locNecessaryParameters[1];
        crnNo = locNecessaryParameters[2];
        rowCount = locNecessaryParameters[0];
        nearRow = locNecessaryParameters[3];
        Wrapper<StaDesc> wrapper = null;
        StaDesc staDesc = null;
        BasDevp staNo = null;
        if (Utils.BooleanWhsTypeSta(rowLastno, staDescId)) {
            // 获取目标站
            wrapper = new EntityWrapper<StaDesc>()
                    .eq("type_no", staDescId)
                    .eq("stn_no", sourceStaNo)
                    .eq("crn_no", crnNo);
            staDesc = staDescService.selectOne(wrapper);
            if (Cools.isEmpty(staDesc)) {
                log.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo);
//                throw new CoolException("入库路径不存在");
                crnNo = 0;
            } else {
                staNo = basDevpService.selectById(staDesc.getCrnStn());
                if (!staNo.getAutoing().equals("Y")) {
                    log.error("目标站" + staDesc.getCrnStn() + "不可用");
//                throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
                    crnNo = 0;
                }
                startupDto.setStaNo(staNo.getDevNo());
            }
        }
        // 更新库位排号
        if (Utils.BooleanWhsTypeSta(rowLastno, staDescId) && Cools.isEmpty(locMast)) {
            rowLastno.setCurrentRow(curRow);
            rowLastnoService.updateById(rowLastno);
        }
        // 开始查找库位 ==============================>>
        // 1.按规则查找库位
        if (Cools.isEmpty(locMast) && crnNo != 0) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("row1", nearRow)
                    .eq("loc_sts", "O").eq("whs_type", rowLastnoType.getType().longValue())
                    .orderBy("lev1", true).orderBy("bay1", true));//最浅库位
            for (LocMast locMast1 : locMasts) {
                if (!VersionUtils.locMoveCheckLocTypeComplete(locMast1, locTypeDto)) {
                    continue;
                }
                if (Utils.BooleanWhsTypeStaIoType(rowLastno)) {
                    //获取目标库位所在巷道最深空库位
                    LocMast locMast2 = locMastService.selectLocByLocStsPakInO(curRow, nearRow, locMast1, rowLastnoType.getType().longValue());
                    if (!Cools.isEmpty(locMast2) && locMast2.getBay1() == curRow) {
                        locMast = locMast2;
                        break;
                    }
                }
            }
        }
        // 递归查询
        if (Cools.isEmpty(locMast) || !locMast.getLocSts().equals("O")) {
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归
            if (times < rowCount * 2) {
                times = times + 1;
                return getLocNoRun5(whsType, staDescId, sourceStaNo, findLocNoAttributeVo, moveCrnNo, locTypeDto, 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.setLocNo(locNo);
        return startupDto;
    }
    public static String zerofill(String msg, Integer count) {
        if (msg.length() == count) {
@@ -289,40 +1268,10 @@
            return msg.substring(0, 16);
        } else {
            StringBuilder msgBuilder = new StringBuilder(msg);
            for(int i = 0; i < count - msg.length(); ++i) {
            for (int i = 0; i < count - msg.length(); ++i) {
                msgBuilder.insert(0, "0");
            }
            return msgBuilder.toString();
        }
    }
    /**
     * 根据入库站获取库位排号分配
     */
    private Integer getWhsType(Integer sourceStaNo, int times) {
        if (times >= 16) {
            return 4;
        }
        switch (sourceStaNo) {
            case 173:
            case 176:
            case 180:
            case 185:
                return 1;
            case 189:
            case 194:
            case 198:
            case 202:
                return 2;
            case 206:
//            case 211:
//            case 215:
//            case 219:
//            case 223:
            case 227:
                return 3;
            default:
                throw new CoolException("根据入库站获取库位排号分配失败,入库站:" + sourceStaNo);
        }
    }