liding
2025-04-18 4d5a0213352c1e5fd9410241668cbd06ea47e4c6
cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
@@ -48,7 +48,7 @@
            if (device.getFileType().equals(".xlsx")) {
                String url = device.getCollectUrl() + "\\" + sampleCode.replace("/", "");
                if (StringUtils.isNotBlank(dataConfig.get(0).getAnotherName())) {
                    url += dataConfig.get(0).getAnotherName() ;
                    url += dataConfig.get(0).getAnotherName();
                }
                url += ".xlsx";
                device.setCollectUrl(url);
@@ -86,7 +86,7 @@
        JSONObject jsonObject = JSON.parseObject(result);
        if (Objects.equals(jsonObject.get("code"), 1)) {
            if (ObjectUtils.isEmpty(jsonObject.get("msg"))) {
                throw new ErrorException("未查询到文件!可能该路径(" + device.getCollectUrl() + ")下并没有所需(" + device.getFileType() +")文件!");
                throw new ErrorException("未查询到文件!可能该路径(" + device.getCollectUrl() + ")下并没有所需(" + device.getFileType() + ")文件!");
            } else {
                throw new ErrorException(jsonObject.get("msg") + "");
            }
@@ -113,6 +113,9 @@
                case ".xlsx":
                    map = analysisList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".xls":
                    map = analysisXlsList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".txt":
                    map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
                    break;
@@ -133,6 +136,9 @@
                case ".png":
                    map = readPngString(data, userMap, device);
                    break;
                case ".pdf":
                    map = readPdfString(data, userMap, device);
                    break;
                default:
                    map = null;
                    break;
@@ -144,6 +150,42 @@
            }
            return map;
        }
    }
    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 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);
                    }
                }
                if (ObjectUtils.isNotEmpty(result)) {
                    list.add(result);
                }
            }
            // 进行公式计算
            Object resultValue = calculationFormula(list, v.get(0), k, device);
            map.put(k, resultValue);
        });
        return map;
    }
    public static Map<String, Object> createFrequency(String entrustCode, String sampleCode, Map<String, Object> map) {
@@ -528,7 +570,33 @@
            List<Object> list = new ArrayList<>();
            // 委托编号与样品编号不存在,定:1、Y定范围,X定横坐标;2、只存在Y;3、只存在X
            if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
                list = analyzeData(data, v, k, splitIdentifier);
                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);
            }
            // 进行公式计算
            Object resultValue = calculationFormula(list, v.get(0), k, device);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * 取X定位
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x定位数据与参照物
     * @return
     */
    public static Map<String, Object> analysisXlsList(String data, Map<String, List<DataConfig>> dataConfig,
                                                   Device device, String entrustCode, String sampleCode) {
        Map<String, Object> map = new HashMap<>();
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            // 委托编号与样品编号不存在,定:1、Y定范围,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);
@@ -569,7 +637,7 @@
                        if (aLine[j].contains(entrustCode)) {
                            entrustCodeY = j;
                        }
                        if (aLine[j].contains(sampleCode) ) {
                        if (aLine[j].contains(sampleCode)) {
                            sampleCodeY = j;
                        }
                        if (aLine[j].contains(refery)) {
@@ -588,7 +656,8 @@
                                list.add(result);
                            }
                        }
                    } catch (Exception e) {}
                    } catch (Exception e) {
                    }
                }
            }
        }
@@ -720,6 +789,72 @@
        return list;
    }
    public static List<Object> analyzeXlsData(String data, List<DataConfig> v, String k, String split) {
        List<Object> finalResult = new ArrayList<>(); // 最终返回的结果列表
        for (DataConfig config : v) {
            // 取用户配置的参照物
            String referx = getRefer(config.getReferx());
            if (ObjectUtils.isEmpty(referx)) {
                continue;
            }
            // 第一步:解析数据,找到符合条件的行(第二个分隔符前和冒号后为数字)
            List<String> validLines = new ArrayList<>();
            String[] lines = data.replaceAll(" ", "").split("\n");
            for (String line : lines) {
                String[] parts = line.split(split);
                if (parts.length > 1) {
                    // 检查第二个分隔符前面和冒号后面是否为数字
                    String beforeSplit = parts[1];
                    String afterColon = "";
                    if (beforeSplit.contains(":")) {
                        String[] colonParts = beforeSplit.split(":");
                        if (colonParts.length > 1) {
                            afterColon = colonParts[1].trim();
                        }
                    }
                    try {
                        Double.parseDouble(afterColon); // 冒号后是否为数字
                        validLines.add(line); // 如果符合条件,添加到临时列表中
                    } catch (NumberFormatException e) {
                        // 不符合数字条件,忽略
                    }
                }
            }
            // 第二步:从临时列表中提取包含 referx 的行,并获取其后面的数字值
            List<Object> result = new ArrayList<>();
            for (String line : validLines) {
                if (line.contains(referx)) {
                    String[] parts = line.split(referx);
                    if (parts.length > 1) {
                        String xValue = parts[1].trim();
                        // 提取 referx 后面到下一个分隔符之间的数字
                        String[] splitParts = xValue.split(split);
                        if (splitParts.length > 0) {
                            try {
                                String trim = splitParts[0].trim();
                                if (trim.contains(":")) {
                                    trim = trim.replaceAll(":", "");
                                }
                                Double.parseDouble(trim); // 判断是否为数字
                                result.add(trim);
                            } catch (NumberFormatException e) {
                                // 不是数字,忽略
                            }
                        }
                    }
                }
            }
            // 防止计算公式的时候出现:[null] 这种数据
            if (!result.isEmpty()) {
                finalResult.addAll(result);
            }
        }
        return finalResult;
    }
    public static String getFormatProcessing(String value) {
        value = value.replaceAll("%", "");
        if (value.contains("=")) {