From 795836ddc0dc78cc598b94419753e9750c04fbfb Mon Sep 17 00:00:00 2001 From: Crunchy <3114200645@qq.com> Date: 星期六, 20 七月 2024 19:01:46 +0800 Subject: [PATCH] 文件采集完成 - 100% --- cnas-server/src/main/java/com/yuanchu/mom/utils/DataAcquisition.java | 329 +++++++++++++++++++++++++----------------------------- 1 files changed, 150 insertions(+), 179 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..505a27f 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,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("鎵�鍦ㄧ數鑴戞湭瀹夎鎴栨湭鍚姩锛歀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("鏈煡璇㈠埌鏁版嵁锛屽彲鑳芥枃浠惰矾寰勯厤缃敊璇紒"); @@ -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,13 +95,29 @@ } } } - map.put("frequency", "1"); - return Result.success(map); + String frequency = createFrequency(entrustCode, sampleCode); + map.put("frequency", frequency); + return map; } + } + + private 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 @@ -116,12 +130,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())) { - continue; - } - 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)) { @@ -143,6 +154,7 @@ /** * 闇�瑕侀�氳繃X,Y杞村畾浣� + * * @param data * @param dataConfig * @return @@ -155,8 +167,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)) { @@ -181,9 +193,11 @@ } private static Pattern SPATTERN = Pattern.compile("([-+])?\\d+(\\.\\d+)?"); + /** * 鍙渶X杞� - * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓� + * + * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓� * @param dataConfig 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗� * @return */ @@ -192,15 +206,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()) { @@ -209,11 +223,11 @@ } } 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); } } // 杩涜鍏紡璁$畻 @@ -225,16 +239,20 @@ /** * 浠庢枃浠朵腑鎻愬彇鍑烘潵鐨勬枃瀛楋紝濡傛灉鏈夊叕寮忥紝杩涜鍏紡璁$畻锛屽惁鍒欏彇鍒楄〃绗竴涓�� - * @param list 鎻愬彇鍑虹殑鏁板瓧 + * + * @param list 鎻愬彇鍑虹殑鏁板瓧 * @param dataConfig 瀛樺偍鍏紡鐨勫璞� * @return */ 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() > 1) { @@ -247,192 +265,145 @@ /** * 瑙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<>(); 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 + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } - } + List<Object> list = analyzeData(data, v, k, ","); // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + String resultValue = calculationFormula(list, v.get(0), k); + 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", ""); + 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 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 + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } - } + List<Object> list = analyzeData(processingDataAfterSpaces, v, k, splitIdentifier); // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + String resultValue = calculationFormula(list, v.get(0), k); + 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<>(); 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 + "鍙栧緱鐨勬暟鎹笉鐩稿悓锛佽妫�鏌ユ暟閲囬厤缃紒"); - } - } - } + // 鐢ㄦ埛鍙兘缁欎竴涓瓙椤圭洰閰嶇疆浜嗗涓厤缃紝鍗充竴涓」鐩彇澶氫釜鍊硷紝鎵�浠ラ渶瑕佸惊鐜� + List<Object> list = analyzeData(data, v, k, splitIdentifier); // 杩涜鍏紡璁$畻 - if (list.size() > 0) { - String resultValue = calculationFormula(list, v.get(0), k); - map.put(k, resultValue); - } else { - map.put(k, null); - } + 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; + // 閲囬泦鏁版嵁锛歒杞� + for (int i = 0; i < aColumnY.length; i++) { + // 鍙杧鐨勫�硷紝闃叉鎶ラ敊 + int x = getXOrY(v.get(config).getX(), k, "X"); + // 濡傛灉Y涓嶺鐢ㄦ埛閮介厤缃簡鍒欐墽琛� + if (ObjectUtils.isNotEmpty(refery)) { + // 鍙朰鍧愭爣鍊� + 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); + 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; + } + } } -- Gitblit v1.9.3