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.utils.News; 
 | 
import com.zy.core.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.List; 
 | 
  
 | 
/** 
 | 
 * 货架核心功能 
 | 
 * Created by vincent on 2020/6/11 
 | 
 */ 
 | 
@Slf4j 
 | 
@Service 
 | 
public class CommonService { 
 | 
  
 | 
    @Autowired 
 | 
    private WrkMastService wrkMastService; 
 | 
    @Autowired 
 | 
    private WrkLastnoService wrkLastnoService; 
 | 
    @Autowired 
 | 
    private RowLastnoService rowLastnoService; 
 | 
    @Autowired 
 | 
    private BasCrnpService basCrnpService; 
 | 
    @Autowired 
 | 
    private StaDescService staDescService; 
 | 
    @Autowired 
 | 
    private BasDevpService basDevpService; 
 | 
    @Autowired 
 | 
    private LocMastService locMastService; 
 | 
    @Autowired 
 | 
    private SlaveProperties slaveProperties; 
 | 
    @Autowired 
 | 
    private LocDetlService locDetlService; 
 | 
  
 | 
    /** 
 | 
     * 生成工作号 
 | 
     * @return workNo(工作号) 
 | 
     */ 
 | 
    public int getWorkNo(Integer wrkMk) { 
 | 
        WrkLastno wrkLastno = wrkLastnoService.selectById(wrkMk); 
 | 
        if (Cools.isEmpty(wrkLastno)) { 
 | 
            throw new CoolException("数据异常,请联系管理员"); 
 | 
        } 
 | 
  
 | 
        int 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) { 
 | 
                workNo = workNo>=eNo ? sNo : workNo+1; 
 | 
            } else { 
 | 
                break; 
 | 
            } 
 | 
        } 
 | 
        // 修改序号记录 
 | 
        if (workNo > 0){ 
 | 
            wrkLastno.setWrkNo(workNo); 
 | 
            wrkLastnoService.updateById(wrkLastno); 
 | 
        } 
 | 
        // 检验 
 | 
        if (workNo == 0) { 
 | 
            throw new CoolException("生成工作号失败,请联系管理员"); 
 | 
        } else { 
 | 
            if (wrkMastService.selectById(workNo)!=null) { 
 | 
                throw new CoolException("生成工作号" + workNo + "在工作档中已存在"); 
 | 
            } 
 | 
        } 
 | 
        return workNo; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 检索库位号 
 | 
     * @param whsType 类型 1:双深式货架 
 | 
     * @param staDescId 路径工作类型 
 | 
     * @param sourceStaNo 源站 
 | 
     * @param matNos 物料号集合 
 | 
     * @return locNo 检索到的库位号 
 | 
     */ 
 | 
    @Transactional 
 | 
    public StartupDto getLocNo(Integer whsType, Integer staDescId, Integer sourceStaNo, List<String> matNos, LocTypeDto locTypeDto, int times) { 
 | 
        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; 
 | 
            case 110: 
 | 
                whsType = 4; 
 | 
                break; 
 | 
            case 301: 
 | 
                whsType = 5; 
 | 
                break; 
 | 
            default: 
 | 
                throw new CoolException("库位排号分配错误, 源站号:" + sourceStaNo); 
 | 
        } 
 | 
        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; 
 | 
        // 目标库位 
 | 
        LocMast locMast = null; 
 | 
  
 | 
        // 靠近摆放规则 --- 同天同规格物料 
 | 
        if (!Cools.isEmpty(matNos)) { 
 | 
            List<String> locNos = locDetlService.getSameDetlToday(matNos.get(0), sRow, eRow); 
 | 
            for (String locNo : locNos) { 
 | 
                if (Utils.isShallowLoc(slaveProperties, locNo)) { 
 | 
                    continue; 
 | 
                } 
 | 
                String shallowLocNo = Utils.getShallowLoc(slaveProperties, locNo); 
 | 
                // 检测目标库位是否为空库位 
 | 
                LocMast shallowLoc = locMastService.selectById(shallowLocNo); 
 | 
                if (shallowLoc != null && shallowLoc.getLocSts().equals("O")) { 
 | 
                    if (basCrnpService.checkSiteError(shallowLoc.getCrnNo(), true)) { 
 | 
                        if (VersionUtils.locMoveCheckLocType(shallowLoc, locTypeDto)) { 
 | 
                            locMast = shallowLoc; 
 | 
                            crnNo = locMast.getCrnNo(); 
 | 
                            break; 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        // 靠近摆放规则 --- 空托 
 | 
        if (staDescId == 10) { 
 | 
            List<LocMast> locMasts = locMastService.selectList(new EntityWrapper<LocMast>().eq("loc_sts", "D").ge("row1", sRow).le("row1", eRow)); 
 | 
            if (locMasts.size() > 0) { 
 | 
                for (LocMast loc : locMasts) { 
 | 
                    if (Utils.isShallowLoc(slaveProperties, loc.getLocNo())) { 
 | 
                        continue; 
 | 
                    } 
 | 
                    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(); 
 | 
                            break; 
 | 
                        } 
 | 
  
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        // 如果没有相近物料,则按规则轮询货架 
 | 
        if (null == locMast) { 
 | 
            Shelves shelves = new Shelves(rowCount, crn_qty); 
 | 
            // 1-4排 
 | 
            if (whsType == 1) { 
 | 
                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; 
 | 
                        break; 
 | 
                    } 
 | 
                } 
 | 
            // 5-8排 
 | 
            } else if (whsType == 2) { 
 | 
                curRow = curRow - 4; 
 | 
                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 + 1, true)) { 
 | 
                        // 偏移量补偿 
 | 
                        curRow = curRow + 4; 
 | 
                        crnNo = crnNo1 + 1; 
 | 
                        break; 
 | 
                    } 
 | 
  
 | 
                } 
 | 
            // 126空板入 1-8排 
 | 
            } 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; 
 | 
                        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)) { 
 | 
            News.error("type_no={},stn_no={},crn_no={}", staDescId, sourceStaNo, crnNo); 
 | 
            throw new CoolException("入库路径不存在"); 
 | 
        } 
 | 
        BasDevp staNo = basDevpService.selectById(staDesc.getCrnStn()); 
 | 
        if (!staNo.getAutoing().equals("Y")) { 
 | 
            throw new CoolException("目标站"+staDesc.getCrnStn()+"不可用"); 
 | 
        } 
 | 
        // 更新库位排号 
 | 
        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()); 
 | 
                // 因库位移转、需预留空库位 
 | 
                if (!locMastService.checkEmptyCount(locMast)) { 
 | 
                    locMast = null; 
 | 
                } 
 | 
            } 
 | 
            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; 
 | 
                    } 
 | 
                } 
 | 
                // 目标库位 ===>> 深库位, 则校验其浅库位是否为 O 
 | 
                if (null != locMast && Utils.isDeepLoc(slaveProperties, locMast.getLocNo())) { 
 | 
                    LocMast shallowLoc = locMastService.selectById(Utils.getShallowLoc(slaveProperties, locMast.getLocNo())); 
 | 
                    if (!shallowLoc.getLocSts().equals("O")) { 
 | 
                        locMast = null; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        // 2.库位当前所属尺寸无空库位时,调整尺寸参数,向上兼容检索库位 
 | 
        if (Cools.isEmpty(locMast)) { 
 | 
            // 当前巷道无空库位时,递归调整至下一巷道,检索全部巷道无果后,跳出递归 
 | 
            if (times < rowCount) { 
 | 
                times = times + 1; 
 | 
                return getLocNo(1, staDescId, sourceStaNo, matNos, locTypeDto, times); 
 | 
            } else { 
 | 
                times = 0; 
 | 
            } 
 | 
  
 | 
            // 轻货物找轻库位为空时,可以去找重库位仓 
 | 
            if (locTypeDto.getLocType1() == 1) { 
 | 
                locTypeDto.setLocType1((short) 2); 
 | 
                return getLocNo(null, staDescId, sourceStaNo, matNos, locTypeDto, times); 
 | 
            } 
 | 
            News.error("系统没有空库位!!! 尺寸规格: {}, 轮询次数:{}", JSON.toJSONString(locTypeDto), times); 
 | 
            throw new CoolException("没有空库位"); 
 | 
        } 
 | 
        String locNo = locMast.getLocNo(); 
 | 
  
 | 
        // 返回dto 
 | 
        startupDto.setWorkNo(workNo); 
 | 
        startupDto.setCrnNo(crnNo); 
 | 
        startupDto.setSourceStaNo(sourceStaNo); 
 | 
        startupDto.setStaNo(staNo.getDevNo()); 
 | 
        startupDto.setLocNo(locNo); 
 | 
        return startupDto; 
 | 
    } 
 | 
  
 | 
    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); 
 | 
    } 
 | 
  
 | 
    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(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |