package com.zy.asrs.common.wms.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.zy.asrs.common.domain.dto.LocRangeDto;
|
import com.zy.asrs.common.domain.dto.LocTypeDto;
|
import com.zy.asrs.common.domain.dto.StartupDto;
|
import com.zy.asrs.common.domain.entity.StaDesc;
|
import com.zy.asrs.common.sys.service.StaDescService;
|
import com.zy.asrs.common.utils.Utils;
|
import com.zy.asrs.common.utils.VersionUtils;
|
import com.zy.asrs.common.wms.entity.*;
|
import com.zy.asrs.framework.common.Cools;
|
import com.zy.asrs.framework.exception.CoolException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 货架核心功能
|
* Created by vincent on 2020/6/11
|
*/
|
@Slf4j
|
@Service
|
public class CommonService {
|
|
@Autowired
|
private WrkMastService wrkMastService;
|
@Autowired
|
private WrkLastnoService wrkLastnoService;
|
@Autowired
|
private RowLastnoService rowLastnoService;
|
@Autowired
|
private StaDescService staDescService;
|
@Autowired
|
private BasDevpService basDevpService;
|
@Autowired
|
private LocMastService locMastService;
|
@Autowired
|
private LocDetlService locDetlService;
|
@Autowired
|
private LocRuleService locRuleService;
|
@Autowired
|
private LocDirectionService locDirectionService;
|
|
private Long WHS_TYPE = 0L;//默认立库
|
|
/**
|
* 生成工作号
|
*
|
* @param wrkMk
|
* @return workNo(工作号)
|
*/
|
public int getWorkNo(Integer wrkMk) {
|
WrkLastno wrkLastno = wrkLastnoService.getOne(new LambdaQueryWrapper<WrkLastno>().eq(WrkLastno::getWrkMk, wrkMk));
|
if (Cools.isEmpty(wrkLastno)) {
|
throw new CoolException("数据异常,请联系管理员");
|
}
|
|
int workNo = wrkLastno.getWrkNo();
|
int sNo = wrkLastno.getStartNo();
|
int eNo = wrkLastno.getTargetNo();
|
workNo = workNo >= eNo ? sNo : workNo + 1;
|
while (true) {
|
WrkMast wrkMast = wrkMastService.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, 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.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, workNo)) != null) {
|
throw new CoolException("生成工作号" + workNo + "在工作档中已存在");
|
}
|
}
|
return workNo;
|
}
|
|
/**
|
* 检索库位号
|
*/
|
@Transactional
|
public StartupDto getLocNo(Integer staDescId, Integer sourceStaNo, List<String> matnrs, String batch, Long hostId, LocTypeDto locTypeDto, int times) {
|
if (sourceStaNo==999){//平库内置判断站点(临时解决方案)
|
WHS_TYPE = 1L;//平库
|
} else {
|
WHS_TYPE = 0L;//立库
|
}
|
// 目标库位
|
LocMast locMast = null;
|
|
// 靠近摆放规则 --- 空托
|
locMast = getLocNoEmptyPakIn(staDescId, locTypeDto, sourceStaNo, hostId);
|
if (locMast != null) {
|
//找到库位,返回dto
|
return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
|
}
|
|
//库位规则
|
locMast = getLocNoStepRule(matnrs, batch, locTypeDto, hostId);
|
if (locMast != null) {
|
//找到库位,返回dto
|
return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
|
}
|
|
// 靠近摆放规则 --- 相同订单号, 同天同规格物料
|
locMast = getLocNoNearPlace(staDescId, matnrs, batch, locTypeDto, sourceStaNo, hostId);
|
if (locMast != null) {
|
//找到库位,返回dto
|
return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
|
}
|
|
//搜索整个空库位组
|
locMast = getLocNoStepSingle(locTypeDto, staDescId, sourceStaNo, hostId);
|
if (locMast != null) {
|
//找到库位,返回dto
|
return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
|
}
|
|
//找不到库位,抛出异常
|
throw new CoolException("没有空库位");
|
}
|
|
/**
|
* 库位规则
|
*/
|
private LocMast getLocNoStepRule(List<String> matnrs, String batch, LocTypeDto locTypeDto, Long hostId) {
|
if (matnrs.size() != 1) {
|
return null;
|
}
|
String matnr = matnrs.get(0);
|
LambdaQueryWrapper<LocRule> wrapper = new LambdaQueryWrapper<LocRule>()
|
.eq(LocRule::getHostId, hostId)
|
.eq(LocRule::getMatnr, matnr);
|
if (batch != null) {
|
wrapper.eq(LocRule::getBatch, batch);
|
}
|
List<LocRule> locRules = locRuleService.list(wrapper);
|
if (locRules == null || locRules.isEmpty()) {
|
return null;//没有库位规则,跳出当前任务
|
}
|
|
for (LocRule locRule : locRules) {
|
if (locRule == null) {
|
continue;
|
}
|
|
List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1(), locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd(), hostId,WHS_TYPE);
|
for (LocMast locMast0 : locMasts) {
|
//预留空库位
|
if (locMastService.checkEmptyCount(locMast0, 5, hostId)) {
|
return locMast0;
|
}
|
}
|
}
|
|
if (locRules.get(0).getKeepGo() == 0) {
|
//找不到空库位,且禁止继续寻找其他非混载区域库位
|
//找不到库位,抛出异常
|
throw new CoolException("规则区域没有空库位");
|
}
|
|
return null;
|
}
|
|
// 靠近摆放规则 --- 相同订单号, 同天同规格物料
|
private LocMast getLocNoNearPlace(Integer staDescId, List<String> matnrs, String batch, LocTypeDto locTypeDto, Integer sourceStaNo, Long hostId) {
|
LocMast locMast = null;
|
if (staDescId == 1) {
|
// 获取目标站
|
LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
|
.eq(StaDesc::getTypeNo, staDescId)
|
.eq(StaDesc::getStnNo, sourceStaNo)
|
.eq(StaDesc::getHostId, hostId);
|
StaDesc staDesc = staDescService.getOne(wrapper);
|
if (staDesc == null) {
|
throw new CoolException("入库路径不存在");
|
}
|
|
String matnr = matnrs.get(0);
|
|
//库位搜索范围
|
LocRangeDto locRangeDto = locTypeDto.getLocRangeDto();
|
|
//先判断工作档,查找主档IOType=1, wrkSts < 10的工作明细,料号相同的明细库位
|
List<WrkMast> wrkMasts = wrkMastService.selectSameMatnrs(matnr, batch, locTypeDto.getLocRangeDto(), hostId);
|
for (WrkMast wrkMast : wrkMasts) {
|
LocMast locMast0 = locMastService.findNearloc(wrkMast.getLocNo(), hostId, locRangeDto);
|
if (null != locMast0) {
|
// 浅库位符合尺寸检测
|
if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
|
locMast = locMast0;
|
}
|
}
|
}
|
if (Cools.isEmpty(locMast)) {
|
//再判断库存明细档,料号相同的明细库位
|
List<String> locNos = locDetlService.getSameDetl(matnr, batch, hostId);
|
for (String locNo : locNos) {
|
LocMast locMast0 = locMastService.findNearloc(locNo, hostId, locRangeDto);
|
if (null != locMast0) {
|
// 浅库位符合尺寸检测
|
if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
|
locMast = locMast0;
|
break;
|
}
|
}
|
}
|
}
|
}
|
|
return locMast;
|
}
|
|
// 靠近摆放规则 --- 空托
|
private LocMast getLocNoEmptyPakIn(Integer staDescId, LocTypeDto locTypeDto, Integer sourceStaNo, Long hostId) {
|
LocMast locMast = null;
|
// 靠近摆放规则 --- 空托
|
if (staDescId == 10) {
|
// 获取目标站
|
LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
|
.eq(StaDesc::getTypeNo, staDescId)
|
.eq(StaDesc::getStnNo, sourceStaNo)
|
.eq(StaDesc::getHostId,hostId);
|
StaDesc staDesc = staDescService.getOne(wrapper);
|
if (staDesc == null) {
|
throw new CoolException("入库路径不存在");
|
}
|
|
//找相同空托盘
|
LambdaQueryWrapper<LocMast> wrapper0 = new LambdaQueryWrapper<LocMast>()
|
.eq(LocMast::getLocSts, "D")
|
.eq(LocMast::getLocType1, locTypeDto.getLocType1())
|
.eq(LocMast::getCrnNo, staDesc.getDeviceNo())
|
.eq(LocMast::getWhsType, WHS_TYPE)
|
.eq(LocMast::getHostId, hostId);
|
|
LambdaQueryWrapper<LocMast> wrapper1 = new LambdaQueryWrapper<LocMast>()
|
.eq(LocMast::getLocSts, "O")
|
.eq(LocMast::getLocType1, locTypeDto.getLocType1())
|
.eq(LocMast::getCrnNo, staDesc.getDeviceNo())
|
.eq(LocMast::getWhsType, WHS_TYPE)
|
.eq(LocMast::getHostId, hostId);
|
|
//库位搜索范围
|
LocRangeDto locRangeDto = locTypeDto.getLocRangeDto();
|
if (locRangeDto != null) {
|
wrapper1.ge(LocMast::getRow1, locRangeDto.getStartRow());
|
wrapper1.le(LocMast::getRow1, locRangeDto.getTargetRow());
|
wrapper1.ge(LocMast::getBay1, locRangeDto.getStartBay());
|
wrapper1.le(LocMast::getBay1, locRangeDto.getTargetBay());
|
wrapper1.ge(LocMast::getLev1, locRangeDto.getStartLev());
|
wrapper1.le(LocMast::getLev1, locRangeDto.getTargetLev());
|
}
|
|
List<LocMast> locMasts0 = locMastService.list(wrapper0);
|
if (!locMasts0.isEmpty()) {
|
for (LocMast loc : locMasts0) {
|
LocMast locMast0 = locMastService.findNearloc(loc.getLocNo(), hostId, locRangeDto);
|
if (null != locMast0) {
|
locMast = locMast0;
|
break;
|
}
|
}
|
}
|
|
if (locMast == null) {
|
List<LocMast> locMasts = locMastService.list(wrapper1);
|
if (!locMasts.isEmpty()) {
|
for (LocMast loc : locMasts) {
|
LocMast locMast0 = locMastService.findNearloc(loc.getLocNo(), hostId, locRangeDto);
|
if (null != locMast0) {
|
locMast = locMast0;
|
break;
|
}
|
}
|
}
|
}
|
|
if (locMast == null) {
|
throw new CoolException("缺少空库位");
|
}
|
}
|
return locMast;
|
}
|
|
// 搜索单品(整个库位组)
|
private LocMast getLocNoStepSingle(LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo, Long hostId) {
|
LocMast locMast = null;
|
// 获取目标站
|
LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
|
.eq(StaDesc::getTypeNo, staDescId)
|
.eq(StaDesc::getStnNo, sourceStaNo)
|
.eq(StaDesc::getHostId, hostId);
|
StaDesc staDesc = staDescService.getOne(wrapper);
|
if (staDesc == null) {
|
throw new CoolException("入库路径不存在");
|
}
|
|
RowLastno rowLastno = rowLastnoService.getOne(new LambdaQueryWrapper<RowLastno>().eq(RowLastno::getWhsType, 1).eq(RowLastno::getHostId, hostId));
|
if (Cools.isEmpty(rowLastno)) {
|
throw new CoolException("1数据异常,请联系管理员");
|
}
|
|
int curRow = rowLastno.getCurrentRow();
|
int sRow = rowLastno.getStartRow();
|
int eRow = rowLastno.getTargetRow();
|
|
LambdaQueryWrapper<LocMast> wrapper1 = new LambdaQueryWrapper<LocMast>()
|
.eq(LocMast::getLocSts, "O")
|
.eq(LocMast::getHostId, hostId)
|
.eq(LocMast::getLocType1, locTypeDto.getLocType1())
|
.le(LocMast::getRow1, eRow)
|
.ge(LocMast::getRow1, sRow)
|
// .in(LocMast::getRow1, curRow)
|
.orderByAsc(LocMast::getLev1)
|
.orderByAsc(LocMast::getBay1);
|
|
|
//库位搜索范围
|
LocRangeDto locRangeDto = locTypeDto.getLocRangeDto();
|
if (locRangeDto != null) {
|
wrapper1.ge(LocMast::getRow1, locRangeDto.getStartRow());
|
wrapper1.le(LocMast::getRow1, locRangeDto.getTargetRow());
|
wrapper1.ge(LocMast::getBay1, locRangeDto.getStartBay());
|
wrapper1.le(LocMast::getBay1, locRangeDto.getTargetBay());
|
wrapper1.ge(LocMast::getLev1, locRangeDto.getStartLev());
|
wrapper1.le(LocMast::getLev1, locRangeDto.getTargetLev());
|
}
|
List<LocMast> locMasts = locMastService.list(wrapper1);//搜索货物
|
for (LocMast mast : locMasts) {
|
LocMast innerLoc = null;
|
for (String loc : Utils.getGroupLoc(mast.getLocNo(), hostId)) {
|
//1 2
|
LocMast one = locMastService.getOne(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocNo, loc).eq(LocMast::getHostId, hostId));
|
if (!one.getLocSts().equals("O")) {
|
// innerLoc = null;
|
continue;
|
}
|
if(Utils.getRow(one.getLocNo())==2){
|
LocMast locMast1=locMastService.getOne(new LambdaQueryWrapper<LocMast>()
|
.eq(LocMast::getRow1, 1)
|
.eq(LocMast::getBay1,one.getBay1())
|
.eq(LocMast::getLev1,one.getLev1())
|
.eq(LocMast::getHostId, hostId));
|
if(!locMast1.getLocSts().equals("F")){
|
continue;
|
}else if (locMast1.getLocSts().equals("O")){
|
one=locMast1;
|
}
|
}else if(Utils.getRow(one.getLocNo())==3){
|
LocMast locMast1=locMastService.getOne(new LambdaQueryWrapper<LocMast>()
|
.eq(LocMast::getRow1, 4)
|
.eq(LocMast::getBay1,one.getBay1())
|
.eq(LocMast::getLev1,one.getLev1())
|
.eq(LocMast::getHostId, hostId));
|
if(!locMast1.getLocSts().equals("F")){
|
continue;
|
}else if (locMast1.getLocSts().equals("O")){
|
one=locMast1;
|
}
|
}
|
innerLoc = one;
|
|
// if (innerLoc == null) {
|
// List<Integer> innerDeepRow = locDirectionService.getInnerDeepRow(hostId);
|
//// if (innerDeepRow.contains(one.getRow1())) {
|
// innerLoc = one;
|
//// }
|
// }
|
}
|
|
if(innerLoc == null) {
|
continue;
|
}
|
|
if (VersionUtils.checkLocType(innerLoc, locTypeDto)) {
|
locMast = innerLoc;
|
break;
|
}
|
}
|
|
curRow++;
|
if (curRow > eRow) {
|
curRow = sRow;
|
}
|
rowLastno.setCurrentRow(curRow);
|
rowLastnoService.updateById(rowLastno);
|
return locMast;
|
}
|
|
//返回dto
|
private StartupDto getLocNoStep6(Integer staDescId, Integer sourceStaNo, LocMast locMast) {
|
StartupDto startupDto = new StartupDto();
|
// 获取目标站
|
LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
|
.eq(StaDesc::getTypeNo, staDescId)
|
.eq(StaDesc::getStnNo, sourceStaNo);
|
StaDesc staDesc = staDescService.getOne(wrapper);
|
if (Cools.isEmpty(staDesc)) {
|
log.error("入库路径不存在, staDescId={}, sourceStaNo={}", staDescId, sourceStaNo);
|
throw new CoolException("入库路径不存在");
|
}
|
|
// 检测目标站
|
BasDevp staNo = basDevpService.getById(staDesc.getDeviceStn());
|
if (!staNo.getAutoing().equals("Y")) {
|
throw new CoolException("目标站" + staDesc.getDeviceStn() + "不可用");
|
}
|
|
// 生成工作号
|
int workNo = getWorkNo(0);
|
// 返回dto
|
startupDto.setWorkNo(workNo);
|
startupDto.setSourceStaNo(sourceStaNo);
|
startupDto.setStaNo(staNo.getDevNo());
|
startupDto.setLocNo(locMast.getLocNo());
|
startupDto.setCrnNo(locMast.getCrnNo());
|
return startupDto;
|
}
|
}
|