From e8eb756760d2a1fe65f70545482364a07fa1060a Mon Sep 17 00:00:00 2001 From: yaowanxin <3588231647@qq.com> Date: 星期六, 19 七月 2025 10:21:32 +0800 Subject: [PATCH] 修改方法2 --- cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java | 196 +++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 190 insertions(+), 6 deletions(-) diff --git a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java index 5065c0b..9d740d3 100644 --- a/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java +++ b/cnas-device/src/main/java/com/ruoyi/device/utils/DataAcquisition.java @@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.ruoyi.device.dto.DeviceCollectionDto; import com.ruoyi.device.pojo.DataConfig; import com.ruoyi.device.pojo.Device; import com.ruoyi.framework.exception.ErrorException; @@ -42,7 +43,7 @@ * @param device * @return */ - public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag) { + public static Map<String, Object> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip, String cableTag, String dbUserName, String dbPassword) { // 鍒ゆ柇鏄惁鏄奖鍍忔祴閲忎华 if (device.getManagementNumber().equals("JCZX-ZB-OP07001")) { if (device.getFileType().equals(".xlsx")) { @@ -75,10 +76,14 @@ "&sampleCode=" + sampleCode + "&mdbEntrustCode=" + device.getEntrustCode() + "&mdbSampleCode=" + device.getSampleCode() + - "&dbFileName=" + device.getDbFileName(); + "&dbFileName=" + device.getDbFileName()+ + "&dbUserName=" + dbUserName + + "&dbPassword=" + dbPassword; + System.out.println("璇锋眰鐨� URL: " + http); String result = null; try { result = HttpUtil.get(http); + System.out.println("璇锋眰杩斿洖缁撴灉: " + result); } catch (IORuntimeException e) { e.printStackTrace(); throw new ErrorException("鎵�鍦ㄧ數鑴戞湭瀹夎鎴栨湭鍚姩锛歀IMS鏂囦欢閲囬泦鍣紒"); @@ -113,6 +118,9 @@ case ".xlsx": map = analysisList(data, userMap, device, entrustCode, sampleCode); break; + case ".xls": + map = analysisTxt(data, userMap, device, entrustCode, sampleCode); + break; case ".txt": map = analysisTxt(data, userMap, device, entrustCode, sampleCode); break; @@ -120,7 +128,7 @@ map = analysisList(data, userMap, device, entrustCode, sampleCode); break; case ".mdb": - // 鍒ゆ柇鏄惁鏄媺鍔涙満 + // 鍒ゆ柇鏄惁鏄媺鍔涙満JCZX-ZB-FF01014 if (device.getManagementNumber().equals("JCZX-ZB-FF01014")) { map = analysisMdbByPull(data, userMap, device, cableTag); } else { @@ -128,7 +136,8 @@ } break; case ".db": - map = analysisDb(data, userMap, device); + map = analysisDb1(data, userMap, device); +// map = analysisDb(data, userMap, device); break; case ".png": map = readPngString(data, userMap, device); @@ -181,6 +190,56 @@ return "1"; } + private static Map<String, Object> analysisDb1(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; + } + // 浠� JSON 鏁版嵁涓彁鍙� data 鏁扮粍 + 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()); + if (ObjectUtils.isNotEmpty(jsonObject1.get(refery))) { + numberOfDataEntries.addAndGet(1); + list.add(jsonObject1.get(refery)); + } +// 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++) { + StringBuilder aggregate = new StringBuilder(); + for (int j = 0; j < v.size(); j++) { + int index; + if (j == 0) { + index = i; + } else { + index = numberOfDataEntries.get() + i; + } + aggregate.append(list.get(index).toString()).append(","); + } + if (aggregate.length() > 0) { + aggregate.setLength(aggregate.length() - 1); + } + result.add(aggregate.toString()); + } + // 杩涜鍏紡璁$畻 + Object resultValue = calculationFormula(result, v.get(0), k, device); + map.put(k, resultValue); + }); + return map; + } /** * 闇�瑕侀�氳繃X,Y杞村畾浣� @@ -189,17 +248,20 @@ * @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; } + // 浠� JSON 鏁版嵁涓彁鍙� data 鏁扮粍 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 = v.get(config).getRefery(); String refery = getRefer(v.get(config).getRefery()); for (int i = 0; i < dataList.size(); i++) { JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString()); @@ -473,6 +535,7 @@ String firstNumber = lastLine.split("\t")[0]; list.add(firstNumber); } else { + list = analyzeData(data, v, k, ","); } // 濮旀墭缂栧彿涓庢牱鍝佺紪鍙峰瓨鍦� @@ -625,7 +688,117 @@ private static String getRefer(String refer) { return ObjectUtils.isNotEmpty(refer) ? refer.replaceAll(" ", "") : ""; } + /** + * 濮旀墭缂栧彿涓庢牱鍝佺紪鍙烽兘涓虹┖鎵ц + * + * @param data 閲囬泦鍒扮殑鏂囦欢瀛楃涓� + * @param v 鐢ㄦ埛閰嶇疆濂界殑x,y杞村畾浣嶆暟鎹笌鍙傜収鐗� + * @param k 妫�楠岄」鍚嶇О + * @param split 鍒嗗壊绗� + * @return 鎻愬彇鐨勬暟鎹垪琛� + */ + public static List<Object> analyzeData1(String data, List<DataConfig> v, String k, String split) { + List<Object> list = new ArrayList<>(); + // 棰勫鐞嗘暟鎹細绉婚櫎澶氫綑绌烘牸骞朵繚鐣欏叧閿垎闅旂 + String processedData = data.replaceAll("\\s+", " ").trim(); + for (int configIndex = 0; configIndex < v.size(); configIndex++) { + // 鍙栦袱涓敤鎴烽厤缃殑鍙傜収鐗� + String referx = getRefer(v.get(configIndex).getReferx()); + String refery = getRefer(v.get(configIndex).getRefery()); + if (ObjectUtils.isEmpty(refery) && ObjectUtils.isEmpty(referx)) { + System.out.println("鍙傜収鐗╀负绌猴紝璺宠繃褰撳墠閰嶇疆锛宬: " + k); + continue; + } + // 鏈�缁堢粨鏋� + List<Object> result = new ArrayList<>(); + // 閫氳繃\n灏嗗瓧绗︿覆鍒嗗壊涓鸿 + String[] aColumnY = processedData.replaceAll(" ", "").split("\n"); + Integer end = null; + // 閲囬泦鏁版嵁锛歒杞� + for (int i = 0; i < aColumnY.length; i++) { + String line = aColumnY[i].trim(); + if (line.isEmpty()) continue; // 璺宠繃绌鸿 + // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓虹┖鍒欐墽琛岋紝鍚屾椂璇ヨ鍖呭惈Y鍙傜収 + if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && line.contains(refery)) { + try { + // 鍙朰鍧愭爣鍊� + int y = getXOrY(v.get(configIndex).getY(), k, "Y"); + String[] aLineX = line.split(split); + for (int j = 0; j < aLineX.length; j++) { + if (aLineX[j].contains(refery)) { + if (i + y >= aColumnY.length) { + System.err.println(k + "锛歒杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠鏁版嵁琛屾暟: " + aColumnY.length + "锛屽皾璇曡闂: " + (i + y)); + continue; + } + String[] split1 = aColumnY[i + y].split(split); + if (j >= split1.length) { + System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + split1.length + "锛屽皾璇曡闂綅缃�: " + j); + continue; + } + result.add(split1[j]); + } + } + } catch (Exception e) { + System.err.println(k + "锛氬湪澶勭悊浠匶鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage()); + } + } + // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓嶄负绌哄垯鎵ц,姝ゅY瀹氬尯鍩� + else if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isNotEmpty(referx)) { + try { + // 鍙杧鐨勫�硷紝闃叉鎶ラ敊 + int x = getXOrY(v.get(configIndex).getX(), k, "X"); + // 鍙朰鍧愭爣鍊� + int y = getXOrY(v.get(configIndex).getY(), k, "Y"); + // 缂撳瓨Y鐨勭粨鏉熷�� + if (ObjectUtils.isEmpty(end) && line.contains(refery)) { + end = i + y; + } + // 鍒ゆ柇鏄惁鍦ㄥ弬鐓х墿涓鸿捣鐐癸紝Y鍧愭爣鍊间负鏈�缁堣寖鍥� + if (ObjectUtils.isNotEmpty(end) && i <= end) { + String[] aLineX = line.split(split); + for (int j = 0; j < aLineX.length; j++) { + if (aLineX[j].contains(referx)) { + if (j + x >= aLineX.length) { + System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + aLineX.length + "锛屽皾璇曡闂綅缃�: " + (j + x)); + continue; + } + result.add(aLineX[j + x]); + break; + } + } + } + } catch (Exception e) { + System.err.println(k + "锛氬湪澶勭悊XY鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage()); + } + } + // 濡傛灉X鍙傜収涓嶄负绌哄悓鏃惰琛屽寘鍚玐鍙傜収锛屽垯鎵ц涓嬮潰鐨勪唬鐮� + else if (line.contains(referx) && ObjectUtils.isEmpty(refery)) { + try { + String[] aLineX = line.split(split); + // 鍙杧鐨勫�硷紝闃叉鎶ラ敊 + int x = getXOrY(v.get(configIndex).getX(), k, "X"); + for (int j = 0; j < aLineX.length; j++) { + if (aLineX[j].contains(referx)) { + if (j + x >= aLineX.length) { + System.err.println(k + "锛歑杞村畾浣嶈秴鍑烘暟鎹寖鍥达紝褰撳墠琛屽厓绱犱釜鏁�: " + aLineX.length + "锛屽皾璇曡闂綅缃�: " + (j + x)); + continue; + } + result.add(aLineX[j + x]); + } + } + } catch (Exception e) { + System.err.println(k + "锛氬湪澶勭悊浠匵鍙傜収閫昏緫鏃跺嚭鐜板紓甯�: " + e.getMessage()); + } + } + } + // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁 + if (ObjectUtils.isNotEmpty(result)) { + list.addAll(result); + } + } + return list; + } /** * 濮旀墭缂栧彿涓庢牱鍝佺紪鍙烽兘涓虹┖鎵ц * @@ -637,6 +810,10 @@ */ public static List<Object> analyzeData(String data, List<DataConfig> v, String k, String split) { List<Object> list = new ArrayList<>(); + + // 棰勫鐞嗘暟鎹細绉婚櫎澶氫綑绌烘牸骞朵繚鐣欏叧閿垎闅旂 +// String processedData = data.replaceAll("\\s+", " ").trim(); + for (int config = 0; config < v.size(); config++) { // 鍙栦袱涓敤鎴烽厤缃殑鍙傜収鐗� String referx = getRefer(v.get(config).getReferx()); @@ -651,11 +828,14 @@ Integer end = null; // 閲囬泦鏁版嵁锛歒杞� for (int i = 0; i < aColumnY.length; i++) { +// String line = aColumnY[i].trim(); +// if (line.isEmpty()) continue; // 璺宠繃绌鸿 // 濡傛灉Y鍙傜収涓嶄负绌轰笌X鍙傜収涓虹┖鍒欐墽琛岋紝鍚屾椂璇ヨ鍖呭惈Y鍙傜収 - if (ObjectUtils.isNotEmpty(refery) && ObjectUtils.isEmpty(referx) && aColumnY[i].contains(refery)) { + 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); +// String[] aLineX = line.split(split); for (int j = 0; j < aLineX.length; j++) { if (aLineX[j].contains(refery)) { String[] split1 = new String[0]; @@ -665,7 +845,9 @@ throw new ErrorException(k + "锛歒杞村畾浣嶈秴鍑猴紒"); } try { +// System.out.println("----------"); result.add(split1[j]); + } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); } @@ -687,6 +869,7 @@ for (int j = 0; j < aLineX.length; j++) { if (aLineX[j].contains(referx)) { try { +// System.out.println("77777777777777"); result.add(aLineX[j + x]); } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); @@ -703,6 +886,7 @@ for (int j = 0; j < aLineX.length; j++) { if (aLineX[j].contains(referx)) { try { +// System.out.println("4444444444444444"); result.add(aLineX[j + x]); } catch (Exception e) { throw new ErrorException(k + "锛歑杞村畾浣嶈秴鍑猴紒"); @@ -711,7 +895,7 @@ } } } - // 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁 +// 闃叉璁$畻鍏紡鐨勬椂鍊欏嚭鐜帮細[null] 杩欑鏁版嵁 if (ObjectUtils.isNotEmpty(result)) { // String formatProcessing = getFormatProcessing(result); list.addAll(result); -- Gitblit v1.9.3