yaowanxin
22 小时以前 b1c6c32fdcee89f97c05bd21f4fb0b95f4009764
cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java
@@ -43,7 +43,7 @@
     * @param device
     * @return
     */
    public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag) {
    public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag, String dbUserName, String dbPassword) {
        // 判断是否是影像测量仪
        if (device.getManagementNumber().equals("JCZX-ZB-OP07001")) {
            if (device.getFileType().equals(".xlsx")) {
@@ -76,7 +76,9 @@
                "&sampleCode=" + sampleCode +
                "&mdbEntrustCode=" + device.getEntrustCode() +
                "&mdbSampleCode=" + device.getSampleCode() +
                "&dbFileName=" + device.getDbFileName();
                "&dbFileName=" + device.getDbFileName()+
                "&dbUserName=" + dbUserName +
                "&dbPassword=" + dbPassword;
        System.out.println("请求的 URL: " + http);
        String result = null;
        try {
@@ -117,7 +119,7 @@
                    map = analysisList(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".xls":
                    map = analysisString(data, userMap, device, entrustCode, sampleCode);
                    map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
                    break;
                case ".txt":
                    map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
@@ -134,7 +136,8 @@
                    }
                    break;
                case ".db":
                    map = analysisDb(data, userMap, device);
                    map = analysisDb1(data, userMap, device);
//                    map = analysisDb(data, userMap, device);
                    break;
                case ".png":
                    map = readPngString(data, userMap, device);
@@ -187,6 +190,56 @@
        return "1";
    }
    private static Map<String, Object> analysisDb1(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
        JSONObject jsonObject = JSON.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        if (jsonObject.isEmpty()) {
            return map;
        }
        // 从 JSON 数据中提取 data 数组
        JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
        dataConfig.forEach((k, v) -> {
            AtomicInteger numberOfDataEntries = new AtomicInteger();
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String refery = getRefer(v.get(config).getRefery());
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString());
                    if (ObjectUtils.isNotEmpty(jsonObject1.get(refery))) {
                        numberOfDataEntries.addAndGet(1);
                        list.add(jsonObject1.get(refery));
                    }
//                    Object o = jsonObject1.get(refery);
//                    if (ObjectUtils.isNotEmpty(o)) {
//                        numberOfDataEntries.addAndGet(1);
//                        list.add(o);
//                    }
                }
            }
            // 拼接数采配置
            List<Object> result = new ArrayList<>();
            for (int i = 0; i < numberOfDataEntries.get(); i++) {
                StringBuilder aggregate = new StringBuilder();
                for (int j = 0; j < v.size(); j++) {
                    int index;
                    if (j == 0) {
                        index = i;
                    } else {
                        index = numberOfDataEntries.get() + i;
                    }
                    aggregate.append(list.get(index).toString()).append(",");
                }
                if (aggregate.length() > 0) {
                    aggregate.setLength(aggregate.length() - 1);
                }
                result.add(aggregate.toString());
            }
            // 进行公式计算
            Object resultValue = calculationFormula(result, v.get(0), k, device);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * 需要通过X,Y轴定位
@@ -195,17 +248,20 @@
     * @param dataConfig
     * @return
     */
    private static Map<String, Object> analysisDb(String data, Map<String, List<DataConfig>> dataConfig, Device device) {
        JSONObject jsonObject = JSON.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        if (jsonObject.isEmpty()) {
            return map;
        }
        // 从 JSON 数据中提取 data 数组
        JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
        dataConfig.forEach((k, v) -> {
            AtomicInteger numberOfDataEntries = new AtomicInteger();
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
//                String refery = v.get(config).getRefery();
                String refery = getRefer(v.get(config).getRefery());
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString());
@@ -479,6 +535,7 @@
                    String firstNumber = lastLine.split("\t")[0];
                    list.add(firstNumber);
                } else {
                    list = analyzeData(data, v, k, ",");
                }
                // 委托编号与样品编号存在
@@ -545,7 +602,24 @@
        });
        return map;
    }
    public static Map<String, Object> analysisList1(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 = analyzeData(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;
    }
    private static List<Object> analyzeDataEntrustCodAndSampleCode(String data, List<DataConfig> v, String k, String splitIdentifier,
                                                                   Device device, String entrustCodeValue, String sampleCodeValue) {
        entrustCodeValue = entrustCodeValue.replaceAll(" ", "");
@@ -631,7 +705,117 @@
    private static String getRefer(String refer) {
        return ObjectUtils.isNotEmpty(refer) ? refer.replaceAll(" ", "") : "";
    }
    /**
     * 委托编号与样品编号都为空执行
     *
     * @param data  采集到的文件字符串
     * @param v     用户配置好的x,y轴定位数据与参照物
     * @param k     检验项名称
     * @param split 分割符
     * @return 提取的数据列表
     */
    public static List<Object> analyzeData1(String data, List<DataConfig> v, String k, String split) {
        List<Object> list = new ArrayList<>();
        // 预处理数据:移除多余空格并保留关键分隔符
        String processedData = data.replaceAll("\\s+", " ").trim();
        for (int configIndex = 0; configIndex < v.size(); configIndex++) {
            // 取两个用户配置的参照物
            String referx = getRefer(v.get(configIndex).getReferx());
            String refery = getRefer(v.get(configIndex).getRefery());
            if (ObjectUtils.isEmpty(refery) && ObjectUtils.isEmpty(referx)) {
                System.out.println("参照物为空,跳过当前配置,k: " + k);
                continue;
            }
            // 最终结果
            List<Object> result = new ArrayList<>();
            // 通过\n将字符串分割为行
            String[] aColumnY = processedData.replaceAll(" ", "").split("\n");
            Integer end = null;
            // 采集数据:Y轴
            for (int i = 0; i < aColumnY.length; i++) {
                String line = aColumnY[i].trim();
                if (line.isEmpty()) continue; // 跳过空行
                // 如果Y参照不为空与X参照为空则执行,同时该行包含Y参照
                if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && line.contains(refery)) {
                    try {
                        // 取Y坐标值
                        int y = getXOrY(v.get(configIndex).getY(), k, "Y");
                        String[] aLineX = line.split(split);
                        for (int j = 0; j < aLineX.length; j++) {
                            if (aLineX[j].contains(refery)) {
                                if (i + y >= aColumnY.length) {
                                    System.err.println(k + ":Y轴定位超出数据范围,当前数据行数: " + aColumnY.length + ",尝试访问行: " + (i + y));
                                    continue;
                                }
                                String[] split1 = aColumnY[i + y].split(split);
                                if (j >= split1.length) {
                                    System.err.println(k + ":X轴定位超出数据范围,当前行元素个数: " + split1.length + ",尝试访问位置: " + j);
                                    continue;
                                }
                                result.add(split1[j]);
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理仅Y参照逻辑时出现异常: " + e.getMessage());
                    }
                }
                // 如果Y参照不为空与X参照不为空则执行,此处Y定区域
                else if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isNotEmpty(referx)) {
                    try {
                        // 取x的值,防止报错
                        int x = getXOrY(v.get(configIndex).getX(), k, "X");
                        // 取Y坐标值
                        int y = getXOrY(v.get(configIndex).getY(), k, "Y");
                        // 缓存Y的结束值
                        if (ObjectUtils.isEmpty(end) && line.contains(refery)) {
                            end = i + y;
                        }
                        // 判断是否在参照物为起点,Y坐标值为最终范围
                        if (ObjectUtils.isNotEmpty(end) && i <= end) {
                            String[] aLineX = line.split(split);
                            for (int j = 0; j < aLineX.length; j++) {
                                if (aLineX[j].contains(referx)) {
                                    if (j + x >= aLineX.length) {
                                        System.err.println(k + ":X轴定位超出数据范围,当前行元素个数: " + aLineX.length + ",尝试访问位置: " + (j + x));
                                        continue;
                                    }
                                    result.add(aLineX[j + x]);
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理XY参照逻辑时出现异常: " + e.getMessage());
                    }
                }
                // 如果X参照不为空同时该行包含X参照,则执行下面的代码
                else if (line.contains(referx) && ObjectUtils.isEmpty(refery)) {
                    try {
                        String[] aLineX = line.split(split);
                        // 取x的值,防止报错
                        int x = getXOrY(v.get(configIndex).getX(), k, "X");
                        for (int j = 0; j < aLineX.length; j++) {
                            if (aLineX[j].contains(referx)) {
                                if (j + x >= aLineX.length) {
                                    System.err.println(k + ":X轴定位超出数据范围,当前行元素个数: " + aLineX.length + ",尝试访问位置: " + (j + x));
                                    continue;
                                }
                                result.add(aLineX[j + x]);
                            }
                        }
                    } catch (Exception e) {
                        System.err.println(k + ":在处理仅X参照逻辑时出现异常: " + e.getMessage());
                    }
                }
            }
            // 防止计算公式的时候出现:[null] 这种数据
            if (ObjectUtils.isNotEmpty(result)) {
                list.addAll(result);
            }
        }
        return list;
    }
    /**
     * 委托编号与样品编号都为空执行
     *
@@ -643,6 +827,10 @@
     */
    public static List<Object> analyzeData(String data, List<DataConfig> v, String k, String split) {
        List<Object> list = new ArrayList<>();
        // 预处理数据:移除多余空格并保留关键分隔符
//        String processedData = data.replaceAll("\\s+", " ").trim();
        for (int config = 0; config < v.size(); config++) {
            // 取两个用户配置的参照物
            String referx = getRefer(v.get(config).getReferx());
@@ -657,11 +845,14 @@
            Integer end = null;
            // 采集数据:Y轴
            for (int i = 0; i < aColumnY.length; i++) {
//                String line = aColumnY[i].trim();
//                if (line.isEmpty()) continue; // 跳过空行
                // 如果Y参照不为空与X参照为空则执行,同时该行包含Y参照
                if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && aColumnY[i].contains(refery)) {
                if (ObjectUtils.isNotEmpty(refery) && ObjectUtils .isEmpty(referx) && aColumnY[i].contains(refery)) {
                    // 取Y坐标值
                    int y = getXOrY(v.get(config).getY(), k, "Y");
                    String[] aLineX = aColumnY[i].split(split);
//                    String[] aLineX = line.split(split);
                    for (int j = 0; j < aLineX.length; j++) {
                        if (aLineX[j].contains(refery)) {
                            String[] split1 = new String[0];
@@ -671,7 +862,9 @@
                                throw new ErrorException(k + ":Y轴定位超出!");
                            }
                            try {
//                                System.out.println("----------");
                                result.add(split1[j]);
                            } catch (Exception e) {
                                throw new ErrorException(k + ":X轴定位超出!");
                            }
@@ -693,6 +886,7 @@
                        for (int j = 0; j < aLineX.length; j++) {
                            if (aLineX[j].contains(referx)) {
                                try {
//                                    System.out.println("77777777777777");
                                    result.add(aLineX[j + x]);
                                } catch (Exception e) {
                                    throw new ErrorException(k + ":X轴定位超出!");
@@ -709,6 +903,7 @@
                    for (int j = 0; j < aLineX.length; j++) {
                        if (aLineX[j].contains(referx)) {
                            try {
//                                System.out.println("4444444444444444");
                                result.add(aLineX[j + x]);
                            } catch (Exception e) {
                                throw new ErrorException(k + ":X轴定位超出!");
@@ -717,7 +912,7 @@
                    }
                }
            }
            // 防止计算公式的时候出现:[null] 这种数据
//             防止计算公式的时候出现:[null] 这种数据
            if (ObjectUtils.isNotEmpty(result)) {
//                String formatProcessing = getFormatProcessing(result);
                list.addAll(result);