自动化立体仓库 - WMS系统
#
LSH
2023-02-14 c46f9c28f8d40d2a4efcc7ef6e528661d97a4be0
src/main/java/com/zy/common/service/CommonService.java
@@ -1,25 +1,41 @@
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.Arith;
import com.core.common.Cools;
import com.core.exception.CoolException;
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 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(2);add(3);add(4);add(5);add(6);add(7);add(8);add(9);add(10);
        add(11);add(12);add(13);add(14);add(15);add(16);add(17);
    }};
    public static final List<Integer> SECOND_GROUP_ROW_LIST = new ArrayList<Integer>() {{
        add(18);add(19);add(20);
        add(21);add(22);add(23);add(24);add(25);add(26);add(27);add(28);add(29);add(30);
    }};
    @Autowired
    private WrkMastService wrkMastService;
@@ -37,6 +53,12 @@
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private MatService matService;
    @Autowired
    private LocRuleService locRuleService;
    /**
     * 生成工作号
@@ -50,25 +72,23 @@
        }
        int workNo = 0;
        // 入出库类型
        if (wrkLastno.getWrkMk() == 0) {
            workNo = wrkLastno.getWrkNo();
            int sNo = wrkLastno.getSNo();
            int eNo = wrkLastno.getENo();
        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;
                } else {
                    break;
                }
        while (true) {
            WrkMast wrkMast = wrkMastService.selectById(workNo);
            if (null != wrkMast) {
                workNo = workNo>=eNo ? sNo : workNo+1;
            } else {
                break;
            }
            if (workNo > 0){
                wrkLastno.setWrkNo(workNo);
                wrkLastnoService.updateById(wrkLastno);
            }
        }
        if (workNo > 0){
            wrkLastno.setWrkNo(workNo);
            wrkLastnoService.updateById(wrkLastno);
        }
        if (workNo == 0) {
            throw new CoolException("生成工作号失败,请联系管理员");
@@ -80,132 +100,385 @@
        return workNo;
    }
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int times) {
        LocTypeDto oldLocType = locTypeDto.clone();
        return getLocNo(whsType, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
    }
    /**
     * 检索库位号
     * @param whsType 类型 1:双深式货架
     * @param staDescId 路径ID
     * @param sourceStaNo 源站
     * @param matNos 物料号集合
     * @return locNo 检索到的库位号
     */
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo) {
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, LocTypeDto oldLocType, int times) {
        StartupDto startupDto = new StartupDto();
        int start;
        int end;
        Integer theCrnNo = null;
        switch (sourceStaNo) {
            case 103:
                whsType = 1;
                start = 1;
                end = 14;
//                if (null != wrkMastService.selectPakoutOfStaNo(104)) {
//                    start = 8;
//                    theCrnNo = 2;
//                }
                break;
            case 203:
                whsType = 2;
                start = 8;
                end = 21;
//                if (null != wrkMastService.selectPakoutOfStaNo(204)) {
//                    end = 14;
//                    theCrnNo = 2;
//                }
                break;
            default:
                throw new CoolException("检索库位失败,请联系管理员");
        }
        // 生成工作号
        int workNo = getWorkNo(0);
        RowLastno rowLastno = rowLastnoService.selectById(whsType);
        if (Cools.isEmpty(rowLastno)) {
            throw new CoolException("数据异常,请联系管理员");
        }
        if (rowLastno.getWhsType() == 1){
            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;
            // 目标库位
            LocMast locMast = null;
        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;
        // 目标库位
        LocMast locMast = null;
            // 同一天同规格货物靠近摆法规则
            List<String> locNos = locDetlService.getSameDetlToday("X004073XX");
            for (String locNo : locNos) {
                // 获取排
                int row = Integer.parseInt(locNo.substring(0, 2));
                // 判断是否为深库位
                double remainder = Arith.remainder(row, rowCount / crn_qty);
                int targetRow;
                if (remainder == 1) {
                    // 得到当前库位的外围库位
                    targetRow =  row + 1;
                } else if (remainder == 0) {
                    // 得到当前库位的内围库位
                    targetRow =  row - 1;
                } else {
                    continue;
                }
                String targetLocNo = zerofill(String.valueOf(targetRow), 2)+locNo.substring(2);
                // 检测目标库位是否为空库位
                LocMast targetLocMast = locMastService.selectById(targetLocNo);
                if (targetLocMast != null && !targetLocMast.getLocSts().equals("O")) {
                    locMast = targetLocMast;
                    crnNo = locMast.getCrnNo();
        boolean inoutEveryday = false;
        if (!Cools.isEmpty(matNos)) {
            for (String matNo : matNos) {
                Mat mat = matService.selectByMatnr(matNo);
                if (mat.getInoutEveryday() != null && mat.getInoutEveryday()) {
                    inoutEveryday = true;
                    break;
                }
            }
        }
            // 如果没有相近物料,则按规则轮询货架
            if (null == locMast) {
                // 获取目标站所在货架排号
                Shelves shelves = new Shelves(rowCount, crn_qty);
        // 靠近摆放规则 --- 同天同规格物料
        if (!Cools.isEmpty(matNos) && matNos.size() == 1) {
            List<String> locNos = locDetlService.getSameDetl(matNos.get(0), start, end);
            for (String locNo : locNos) {
                List<String> groupLoc = Utils.getGroupLoc(locNo);
                LocMast locMast0 = locMastService.findOutMost(groupLoc);
                if (null != locMast0) {
                    // 浅库位符合尺寸检测
                    if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                        // 浅库位对应堆垛机必须可用且无异常
                        if (basCrnpService.checkSiteError(locMast0.getCrnNo(), true)) {
                            // 因库位移转、需预留空库位
                            if (locMastService.checkEmptyCount(locMast0, 10)) {
                                crnNo = locMast0.getCrnNo();
                                locMast = locMast0;
                                break;
                            }
                        }
                    }
                }
            }
        }
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>()
                    .eq("loc_sts", "D").ge("row1", start).le("row1", end));
            if (locMasts.size() > 0) {
                for (LocMast loc : locMasts) {
                    List<String> groupLoc = Utils.getGroupLoc(loc.getLocNo());
                    LocMast locMast0 = locMastService.findOutMost(groupLoc);
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                            // 浅库位对应堆垛机必须可用且无异常
                            if (basCrnpService.checkSiteError(locMast0.getCrnNo(), true)) {
                                // 因库位移转、需预留空库位
                                if (locMastService.checkEmptyCount(locMast0, 10)) {
                                    crnNo = locMast0.getCrnNo();
                                    locMast = locMast0;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // 库区锁定
        if (Cools.isEmpty(locMast)) {
            LocRule locRule = locRuleService.find(Cools.isEmpty(matNos) ? null : matNos.get(0), null, start, end);
            if (!Cools.isEmpty(locRule)) {
                List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1()
                        , locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd());
                for (LocMast one : locMasts) {
                    List<String> groupLoc = Utils.getGroupLoc(one.getLocNo());
                    LocMast locMast0 = locMastService.findOutMost(groupLoc);
                    if (null != locMast0) {
                        // 浅库位符合尺寸检测
                        if (VersionUtils.locMoveCheckLocType(locMast0, locTypeDto)) {
                            // 浅库位对应堆垛机必须可用且无异常
                            if (basCrnpService.checkSiteError(locMast0.getCrnNo(), true)) {
                                // 因库位移转、需预留空库位
                                if (locMastService.checkEmptyCount(locMast0, 10)) {
                                    crnNo = locMast0.getCrnNo();
                                    locMast = locMast0;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        // 如果没有相近物料,则按规则轮询货架
        int currentRow = 1;
        if (null == locMast) {
            Shelves shelves = new Shelves(rowCount, crn_qty);
            for (int i = 0; i < shelves.group; i ++) {
                if (i > 0) {
                    curRow = currentRow;
                }
                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;
                currentRow = curRow;
                int crnNo1 = shelves.get(curRow);
                if (whsType == 1) {
                    switch (curRow) {
                        case 1:
                            curRow = 1;
                            if (null != theCrnNo) {
                                curRow = 8;
                                crnNo1 = crnNo1 + 1;
                            }
                            break;
                        case 2:
                            curRow = 7;
                            if (null != theCrnNo) {
                                curRow = 14;
                                crnNo1 = crnNo1 + 1;
                            }
                            break;
                        case 3:
                            curRow = 8;
                            break;
                        case 4:
                            curRow = 14;
                            break;
                        default:
                            throw new CoolException("检索库位失败,请联系管理员");
                    }
                } else {
                    switch (curRow) {
                        case 1:
                            curRow = 8;
                            break;
                        case 2:
                            curRow = 14;
                            break;
                        case 3:
                            curRow = 15;
                            if (null != theCrnNo) {
                                curRow = 8;
                                crnNo1 = crnNo1 - 1;
                            }
                            break;
                        case 4:
                            curRow = 21;
                            if (null != theCrnNo) {
                                curRow = 14;
                                crnNo1 = crnNo1 - 1;
                            }
                            break;
                        default:
                            throw new CoolException("检索库位失败,请联系管理员");
                    }
                    crnNo1 = crnNo1 + 1;
                }
                if (basCrnpService.checkSiteError(crnNo1, true)) {
                    crnNo = crnNo1;
                    break;
                }
            }
        }
        if (crnNo == 0) {
            throw new CoolException("没有可用的堆垛机");
        }
        // 获取目标站
        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("入库路径不存在, staDescId={}, sourceStaNo={}, crnNo={}", staDescId, sourceStaNo, crnNo);
            throw new CoolException("入库路径不存在");
        }
        // 检测目标站
        BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
        if (!staNo.getAutoing().equals("Y")) {
            throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
        }
        // 更新库位排号
        rowLastno.setCurrentRow(currentRow);
        rowLastnoService.updateById(rowLastno);
        // 开始查找库位 ==============================>>
        // 1.当检索库排为浅库位排时,优先寻找当前库排的深库位排
        // 高低频管控、库位组全部为O
        if (locMast == null) {
            List<Integer> rows = Utils.getGroupLoc(curRow);
            List<LocMast> locMasts = locMastService.queryFreeLocMast0(rows, rows.size(), locTypeDto.getLocType1(), inoutEveryday);
            if (!Cools.isEmpty(locMasts)) {
                Integer innermostRow = Utils.getInnermostRow(locMasts.get(0).getLocNo());
                for (LocMast one : locMasts) {
                    if (one.getRow1().equals(innermostRow)) {
                        locMast = one;
                        break;
                    }
                }
            }
            // 因库位移转、需预留空库位
            if (!locMastService.checkEmptyCount(locMast, 10)) { locMast = null; }
        }
            basCrnpService.checkSiteStatus(crnNo);
            // 获取目标站
            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)) {
                throw new CoolException("入库路径不存在");
        // 2.1 高低频管控、库位组无需全部为O,找到库位对应深库位不能为P R
        if (Cools.isEmpty(locMast)) {
            int sign = curRow;
            while (sign != 0 && Cools.isEmpty(locMast)) {
                List<LocMast> locMasts = locMastService.queryFreeLocMastEnd1(sign, locTypeDto.getLocType1(), inoutEveryday);
                for (LocMast loc : locMasts) {
                    // 同库位组对应其钱库位是否为 O.空库位
                    boolean success = true;
                    // 浅库位
                    List<String> outerLocList = Utils.getGroupOuterLoc(loc.getLocNo());
                    for (String outerLocNo : outerLocList) {
                        LocMast outerLoc = locMastService.selectById(outerLocNo);
                        if (Cools.isEmpty(outerLoc)) {
                            continue;
                        }
                        if (!outerLoc.getLocSts().equals("O")) {
                            success = false; break;
                        }
                    }
                    // 深库位判断
                    List<String> insideLocList = Utils.getGroupInsideLoc(loc.getLocNo());
                    for (String insideLocNo : insideLocList) {
                        LocMast insideLoc = locMastService.selectById(insideLocNo);
                        if (Cools.isEmpty(insideLoc)) {
                            continue;
                        }
                        if (insideLoc.getLocSts().equals("R")
                                || insideLoc.getLocSts().equals("P")
                                || insideLoc.getLocSts().equals("Q")
                                || insideLoc.getLocSts().equals("S")) {
                            success = false; break;
                        }
                    }
                    // 因库位移转、需预留空库位
                    if (!locMastService.checkEmptyCount(loc, 10)) {
                        success = false;
                    }
                    if (success) {
                        locMast = loc;
                        break;
                    }
                }
                sign = getCurRow(sign);
            }
            BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn());
            int inQty = staNo.getInQty()==null?0:staNo.getInQty();
            if (staNo.getInEnable().equals("Y") && staNo.getAutoing().equals("Y") && inQty<2) {
                // 查找库位
                if (locMast == null) {
                    locMast = locMastService.queryFreeLocMast(curRow);
                }
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("没有空库位");
                }
                String locNo = locMast.getLocNo();
        }
                // 更新库位排号
                rowLastno.setCurrentRow(curRow);
                rowLastnoService.updateById(rowLastno);
        // 2.无库位时,无视区域锁定,重新查找库位
        if (Cools.isEmpty(locMast)) {
            int sign = curRow;
            while (sign != 0 && Cools.isEmpty(locMast)) {
                List<LocMast> locMasts = locMastService.queryFreeLocMastEnd(sign, locTypeDto.getLocType1());
                for (LocMast loc : locMasts) {
                    // 同库位组对应其钱库位是否为 O.空库位
                    boolean success = true;
                    // 浅库位
                    List<String> outerLocList = Utils.getGroupOuterLoc(loc.getLocNo());
                    for (String outerLocNo : outerLocList) {
                        LocMast outerLoc = locMastService.selectById(outerLocNo);
                        if (Cools.isEmpty(outerLoc)) {
                            continue;
                        }
                        if (!outerLoc.getLocSts().equals("O")) {
                            success = false; break;
                        }
                    }
                    // 深库位
                    List<String> insideLocList = Utils.getGroupInsideLoc(loc.getLocNo());
                    for (String insideLocNo : insideLocList) {
                        LocMast insideLoc = locMastService.selectById(insideLocNo);
                        if (Cools.isEmpty(insideLoc)) {
                            continue;
                        }
                        if (insideLoc.getLocSts().equals("R")
                                || insideLoc.getLocSts().equals("P")
                                || insideLoc.getLocSts().equals("Q")
                                || insideLoc.getLocSts().equals("S")) {
                            success = false; break;
                        }
                    }
                    // 因库位移转、需预留空库位
                    if (!locMastService.checkEmptyCount(loc, 10)) {
                        success = false;
                    }
                    if (success) {
                        locMast = loc;
                        break;
                    }
                }
                sign = getCurRow(sign);
            }
        }
                // 返回dto
                startupDto.setCrnNo(crnNo);
                startupDto.setSourceStaNo(sourceStaNo);
                startupDto.setStaNo(staNo.getDevNo());
                startupDto.setLocNo(locNo);
        // 3.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位
        if (Cools.isEmpty(locMast)) {
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归
            if (times < rowCount) {
                times = times + 1;
                return getLocNo(whsType, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
            } else {
                throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用");
                times = 0;
            }
        } else {
            throw new CoolException(rowLastno.getWhsType()+"号库位排号分配规则不可用");
            // 货物检索低库位仓失败,兼容高库位仓后继续执行
            if (locTypeDto.getLocType1() == 1) {
                locTypeDto.setLocType1((short) 2);
                return getLocNo(1, staDescId, sourceStaNo, matNos, locTypeDto, oldLocType, times);
            }
            log.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times);
            throw new CoolException("没有空库位");
        }
        return startupDto;
    }
        String locNo = locMast.getLocNo();
    public static void main(String[] args) {
        System.out.println(Arith.remainder(1, 4));
        System.out.println("0200201".substring(0, 2));
        String locNo = "0800201";
        int row = Integer.parseInt(locNo.substring(0, 2));
        double remainder = Arith.remainder(row, 4);
        int targetRow;
        if (remainder == 1) {
            // 得到当前库位的外围库位
            targetRow =  row + 1;
        } else if (remainder == 0) {
            // 得到当前库位的内围库位
            targetRow =  row - 1;
        } else {
            return;
        }
        String zerofill = zerofill(String.valueOf(targetRow), 2)+locNo.substring(2);
        System.out.println(zerofill);
        // 返回dto
        startupDto.setWorkNo(workNo);
        startupDto.setCrnNo(crnNo);
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setStaNo(staNo.getDevNo());
        startupDto.setLocNo(locNo);
        return startupDto;
    }
    public static String zerofill(String msg, Integer count) {
@@ -222,4 +495,28 @@
        }
    }
}
    public int getCurRow(int curRow){
        switch (curRow){
            case 1:
            case 2:
            case 8:
            case 9:
            case 10:
            case 15:
            case 16:
            case 17:
                return curRow+1;
            case 5:
            case 6:
            case 7:
            case 13:
            case 14:
            case 20:
            case 21:
                return curRow-1;
            default:
                return 0;
        }
    }
}