From 7a268ed852ca462347cca383a6d0306ff54e1363 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期一, 04 十一月 2024 15:05:21 +0800
Subject: [PATCH] 添加模版
---
cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java | 748 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 732 insertions(+), 16 deletions(-)
diff --git a/cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java b/cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java
index 7766817..dff4725 100644
--- a/cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java
+++ b/cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java
@@ -1,32 +1,748 @@
package com.yuanchu.mom.utils;
+import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.pojo.DataConfig;
-import com.yuanchu.mom.vo.Result;
+import com.yuanchu.mom.pojo.Device;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServletRequest;
-import java.util.List;
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
public class DataAcquisition {
private static final String HTTP = "http://";
- private static final String GETFILE = "/lims/getFile";
+ private static final String GETFILE = ":9527/lims/getFile"; // 鑾峰彇鏂囦欢鎺ュ彛
- private static final String MOVEFILE = "/lims/moveFile";
+ private static final String MOVEFILE = ":9527/lims/moveFile"; // 鏂囦欢绉诲姩鍦板潃
- public static Result<?> dataAcquisitionEntrance(HttpServletRequest request, List<DataConfig> dataConfig) {
+ private static final String splitIdentifier = "@-@"; // 鑷畾涔夊敮涓�鏍囪瘑鍒嗗壊绗�
+
+ public static final String frequency = "frequency";
+
+ /**
+ * 鏁伴噰鍏ュ彛
+ *
+ * @param dataConfig
+ * @param device
+ * @return
+ */
+ public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip) {
+ /**
+ * filePath 鏂囦欢閲囬泦璺緞
+ * fileExtension 鏂囦欢鍚庣紑
+ * entrustCode 濮旀墭缂栧彿
+ * sampleCode 鏍峰搧缂栧彿
+ * mdbEntrustCode mdb鏂囦欢闇�瑕侊細濮旀墭缂栧彿瀛楁 涓轰粈涔堟病鏈夊幓杩欎釜mdb鍓嶇紑鍛紵鍥犱负宸茬粡缁欏鎴风殑閮ㄥ垎鐢佃剳涓婂畨瑁呬簡閲囬泦鍣紝鑰岀敤鎴蜂笉鎺ュ彈閲嶆柊瀹夎閲囬泦鍣紝鎵�浠ュ氨娌℃湁鍘婚櫎
+ * mdbSampleCode mdb鏂囦欢闇�瑕侊細鏍峰搧缂栧彿瀛楁
+ */
+ String http = HTTP + ip + GETFILE +
+ "?filePath=" + device.getCollectUrl() +
+ "&fileExtension=" + device.getFileType() +
+ "&entrustCode=" + entrustCode +
+ "&sampleCode=" + sampleCode +
+ "&mdbEntrustCode=" + device.getEntrustCode() +
+ "&mdbSampleCode=" + device.getSampleCode() +
+ "&dbFileName=" + device.getDbFileName();
+ String result = null;
+ try {
+ result = HttpUtil.get(http);
+ } catch (IORuntimeException e) {
+ throw new ErrorException("鎵�鍦ㄧ數鑴戞湭瀹夎鎴栨湭鍚姩锛歀IMS鏂囦欢閲囬泦鍣紒");
+ }
+ 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() +")鏂囦欢锛�");
+ } else {
+ throw new ErrorException(jsonObject.get("msg") + "");
+ }
+ } else {
+ String data = jsonObject.get("data") + "";
+ // 鑰冭檻鍒颁竴涓娴嬮」鍙兘浼氬瓨鍦ㄥ涓暟閲囬厤缃紝鎵�浠ラ渶瑕佽繘琛屽垎缁�
+ Map<String, List<DataConfig>> userMap = dataConfig.stream()
+ .peek(i -> i.setInsProductItem(
+ i.getInspectionItem().equals(i.getInspectionItemSubclass()) ? i.getInspectionItem() + "," : i.getInspectionItem() + "," + i.getInspectionItemSubclass()
+ ))
+ .collect(Collectors.groupingBy(DataConfig::getInsProductItem));
+ Map<String, Object> map;
+ switch (device.getFileType()) {
+ case ".docx":
+ map = analysisString(data, userMap, device, entrustCode, sampleCode);
+ break;
+ case ".xlsx":
+ map = analysisList(data, userMap, device, entrustCode, sampleCode);
+ break;
+ case ".txt":
+ map = analysisTxt(data, userMap, device, entrustCode, sampleCode);
+ break;
+ case ".csv":
+ map = analysisList(data, userMap, device, entrustCode, sampleCode);
+ break;
+ case ".mdb":
+ map = analysisMdb(data, userMap, device);
+ break;
+ case ".db":
+ map = analysisDb(data, userMap, device);
+ break;
+ case ".png":
+ map = readPngString(data, userMap, device);
+ break;
+ default:
+ map = null;
+ break;
+ }
+ // 濡傛灉瀛樺湪瀛樺偍鍦板潃锛屽垯绉诲姩鍦板潃
+ if (ObjectUtils.isNotEmpty(device.getStorageUrl())) {
+ String s = HTTP + ip + MOVEFILE + "?startFilePath=" + device.getCollectUrl() + "&endFilePath=" + device.getStorageUrl() + "&fileType=" + device.getFileType();
+ HttpUtil.get(s);
+ }
+ return map;
+ }
+ }
+
+ public static Map<String, Object> createFrequency(String entrustCode, String sampleCode, Map<String, Object> map) {
+ HashSet<String> set = new HashSet<>();
+ map.forEach((key, value) -> {
+ String[] split = key.split(",");
+ String inspectionItem = split[0];
+ // 鍙鏈変竴涓笉涓虹┖灏眘et杩涘幓
+ if (ObjectUtils.isNotEmpty(value)) {
+ set.add(inspectionItem);
+ }
+ });
+ Map<String, Object> result = new HashMap<>();
+ for (String inspectionItemKey : set) {
+ Map<String, Object> hashMap = new HashMap<>();
+ map.forEach((key, value) -> {
+ String[] split = key.split(",");
+ String inspectionItem = split[0];
+ if (inspectionItemKey.equals(inspectionItem)) {
+ if (split.length > 1) {
+ hashMap.put(split[1], value);
+ } else {
+ hashMap.put(split[0], value);
+ }
+ }
+ });
+ String frequency = createKey(entrustCode, sampleCode, inspectionItemKey);
+ hashMap.put("frequency", frequency);
+ result.put(inspectionItemKey, hashMap);
+ }
+ return result;
+ }
+
+ public static String createKey(String entrustCode, String sampleCode, String inspectionItemKey) {
+ String key = frequency + ":" + entrustCode + ":" + sampleCode + ":" + inspectionItemKey;
+ 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, 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;
+ }
+ 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());
+ 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++) {
+ String aggregate = "";
+ for (int j = 0; j < v.size(); j++) {
+ int index;
+ if (j == 0) {
+ index = i;
+ } else {
+ index = numberOfDataEntries.get() + i;
+ }
+ aggregate += list.get(index).toString() + ",";
+ }
+ int lastIndex = aggregate.lastIndexOf(",");
+ String substring = aggregate.substring(0, lastIndex);
+ result.add(substring);
+ }
+ // 杩涜鍏紡璁$畻
+ Object resultValue = calculationFormula(result, v.get(0), k, device);
+ map.put(k, resultValue);
+ });
+ return map;
+ }
+
+ /**
+ * @param data
+ * @param dataConfig
+ * @return
+ */
+ private static Map<String, Object> analysisMdb(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;
+ }
+ 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());
+ 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++) {
+ String aggregate = "";
+ for (int j = 0; j < v.size(); j++) {
+ int index;
+ if (j == 0) {
+ index = i;
+ } else {
+ index = numberOfDataEntries.get() + i;
+ }
+ aggregate += list.get(index).toString() + ",";
+ }
+ int lastIndex = aggregate.lastIndexOf(",");
+ String substring = aggregate.substring(0, lastIndex);
+ result.add(substring);
+ }
+ // 杩涜鍏紡璁$畻
+ Object resultValue = calculationFormula(result, v.get(0), k, device);
+ map.put(k, resultValue);
+ });
+ return map;
+ }
+
+ private static Pattern SPATTERN = Pattern.compile("([-+])?\\d+(\\.\\d+)?");
+
+ /**
+ * 鍙渶X杞�
+ *
+ * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓�
+ * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗�
+ * @return
+ */
+ private static Map<String, Object> readPngString(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;
+ }
+
+ /**
+ * 浠庢枃浠朵腑鎻愬彇鍑烘潵鐨勬枃瀛楋紝濡傛灉鏈夊叕寮忥紝杩涜鍏紡璁$畻锛屽惁鍒欏彇鍒楄〃绗竴涓��
+ *
+ * @param list 鎻愬彇鍑虹殑鏁板瓧
+ * @param dataConfig 瀛樺偍鍏紡鐨勫璞�
+ * @return
+ */
+ private static Object calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem, Device device) {
+ if (list.size() == 0) {
+ Map<String, Object> hashMap = new HashMap<>();
+ hashMap.put("equipName", device.getDeviceName());
+ hashMap.put("equipValue", device.getManagementNumber());
+ return hashMap;
+ }
+ ArrayList<Object> listResult = new ArrayList<>();
+ Map<String, Object> hashMap = new HashMap<>();
+ // 濡傛灉涓嶄负绌猴紝杩涜鍏紡璁$畻
+ if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) {
+ // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹�
+ if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
+ String s = calculationFormulaList(list, dataConfig.getFormula());
+ listResult.add(s);
+ } else {
+ list.forEach(i -> {
+ List<Object> strings = Arrays.asList(i.toString().split(","));
+ String s = calculationFormulaList(strings, dataConfig.getFormula());
+ listResult.add(s);
+ });
+ }
+ } else {
+ listResult.addAll(list);
+ }
+ // 涓轰簡缁欏墠绔仛鏁版嵁鍖哄垎
+ if (listResult.size() > 1) {
+ hashMap.put("result", listResult);
+ } else {
+ hashMap.put("result", listResult.get(0).toString());
+ }
+ hashMap.put("equipName", device.getDeviceName());
+ hashMap.put("equipValue", device.getManagementNumber());
+ return hashMap;
+ }
+
+ /**
+ * 瑙f瀽String鏁版嵁
+ *
+ * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓�
+ * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗�
+ * @return
+ */
+ private static Map<String, Object> analysisTxt(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銆乊瀹氳寖鍥达紝X瀹氭í鍧愭爣锛�2銆佸彧瀛樺湪Y锛�3銆佸彧瀛樺湪X
+ if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) {
+ list = analyzeData(data, v, k, ",");
+ // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦�
+ } else if (ObjectUtils.isNotEmpty(device.getEntrustCode()) && ObjectUtils.isNotEmpty(device.getSampleCode())) {
+ list = analyzeDataEntrustCodAndSampleCode(data, v, k, ",", device, entrustCode, sampleCode);
+ }
+ // 杩涜鍏紡璁$畻
+ Object resultValue = calculationFormula(list, v.get(0), k, device);
+ map.put(k, resultValue);
+ });
+ return map;
+ }
+
+ /**
+ * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓�
+ * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗�
+ * @return
+ */
+ private static Map<String, Object> analysisString(String data, Map<String, List<DataConfig>> dataConfig, Device device,
+ String entrustCode, String sampleCode) {
+ String processingDataAfterSpaces = data
+ .replaceAll(" +", splitIdentifier)
+ .replaceAll("\r", "")
+ .replaceAll(" ", "");
+ Map<String, Object> map = new HashMap<>();
+ 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 = analyzeData(processingDataAfterSpaces, v, k, splitIdentifier);
+ // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦�
+ } else if (ObjectUtils.isNotEmpty(device.getEntrustCode()) && ObjectUtils.isNotEmpty(device.getSampleCode())) {
+ list = analyzeDataEntrustCodAndSampleCode(processingDataAfterSpaces, v, k, splitIdentifier, device, entrustCode, sampleCode);
+ }
+ // 杩涜鍏紡璁$畻
+ Object resultValue = calculationFormula(list, v.get(0), k, device);
+ map.put(k, resultValue);
+ });
+ return map;
+ }
+
+ /**
+ * 鍙朮锛孻涓や釜瀹氫綅
+ *
+ * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓�
+ * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗�
+ * @return
+ */
+ 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<>();
+ 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 = 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(" ", "");
+ sampleCodeValue = sampleCodeValue.replaceAll(" ", "");
+ // 鏈�缁堢粨鏋�
+ List<Object> list = new ArrayList<>();
+ int numberOfDataEntries = 0;
+ // 鍙杄ntrustCode涓巗ampleCode鎵�鍦ㄤ綅
+ for (int config = 0; config < v.size(); config++) {
+ numberOfDataEntries = 0;
+ Integer entrustCodeY = null;
+ Integer sampleCodeY = null;
+ Integer referYCoordinate = null;
+ String refery = getRefer(v.get(config).getRefery());
+ String entrustCode = getRefer(device.getEntrustCode()); // 濮旀墭缂栧彿瀛楁
+ String sampleCode = getRefer(device.getSampleCode()); // 鏍峰搧缂栧彿瀛楁
+ if (ObjectUtils.isEmpty(refery)) {
+ continue;
+ }
+ // 鍘婚櫎鎵�鏈夌殑绌烘牸锛岄�氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿
+ String[] aColumnY = data.replaceAll(" ", "").split("\n");
+ for (int i = 0; i < aColumnY.length; i++) {
+ // 濡傛灉閫氳繃鍒ゆ柇锛屽畾浣嶅埌Y杞�
+ if (aColumnY[i].contains(entrustCode) && aColumnY[i].contains(sampleCode)) {
+ String[] aLine = aColumnY[i].split(splitIdentifier);
+ for (int j = 0; j < aLine.length; j++) {
+ if (aLine[j].contains(entrustCode)) {
+ entrustCodeY = j;
+ }
+ if (aLine[j].contains(sampleCode) ) {
+ sampleCodeY = j;
+ }
+ if (aLine[j].contains(refery)) {
+ referYCoordinate = j;
+ }
+ }
+ }
+ if (ObjectUtils.isNotEmpty(entrustCodeY) && ObjectUtils.isNotEmpty(sampleCodeY) && ObjectUtils.isNotEmpty(referYCoordinate)) {
+ String[] aLine = aColumnY[i].split(splitIdentifier);
+ try {
+ if (aLine[entrustCodeY].contains(entrustCodeValue) && aLine[sampleCodeY].contains(sampleCodeValue)) {
+ String result = aLine[referYCoordinate];
+ // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁
+ if (ObjectUtils.isNotEmpty(result)) {
+ numberOfDataEntries += 1;
+ list.add(result);
+ }
+ }
+ } catch (Exception e) {}
+ }
+ }
+ }
+ // 鎷兼帴鏁伴噰閰嶇疆
+ List<Object> result = new ArrayList<>();
+ for (int i = 0; i < numberOfDataEntries; i++) {
+ String aggregate = "";
+ for (int j = 0; j < v.size(); j++) {
+ int index;
+ if (j == 0) {
+ index = i;
+ } else {
+ index = numberOfDataEntries + i;
+ }
+ aggregate += list.get(index).toString() + ",";
+ }
+ int lastIndex = aggregate.lastIndexOf(",");
+ String substring = aggregate.substring(0, lastIndex);
+ result.add(substring);
+ }
+ return result;
+ }
+
+ // 鐢变簬鍦ㄦ柟娉曚腑浼氬ぇ閲忕殑鍒ゆ柇锛屾墍浠ュ仛涓�涓柟娉�
+ 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(" ", "") : "";
+ }
+
+ /**
+ * 濮旀墭缂栧彿涓庢牱鍝佺紪鍙烽兘涓虹┖鎵ц
+ *
+ * @param data
+ * @param v
+ * @param k
+ * @param split
+ * @return
+ */
+ 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());
+ if (ObjectUtils.isEmpty(refery) && ObjectUtils.isEmpty(referx)) {
+ continue;
+ }
+ // 鏈�缁堢粨鏋�
+ String result = null;
+ // 閫氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿
+ String[] aColumnY = data.replaceAll(" ", "").split("\n");
+ Integer end = null;
+ // 閲囬泦鏁版嵁锛歒杞�
+ for (int i = 0; i < aColumnY.length; i++) {
+ // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓虹┖鍒欐墽琛岋紝鍚屾椂璇ヨ鍖呭惈Y鍙傜収
+ if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && aColumnY[i].contains(refery)) {
+ // 鍙朰鍧愭爣鍊�
+ int y = getXOrY(v.get(config).getY(), k, "Y");
+ String[] aLineX = aColumnY[i].split(split);
+ for (int j = 0; j < aLineX.length; j++) {
+ if (aLineX[j].contains(refery)) {
+ String[] split1 = new String[0];
+ try {
+ split1 = aColumnY[i + y].split(split);
+ } catch (Exception e) {
+ throw new ErrorException(k + "锛歒杞村畾浣嶈秴鍑猴紒");
+ }
+ try {
+ result = split1[j];
+ } catch (Exception e) {
+ throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒");
+ }
+ }
+ }
+ // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓嶄负绌哄垯鎵ц,姝ゅY瀹氬尯鍩�
+ } else if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isNotEmpty(referx)) {
+ // 鍙杧鐨勫�硷紝闃叉鎶ラ敊
+ int x = getXOrY(v.get(config).getX(), k, "X");
+ // 鍙朰鍧愭爣鍊�
+ 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 + "锛歑杞村畾浣嶈秴鍑猴紒");
+ }
+ break;
+ }
+ }
+ }
+ // 濡傛灉X鍙傜収涓嶄负绌哄悓鏃惰琛屽寘鍚玐鍙傜収锛屽垯鎵ц涓嬮潰鐨勪唬鐮�
+ } else if (aColumnY[i].contains(referx) && ObjectUtils.isEmpty(refery)) {
+ String[] aLineX = aColumnY[i].split(split);
+ // 鍙杧鐨勫�硷紝闃叉鎶ラ敊
+ int x = getXOrY(v.get(config).getX(), k, "X");
+ 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 + "锛歑杞村畾浣嶈秴鍑猴紒");
+ }
+ }
+ }
+ }
+ }
+ // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[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;
+ }
+ }
+
+ public static String getIp(HttpServletRequest request) {
String ipAddress = request.getRemoteAddr();
// 闃叉鍥炵幆鍦板潃鍙樹负IPv6
- String ip = ipAddress.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipAddress;
-// String http = HTTP + ip + GETFILE + "?filePath=" + dataConfig.get(0).getCollectUrl() + "&fileExtension=" + dataConfig.get(0).getFileType();
-// String result = HttpUtil.get(http);
-// JSONObject jsonObject = JSON.parseObject(result);
-// if (Objects.equals(jsonObject.get("code"), 1)) {
-// throw new ErrorException(jsonObject.get("message").toString());
-// } else {
-// Object data = jsonObject.get("data");
-//
-// }
- return null;
+ return ipAddress.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipAddress;
+ }
+
+ public static String calculationFormulaList(List<Object> list, String formula) {
+ //棣栧厛灏唋ist杞崲涓篵igdecmic
+ List<BigDecimal> bigDecimalList = list.stream()
+ .map(obj -> {
+ return new BigDecimal((obj).toString());
+ }).collect(Collectors.toList());
+
+ //灏嗕腑鏂囩殑(杞崲鑻辨枃鐨�())
+ formula = formula.replace("锛�", "(")
+ .replace("锛�", ")")
+ .replace("锛�", ",");
+ //鐒跺悗鎻愬彇鍏紡
+ String strs = formula.substring(0, formula.indexOf("("));
+ String upperStr = strs.toUpperCase();
+ if (upperStr.matches(".*\\d.*")) {
+ upperStr = "";
+ }
+ //鐒跺悗鑾峰彇鏈�澶栭潰鎷彿閲岄潰鐨勫��,鍐嶆牴鎹�","鍒嗗壊
+ int start = formula.indexOf("(");
+ int end = -1;
+ int a = 0;
+ for (int i = start; i < formula.length(); i++) {
+ char c = formula.charAt(i);
+ if (c == '(') {
+ a++;
+ } else if (c == ')') {
+ a--;
+ if (a == 0) {
+ end = i;
+ }
+ }
+ }
+ if (start == -1 || end == -1) {
+ throw new ErrorException("鍏紡鎷彿涓嶅尮閰�: " + formula);
+ }
+
+ String argumentsStr = formula.substring(start + 1, end);
+ List<String> arguments = new ArrayList<>();
+ int bracketCount = 0;
+ StringBuilder currentArgument = new StringBuilder();
+ for (char c : argumentsStr.toCharArray()) {
+ if (c == ',' && bracketCount == 0) {
+ arguments.add(currentArgument.toString());
+ currentArgument.setLength(0);
+ } else {
+ if (c == '(') bracketCount++;
+ if (c == ')') bracketCount--;
+ currentArgument.append(c);
+ }
+ }
+ arguments.add(currentArgument.toString());
+ String[] bracketStrs = arguments.toArray(new String[0]);
+ List<BigDecimal> results = new ArrayList<>();
+ for (String expr : bracketStrs) {
+ Pattern pattern = Pattern.compile("([A-Z])(\\d+)");
+ Matcher matcher = pattern.matcher(expr);
+ StringBuffer sb = new StringBuffer();
+ while (matcher.find()) {
+ String letter = matcher.group(1);
+ int index = Integer.parseInt(matcher.group(2)) - 1; // 灏�1-based杞负0-based
+ if (index < bigDecimalList.size()) {
+ matcher.appendReplacement(sb, bigDecimalList.get(index).toString());
+ } else {
+ throw new RuntimeException("鍏紡涓殑涓嬫爣 " + index + " 瓒呭嚭鑼冨洿");
+ }
+ }
+ matcher.appendTail(sb);
+
+ // 璁$畻琛ㄨ揪寮�
+ ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
+ try {
+ Object result = engine.eval(sb.toString());
+ results.add(new BigDecimal(result.toString()));
+ } catch (Exception e) {
+ throw new IllegalArgumentException("鏃犳硶璁$畻鍏紡: " + sb, e);
+ }
+ }
+ // 鏍规嵁鍑芥暟鍚嶇О杩涜鐩稿簲璁$畻
+ BigDecimal finalResult;
+ if (upperStr.equals("") || upperStr == null) {
+ finalResult = results.get(0);
+ } else {
+ switch (upperStr) {
+ case "MAX":
+ finalResult = results.stream().max(BigDecimal::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("鏃犳硶璁$畻MAX鍊�"));
+ break;
+ case "MIN":
+ finalResult = results.stream().min(BigDecimal::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("鏃犳硶璁$畻MIN鍊�"));
+ break;
+ case "SUM":
+ finalResult = results.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
+ break;
+ case "ABS":
+ finalResult = results.stream().map(BigDecimal::abs).reduce(BigDecimal.ZERO, BigDecimal::add);
+ break;
+ case "AVERAGE":
+ finalResult = results.stream().reduce(BigDecimal.ZERO, BigDecimal::divide)
+ .divide(BigDecimal.valueOf(results.size()), 2, BigDecimal.ROUND_HALF_UP);
+ break;
+ case "MEDIAN":
+ int size = results.size();
+ if (size % 2 == 1) {
+ finalResult = results.get(size / 2);
+ } else {
+ BigDecimal sum = results.get(size / 2 - 1).add(results.get(size / 2));
+ finalResult = sum.divide(BigDecimal.valueOf(2), 2, BigDecimal.ROUND_HALF_UP);
+ }
+ break;
+ default:
+ throw new UnsupportedOperationException("鏆備笉鏀寔鍑芥暟: " + upperStr);
+ }
+ }
+
+ return finalResult.toString();
+ // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹�
}
}
--
Gitblit v1.9.3