自动化立体仓库 - WMS系统
lty
4 天以前 b7d562f09526fc5d1fddc3a692f47aeb3ac956f6
src/main/java/com/zy/asrs/task/handler/AutoEmptyOutHandler.java
@@ -1,20 +1,22 @@
package com.zy.asrs.task.handler;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
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.StaDesc;
import com.zy.asrs.entity.WrkDetl;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.entity.param.EmptyPlateOutParam;
import com.zy.asrs.service.BasDevpService;
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;
@@ -23,7 +25,9 @@
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
@@ -38,9 +42,213 @@
    @Autowired
    private WorkService workService;
    @Autowired
    private BasDevpService basDevpService;
    private LocDetlMapper locDetlMapper;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private StaDescService staDescService;
    @Transactional
    public ReturnT<String> 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<Integer> crnNos = new ArrayList<>();
            if (crnNo == null) {
                for (int i = 1; i < 5; i++) {
                    crnNos.add(i);
                }
            } else {
                crnNos.add(crnNo);
            }
            List<String> moved = new ArrayList<>();
            for (Integer currentCrnNo : crnNos) {
                if (currentCrnNo == null) {
                    continue;
                }
                List<WrkMast> wrkMastList = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                        .eq("crn_no", currentCrnNo)
                        .eq("io_type", 110));
                if (!wrkMastList.isEmpty()) {
                    continue;
                }
                Integer emptyLocCount = locMastService.selectCount(new EntityWrapper<LocMast>()
                        .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<String> 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());
        }
    }
    private String moveOldestInventoryInner(Integer crnNo, Long userId) {
        LocDetl oldest = locDetlMapper.selectOldestLocDetl(crnNo);
        if (Cools.isEmpty(oldest) || Cools.isEmpty(oldest.getLocNo())) {
            return null;
        }
        LocMast sourceLoc = locMastService.selectById(oldest.getLocNo());
        if (Cools.isEmpty(sourceLoc) || Cools.isEmpty(sourceLoc.getBarcode())) {
            return null;
        }
        Integer runningCount = wrkMastService.selectCount(new EntityWrapper<WrkMast>()
                .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 == crnNo) {
                continue;
            }
            Integer candidateEmpty = locMastService.selectCount(new EntityWrapper<LocMast>()
                    .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<LocMast>()
                .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;
        }
        List<Integer> sourceStaNos = staDescService.queryOutStaNosByLocNo(sourceLoc.getLocNo(), 101);
        if (Cools.isEmpty(sourceStaNos)) {
            return null;
        }
        Integer sourceStaNo = sourceStaNos.get(0);
        List<Integer> targetStaNos = staDescService.queryOutStaNosByLocNo(targetLoc.getLocNo(), 1);
        if (Cools.isEmpty(targetStaNos)) {
            return null;
        }
        Integer targetStaNo = targetStaNos.get(0);
        List<LocDetl> sourceDetls = locDetlMapper.selectList(new EntityWrapper<LocDetl>().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(10D);
        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.setPicking("N");
        wrkMast.setExitMk("Y");
        wrkMast.setEmptyMk("N");
        wrkMast.setLinkMis("N");
        wrkMast.setPacked(null);
        wrkMast.setOnlineYn("N");
        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 sourceDetl : sourceDetls) {
            WrkDetl wrkDetl = new WrkDetl();
            wrkDetl.sync(sourceDetl);
            wrkDetl.setWrkNo(workNo);
            wrkDetl.setIoTime(now);
            wrkDetl.setAnfme(sourceDetl.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<String> start() {
        try {
@@ -78,6 +286,11 @@
                                .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;
                }