package com.zy.asrs.task.handler; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.common.Cools; import com.zy.asrs.entity.LocDetl; import com.zy.asrs.entity.LocMast; import com.zy.asrs.entity.WrkDetl; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.entity.param.EmptyPlateOutParam; import com.zy.asrs.mapper.LocDetlMapper; import com.zy.asrs.service.LocMastService; import com.zy.asrs.service.StaDescService; import com.zy.asrs.service.WorkService; import com.zy.asrs.service.WrkDetlService; import com.zy.asrs.service.WrkMastService; import com.zy.asrs.task.AbstractHandler; import com.zy.asrs.task.core.ReturnT; import com.zy.common.service.CommonService; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import java.util.Date; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @Slf4j @Service public class AutoEmptyOutHandler extends AbstractHandler { @Autowired private ConfigService configService; @Autowired private WrkMastService wrkMastService; @Autowired private LocMastService locMastService; @Autowired private WorkService workService; @Autowired private LocDetlMapper locDetlMapper; @Autowired private WrkDetlService wrkDetlService; @Autowired private CommonService commonService; @Autowired private StaDescService staDescService; @Transactional public ReturnT moveOldestInventory(Integer crnNo, Integer max, Long userId) { try { Long operatorUserId = userId == null ? 1L : userId; Integer threshold = max; if (threshold == null) { Config config = configService.selectConfigByCode("EmptyMax"); if (Cools.isEmpty(config)) { threshold = 10; } else { threshold = Integer.valueOf(config.getValue()); } } Config configInventory = configService.selectConfigByCode("InventoryAutoOut"); if (Cools.isEmpty(configInventory) || "false".equalsIgnoreCase(configInventory.getValue())) { return new ReturnT<>(ReturnT.SUCCESS_CODE, "InventoryAutoOut=false"); } List crnNos = new ArrayList<>(); if (crnNo == null) { for (int i = 1; i < 5; i++) { crnNos.add(i); } } else { crnNos.add(crnNo); } List moved = new ArrayList<>(); for (Integer currentCrnNo : crnNos) { if (currentCrnNo == null) { continue; } List wrkMastList = wrkMastService.selectList(new EntityWrapper() .eq("crn_no", currentCrnNo) .eq("io_type", 110)); if (!wrkMastList.isEmpty()) { continue; } Integer emptyLocCount = locMastService.selectCount(new EntityWrapper() .eq("crn_no", currentCrnNo) .eq("loc_sts", "O") .eq("frozen", 0) .eq("deleted", 0) .eq("whs_type", 1)); if (emptyLocCount == null || emptyLocCount >= threshold) { continue; } String moveResult = moveOldestInventoryInner(currentCrnNo, operatorUserId); if (!Cools.isEmpty(moveResult)) { moved.add(moveResult); } } ReturnT res = new ReturnT<>(ReturnT.SUCCESS_CODE, "OK"); if (!moved.isEmpty()) { res.setContent(String.join(";", moved)); } return res; } catch (Exception e) { log.error("fail", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); } } public String moveOldestInventoryInner(Integer crnNo, Long userId) { LocDetl oldest = locDetlMapper.selectOldestLocDetl(crnNo); return moveInventoryFromLocDetl(oldest, userId); } public String moveMostMatnrInventoryInner(Integer crnNo, Long userId) { String matnr = locDetlMapper.selectMostMatnrByCrnNo(crnNo); if (Cools.isEmpty(matnr)) { return null; } LocDetl detl = locDetlMapper.selectOldestLocDetlByCrnNoAndMatnr(crnNo, matnr); return moveInventoryFromLocDetl(detl, userId); } private String moveInventoryFromLocDetl(LocDetl sourceDetl, Long userId) { if (Cools.isEmpty(sourceDetl) || Cools.isEmpty(sourceDetl.getLocNo())) { return null; } LocMast sourceLoc = locMastService.selectById(sourceDetl.getLocNo()); if (Cools.isEmpty(sourceLoc) || Cools.isEmpty(sourceLoc.getBarcode())) { return null; } Integer sourceCrnNo = sourceLoc.getCrnNo(); if (sourceCrnNo == null) { return null; } Integer runningCount = wrkMastService.selectCount(new EntityWrapper() .eq("barcode", sourceLoc.getBarcode()) .in("wrk_sts", Arrays.asList(2L, 3L, 4L, 11L, 12L))); if (runningCount != null && runningCount > 0) { return null; } Integer targetCrnNo = null; Integer targetEmptyCount = null; for (int candidate = 1; candidate < 5; candidate++) { if (candidate == sourceCrnNo) { continue; } // Integer runingCount = wrkMastService.selectCount(new EntityWrapper() // .eq("io_type", 101).eq("crn_no",candidate) // .in("wrk_sts", Arrays.asList(2L, 3L, 4L, 11L, 12L))); // if(runingCount != null && runingCount > 3) {continue;} Integer existCount = wrkMastService.selectCount(new EntityWrapper() .eq("io_type", 101) .eq("log_mk", "Y") .eq("ove_mk", "N")); int allow = 3 - (existCount == null ? 0 : existCount); if (allow <= 0) { continue; } Integer candidateEmpty = locMastService.selectCount(new EntityWrapper() .eq("crn_no", candidate) .eq("loc_sts", "O") .eq("frozen", 0) .eq("deleted", 0) .eq("whs_type", 1)); if (candidateEmpty == null || candidateEmpty <= 0) { continue; } if (targetCrnNo == null || candidateEmpty > targetEmptyCount) { targetCrnNo = candidate; targetEmptyCount = candidateEmpty; } } if (targetCrnNo == null) { return null; } LocMast targetLoc = locMastService.selectOne(new EntityWrapper() .eq("crn_no", targetCrnNo) .eq("loc_sts", "O") .eq("frozen", 0) .eq("deleted", 0) .eq("whs_type", 1) .orderBy("lev1,bay1")); if (Cools.isEmpty(targetLoc)) { return null; } if (sourceCrnNo.equals(targetLoc.getCrnNo())) { return null; } List sourceStaNos = staDescService.queryOutStaNosByLocNo(sourceLoc.getLocNo(), 101); if (Cools.isEmpty(sourceStaNos)) { return null; } Integer sourceStaNo = sourceStaNos.get(0); List targetStaNos = staDescService.queryOutStaNosByLocNo(targetLoc.getLocNo(), 1); if (Cools.isEmpty(targetStaNos)) { return null; } Integer targetStaNo = targetStaNos.get(0); List sourceDetls = locDetlMapper.selectList(new EntityWrapper().eq("loc_no", sourceLoc.getLocNo())); if (Cools.isEmpty(sourceDetls)) { return null; } Date now = new Date(); Integer workNo = commonService.getWorkNo(0); WrkMast wrkMast = new WrkMast(); wrkMast.setWrkNo(workNo); wrkMast.setIoTime(now); wrkMast.setWrkSts(11L); wrkMast.setIoType(101); wrkMast.setIoPri(12D); wrkMast.setCrnNo(sourceLoc.getCrnNo()); wrkMast.setSourceLocNo(sourceLoc.getLocNo()); wrkMast.setLocNo(targetLoc.getLocNo()); wrkMast.setBarcode(sourceLoc.getBarcode()); wrkMast.setSourceStaNo(String.valueOf(sourceStaNo)); wrkMast.setStaNo(String.valueOf(targetStaNo)); wrkMast.setFullPlt("Y"); wrkMast.setExitMk("N"); wrkMast.setLogMk("Y"); wrkMast.setEmptyMk("N"); wrkMast.setPacked(null); wrkMast.setOveMk("N"); wrkMast.setAppeUser(userId); wrkMast.setAppeTime(now); wrkMast.setModiUser(userId); wrkMast.setModiTime(now); if (!wrkMastService.insert(wrkMast)) { throw new RuntimeException("自动跨巷道移库生成任务失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo()); } for (LocDetl sourceDetl1 : sourceDetls) { WrkDetl wrkDetl = new WrkDetl(); wrkDetl.sync(sourceDetl1); wrkDetl.setWrkNo(workNo); wrkDetl.setIoTime(now); wrkDetl.setAnfme(sourceDetl1.getAnfme()); wrkDetl.setAppeTime(now); wrkDetl.setAppeUser(userId); wrkDetl.setModiTime(now); wrkDetl.setModiUser(userId); if (!wrkDetlService.insert(wrkDetl)) { throw new RuntimeException("自动跨巷道移库生成任务明细失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo()); } } sourceLoc.setLocSts("R"); sourceLoc.setModiUser(userId); sourceLoc.setModiTime(now); if (!locMastService.updateById(sourceLoc)) { throw new RuntimeException("自动跨巷道移库更新源库位失败,workNo=" + workNo + ", sourceLocNo=" + sourceLoc.getLocNo()); } targetLoc.setLocSts("S"); targetLoc.setModiUser(userId); targetLoc.setModiTime(now); if (!locMastService.updateById(targetLoc)) { throw new RuntimeException("自动跨巷道移库更新目标库位失败,workNo=" + workNo + ", targetLocNo=" + targetLoc.getLocNo()); } return "workNo=" + workNo + ",sourceLocNo=" + sourceLoc.getLocNo() + ",targetLocNo=" + targetLoc.getLocNo(); } @Transactional public ReturnT start() { try { Config config = configService.selectConfigByCode("EmptyMax"); Integer max; if (Cools.isEmpty(config)) { max = 10; }else{ max = Integer.valueOf(config.getValue()); } for(Integer crnNo = 1; crnNo < 5; crnNo++){ List wrkMastList = wrkMastService.selectList(new EntityWrapper() .eq("crn_no", crnNo) .eq("io_type", 110) ); if(!wrkMastList.isEmpty()){continue;} Integer emptyLocCount = locMastService.selectCount(new EntityWrapper() .eq("crn_no", crnNo) .eq("loc_sts", "O") .eq("frozen", 0) .eq("deleted", 0) .eq("whs_type", 1)); if (emptyLocCount == null || emptyLocCount >= max) { continue; } List locMastDList = locMastService.selectPage( new Page<>(1, 3), //选三个空轴库位 new EntityWrapper() .eq("crn_no", crnNo) .eq("loc_sts", "D") .eq("frozen", 0) .eq("deleted", 0) .eq("whs_type", 1) .orderBy("lev1,bay1") ).getRecords(); if (Cools.isEmpty(locMastDList)) { Config configInventory = configService.selectConfigByCode("InventoryAutoOut"); if (Cools.isEmpty(configInventory) || "false".equalsIgnoreCase(configInventory.getValue())) { continue; } moveOldestInventoryInner(crnNo, 1L); continue; } List locNos = new ArrayList<>(); for (LocMast locMast : locMastDList) { locNos.add(locMast.getLocNo()); } EmptyPlateOutParam emptyPlateOutParam = new EmptyPlateOutParam(); emptyPlateOutParam.setOutSite(1076); emptyPlateOutParam.setLocNos(locNos); workService.emptyPlateOut(emptyPlateOutParam, 1L); } } catch (Exception e) { log.error("fail", e); e.printStackTrace(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return FAIL.setMsg(e.getMessage()); } return SUCCESS; } }