From 4d7d9ee75f529c9e1d66f98608edf18221f76acd Mon Sep 17 00:00:00 2001 From: zss <zss@example.com> Date: 星期四, 18 七月 2024 20:53:55 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java | 392 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 378 insertions(+), 14 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..99e3fd7 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,396 @@ 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.pojo.Device; import com.yuanchu.mom.vo.Result; import javax.servlet.http.HttpServletRequest; -import java.util.List; +import java.util.*; +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 = "@-@"; // 鑷畾涔夊敮涓�鏍囪瘑鍒嗗壊绗� + + /** + * 鏁伴噰鍏ュ彛 + * @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=" + 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; + String http = HTTP + ip + GETFILE + "?filePath=" + device.getCollectUrl() + "&fileExtension=" + device.getFileType(); + 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("鏈煡璇㈠埌鏁版嵁锛屽彲鑳芥枃浠惰矾寰勯厤缃敊璇紒"); + } else { + System.out.println(jsonObject); + throw new ErrorException("鏈煡閿欒锛岃鑱旂郴绠$悊鍛橈紒"); + } + } else { + String data = jsonObject.get("data").toString(); + // 鑰冭檻鍒颁竴涓娴嬮」鍙兘浼氬瓨鍦ㄥ涓暟閲囬厤缃紝鎵�浠ラ渶瑕佽繘琛屽垎缁� + Map<String, List<DataConfig>> userMap = dataConfig.stream().collect(Collectors.groupingBy(DataConfig::getInsProductItem)); + Map<String, String> map; + switch (device.getFileType()) { + case ".docx": + map = analysisString(data, userMap); + break; + case ".xlsx": + map = analysisList(data, userMap); + break; + case ".txt": + map = analysisTxt(data, userMap); + break; + case ".csv": + map = analysisList(data, userMap); + break; + case ".mdb": + map = analysisMdb(data, userMap, entrustCode, sampleCode); + break; + case ".db": + map = analysisDb(data, userMap); + break; + case ".png": + map = readPngString(data, userMap); + break; + default: + map = null; + break; + } + // 濡傛灉瀛樺湪瀛樺偍鍦板潃锛屽垯绉诲姩鍦板潃 + 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("瀛樺偍鍦板潃閿欒锛屽彲鑳芥枃浠惰矾寰勯厤缃敊璇紒"); + } else { + throw new ErrorException("鏈煡閿欒锛岃鑱旂郴绠$悊鍛橈紒"); + } + } + } + return Result.success(map); + } + } + + 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()); + JSONArray columnList = JSONArray.parseArray(jsonObject.get("column").toString()); + 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(); + if(ObjectUtils.isEmpty(v.get(config).getX()) && ObjectUtils.isEmpty(v.get(config).getY())) { + throw new ErrorException("鏈粰" + k + "杩涜鏁伴噰閰嶇疆x锛寉锛�"); + } + 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(); + } + } + JSONObject jsonObject1 = JSON.parseObject(dataList.get(y).toString()); + Object o = jsonObject1.get(key); + if (ObjectUtils.isNotEmpty(o)) { + list.add(o); + } + } + // 杩涜鍏紡璁$畻 + String resultValue = calculationFormula(list, v.get(0)); + map.put(k, resultValue); + }); + return map; + } + + 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()); + JSONArray columnList = JSONArray.parseArray(jsonObject.get("column").toString()); + 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(); + 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(); + } + } + 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); + } + } + } + } + // 杩涜鍏紡璁$畻 + String resultValue = calculationFormula(list, v.get(0)); + map.put(k, resultValue); + }); + return map; + } + + private static Pattern SPATTERN = Pattern.compile("([-+])?\\d+(\\.\\d+)?"); + /** + * 瑙f瀽String鏁版嵁 + * @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<>(); + 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 = 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()); + 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)) { + xResult = list1.get(x); + } + } + if (ObjectUtils.isNotEmpty(xResult)) { + list.add(xResult); + } + } + // 杩涜鍏紡璁$畻 + String resultValue = calculationFormula(list, v.get(0)); + map.put(k, resultValue); + }); + return map; + } + + /** + * 浠庢枃浠朵腑鎻愬彇鍑烘潵鐨勬枃瀛楋紝濡傛灉鏈夊叕寮忥紝杩涜鍏紡璁$畻锛屽惁鍒欏彇鍒楄〃绗竴涓�� + * @param list 鎻愬彇鍑虹殑鏁板瓧 + * @param dataConfig 瀛樺偍鍏紡鐨勫璞� + * @return + */ + private static String calculationFormula(List<Object> list, DataConfig dataConfig) { + // 濡傛灉涓嶄负绌猴紝杩涜鍏紡璁$畻 + if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) { + + return null; + // 鍚﹀垯锛氭病鏈夊叕寮忎唬琛ㄤ笉闇�瑕佽绠楋紝鐩存帴鎻愬彇List閲岄潰鐨勬暟鎹� + } else { + // 杩欓噷鍙細鍙栧垪琛ㄧ涓�涓暟鎹� + if (list.size() > 0) { + return list.get(0).toString(); + } + return null; + } + } + + /** + * 瑙f瀽String鏁版嵁 + * @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++) { + // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� + 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); + } + // 杩涜鍏紡璁$畻 + String resultValue = calculationFormula(list, v.get(0)); + map.put(k, resultValue); + }); + return map; + } + + /** + * 瑙f瀽String鏁版嵁 + * @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<>(); + 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++) { + // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� + 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); + } + } + // 杩涜鍏紡璁$畻 + String resultValue = calculationFormula(list, v.get(0)); + map.put(k, resultValue); + }); + return map; + } + + /** + * 瑙f瀽String鏁版嵁 + * @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++) { + // 寰楀嚭鐢ㄦ埛閰嶇疆鐨剎杞� + 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杞磋秴鍑猴紒"); + } + } + if (ObjectUtils.isNotEmpty(refery) && refery.equals(aLineX[j])) { + int y = Integer.parseInt(v.get(config).getY()); + String aColumnData = aColumnY[i + y]; // 鑾峰彇鍒扮Y琛岀殑鏁版嵁 + try { + yResult = aColumnData.split(splitIdentifier)[j]; + } catch (Exception e) { + throw new ErrorException("鏁伴噰閰嶇疆Y杞磋秴鍑猴紒"); + } + } + } + } + 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; } } -- Gitblit v1.9.3