From a1b99c6eb0e36c36f7696eff9bc23ecf57206661 Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期三, 08 四月 2026 19:51:49 +0800
Subject: [PATCH] WCS申请任务重新分配入库位

---
 src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java |  228 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 228 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java b/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
index 5d658fb..6f6c65f 100644
--- a/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
+++ b/src/main/java/com/zy/api/service/impl/WcsApiServiceImpl.java
@@ -7,6 +7,7 @@
 import com.core.common.Cools;
 import com.core.common.R;
 import com.core.exception.CoolException;
+import com.zy.api.controller.params.ReassignLocParams;
 import com.zy.api.controller.params.ReceviceTaskParams;
 import com.zy.api.controller.params.StopOutTaskParams;
 import com.zy.api.controller.params.WorkTaskParams;
@@ -18,6 +19,8 @@
 import com.zy.asrs.service.*;
 import com.zy.asrs.utils.Utils;
 import com.zy.common.constant.MesConstant;
+import com.zy.common.model.LocTypeDto;
+import com.zy.common.model.StartupDto;
 import com.zy.common.service.CommonService;
 import com.zy.common.utils.HttpHandler;
 import lombok.extern.slf4j.Slf4j;
@@ -28,6 +31,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -90,6 +94,8 @@
     private BasCrnpService basCrnpService;
     @Autowired
     private ApiLogService apiLogService;
+    @Autowired
+    private RowLastnoService rowLastnoService;
 
 
     /**
@@ -607,6 +613,62 @@
         }
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R reassignInboundLoc(ReassignLocParams params) {
+        if (params == null) {
+            return R.error("鍙傛暟涓嶈兘涓虹┖锛侊紒");
+        }
+        if (Cools.isEmpty(params.getTaskNo())) {
+            return R.error("浠诲姟鍙蜂笉鑳戒负绌猴紒锛�");
+        }
+
+        WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("wrk_no", params.getTaskNo()));
+        String validateMsg = validateReassignInboundTask(wrkMast);
+        if (!Cools.isEmpty(validateMsg)) {
+            return R.error(validateMsg);
+        }
+
+        LocMast currentLoc = locMastService.selectById(wrkMast.getLocNo());
+        if (Cools.isEmpty(currentLoc)) {
+            return R.error("褰撳墠鐩爣搴撲綅涓嶅瓨鍦�");
+        }
+
+        Integer preferredArea = resolveReassignArea(wrkMast, currentLoc);
+        if (preferredArea == null) {
+            return R.error("鏃犳硶纭畾浠诲姟鎵�灞炲簱鍖�");
+        }
+
+        List<Integer> candidateCrnNos = buildReassignCandidateCrnNos(preferredArea, wrkMast.getCrnNo());
+        if (candidateCrnNos.isEmpty()) {
+            return R.error("褰撳墠搴撳尯娌℃湁鍏朵粬鍫嗗灈鏈哄彲渚涢噸鍒嗛厤");
+        }
+
+        LocTypeDto locTypeDto = buildReassignLocTypeDto(currentLoc);
+        StartupDto startupDto = commonService.findRun2InboundLocByCandidateCrnNos(
+                wrkMast.getSourceStaNo(), wrkMast.getIoType(), preferredArea, candidateCrnNos, locTypeDto);
+        if (startupDto == null || Cools.isEmpty(startupDto.getLocNo())) {
+            return R.error("褰撳墠搴撳尯娌℃湁鍙噸鏂板垎閰嶇殑绌哄簱浣�");
+        }
+
+        LocMast targetLoc = locMastService.selectById(startupDto.getLocNo());
+        if (Cools.isEmpty(targetLoc)) {
+            throw new CoolException("鏂扮洰鏍囧簱浣嶄笉瀛樺湪");
+        }
+        if (!"O".equals(targetLoc.getLocSts())) {
+            throw new CoolException(targetLoc.getLocNo() + "鐩爣搴撲綅宸茶鍗犵敤");
+        }
+
+        Date now = new Date();
+        updateReassignTargetLoc(targetLoc, wrkMast, currentLoc, now);
+        updateReassignWorkMast(wrkMast, startupDto, now);
+        releaseOldReservedLocIfNeeded(currentLoc, targetLoc.getLocNo(), now);
+
+        Map<String, Object> result = new LinkedHashMap<>();
+        result.put("locNo", Utils.WMSLocToWCSLoc(targetLoc.getLocNo()));
+        return R.ok("鎿嶄綔鎴愬姛").add(result);
+    }
+
     private boolean requiresOutboundErpConfirm(WrkMast wrkMast) {
         Integer ioType = wrkMast == null ? null : wrkMast.getIoType();
         return ioType != null && (ioType == 101 || ioType == 103 || ioType == 104 || ioType == 107 || ioType == 110);
@@ -643,6 +705,172 @@
         return null;
     }
 
+    private String validateReassignInboundTask(WrkMast wrkMast) {
+        if (wrkMast == null) {
+            return "浠诲姟涓嶅瓨鍦�";
+        }
+        if (wrkMast.getIoType() == null || (wrkMast.getIoType() != 1 && wrkMast.getIoType() != 10)) {
+            return "褰撳墠浠诲姟涓嶆槸鍏ュ簱浠诲姟";
+        }
+        if (!Objects.equals(wrkMast.getWrkSts(), 2L)) {
+            return "褰撳墠浠诲姟鐘舵�佷笉鍏佽閲嶆柊鍒嗛厤鍏ュ簱浣�";
+        }
+        if (wrkMast.getCrnNo() == null) {
+            return "褰撳墠浠诲姟鏈垎閰嶅爢鍨涙満";
+        }
+        if (Cools.isEmpty(wrkMast.getLocNo())) {
+            return "褰撳墠浠诲姟鏈垎閰嶇洰鏍囧簱浣�";
+        }
+        if (wrkMast.getSourceStaNo() == null) {
+            return "褰撳墠浠诲姟缂哄皯婧愮珯淇℃伅";
+        }
+        return null;
+    }
+
+    private Integer resolveReassignArea(WrkMast wrkMast, LocMast currentLoc) {
+        Integer stationArea = Utils.getStationStorageArea(wrkMast.getSourceStaNo());
+        if (belongsToArea(stationArea, wrkMast.getCrnNo(), currentLoc)) {
+            return stationArea;
+        }
+        Integer fallbackArea = findAreaByCurrentTask(wrkMast.getCrnNo(), currentLoc);
+        if (fallbackArea != null) {
+            return fallbackArea;
+        }
+        return stationArea;
+    }
+
+    private Integer findAreaByCurrentTask(Integer currentCrnNo, LocMast currentLoc) {
+        for (int area = 1; area <= 3; area++) {
+            if (belongsToArea(area, currentCrnNo, currentLoc)) {
+                return area;
+            }
+        }
+        return null;
+    }
+
+    private boolean belongsToArea(Integer area, Integer currentCrnNo, LocMast currentLoc) {
+        if (area == null || area <= 0) {
+            return false;
+        }
+        RowLastno areaRowLastno = rowLastnoService.selectById(area);
+        if (areaRowLastno == null) {
+            return false;
+        }
+        Integer startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
+        Integer endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
+        if (currentCrnNo != null && currentCrnNo >= startCrnNo && currentCrnNo <= endCrnNo) {
+            return true;
+        }
+        Integer row = currentLoc == null ? null : currentLoc.getRow1();
+        Integer startRow = areaRowLastno.getsRow();
+        Integer endRow = areaRowLastno.geteRow();
+        return row != null && startRow != null && endRow != null && row >= startRow && row <= endRow;
+    }
+
+    private List<Integer> buildReassignCandidateCrnNos(Integer area, Integer currentCrnNo) {
+        RowLastno areaRowLastno = rowLastnoService.selectById(area);
+        if (areaRowLastno == null) {
+            throw new CoolException("鏈壘鍒板簱鍖鸿疆璇㈣鍒�");
+        }
+        int startCrnNo = resolveAreaStartCrnNo(areaRowLastno);
+        int endCrnNo = resolveAreaEndCrnNo(areaRowLastno, startCrnNo);
+        if (currentCrnNo == null || currentCrnNo < startCrnNo || currentCrnNo > endCrnNo) {
+            throw new CoolException("褰撳墠浠诲姟鍫嗗灈鏈轰笉鍦ㄦ墍灞炲簱鍖鸿寖鍥村唴");
+        }
+        List<Integer> candidateCrnNos = new ArrayList<>();
+        for (int crnNo = currentCrnNo - 1; crnNo >= startCrnNo; crnNo--) {
+            candidateCrnNos.add(crnNo);
+        }
+        for (int crnNo = endCrnNo; crnNo > currentCrnNo; crnNo--) {
+            candidateCrnNos.add(crnNo);
+        }
+        return candidateCrnNos;
+    }
+
+    private int resolveAreaStartCrnNo(RowLastno areaRowLastno) {
+        if (areaRowLastno.getsCrnNo() != null && areaRowLastno.getsCrnNo() > 0) {
+            return areaRowLastno.getsCrnNo();
+        }
+        return 1;
+    }
+
+    private int resolveAreaEndCrnNo(RowLastno areaRowLastno, int startCrnNo) {
+        if (areaRowLastno.geteCrnNo() != null && areaRowLastno.geteCrnNo() >= startCrnNo) {
+            return areaRowLastno.geteCrnNo();
+        }
+        int crnQty = areaRowLastno.getCrnQty() == null || areaRowLastno.getCrnQty() <= 0 ? 1 : areaRowLastno.getCrnQty();
+        return startCrnNo + crnQty - 1;
+    }
+
+    private LocTypeDto buildReassignLocTypeDto(LocMast currentLoc) {
+        LocTypeDto locTypeDto = new LocTypeDto();
+        if (currentLoc == null) {
+            return locTypeDto;
+        }
+        locTypeDto.setLocType1(normalizeLocType(currentLoc.getLocType1()));
+        locTypeDto.setLocType2(normalizeLocType(currentLoc.getLocType2()));
+        locTypeDto.setLocType3(normalizeLocType(currentLoc.getLocType3()));
+        return locTypeDto;
+    }
+
+    private Short normalizeLocType(Short locType) {
+        return locType == null || locType <= 0 ? null : locType;
+    }
+
+    private void updateReassignTargetLoc(LocMast targetLoc, WrkMast wrkMast, LocMast currentLoc, Date now) {
+        targetLoc.setLocSts("S");
+        targetLoc.setModiUser(WCS_SYNC_USER);
+        targetLoc.setModiTime(now);
+        if (!Cools.isEmpty(wrkMast.getBarcode())) {
+            targetLoc.setBarcode(wrkMast.getBarcode());
+        } else if (!Cools.isEmpty(currentLoc) && !Cools.isEmpty(currentLoc.getBarcode())) {
+            targetLoc.setBarcode(currentLoc.getBarcode());
+        } else {
+            targetLoc.setBarcode("");
+        }
+        if (wrkMast.getScWeight() != null) {
+            targetLoc.setScWeight(wrkMast.getScWeight());
+        } else if (!Cools.isEmpty(currentLoc) && currentLoc.getScWeight() != null) {
+            targetLoc.setScWeight(currentLoc.getScWeight());
+        } else {
+            targetLoc.setScWeight(BigDecimal.ZERO);
+        }
+        if (!locMastService.updateById(targetLoc)) {
+            throw new CoolException("鏀瑰彉搴撲綅鐘舵�佸け璐�");
+        }
+    }
+
+    private void updateReassignWorkMast(WrkMast wrkMast, StartupDto startupDto, Date now) {
+        wrkMast.setLocNo(startupDto.getLocNo());
+        wrkMast.setCrnNo(startupDto.getCrnNo());
+        if (startupDto.getStaNo() != null) {
+            wrkMast.setStaNo(startupDto.getStaNo());
+        }
+        wrkMast.setWrkSts(2L);
+        wrkMast.setModiUser(WCS_SYNC_USER);
+        wrkMast.setModiTime(now);
+        if (!wrkMastService.updateById(wrkMast)) {
+            throw new CoolException("淇敼宸ヤ綔妗eけ璐�");
+        }
+    }
+
+    private void releaseOldReservedLocIfNeeded(LocMast currentLoc, String newLocNo, Date now) {
+        if (currentLoc == null || Cools.isEmpty(currentLoc.getLocNo()) || currentLoc.getLocNo().equals(newLocNo)) {
+            return;
+        }
+        if (!"S".equals(currentLoc.getLocSts())) {
+            return;
+        }
+        currentLoc.setLocSts("O");
+        currentLoc.setBarcode("");
+        currentLoc.setScWeight(BigDecimal.ZERO);
+        currentLoc.setModiUser(WCS_SYNC_USER);
+        currentLoc.setModiTime(now);
+        if (!locMastService.updateById(currentLoc)) {
+            throw new CoolException("閲婃斁鍘熺洰鏍囧簱浣嶅け璐�");
+        }
+    }
+
     /**
      * 鎸変换鍔$被鍨嬮�夋嫨 WCS 鎺ュ彛鍦板潃銆�
      * in -> 鍏ュ簱鎺ュ彛

--
Gitblit v1.9.1