From 27c184d7af2775a4d647348ee508f4de335df8fb Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期三, 11 三月 2026 15:28:37 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/utils/Utils.java |  453 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 453 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/asrs/utils/Utils.java b/src/main/java/com/zy/asrs/utils/Utils.java
index e0eb6dc..a67ee58 100644
--- a/src/main/java/com/zy/asrs/utils/Utils.java
+++ b/src/main/java/com/zy/asrs/utils/Utils.java
@@ -5,10 +5,16 @@
 import com.core.common.Cools;
 import com.core.common.SpringUtils;
 import com.core.exception.CoolException;
+import com.zy.asrs.entity.BasCrnp;
+import com.zy.asrs.entity.BasDevp;
 import com.zy.asrs.entity.LocMast;
 import com.zy.asrs.entity.RowLastno;
+import com.zy.asrs.service.BasCrnpService;
+import com.zy.asrs.service.BasDevpService;
+import com.zy.asrs.service.LocMastService;
 import com.zy.asrs.service.RowLastnoService;
 import com.zy.common.CodeBuilder;
+import com.zy.common.entity.Parameter;
 import com.zy.common.model.LocDetlDto;
 import com.zy.common.properties.SlaveProperties;
 import com.zy.common.service.CommonService;
@@ -19,7 +25,11 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Locale;
 
 /**
  * Created by vincent on 2020/8/27
@@ -120,6 +130,441 @@
         }
     }
 
+
+    public static Integer getStationStorageArea(Integer stationId) {
+        if (stationId == null || stationId <= 0) {
+            return null;
+        }
+        BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
+        BasDevp station = basDevpService.selectById(stationId);
+        if (station == null) {
+            return null;
+        }
+        return parseStorageArea(station.getArea());
+    }
+
+    /**
+     * 鐢熸垚鍏ュ簱鎵惧簱浣嶆椂鐨勫爢鍨涙満浼樺厛椤哄簭銆�
+     *
+     * <p>澶勭悊瑙勫垯锛�
+     * 1. 鍏堟牴鎹叆搴撶珯鐐规煡璇㈡墍灞炲簱鍖恒��
+     * 2. 鍏堟彁鍙栬搴撳尯鍐呯殑鍫嗗灈鏈猴紝骞舵寜鍙敤绌哄簱浣嶈繃婊や笉鍙敤鍫嗗灈鏈恒��
+     * 3. 鑻ュ綋鍓嶅簱鍖烘病鏈夋弧瓒虫潯浠剁殑绌哄簱浣嶏紝鍐嶈ˉ鍏呭叾浠栧簱鍖虹殑鍫嗗灈鏈恒��
+     * 4. 褰� {@code locType1 = 1} 鏃讹紝鍏堣繑鍥炰綆搴撲綅鍫嗗灈鏈猴紝鍐嶆妸鍚屾壒鍫嗗灈鏈虹殑楂樺簱浣嶈拷鍔犲埌鍚庨潰銆�
+     * 5. 瀵逛笉瀛樺湪銆佹晠闅溿�佷笉鍙叆浠ュ強鏃犵┖搴撲綅鐨勫爢鍨涙満鐩存帴鍓旈櫎銆�
+     * 6. 褰撶墿鏂欎负 {@code emptyPallet} 鏃讹紝鎸夌┖鏉垮叆搴撲紭鍏堣鍒欓噸鏂版帓搴忋��
+     *
+     * <p>杩斿洖缁撴灉涓殑姣忎竴椤规牸寮忎负锛�
+     * {@code {crnNo: 鍫嗗灈鏈哄彿, locType1: 搴撲綅楂樹綆绫诲瀷}}
+     *
+     * @param stationId 鍏ュ簱绔欑偣
+     * @param locType1 鐩爣搴撲綅楂樹綆绫诲瀷锛�1=浣庡簱浣嶏紝2=楂樺簱浣�
+     * @param matnr 鐗╂枡缂栫爜锛屼紶鍏� {@code emptyPallet} 鏃朵娇鐢ㄧ┖鏉挎帓搴忚鍒�
+     * @return 鎸変紭鍏堢骇鎺掑ソ搴忕殑鍫嗗灈鏈哄垪琛�
+     */
+    public static List<Map<String, Integer>> getStationStorageAreaName(Integer stationId, Integer locType1, String matnr) {
+        List<Map<String, Integer>> result = new ArrayList<>();
+        // 鍏堝畾浣嶅叆搴撶珯鐐规墍灞炲簱鍖恒��
+        Integer storageArea = getStationStorageArea(stationId);
+        Integer whsType = GetWhsType(stationId);
+        if (storageArea == null || whsType == null || whsType <= 0) {
+            return result;
+        }
+        RowLastnoService rowLastnoService = SpringUtils.getBean(RowLastnoService.class);
+        RowLastno rowLastno = rowLastnoService.selectById(whsType);
+        if (rowLastno == null) {
+            return result;
+        }
+
+        BasCrnpService basCrnpService = SpringUtils.getBean(BasCrnpService.class);
+        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
+        boolean emptyPallet = "emptyPallet".equalsIgnoreCase(matnr);
+
+        // 鍏堝彇褰撳墠搴撳尯瀵瑰簲鐨勫爢鍨涙満銆�
+        List<Integer> preferredCrnNos = getAreaCrnNos(storageArea, rowLastno);
+        List<Integer> preferredAvailableCrnNos = getAvailableCrnNos(preferredCrnNos, locType1, emptyPallet, basCrnpService, locMastService);
+        appendCrnLocTypeEntries(result, preferredAvailableCrnNos, locType1, locMastService);
+
+        // 褰撳墠搴撳尯娌℃湁鍙敤瀹归噺鏃讹紝鍐嶈ˉ鍏呭叾浠栧簱鍖哄爢鍨涙満銆�
+        if (!hasAvailableCapacity(preferredCrnNos, locType1, basCrnpService, locMastService)) {
+            List<Integer> otherAreaCrnNos = getOtherAreaCrnNos(storageArea, rowLastno);
+            List<Integer> otherAvailableCrnNos = getAvailableCrnNos(otherAreaCrnNos, locType1, emptyPallet, basCrnpService, locMastService);
+            appendCrnLocTypeEntries(result, otherAvailableCrnNos, locType1, locMastService);
+        }
+        return result;
+    }
+    private static void appendCrnLocTypeEntries(List<Map<String, Integer>> result, List<Integer> crnNos, Integer locType1, LocMastService locMastService) {
+        Short normalizedLocType1 = normalizeLocType1(locType1);
+        if (normalizedLocType1 == null) {
+            appendCrnLocTypeEntries(result, crnNos, (short) 1, locMastService);
+            appendCrnLocTypeEntries(result, crnNos, (short) 2, locMastService);
+            return;
+        }
+        appendCrnLocTypeEntries(result, crnNos, normalizedLocType1, locMastService);
+        if (normalizedLocType1 == 1) {
+            appendCrnLocTypeEntries(result, crnNos, (short) 2, locMastService);
+        }
+    }
+
+    private static void appendCrnLocTypeEntries(List<Map<String, Integer>> result, List<Integer> crnNos, Short targetLocType1, LocMastService locMastService) {
+        if (targetLocType1 == null || Cools.isEmpty(crnNos)) {
+            return;
+        }
+        for (Integer crnNo : crnNos) {
+            if (!hasAvailableLoc(crnNo, targetLocType1, locMastService) || containsCrnLocType(result, crnNo, targetLocType1)) {
+                continue;
+            }
+            Map<String, Integer> item = new LinkedHashMap<>();
+            item.put("crnNo", crnNo);
+            item.put("locType1", targetLocType1.intValue());
+            result.add(item);
+        }
+    }
+
+    private static boolean containsCrnLocType(List<Map<String, Integer>> result, Integer crnNo, Short locType1) {
+        for (Map<String, Integer> item : result) {
+            if (item == null) {
+                continue;
+            }
+            if (crnNo.equals(item.get("crnNo")) && locType1.intValue() == item.get("locType1")) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static boolean hasAvailableCapacity(List<Integer> crnNos, Integer locType1, BasCrnpService basCrnpService, LocMastService locMastService) {
+        return !getAvailableCrnNos(crnNos, locType1, false, basCrnpService, locMastService).isEmpty();
+    }
+
+    private static List<Integer> getAvailableCrnNos(List<Integer> candidateCrnNos, Integer locType1, boolean emptyPallet,
+                                                     BasCrnpService basCrnpService, LocMastService locMastService) {
+        LinkedHashSet<Integer> availableCrnNos = new LinkedHashSet<>();
+        if (Cools.isEmpty(candidateCrnNos)) {
+            return new ArrayList<>();
+        }
+        for (Integer crnNo : candidateCrnNos) {
+            if (crnNo == null || !basCrnpService.checkSiteError(crnNo, true)) {
+                continue;
+            }
+            if (!hasAvailableLocForRequest(crnNo, locType1, locMastService)) {
+                continue;
+            }
+            availableCrnNos.add(crnNo);
+        }
+        List<Integer> result = new ArrayList<>(availableCrnNos);
+        return result;
+    }
+
+    private static int compareEmptyPalletCrn(Integer leftCrnNo, Integer rightCrnNo, BasCrnpService basCrnpService) {
+        int leftPriority = getEmptyPalletPriority(basCrnpService.selectById(leftCrnNo));
+        int rightPriority = getEmptyPalletPriority(basCrnpService.selectById(rightCrnNo));
+        if (leftPriority != rightPriority) {
+            return Integer.compare(rightPriority, leftPriority);
+        }
+        return Integer.compare(leftCrnNo, rightCrnNo);
+    }
+
+    private static int getEmptyPalletPriority(BasCrnp basCrnp) {
+        if (basCrnp == null) {
+            return -1;
+        }
+        return "Y".equalsIgnoreCase(basCrnp.getEmpIn()) ? 1 : 0;
+    }
+
+    private static boolean hasAvailableLocForRequest(Integer crnNo, Integer locType1, LocMastService locMastService) {
+        Short normalizedLocType1 = normalizeLocType1(locType1);
+        if (normalizedLocType1 == null) {
+            return hasAvailableLoc(crnNo, (short) 1, locMastService) || hasAvailableLoc(crnNo, (short) 2, locMastService);
+        }
+        if (hasAvailableLoc(crnNo, normalizedLocType1, locMastService)) {
+            return true;
+        }
+        return normalizedLocType1 == 1 && hasAvailableLoc(crnNo, (short) 2, locMastService);
+    }
+
+    private static boolean hasAvailableLoc(Integer crnNo, Short locType1, LocMastService locMastService) {
+        if (crnNo == null || locType1 == null) {
+            return false;
+        }
+        return locMastService.selectCount(new EntityWrapper<LocMast>()
+                .eq("crn_no", crnNo)
+                .eq("loc_sts", "O")
+                .eq("loc_type1", locType1)) > 0;
+    }
+
+    private static Short normalizeLocType1(Integer locType1) {
+        if (locType1 == null || (locType1 != 1 && locType1 != 2)) {
+            return null;
+        }
+        return locType1.shortValue();
+    }
+
+    private static List<Integer> getOtherAreaCrnNos(Integer preferredArea, RowLastno rowLastno) {
+        LinkedHashSet<Integer> otherAreaCrnNos = new LinkedHashSet<>();
+        for (int area = 1; area <= 3; area++) {
+            if (preferredArea != null && preferredArea == area) {
+                continue;
+            }
+            otherAreaCrnNos.addAll(getAreaCrnNos(area, rowLastno));
+        }
+        if (otherAreaCrnNos.isEmpty()) {
+            otherAreaCrnNos.addAll(getAllCrnNos(rowLastno));
+            otherAreaCrnNos.removeAll(getAreaCrnNos(preferredArea, rowLastno));
+        }
+        return new ArrayList<>(otherAreaCrnNos);
+    }
+
+    private static List<Integer> getAreaCrnNos(Integer area, RowLastno rowLastno) {
+        LinkedHashSet<Integer> crnNos = new LinkedHashSet<>();
+        RowLastno areaRowLastno = findAreaRowLastno(area, rowLastno);
+        if (areaRowLastno == null) {
+            return new ArrayList<>(crnNos);
+        }
+        Integer startCrnNo = resolveAreaStartCrnNo(areaRowLastno, rowLastno);
+        Integer endCrnNo = resolveAreaEndCrnNo(areaRowLastno, rowLastno);
+        if (startCrnNo != null && endCrnNo != null && startCrnNo <= endCrnNo) {
+            for (int crnNo = startCrnNo; crnNo <= endCrnNo; crnNo++) {
+                addAreaCrnNo(crnNos, crnNo, 1, endCrnNo);
+            }
+            for (int crnNo = areaRowLastno.getsCrnNo(); crnNo <= startCrnNo; crnNo++) {
+                addAreaCrnNo(crnNos, crnNo, 1, endCrnNo);
+            }
+            Integer nextCrnQty = startCrnNo + 1;
+            if (areaRowLastno.geteCrnNo() != null && nextCrnQty > areaRowLastno.geteCrnNo()) {
+                nextCrnQty = areaRowLastno.getsCrnNo() == null ? 1 : areaRowLastno.getsCrnNo();
+            }
+            areaRowLastno.setCrnQty(nextCrnQty);
+            SpringUtils.getBean(RowLastnoService.class).updateById(areaRowLastno);
+        }
+
+        if (crnNos.isEmpty()) {
+            crnNos.addAll(getFallbackAreaCrnNos(area, rowLastno));
+        }
+        return new ArrayList<>(crnNos);
+    }
+
+    private static RowLastno findAreaRowLastno(Integer area, RowLastno defaultRowLastno) {
+        if (area == null) {
+            return defaultRowLastno;
+        }
+        RowLastnoService rowLastnoService = SpringUtils.getBean(RowLastnoService.class);
+        List<RowLastno> typeMatched = rowLastnoService.selectList(new EntityWrapper<RowLastno>()
+                .eq("type_id", area));
+        if (!Cools.isEmpty(typeMatched)) {
+            return typeMatched.get(0);
+        }
+        List<RowLastno> whsMatched = rowLastnoService.selectList(new EntityWrapper<RowLastno>()
+                .eq("whs_type", area));
+        if (!Cools.isEmpty(whsMatched)) {
+            return whsMatched.get(0);
+        }
+        return defaultRowLastno;
+    }
+
+    private static Integer resolveAreaStartCrnNo(RowLastno areaRowLastno, RowLastno defaultRowLastno) {
+        if (areaRowLastno != null && areaRowLastno.getCrnQty() != null && areaRowLastno.getCrnQty() > 0) {
+            return areaRowLastno.getCrnQty();
+        }
+        if (areaRowLastno != null && areaRowLastno.getsCrnNo() != null && areaRowLastno.getsCrnNo() > 0) {
+            return areaRowLastno.getsCrnNo();
+        }
+        if (defaultRowLastno != null && defaultRowLastno.getsCrnNo() != null && defaultRowLastno.getsCrnNo() > 0) {
+            return defaultRowLastno.getsCrnNo();
+        }
+        return 1;
+    }
+
+    private static Integer resolveAreaEndCrnNo(RowLastno areaRowLastno, RowLastno defaultRowLastno) {
+        if (areaRowLastno != null && areaRowLastno.geteCrnNo() != null && areaRowLastno.geteCrnNo() > 0) {
+            return areaRowLastno.geteCrnNo();
+        }
+        return null;
+    }
+
+    private static void addAreaCrnNo(LinkedHashSet<Integer> crnNos, Integer crnNo, Integer startCrnNo, Integer endCrnNo) {
+        if (crnNos == null || crnNo == null || startCrnNo == null || endCrnNo == null) {
+            return;
+        }
+        if (crnNo < startCrnNo || crnNo > endCrnNo) {
+            return;
+        }
+        crnNos.add(crnNo);
+    }
+
+    private static List<Integer> getAllCrnNos(RowLastno rowLastno) {
+        List<Integer> crnNos = new ArrayList<>();
+        if (rowLastno == null) {
+            return crnNos;
+        }
+        int startCrnNo = rowLastno.getsCrnNo() == null ? 1 : rowLastno.getsCrnNo();
+        int endCrnNo = rowLastno.geteCrnNo() == null ? startCrnNo + ((rowLastno.getCrnQty() == null ? 1 : rowLastno.getCrnQty()) - 1) : rowLastno.geteCrnNo();
+        for (int crnNo = startCrnNo; crnNo <= endCrnNo; crnNo++) {
+            crnNos.add(crnNo);
+        }
+        return crnNos;
+    }
+
+    private static List<Integer> getFallbackAreaCrnNos(Integer area, RowLastno rowLastno) {
+        List<Integer> allCrnNos = getAllCrnNos(rowLastno);
+        List<Integer> result = new ArrayList<>();
+        if (Cools.isEmpty(allCrnNos) || area == null || area < 1 || area > 3) {
+            return result;
+        }
+        int total = allCrnNos.size();
+        int baseSize = total / 3;
+        int remainder = total % 3;
+        int startIndex = 0;
+        for (int currentArea = 1; currentArea < area; currentArea++) {
+            startIndex += baseSize + (currentArea <= remainder ? 1 : 0);
+        }
+        int currentSize = baseSize + (area <= remainder ? 1 : 0);
+        int endIndex = Math.min(startIndex + currentSize, total);
+        for (int index = startIndex; index < endIndex; index++) {
+            result.add(allCrnNos.get(index));
+        }
+        return result;
+    }
+
+    private static List<Integer> mapRowsToCrnNos(RowLastno rowLastno, List<Integer> rows) {
+        List<Integer> result = new ArrayList<>();
+        if (rowLastno == null || Cools.isEmpty(rows)) {
+            return result;
+        }
+        LinkedHashSet<Integer> orderedCrnNos = new LinkedHashSet<>();
+        Integer rowSpan = getCrnRowSpan(rowLastno.getTypeId());
+        if (rowSpan == null || rowSpan <= 0) {
+            rowSpan = 2;
+        }
+        int startCrnNo = rowLastno.getsCrnNo() == null ? 1 : rowLastno.getsCrnNo();
+        int endCrnNo = rowLastno.geteCrnNo() == null ? startCrnNo + ((rowLastno.getCrnQty() == null ? 1 : rowLastno.getCrnQty()) - 1) : rowLastno.geteCrnNo();
+        int startRow = rowLastno.getsRow() == null ? 1 : rowLastno.getsRow();
+        int endRow = rowLastno.geteRow() == null ? Integer.MAX_VALUE : rowLastno.geteRow();
+        for (Integer row : rows) {
+            if (row == null || row < startRow || row > endRow) {
+                continue;
+            }
+            int crnNo = startCrnNo + (row - startRow) / rowSpan;
+            if (crnNo >= startCrnNo && crnNo <= endCrnNo) {
+                orderedCrnNos.add(crnNo);
+            }
+        }
+        result.addAll(orderedCrnNos);
+        return result;
+    }
+
+    private static Integer getCrnRowSpan(Integer typeId) {
+        if (typeId == null) {
+            return null;
+        }
+        switch (typeId) {
+            case 1:
+                return 4;
+            case 2:
+                return 2;
+            default:
+                return null;
+        }
+    }
+
+    private static String getRun2AreaRowsConfig(Integer area) {
+        Parameter parameter = Parameter.get();
+        if (parameter == null || area == null) {
+            return null;
+        }
+        switch (area) {
+            case 1:
+                return parameter.getRun2Area1Rows();
+            case 2:
+                return parameter.getRun2Area2Rows();
+            case 3:
+                return parameter.getRun2Area3Rows();
+            default:
+                return null;
+        }
+    }
+
+    private static List<Integer> parseAreaRows(String configValue, RowLastno rowLastno) {
+        List<Integer> rows = new ArrayList<>();
+        if (rowLastno == null || Cools.isEmpty(configValue)) {
+            return rows;
+        }
+        LinkedHashSet<Integer> orderedRows = new LinkedHashSet<>();
+        String normalized = configValue.replace("锛�", ",")
+                .replace("锛�", ";")
+                .replace("銆�", ",")
+                .replaceAll("\\s+", "");
+        if (normalized.isEmpty()) {
+            return rows;
+        }
+        for (String segment : normalized.split("[,;]")) {
+            if (segment == null || segment.isEmpty()) {
+                continue;
+            }
+            if (segment.contains("-")) {
+                String[] rangeParts = segment.split("-", 2);
+                Integer startRow = safeParseInt(rangeParts[0]);
+                Integer endRow = safeParseInt(rangeParts[1]);
+                if (startRow == null || endRow == null) {
+                    continue;
+                }
+                int step = startRow <= endRow ? 1 : -1;
+                for (int row = startRow; step > 0 ? row <= endRow : row >= endRow; row += step) {
+                    addAreaRow(orderedRows, row, rowLastno);
+                }
+                continue;
+            }
+            addAreaRow(orderedRows, safeParseInt(segment), rowLastno);
+        }
+        rows.addAll(orderedRows);
+        return rows;
+    }
+
+    private static void addAreaRow(LinkedHashSet<Integer> rows, Integer row, RowLastno rowLastno) {
+        if (rows == null || row == null || rowLastno == null) {
+            return;
+        }
+        if (row < rowLastno.getsRow() || row > rowLastno.geteRow()) {
+            return;
+        }
+        rows.add(row);
+    }
+
+    private static Integer safeParseInt(String value) {
+        if (Cools.isEmpty(value)) {
+            return null;
+        }
+        try {
+            return Integer.parseInt(value.trim());
+        } catch (NumberFormatException ignored) {
+            return null;
+        }
+    }
+    private static Integer parseStorageArea(String area) {
+        if (Cools.isEmpty(area)) {
+            return null;
+        }
+        String normalized = area.trim();
+        if (normalized.isEmpty()) {
+            return null;
+        }
+        try {
+            int areaNo = Integer.parseInt(normalized);
+            return areaNo >= 1 && areaNo <= 3 ? areaNo : null;
+        } catch (NumberFormatException ignored) {
+        }
+        String upper = normalized.toUpperCase(Locale.ROOT);
+        if ("A".equals(upper) || "A鍖�".equals(upper) || "A搴�".equals(upper) || "A搴撳尯".equals(upper)) {
+            return 1;
+        }
+        if ("B".equals(upper) || "B鍖�".equals(upper) || "B搴�".equals(upper) || "B搴撳尯".equals(upper)) {
+            return 2;
+        }
+        if ("C".equals(upper) || "C鍖�".equals(upper) || "C搴�".equals(upper) || "C搴撳尯".equals(upper)) {
+            return 3;
+        }
+        return null;
+    }
     public static String zerofill(String msg, Integer count) {
         if (msg.length() == count) {
             return msg;
@@ -774,3 +1219,11 @@
         return row + "-" + boy + "-" + lev;
     }
 }
+
+
+
+
+
+
+
+

--
Gitblit v1.9.1