From 12ecef99059f411e802c6fbc6f9a641c422b1747 Mon Sep 17 00:00:00 2001 From: Crunchy <3114200645@qq.com> Date: 星期六, 03 八月 2024 22:34:20 +0800 Subject: [PATCH] 数采bug修复 --- cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java | 801 ++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 561 insertions(+), 240 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 b2ffe28..b143832 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 @@ -9,10 +9,13 @@ 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.script.ScriptEngine; +import javax.script.ScriptEngineManager; import javax.servlet.http.HttpServletRequest; +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; @@ -27,58 +30,75 @@ 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; - String http = HTTP + ip + GETFILE + "?filePath=" + device.getCollectUrl() + "&fileExtension=" + device.getFileType(); + 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); + JSONObject jsonObject = JSON.parseObject(result); if (Objects.equals(jsonObject.get("code"), 1)) { if (ObjectUtils.isEmpty(jsonObject.get("msg"))) { - throw new ErrorException("鏈煡璇㈠埌鏁版嵁锛屽彲鑳芥枃浠惰矾寰勯厤缃敊璇紒"); + throw new ErrorException("鏈煡璇㈠埌鏂囦欢锛佸彲鑳借璺緞锛�" + device.getCollectUrl() + "锛変笅骞舵病鏈夋墍闇�(" + device.getFileType() +")鏂囦欢锛�"); } else { - System.out.println(jsonObject); - throw new ErrorException("鏈煡閿欒锛岃鑱旂郴绠$悊鍛橈紒"); + throw new ErrorException(jsonObject.get("msg") + ""); } } else { - String data = jsonObject.get("data").toString(); + String data = jsonObject.get("data") + ""; // 鑰冭檻鍒颁竴涓娴嬮」鍙兘浼氬瓨鍦ㄥ涓暟閲囬厤缃紝鎵�浠ラ渶瑕佽繘琛屽垎缁� - Map<String, List<DataConfig>> userMap = dataConfig.stream().collect(Collectors.groupingBy(DataConfig::getInsProductItem)); - Map<String, String> map; + 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); + map = analysisString(data, userMap, device, entrustCode, sampleCode); break; case ".xlsx": - map = analysisList(data, userMap); + map = analysisList(data, userMap, device, entrustCode, sampleCode); break; case ".txt": - map = analysisTxt(data, userMap); + map = analysisTxt(data, userMap, device, entrustCode, sampleCode); break; case ".csv": - map = analysisList(data, userMap); + map = analysisList(data, userMap, device, entrustCode, sampleCode); break; case ".mdb": - map = analysisMdb(data, userMap, entrustCode, sampleCode); + map = analysisMdb(data, userMap, device); break; case ".db": - map = analysisDb(data, userMap); + map = analysisDb(data, userMap, device); break; case ".png": - map = readPngString(data, userMap); + map = readPngString(data, userMap, device); break; default: map = null; @@ -86,121 +106,191 @@ } // 濡傛灉瀛樺湪瀛樺偍鍦板潃锛屽垯绉诲姩鍦板潃 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); - if (Objects.equals(storageUrlResultJson.get("code"), 1)) { - if (ObjectUtils.isEmpty(storageUrlResultJson.get("msg"))) { - throw new ErrorException("瀛樺偍鍦板潃閿欒锛屽彲鑳芥枃浠惰矾寰勯厤缃敊璇紒"); + 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 { - throw new ErrorException("鏈煡閿欒锛岃鑱旂郴绠$悊鍛橈紒"); + hashMap.put(split[0], value); } } - } - map.put("frequency", "1"); - return Result.success(map); + }); + 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, String> analysisDb(String data, Map<String, List<DataConfig>> dataConfig) { + 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()); - JSONArray columnList = JSONArray.parseArray(jsonObject.get("column").toString()); - Map<String, String> map = new HashMap<>(); dataConfig.forEach((k, v) -> { + AtomicInteger numberOfDataEntries = new AtomicInteger(); 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())) { - continue; - } - int x = Integer.parseInt(v.get(config).getX()); - int y = Integer.parseInt(v.get(config).getY()); - String key = ""; - for (int i = 0; i < columnList.size(); i++) { - if (columnList.get(i).equals(referx)) { - key = columnList.get(i + x).toString(); + 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); + // 灏忔暟鐐硅繘涓変綅 + double v1 = 0; + try { + v1 = Double.parseDouble(o.toString()); + double v2 = v1 / 1000; + list.add(v2); + } catch (NumberFormatException e) { + list.add(o); + } } } - JSONObject jsonObject1 = JSON.parseObject(dataList.get(y).toString()); - Object o = jsonObject1.get(key); - if (ObjectUtils.isNotEmpty(o)) { - 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); } // 杩涜鍏紡璁$畻 - String resultValue = calculationFormula(list, v.get(0), k); + Object resultValue = calculationFormula(result, v.get(0), k, device); 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) { + 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()); - JSONArray columnList = JSONArray.parseArray(jsonObject.get("column").toString()); - Map<String, String> map = new HashMap<>(); dataConfig.forEach((k, v) -> { + AtomicInteger numberOfDataEntries = new AtomicInteger(); 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 key = ""; - for (int i = 0; i < columnList.size(); i++) { - if (columnList.get(i).equals(referx)) { - key = columnList.get(i + x).toString(); - } - } + String refery = getRefer(v.get(config).getRefery()); for (int i = 0; i < dataList.size(); i++) { JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString()); - if (entrustCode.equals(jsonObject1.get("OrderNumber")) && sampleCode.equals(jsonObject1.get("SampleNumber"))) { - Object o = jsonObject1.get(key); - if (ObjectUtils.isNotEmpty(o)) { - list.add(o); - } + 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); + } // 杩涜鍏紡璁$畻 - String resultValue = calculationFormula(list, v.get(0), k); + 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 data 閲囬泦鍒扮殑鏂囦欢瀛楃涓� * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗� * @return */ - private static Map<String, String> readPngString(String data, Map<String, List<DataConfig>> dataConfig) { - Map<String, String> map = new HashMap<>(); + 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 = 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()) { @@ -209,15 +299,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), k); + Object resultValue = calculationFormula(list, v.get(0), k, device); map.put(k, resultValue); }); return map; @@ -225,214 +315,445 @@ /** * 浠庢枃浠朵腑鎻愬彇鍑烘潵鐨勬枃瀛楋紝濡傛灉鏈夊叕寮忥紝杩涜鍏紡璁$畻锛屽惁鍒欏彇鍒楄〃绗竴涓�� - * @param list 鎻愬彇鍑虹殑鏁板瓧 + * + * @param list 鎻愬彇鍑虹殑鏁板瓧 * @param dataConfig 瀛樺偍鍏紡鐨勫璞� * @return */ - private static String calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem) { + private static Object calculationFormula(List<Object> list, DataConfig dataConfig, String insProductItem, Device device) { + if (list.size() == 0) { + return null; + } + ArrayList<Object> listResult = new ArrayList<>(); + Map<String, Object> hashMap = new HashMap<>(); // 濡傛灉涓嶄负绌猴紝杩涜鍏紡璁$畻 if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) { - - return null; - // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹� - } else { - // 杩欓噷鍙細鍙栧垪琛ㄧ涓�涓暟鎹� - if (list.size() > 1) { - throw new ErrorException("鏈粰锛�" + insProductItem + " 閰嶇疆鍏紡锛屽彲鏄嵈閲囬泦鍒颁簡" + list.size() + "涓�硷紒鍒嗗埆涓猴細" + list); + // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹� + if (ObjectUtils.isEmpty(device.getEntrustCode()) && ObjectUtils.isEmpty(device.getSampleCode())) { + String s = calculationFormulaList(list, dataConfig.getFormula()); + listResult.add(s); } else { - return list.get(0).toString(); + 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 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<>(); + 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<>(); - 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++) { - // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� - String[] aLineX = aColumnY[i].split(","); - for (int j = 0; j < aLineX.length; j++) { - if (ObjectUtils.isNotEmpty(referx) && aLineX[j].contains(referx)) { - try { - int x = Integer.parseInt(v.get(config).getX()); - xResult = aLineX[j + x]; - } catch (Exception e) {} - } - if (ObjectUtils.isNotEmpty(refery) && aLineX[j].contains(refery)) { - try { - int y = Integer.parseInt(v.get(config).getY()); - String[] split = aColumnY[i + y].split(","); - yResult = split[split.length - 1]; - } catch (Exception e) {} - } - } - } - if (ObjectUtils.isNotEmpty(xResult) && ObjectUtils.isNotEmpty(yResult)) { - if(xResult.equals(yResult)) { - list.add(xResult); - } else { - throw new ErrorException(k + "椤圭洰X杞村�硷細" + xResult + "锛孻杞村�硷細" + yResult + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } + // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�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); } // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + Object resultValue = calculationFormula(list, v.get(0), k, device); + map.put(k, resultValue); }); return map; } /** - * - * @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", ""); - Map<String, String> map = new HashMap<>(); + 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<>(); - 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 = processingDataAfterSpaces.split("\n"); - // 璇ュ惊鐜緱鍑虹敤鎴烽厤缃殑y杞� - for (int i = 0; i < aColumnY.length; i++) { - if (aColumnY[i].replaceAll(" ", "").contains(refery.replaceAll(" ", ""))) { - int y = Integer.parseInt(v.get(config).getY()); - int x = Integer.parseInt(v.get(config).getX()); - String[] split = aColumnY[i + y].split(splitIdentifier); - for (int i1 = 0; i1 < split.length; i1++) { - if (split[i1].replaceAll(" ", "").equals(referx.replaceAll(" ", ""))) { -// split[i1 + x] - } - } -// String[] aLineX = aColumnY[i].split(splitIdentifier); -// try { -// int y = Integer.parseInt(v.get(config).getY()); -// String[] split = aColumnY[i + y].split(splitIdentifier); -// yResult = split[split.length - 1]; -// } catch (Exception e) {} - } - // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� - -// for (int j = 0; j < aLineX.length; j++) { -// if (ObjectUtils.isNotEmpty(referx) && aLineX[j].replaceAll(" ", "").contains(referx.replaceAll(" ", ""))) { -// try { -// int x = Integer.parseInt(v.get(config).getX()); -// xResult = aLineX[j + x]; -// } catch (Exception e) {} -// } -// -// } - } - if (ObjectUtils.isNotEmpty(yResult)) { - yResult = yResult.replaceAll(" ", ""); - // 缁撴灉鍖呭惈鐗规畩瀛楃锛岄渶瑕佸墧闄ゆ帀 - if(yResult.contains("=")) { - String[] split = yResult.split("="); - list.add(split[split.length - 1]); - } else if (yResult.contains(":")) { - String[] split = yResult.split(":"); - list.add(split[split.length - 1].replaceAll("%", "")); - } - } - System.out.println(xResult + "=========" + yResult); - if (ObjectUtils.isNotEmpty(xResult) && ObjectUtils.isNotEmpty(yResult)) { - if(xResult.equals(yResult)) { - list.add(xResult); - } else { - throw new ErrorException("X杞村�硷細" + xResult + "锛孻杞村�硷細" + yResult + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } + // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�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); } // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + Object resultValue = calculationFormula(list, v.get(0), k, device); + map.put(k, resultValue); }); return map; } /** * 鍙朮锛孻涓や釜瀹氫綅 - * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓� + * + * @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<>(); + 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<>(); - 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++) { - // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� - String[] aLineX = aColumnY[i].split(splitIdentifier); - for (int j = 0; j < aLineX.length; j++) { - if (ObjectUtils.isNotEmpty(referx) && referx.equals(aLineX[j])) { - try { - int x = Integer.parseInt(v.get(config).getX()); - xResult = aLineX[j + x]; - } catch (Exception e) {} - } - if (ObjectUtils.isNotEmpty(refery) && refery.equals(aLineX[j])) { - try { - int y = Integer.parseInt(v.get(config).getY()); - String aColumnData = aColumnY[i + y]; // 鑾峰彇鍒扮Y琛岀殑鏁版嵁 - yResult = aColumnData.split(splitIdentifier)[j]; - } catch (Exception e) {} - } - } - } - if (ObjectUtils.isNotEmpty(xResult) && ObjectUtils.isNotEmpty(yResult)) { - if(xResult.equals(yResult)) { - list.add(xResult); - } else { - throw new ErrorException("X杞村�硷細" + xResult + "锛孻杞村�硷細" + yResult + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } + // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙蜂笉瀛樺湪锛屽畾锛�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); } // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + 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 + 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()); + System.out.println(bigDecimalList); + + //灏嗕腑鏂囩殑(杞崲鑻辨枃鐨�()) + formula = formula.replace("锛�", "(") + .replace("锛�", ")") + .replace("锛�", ","); + //鐒跺悗鎻愬彇鍏紡 + String strs = formula.substring(0, formula.indexOf("(")); + String upperStr = strs.toUpperCase(); + if (upperStr.matches(".*\\d.*")) { + upperStr = ""; + } + System.out.println(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) { + System.out.println("鏇挎崲鍓�" + expr); + 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); + System.out.println("鏇挎崲鍚�" + sb.toString()); + + // 璁$畻琛ㄨ揪寮� + 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); + } + } + System.out.println(results); + System.out.println("璁$畻缁撴灉: " + finalResult); + + return finalResult.toString(); + // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹� + } } -- Gitblit v1.9.3