From ce5c5e897b288a27f00804b68e73647fdfc5b6a7 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期二, 29 四月 2025 10:15:11 +0800
Subject: [PATCH] 数采,成束释放

---
 cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java |  486 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 453 insertions(+), 33 deletions(-)

diff --git a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
index 44a20e5..681a174 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
@@ -15,6 +15,7 @@
 import javax.script.ScriptEngineManager;
 import javax.servlet.http.HttpServletRequest;
 import java.math.BigDecimal;
+import java.text.DecimalFormat;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
@@ -139,6 +140,9 @@
                 case ".pdf":
                     map = readPdfString(data, userMap, device);
                     break;
+                case ".et":
+                    map = readEtString(data, userMap, device);
+                    break;
                 default:
                     map = null;
                     break;
@@ -152,40 +156,262 @@
         }
     }
 
-    private static Map<String, Object> readPdfString(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
+    private static Map<String, Object> readEtString(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
         Map<String, Object> map = new HashMap<>();
+        // 瀹氫箟 RoHS 鎸囨爣鍒楄〃
+        List<String> rohsIndicators = Arrays.asList("RoHS鎸囨爣,闀夛紙Cd锛�", "RoHS鎸囨爣,鎬绘捍鑱旇嫰锛圔r锛�", "RoHS鎸囨爣,鎬婚摤锛圕r锛�", "RoHS鎸囨爣,姹烇紙Hg锛�", "RoHS鎸囨爣,閾咃紙Pb锛�");
         dataConfig.forEach((k, v) -> {
-            List<Object> list = new ArrayList<>();
-            for (int config = 0; config < v.size(); config++) {
-                String referx = getRefer(v.get(config).getReferx());
-                String result = null;
-                // 閫氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿
-                String[] aColumnY = data.split("\n");
-                List<String> list1 = new ArrayList<>();
-                // 璇ュ惊鐜緱鍑虹敤鎴烽厤缃殑y杞�
-                for (int i = 0; i < aColumnY.length; i++) {
-                    String addDataWithSpaces = referx.replaceAll("", " ");
-                    int x = getXOrY(v.get(config).getX(), k, "X");
-                    if (aColumnY[i].contains(addDataWithSpaces)) {
-                        Matcher matcher = SPATTERN.matcher(aColumnY[i]);
-                        while (matcher.find()) {
-                            String group = matcher.group();
-                            list1.add(group);
-                        }
-                    }
-                    if (ObjectUtils.isNotEmpty(list1)) {
-                        result = list1.get(x);
-                    }
+            List<Object> list;
+            boolean containsRoHS = false;
+            for (DataConfig item : v) {
+                if (rohsIndicators.contains(item.getInsProductItem())) {
+                    containsRoHS = true;
+                    break;
                 }
-                if (ObjectUtils.isNotEmpty(result)) {
-                    list.add(result);
-                }
+            }
+            if (containsRoHS) {
+                list = analyzeEt1Data(data, v, splitIdentifier);
+            } else {
+                list = analyzeEtData(data, v, splitIdentifier);
             }
             // 杩涜鍏紡璁$畻
             Object resultValue = calculationFormula(list, v.get(0), k, device);
             map.put(k, resultValue);
         });
         return map;
+    }
+
+    /**
+     * 鑽у厜鍏夎氨浠� .et鏂囦欢
+     *
+     * @param data
+     * @param v
+     * @param splitIdentifier
+     * @return
+     */
+    private static List<Object> analyzeEt1Data(String data, List<DataConfig> v, String splitIdentifier) {
+        List<Object> finalResult = new ArrayList<>();
+        List<String> matchingLines = new ArrayList<>();
+
+        // 閬嶅巻 DataConfig 鍒楄〃
+        for (DataConfig config : v) {
+            String referx = getRefer(config.getReferx());
+            if (ObjectUtils.isEmpty(referx)) {
+                continue;
+            }
+            // 浣跨敤姝e垯琛ㄨ揪寮忔瀯寤哄畬鏁村尮閰嶇殑妯″紡
+            String regex = "\\b" + Pattern.quote(referx) + "\\b";
+            Pattern pattern = Pattern.compile(regex);
+
+            String[] lines = data.replaceAll(" ", "").split("\n");
+            // 绛涢�夊嚭鍖呭惈 referx 鐨勮
+            for (String line : lines) {
+                if (pattern.matcher(line).find()) {
+                    matchingLines.add(line);
+                }
+            }
+        }
+
+        // 閬嶅巻鍖归厤鐨勮锛屾彁鍙� splitIdentifier 绗竷涓拰绗叓涓垎闅旂涓棿鐨勫��
+        for (String line : matchingLines) {
+            int startIndex = -1;
+            int endIndex = -1;
+            int count = 0;
+            int index = 0;
+            while ((index = line.indexOf(splitIdentifier, index)) != -1) {
+                count++;
+                if (count == 7) {
+                    startIndex = index + splitIdentifier.length();
+                } else if (count == 8) {
+                    endIndex = index;
+                    break;
+                }
+                index++;
+            }
+            if (startIndex != -1 && endIndex != -1) {
+                finalResult.add(line.substring(startIndex, endIndex));
+            }
+        }
+
+        // 纭繚杩斿洖鍊间负鏁扮粍鏍煎紡
+        if (finalResult.size() == 1) {
+            List<Object> singleResultAsArray = new ArrayList<>();
+            singleResultAsArray.add(finalResult.get(0));
+            return singleResultAsArray;
+        }
+        return finalResult;
+    }
+
+    /**
+     * 鎷夊姏鏈�.et鏂囦欢
+     *
+     * @param data
+     * @param v
+     * @param splitIdentifier
+     * @return
+     */
+    public static List<Object> analyzeEtData(String data, List<DataConfig> v, String splitIdentifier) {
+        List<Object> finalResult = new ArrayList<>();
+
+        for (DataConfig config : v) {
+            String referx = getRefer(config.getReferx());
+            if (ObjectUtils.isEmpty(referx)) {
+                continue;
+            }
+
+            String[] lines = data.replaceAll(" ", "").split("\n");
+            boolean foundReferx = false;
+            List<String> validLines = new ArrayList<>();
+            int referxColumnIndex = -1; // 鐢ㄤ簬瀛樺偍referx鎵�鍦ㄥ垪绱㈠紩
+
+            // 鍏堟壘鍑簉eferx鎵�鍦ㄥ垪绱㈠紩
+            for (String line : lines) {
+                String[] parts = line.split(splitIdentifier);
+                for (int i = 0; i < parts.length; i++) {
+                    if (parts[i].contains(referx)) {
+                        referxColumnIndex = i;
+                        break;
+                    }
+                }
+                if (referxColumnIndex != -1) {
+                    break;
+                }
+            }
+            // 鑻ユ湭鎵惧埌鍖归厤鐨剅eferx锛屾姏鍑哄紓甯�
+            if (referxColumnIndex == -1) {
+                throw new IllegalArgumentException("璇疯緭鍏ユ纭殑x鍊硷紝鏂囦欢涓湭鎵惧埌涓� " + referx + " 鍖归厤鐨勫�笺��");
+            }
+            for (int i = 0; i < lines.length; i++) {
+                String line = lines[i];
+                String[] parts = line.split(splitIdentifier, 2);
+
+                if (!foundReferx) {
+                    if (containsReferx(parts, referx)) {
+                        foundReferx = true;
+                    }
+                    continue;
+                }
+
+                if (i > 0 && foundReferx) {
+                    if (parts.length > 0 && isNumeric(parts[0])) {
+                        validLines.add(line);
+                    }
+                }
+            }
+            for (String line : validLines) {
+                String[] parts = line.split(splitIdentifier);
+                if (referxColumnIndex < parts.length) {
+                    String value = parts[referxColumnIndex].trim();
+                    if (value.isEmpty()) {
+                        value = "";
+                    }
+                    finalResult.add(value);
+                } else {
+                    finalResult.add("");
+                }
+            }
+        }
+        return finalResult;
+    }
+
+    private static boolean containsReferx(String[] parts, String referx) {
+        for (String part : parts) {
+            if (part.contains(referx)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static boolean isNumeric(String str) {
+        try {
+            Double.parseDouble(str);
+            return true;
+        } catch (NumberFormatException e) {
+            return false;
+        }
+    }
+
+    /**
+     * 鑹茶氨pdf鏂囦欢闇�瑕佸畾浣峹,y
+     *
+     * @param data
+     * @param dataConfig
+     * @param device
+     * @return
+     */
+    private static Map<String, Object> readPdfString(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
+        Map<String, Object> map = new HashMap<>();
+        dataConfig.forEach((k, v) -> {
+            List<Object> list = new ArrayList<>();
+            for (int config = 0; config < v.size(); config++) {
+                String referX = getRefer(v.get(config).getReferx());
+                String referY = getRefer(v.get(config).getRefery());
+                String[] aColumnY = data.split("\n");
+                for (String line : aColumnY) {
+                    if (line.contains(referX) || line.contains(referY)) {
+                        list.add(line);
+                    }
+                }
+
+                int referYColumnIndex = -1;
+                for (Object item : list) {
+                    // 浣跨敤姝e垯琛ㄨ揪寮� \\s+ 鍒嗗壊瀛楃涓�
+                    String[] columns = ((String) item).split("\\s+");
+                    for (int i = 0; i < columns.length; i++) {
+                        if (columns[i].equals(referY)) {
+                            referYColumnIndex = i;
+                            break;
+                        }
+                    }
+                    if (referYColumnIndex != -1) {
+                        break;
+                    }
+                }
+
+                List<Object> targetColumnValues = new ArrayList<>();
+                if (referYColumnIndex != -1) {
+                    for (Object item : list) {
+                        String line = (String) item;
+                        // 杩囨护鎺夊寘鍚� referY 鐨勮
+                        if (!line.contains(referY)) {
+                            String[] columns = line.split("\\s+");
+                            if (columns.length > referYColumnIndex) {
+                                targetColumnValues.add(columns[referYColumnIndex]);
+                            }
+                        }
+                    }
+                }
+                Object resultValue = calculationFormula(targetColumnValues, v.get(0), k, device);
+                map.put(k, resultValue);
+            }
+        });
+        return map;
+    }
+
+    public static List<Object> extractColumnValues(List<Object> list, String columnName) {
+        List<Object> columnValues = new ArrayList<>();
+        if (list.isEmpty()) {
+            return columnValues;
+        }
+        String[] headers = ((String) list.get(0)).split(",");
+        int columnIndex = -1;
+        for (int i = 0; i < headers.length; i++) {
+            if (headers[i].equals(columnName)) {
+                columnIndex = i;
+                break;
+            }
+        }
+        if (columnIndex == -1) {
+            return columnValues;
+        }
+        for (int i = 1; i < list.size(); i++) {
+            String[] columns = ((String) list.get(i)).split(",");
+            if (columnIndex < columns.length) {
+                columnValues.add(columns[columnIndex]);
+            }
+        }
+        return columnValues;
     }
 
     public static Map<String, Object> createFrequency(String entrustCode, String sampleCode, Map<String, Object> map) {
@@ -465,7 +691,7 @@
         ArrayList<Object> listResult = new ArrayList<>();
         Map<String, Object> hashMap = new HashMap<>();
         // 濡傛灉涓嶄负绌猴紝杩涜鍏紡璁$畻
-        if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) {
+        if (ObjectUtils.isNotEmpty(dataConfig.getFormula()) && !"(null)".equals(dataConfig.getFormula())) {
             // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹�
             if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
                 String s = calculationFormulaList(list, dataConfig.getFormula());
@@ -479,6 +705,12 @@
             }
         } else {
             listResult.addAll(list);
+        }
+        // 鏇挎崲 "n.a." 涓虹┖瀛楃涓�
+        for (int i = 0; i < listResult.size(); i++) {
+            if ("n.a.".equals(listResult.get(i))) {
+                listResult.set(i, "");
+            }
         }
         // 涓轰簡缁欏墠绔仛鏁版嵁鍖哄垎
         if (listResult.size() > 1) {
@@ -566,20 +798,208 @@
     public static Map<String, Object> analysisList(String data, Map<String, List<DataConfig>> dataConfig,
                                                    Device device, String entrustCode, String sampleCode) {
         Map<String, Object> map = new HashMap<>();
+        List<String> rohsIndicators = Arrays.asList("鎴愭潫閲婃斁,SPR", "鎴愭潫閲婃斁,HRR", "鎴愭潫閲婃斁,THR", "鎴愭潫閲婃斁,TSP", "鎴愭潫閲婃斁,FIGRA");
         dataConfig.forEach((k, v) -> {
             List<Object> list = new ArrayList<>();
-            // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�1銆乊瀹氳寖鍥达紝X瀹氭í鍧愭爣锛�2銆佸彧瀛樺湪Y锛�3銆佸彧瀛樺湪X
-            if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
-                list = analyzeXlsData(data, v, k, splitIdentifier);
-                // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦�
-            } else if (ObjectUtils.isNotEmpty(device.getEntrustCode()) && ObjectUtils.isNotEmpty(device.getSampleCode())) {
-                list = analyzeDataEntrustCodAndSampleCode(data, v, k, splitIdentifier, device, entrustCode, sampleCode);
+//            // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�1銆乊瀹氳寖鍥达紝X瀹氭í鍧愭爣锛�2銆佸彧瀛樺湪Y锛�3銆佸彧瀛樺湪X
+//            if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
+//                list = analyzeXlsData(data, v, k, splitIdentifier);
+//                // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦�
+//            } else if (ObjectUtils.isNotEmpty(device.getEntrustCode()) && ObjectUtils.isNotEmpty(device.getSampleCode())) {
+//                list = analyzeDataEntrustCodAndSampleCode(data, v, k, splitIdentifier, device, entrustCode, sampleCode);
+//            }
+            boolean containsRoHS = false;
+            for (DataConfig item : v) {
+                if (rohsIndicators.contains(item.getInsProductItem())) {
+                    containsRoHS = true;
+                    break;
+                }
+            }
+            if (containsRoHS) {
+                list = analyzeXlsxData(data, v, k, splitIdentifier);
+            } else {
+                list =  analyzeXlsx1Data(data, v, k, splitIdentifier);
             }
             // 杩涜鍏紡璁$畻
             Object resultValue = calculationFormula(list, v.get(0), k, device);
             map.put(k, resultValue);
         });
         return map;
+    }
+
+    private static List<Object> analyzeXlsx1Data(String data, List<DataConfig> v, String k, String splitIdentifier) {
+        List<Object> finalResult = new ArrayList<>();
+
+        if (data == null || data.isEmpty()) {
+            throw new IllegalArgumentException("杈撳叆鐨勬暟鎹负绌猴紝璇锋鏌ヨ緭鍏ャ��");
+        }
+
+        // 鍒嗗壊鏁版嵁涓鸿
+        String[] lines = data.replaceAll(" ", "").split("\n");
+        if (lines.length == 0) {
+            throw new IllegalArgumentException("杈撳叆鐨勬暟鎹病鏈夋湁鏁堢殑琛岋紝璇锋鏌ヨ緭鍏ャ��");
+        }
+
+        // 鑾峰彇琛ㄥご
+        String[] headers = lines[0].split("\\s+");
+        if (headers.length == 0) {
+            throw new IllegalArgumentException("琛ㄥご鏁版嵁涓虹┖锛岃妫�鏌ヨ緭鍏ユ牸寮忋��");
+        }
+
+        // 鏌ユ壘娴嬭瘯鏃堕棿鍒楃殑绱㈠紩
+        int testTimeIndex = -1;
+        for (int i = 0; i < headers.length; i++) {
+            if (headers[i].contains("娴嬭瘯鏃堕棿")) {
+                testTimeIndex = i;
+                break;
+            }
+        }
+
+        // 鑻ユ湭鎵惧埌娴嬭瘯鏃堕棿鍒楋紝鎶涘嚭寮傚父
+        if (testTimeIndex == -1) {
+            throw new IllegalArgumentException("鏈壘鍒板寘鍚� '娴嬭瘯鏃堕棿' 鐨勫垪锛岃妫�鏌ヨ〃澶淬��");
+        }
+
+        // 鐢ㄤ簬瀛樺偍娴嬭瘯鏃堕棿涓� 2400 鐨勮鏁版嵁
+        String[] targetLine = null;
+
+        // 鏌ユ壘娴嬭瘯鏃堕棿涓� 2400 鐨勮
+        for (int i = 1; i < lines.length; i++) {
+            try {
+                String[] lineData = lines[i].split("\\s+");
+                double testTime = Double.parseDouble(lineData[testTimeIndex]);
+                if (testTime == 2400) {
+                    targetLine = lineData;
+                    break;
+                }
+            } catch (NumberFormatException e) {
+                throw new RuntimeException("绗� " + (i + 1) + " 琛岀殑 '娴嬭瘯鏃堕棿' 鍒楁暟鎹棤娉曡浆鎹负鏁板瓧锛岃妫�鏌ユ暟鎹牸寮忋��", e);
+            } catch (ArrayIndexOutOfBoundsException e) {
+                throw new RuntimeException("绗� " + (i + 1) + " 琛岀殑鏁版嵁鍒楁暟涓嶈冻锛屾棤娉曡幏鍙� '娴嬭瘯鏃堕棿' 鍒楋紝璇锋鏌ユ暟鎹牸寮忋��", e);
+            }
+        }
+
+        // 鑻ユ湭鎵惧埌娴嬭瘯鏃堕棿涓� 2400 鐨勮锛屾姏鍑哄紓甯�
+        if (targetLine == null) {
+            throw new RuntimeException("鏈壘鍒版祴璇曟椂闂翠负 2400 鐨勬暟鎹紝璇锋鏌ユ暟鎹唴瀹广��");
+        }
+
+        for (DataConfig config : v) {
+            String referx = getRefer(config.getReferx());
+            if (ObjectUtils.isEmpty(referx)) {
+                continue;
+            }
+
+            // 鏌ユ壘 referx 鍒楃殑绱㈠紩
+            int referxIndex = -1;
+            for (int i = 0; i < headers.length; i++) {
+                if (headers[i].trim().contains(referx.trim())) {
+                    referxIndex = i;
+                    break;
+                }
+            }
+
+            // 鑻ユ湭鎵惧埌 referx 鍒楋紝鎶涘嚭寮傚父
+            if (referxIndex == -1) {
+                throw new IllegalArgumentException("鏈壘鍒� '" + referx + "' 鍒楋紝璇锋鏌ヨ〃澶村拰閰嶇疆銆�");
+            }
+
+            try {
+                double value = Double.parseDouble(targetLine[referxIndex]);
+                // 鍥涜垗浜斿叆淇濈暀涓や綅灏忔暟
+                DecimalFormat df = new DecimalFormat("#.00");
+                String formattedValue = df.format(value);
+                finalResult.add(Double.parseDouble(formattedValue));
+            } catch (NumberFormatException e) {
+                throw new RuntimeException("'娴嬭瘯鏃堕棿' 涓� 2400 鐨勮涓紝'" + referx + "' 鍒楃殑鏁版嵁鏃犳硶杞崲涓烘暟瀛楋紝璇锋鏌ユ暟鎹牸寮忋��", e);
+            } catch (ArrayIndexOutOfBoundsException e) {
+                throw new RuntimeException("'娴嬭瘯鏃堕棿' 涓� 2400 鐨勮鐨勬暟鎹垪鏁颁笉瓒筹紝鏃犳硶鑾峰彇 '" + referx + "' 鍒楋紝璇锋鏌ユ暟鎹牸寮忋��", e);
+            }
+        }
+        return finalResult;
+    }
+
+    private static List<Object> analyzeXlsxData(String data, List<DataConfig> v, String k, String splitIdentifier) {
+
+        List<Object> finalResult = new ArrayList<>();
+        String ss = k;
+
+        // 瀛樺偍绗﹀悎娴嬭瘯鏃堕棿鑼冨洿鐨勬暟鎹
+        List<String[]> validLines = new ArrayList<>();
+        String[] lines = data.replaceAll(" ", "").split("\n");
+        String[] headers = lines[0].split("\\s+");
+        int testTimeIndex = -1;
+        for (int i = 0; i < headers.length; i++) {
+            if ("娴嬭瘯鏃堕棿".equals(headers[i])) {
+                testTimeIndex = i;
+                break;
+            }
+        }
+
+        if (testTimeIndex == -1) {
+            return finalResult;
+        }
+
+        // 绛涢�夊嚭娴嬭瘯鏃堕棿鍦� 300 鍒� 1500 涔嬮棿鐨勬暟鎹
+        for (int i = 1; i < lines.length; i++) {
+            String[] lineData = lines[i].split("\\s+");
+            try {
+                double testTime = Double.parseDouble(lineData[testTimeIndex]);
+                if (testTime > 300 && testTime <= 1500) {
+                    validLines.add(lineData);
+                } else if (testTime > 1500) {
+                    break;
+                }
+            } catch (NumberFormatException e) {
+                continue;
+            }
+        }
+
+        for (DataConfig config : v) {
+            String referx = getRefer(config.getReferx());
+            if (ObjectUtils.isEmpty(referx)) {
+                continue;
+            }
+
+            int referxIndex = -1;
+            for (int i = 0; i < headers.length; i++) {
+                if (referx.equals(headers[i])) {
+                    referxIndex = i;
+                    break;
+                }
+            }
+
+            if (referxIndex == -1) {
+                continue;
+            }
+
+            List<Double> values = new ArrayList<>();
+            for (String[] lineData : validLines) {
+                try {
+                    double value = Double.parseDouble(lineData[referxIndex]);
+                    values.add(value);
+                } catch (NumberFormatException e) {
+                    continue;
+                }
+            }
+
+            Object result;
+            if ("HRR".equals(referx) || "SPR".equals(referx) || "FIGRA".equals(referx)) {
+                result = values.stream().mapToDouble(Double::doubleValue).max().orElse(0);
+            } else if ("THR".equals(referx) || "TSP".equals(referx)) {
+                result = values.stream().mapToDouble(Double::doubleValue).sum();
+            } else {
+                continue;
+            }
+
+            if (result != null) {
+                // 鍥涜垗浜斿叆淇濈暀涓や綅灏忔暟
+                DecimalFormat df = new DecimalFormat("#.00");
+                result = Double.parseDouble(df.format(result));
+                finalResult.add(result);
+            }
+        }
+        return finalResult;
     }
 
     /**
@@ -590,7 +1010,7 @@
      * @return
      */
     public static Map<String, Object> analysisXlsList(String data, Map<String, List<DataConfig>> dataConfig,
-                                                   Device device, String entrustCode, String sampleCode) {
+                                                      Device device, String entrustCode, String sampleCode) {
         Map<String, Object> map = new HashMap<>();
         dataConfig.forEach((k, v) -> {
             List<Object> list = new ArrayList<>();

--
Gitblit v1.9.3