zss
2024-07-22 c0fd46ed9c7ddf7a3ea4e6a972474e9a62c68a1a
cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java
@@ -9,7 +9,6 @@
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.pojo.DataConfig;
import com.yuanchu.mom.pojo.Device;
import com.yuanchu.mom.vo.Result;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@@ -27,17 +26,16 @@
    private static final String splitIdentifier = "@-@"; // 自定义唯一标识分割符
    public static final String frequency = "frequency";
    /**
     * 数采入口
     *
     * @param request
     * @param dataConfig
     * @param device
     * @return
     */
    public static Result<?> dataAcquisitionEntrance(HttpServletRequest request, List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode) {
        String ipAddress = request.getRemoteAddr();
        // 防止回环地址变为IPv6
        String ip = ipAddress.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipAddress;
    public static Map<String, String> dataAcquisitionEntrance(HttpServletRequest request, List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip) {
        String http = HTTP + ip + GETFILE + "?filePath=" + device.getCollectUrl() + "&fileExtension=" + device.getFileType();
        String result = null;
        try {
@@ -45,7 +43,7 @@
        } catch (IORuntimeException e) {
            throw new ErrorException("所在电脑未安装或未启动:LIMS文件采集器!");
        }
        JSONObject jsonObject =  JSON.parseObject(result);
        JSONObject jsonObject = JSON.parseObject(result);
        if (Objects.equals(jsonObject.get("code"), 1)) {
            if (ObjectUtils.isEmpty(jsonObject.get("msg"))) {
                throw new ErrorException("未查询到数据,可能文件路径配置错误!");
@@ -88,7 +86,7 @@
            if (ObjectUtils.isNotEmpty(device.getStorageUrl())) {
                String s = HTTP + ip + MOVEFILE + "?startFilePath=" + device.getCollectUrl() + "&endFilePath=" + device.getStorageUrl();
                String storageUrlResult = HttpUtil.get(s);
                JSONObject storageUrlResultJson =  JSON.parseObject(storageUrlResult);
                JSONObject storageUrlResultJson = JSON.parseObject(storageUrlResult);
                if (Objects.equals(storageUrlResultJson.get("code"), 1)) {
                    if (ObjectUtils.isEmpty(storageUrlResultJson.get("msg"))) {
                        throw new ErrorException("存储地址错误,可能文件路径配置错误!");
@@ -97,10 +95,31 @@
                    }
                }
            }
            return Result.success(map);
            return map;
        }
    }
    public static String createFrequency(String entrustCode, String sampleCode) {
        String key = frequency + ":" + entrustCode + ":" + sampleCode;
        boolean b = RedisUtil.hasKey(key);
        String frequencyValue;
        if (b) {
            long incr = RedisUtil.incr(key, 1);
            frequencyValue = String.valueOf(incr);
        } else {
            RedisUtil.set(key, 1);
            frequencyValue = "1";
        }
        return frequencyValue;
    }
    /**
     * 需要通过X,Y轴定位
     *
     * @param data
     * @param dataConfig
     * @return
     */
    private static Map<String, String> analysisDb(String data, Map<String, List<DataConfig>> dataConfig) {
        JSONObject jsonObject = JSON.parseObject(data);
        JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
@@ -109,12 +128,9 @@
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                if(ObjectUtils.isEmpty(v.get(config).getX()) && ObjectUtils.isEmpty(v.get(config).getY())) {
                    throw new ErrorException("未给" + k + "进行数采配置x,y!");
                }
                int x = Integer.parseInt(v.get(config).getX());
                int y = Integer.parseInt(v.get(config).getY());
                String referx = getRefer(v.get(config).getReferx());
                int x = getXOrY(v.get(config).getX(), k, "X");
                int y = getXOrY(v.get(config).getY(), k, "Y");
                String key = "";
                for (int i = 0; i < columnList.size(); i++) {
                    if (columnList.get(i).equals(referx)) {
@@ -124,16 +140,32 @@
                JSONObject jsonObject1 = JSON.parseObject(dataList.get(y).toString());
                Object o = jsonObject1.get(key);
                if (ObjectUtils.isNotEmpty(o)) {
                    list.add(o);
                    // 小数点进三位
                    double v1 = 0;
                    try {
                        v1 = Double.parseDouble(o.toString());
                        double v2 = v1 / 1000;
                        list.add(v2);
                    } catch (NumberFormatException e) {
                        list.add(o);
                    }
                }
            }
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * 需要通过X,Y轴定位
     *
     * @param data
     * @param dataConfig
     * @return
     */
    private static Map<String, String> analysisMdb(String data, Map<String, List<DataConfig>> dataConfig, String entrustCode, String sampleCode) {
        JSONObject jsonObject = JSON.parseObject(data);
        JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString());
@@ -142,8 +174,8 @@
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                int x = Integer.parseInt(v.get(config).getX());
                String referx = getRefer(v.get(config).getReferx());
                int x = getXOrY(v.get(config).getX(), k, "X");
                String key = "";
                for (int i = 0; i < columnList.size(); i++) {
                    if (columnList.get(i).equals(referx)) {
@@ -161,16 +193,18 @@
                }
            }
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
    }
    private static Pattern SPATTERN = Pattern.compile("([-+])?\\d+(\\.\\d+)?");
    /**
     * 解析String数据
     * @param data 采集到的文件字符串
     * 只需X轴
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
@@ -179,15 +213,15 @@
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                String xResult = null;
                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 = Integer.parseInt(v.get(config).getX());
                    int x = getXOrY(v.get(config).getX(), k, "X");
                    if (aColumnY[i].contains(addDataWithSpaces)) {
                        Matcher matcher = SPATTERN.matcher(aColumnY[i]);
                        while (matcher.find()) {
@@ -196,15 +230,15 @@
                        }
                    }
                    if (ObjectUtils.isNotEmpty(list1)) {
                        xResult = list1.get(x);
                        result = list1.get(x);
                    }
                }
                if (ObjectUtils.isNotEmpty(xResult)) {
                    list.add(xResult);
                if (ObjectUtils.isNotEmpty(result)) {
                    list.add(result);
                }
            }
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
@@ -212,185 +246,171 @@
    /**
     * 从文件中提取出来的文字,如果有公式,进行公式计算,否则取列表第一个值
     * @param list 提取出的数字
     *
     * @param list       提取出的数字
     * @param dataConfig 存储公式的对象
     * @return
     */
    private static String calculationFormula(List<Object> list, DataConfig dataConfig) {
    private static String calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem) {
        if (list.size() == 0) {
            return null;
        }
        // 如果不为空,进行公式计算
        if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) {
            return null;
        // 否则:没有公式代表不需要计算,直接提取List里面的数据
            // 否则:没有公式代表不需要计算,直接提取List里面的数据
        } else {
            // 这里只会取列表第一个数据
            if (list.size() > 0) {
            if (list.size() > 1) {
                throw new ErrorException("未给:" + insProductItem + " 配置公式,可是却采集到了" + list.size() + "个值!分别为:" + list);
            } else {
                return list.get(0).toString();
            }
            return null;
        }
    }
    /**
     * 解析String数据
     * @param data 采集到的文件字符串
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
    private static Map<String, String> analysisTxt(String data, Map<String, List<DataConfig>> dataConfig) {
        Map<String, String> map = new HashMap<>();
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                String refery = v.get(config).getRefery();
                String xResult = null;
                String yResult = null;
                // 通过\n将字符串分割为行
                String[] aColumnY = data.split("\n");
                // 该循环得出用户配置的y轴
                for (int i = 0; i < aColumnY.length; i++) {
                    // 得出用户配置的x轴
                    String[] aLineX = aColumnY[i].split(",");
                    for (int j = 0; j < aLineX.length; j++) {
                        if (ObjectUtils.isNotEmpty(referx) && aLineX[j].contains(referx)) {
                            int x = Integer.parseInt(v.get(config).getX());
                            try {
                                xResult = aLineX[j + x];
                            } catch (Exception e) {
                                throw new ErrorException("数采配置X轴超出!");
                            }
                        }
                        if (ObjectUtils.isNotEmpty(refery) && aLineX[j].contains(refery)) {
                            int y = Integer.parseInt(v.get(config).getY());
                            try {
                                String[] split = aColumnY[i + y].split(",");
                                yResult = split[split.length - 1];
                            } catch (Exception e) {
                                throw new ErrorException("数采配置Y轴超出!");
                            }
                        }
                    }
                }
                if (ObjectUtils.isEmpty(xResult) && ObjectUtils.isEmpty(yResult)) {
                    throw new ErrorException("参照物为:" + referx + "与" + refery + "未取到值!请检查数采配置!");
                }
                list.add(yResult);
                list.add(xResult);
            }
            List<Object> list = analyzeData(data, v, k, ",");
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * 解析String数据
     * @param data 采集到的文件字符串
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
    private static Map<String, String> analysisString(String data, Map<String, List<DataConfig>> dataConfig) {
        String processingDataAfterSpaces = data.replaceAll("  +", splitIdentifier).replaceAll("\r", "");
        String processingDataAfterSpaces = data
                .replaceAll("  +", splitIdentifier)
                .replaceAll("\r", "")
                .replaceAll(" ", "");
        Map<String, String> map = new HashMap<>();
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                String xResult = null;
                // 通过\n将字符串分割为行
                String[] aColumnY = processingDataAfterSpaces.split("\n");
                // 该循环得出用户配置的y轴
                for (int i = 0; i < aColumnY.length; i++) {
                    // 得出用户配置的x轴
                    String[] aLineX = aColumnY[i].split(splitIdentifier);
                    for (int j = 0; j < aLineX.length; j++) {
                        if (ObjectUtils.isNotEmpty(referx) && aLineX[j].replaceAll(" ", "").contains(referx.replaceAll(" ", ""))) {
                            int x = Integer.parseInt(v.get(config).getX());
                            try {
                                xResult = aLineX[j + x];
                            } catch (Exception e) {
                                throw new ErrorException("数采配置X轴超出!");
                            }
                        }
                    }
                }
                if (ObjectUtils.isEmpty(xResult)) {
                    throw new ErrorException("参照物为:" + referx + "未取到值!请检查数采配置!");
                }
                // 结果包含特殊字符,需要剔除掉
                if(xResult.contains("=")) {
                    String[] split = xResult.split("=");
                    list.add(split[split.length - 1]);
                } else if (xResult.contains(":")) {
                    String[] split = xResult.split(":");
                    list.add(split[split.length - 1].replaceAll("%", ""));
                } else {
                    list.add(xResult);
                }
            }
            List<Object> list = analyzeData(processingDataAfterSpaces, v, k, splitIdentifier);
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
    }
    /**
     * 解析String数据
     * @param data 采集到的文件字符串
     * 取X,Y两个定位
     *
     * @param data       采集到的文件字符串
     * @param dataConfig 用户配置好的x,y轴定位数据与参照物
     * @return
     */
    public static Map<String, String> analysisList(String data, Map<String, List<DataConfig>> dataConfig) {
        Map<String, String> map = new HashMap<>();
        dataConfig.forEach((k, v) -> {
            List<Object> list = new ArrayList<>();
            for (int config = 0; config < v.size(); config++) {
                String referx = v.get(config).getReferx();
                String refery = v.get(config).getRefery();
                String xResult = null;
                String yResult = null;
                // 通过\n将字符串分割为行
                String[] aColumnY = data.split("\n");
                // 该循环得出用户配置的y轴
                for (int i = 0; i < aColumnY.length; i++) {
                    // 得出用户配置的x轴
                    String[] aLineX = aColumnY[i].split(splitIdentifier);
                    for (int j = 0; j < aLineX.length; j++) {
                        if (ObjectUtils.isNotEmpty(referx) && referx.equals(aLineX[j])) {
                            int x = Integer.parseInt(v.get(config).getX());
                            try {
                                xResult = aLineX[j + x];
                            } catch (Exception e) {
                                throw new ErrorException("数采配置X轴超出!");
            // 用户可能给一个子项目配置了多个配置,即一个项目取多个值,所以需要循环
            List<Object> list = analyzeData(data, v, k, splitIdentifier);
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0), k);
            map.put(k, resultValue);
        });
        return map;
    }
    // 由于在方法中会大量的判断,所以做一个方法
    private static int getXOrY(String value, String k, String tips) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            throw new ErrorException(k + ":未配置" + tips + "坐标轴的值!");
        }
    }
    // 防止参照物为空报错,进行判断如果为空赋值空字符
    private static String getRefer(String refer) {
        return ObjectUtils.isNotEmpty(refer) ? refer.replaceAll(" ", "") : "";
    }
    public static List<Object> analyzeData(String data, List<DataConfig> v, String k, String split) {
        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 result = null;
            // 通过\n将字符串分割为行
            String[] aColumnY = data.replaceAll(" ", "").split("\n");
            Integer end = null;
            // 采集数据:Y轴
            for (int i = 0; i < aColumnY.length; i++) {
                // 取x的值,防止报错
                int x = getXOrY(v.get(config).getX(), k, "X");
                // 如果Y与X用户都配置了则执行
                if (ObjectUtils.isNotEmpty(refery)) {
                    // 取Y坐标值
                    int y = getXOrY(v.get(config).getY(), k, "Y");
                    // 缓存Y的结束值
                    if (ObjectUtils.isEmpty(end) && aColumnY[i].contains(refery)) {
                        end = i + y;
                    }
                    // 判断是否在参照物为起到,Y坐标值为最终范围
                    if (ObjectUtils.isNotEmpty(end) && i <= end) {
                        String[] aLineX = aColumnY[i].split(split);
                        for (int j = 0; j < aLineX.length; j++) {
                            if (aLineX[j].contains(referx)) {
                                try {
                                    result = aLineX[j + x];
                                } catch (Exception e) {
                                    throw new ErrorException(k + ":X轴定位超出!");
                                }
                                break;
                            }
                        }
                        if (ObjectUtils.isNotEmpty(refery) && refery.equals(aLineX[j])) {
                            int y = Integer.parseInt(v.get(config).getY());
                            String aColumnData = aColumnY[i + y]; // 获取到第Y行的数据
                    }
                    // 如果只配置了X,则执行下面的代码
                } else if (aColumnY[i].contains(referx) && ObjectUtils.isEmpty(refery)) {
                    String[] aLineX = aColumnY[i].split(split);
                    for (int j = 0; j < aLineX.length; j++) {
                        if (aLineX[j].contains(referx)) {
                            try {
                                yResult = aColumnData.split(splitIdentifier)[j];
                                result = aLineX[j + x];
                            } catch (Exception e) {
                                throw new ErrorException("数采配置Y轴超出!");
                                throw new ErrorException(k + ":X轴定位超出!");
                            }
                        }
                    }
                }
                if (ObjectUtils.isEmpty(xResult) || ObjectUtils.isEmpty(yResult)) {
                    throw new ErrorException("X轴或Y轴未取到值!请检查数采配置!");
                }
                if(xResult.equals(yResult)) {
                    list.add(xResult);
                } else {
                    throw new ErrorException("X轴与Y轴取得的数据不相同!请检查数采配置!");
                }
            }
            // 进行公式计算
            String resultValue = calculationFormula(list, v.get(0));
            map.put(k, resultValue);
        });
        return map;
            // 防止计算公式的时候出现:[null] 这种数据
            if (ObjectUtils.isNotEmpty(result)) {
                String formatProcessing = getFormatProcessing(result);
                list.add(formatProcessing);
            }
        }
        return list;
    }
    public static String getFormatProcessing(String value) {
        value = value.replaceAll("%", "");
        if(value.contains("=")){
            String[] split = value.split("=");
            return split[split.length - 1];
        } else if(value.contains(":")) {
            String[] split = value.split(":");
            return split[split.length - 1];
        } else {
            return value;
        }
    }
}