| | |
| | | import com.yuanchu.mom.pojo.DataConfig; |
| | | import com.yuanchu.mom.pojo.Device; |
| | | |
| | | import javax.script.ScriptEngine; |
| | | import javax.script.ScriptEngineManager; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | private static final String splitIdentifier = "@-@"; // 自定义唯一标识分割符 |
| | | |
| | | public static final String frequency = "frequency"; |
| | | |
| | | /** |
| | | * 数采入口 |
| | | * |
| | |
| | | * @return |
| | | */ |
| | | public static Map<String, String> dataAcquisitionEntrance(List<DataConfig> dataConfig, Device device, String entrustCode, String sampleCode, String ip) { |
| | | /** |
| | | * filePath 文件采集路径 |
| | | * fileExtension 文件后缀 |
| | | * entrustCode 委托编号 |
| | | * sampleCode 样品编号 |
| | | * mdbEntrustCode mdb文件需要:委托编号字段 |
| | | * mdbSampleCode mdb文件需要:样品编号字段 |
| | | */ |
| | | String http = HTTP + ip + GETFILE + |
| | | "?filePath=" + device.getCollectUrl() + |
| | | "&fileExtension=" + device.getFileType() + |
| | | "&entrustCode=" + entrustCode + |
| | | "&sampleCode=" + sampleCode; |
| | | "&sampleCode=" + sampleCode + |
| | | "&mdbEntrustCode=" + device.getMdbEntrustCode() + |
| | | "&mdbSampleCode=" + device.getMdbSampleCode(); |
| | | String result = null; |
| | | try { |
| | | result = HttpUtil.get(http); |
| | |
| | | map = analysisList(data, userMap); |
| | | break; |
| | | case ".mdb": |
| | | map = analysisMdb(data, userMap, entrustCode, sampleCode); |
| | | map = analysisMdb(data, userMap); |
| | | break; |
| | | case ".db": |
| | | map = analysisDb(data, userMap, entrustCode, sampleCode); |
| | | map = analysisDb(data, userMap); |
| | | break; |
| | | case ".png": |
| | | map = readPngString(data, userMap); |
| | |
| | | * @param dataConfig |
| | | * @return |
| | | */ |
| | | private static Map<String, String> analysisDb(String data, Map<String, List<DataConfig>> dataConfig, String entrustCode, String sampleCode) { |
| | | 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()); |
| | | Map<String, String> map = new HashMap<>(); |
| | | if (jsonObject.isEmpty()) { |
| | | return map; |
| | | } |
| | | JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString()); |
| | | dataConfig.forEach((k, v) -> { |
| | | List<Object> list = new ArrayList<>(); |
| | | for (int config = 0; config < v.size(); config++) { |
| | | String referx = getRefer(v.get(config).getReferx()); |
| | | for (int i = 0; i < dataList.size(); i++) { |
| | | JSONObject jsonObject1 = JSON.parseObject(dataList.get(i).toString()); |
| | | if (entrustCode.equals(jsonObject1.get("ExtInfo_Value1"))) { |
| | | Object o = jsonObject1.get(referx); |
| | | if (ObjectUtils.isNotEmpty(o)) { |
| | | list.add(o); |
| | | } |
| | | Object o = jsonObject1.get(referx); |
| | | if (ObjectUtils.isNotEmpty(o)) { |
| | | list.add(o); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 需要通过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, String> analysisMdb(String data, Map<String, List<DataConfig>> dataConfig) { |
| | | JSONObject jsonObject = JSON.parseObject(data); |
| | | JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString()); |
| | | Map<String, String> map = new HashMap<>(); |
| | | if (jsonObject.isEmpty()) { |
| | | return map; |
| | | } |
| | | JSONArray dataList = JSONArray.parseArray(jsonObject.get("data").toString()); |
| | | dataConfig.forEach((k, v) -> { |
| | | List<Object> list = new ArrayList<>(); |
| | | for (int config = 0; config < v.size(); config++) { |
| | | String referx = getRefer(v.get(config).getReferx()); |
| | | 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(referx); |
| | | if (ObjectUtils.isNotEmpty(o)) { |
| | | list.add(o); |
| | | } |
| | | Object o = jsonObject1.get(referx); |
| | | if (ObjectUtils.isNotEmpty(o)) { |
| | | list.add(o); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | // 如果不为空,进行公式计算 |
| | | if (ObjectUtils.isNotEmpty(dataConfig.getFormula())) { |
| | | String formula = dataConfig.getFormula(); |
| | | //首先将list转换为bigdecmic |
| | | List<BigDecimal> bigDecimalList = list.stream() |
| | | .map(obj -> { |
| | | return new BigDecimal((obj).toString()); |
| | | }).collect(Collectors.toList()); |
| | | System.out.println(bigDecimalList); |
| | | |
| | | return null; |
| | | //将中文的(转换英文的()) |
| | | formula = formula.replace("(", "(") |
| | | .replace(")", ")") |
| | | .replace(",", ","); |
| | | //然后提取英文字母,全部转换为大写 |
| | | String[] strs = formula.split("[^A-Za-z]+"); |
| | | String upperStr = ""; |
| | | for (String str : strs) { |
| | | upperStr += str.toUpperCase(); |
| | | } |
| | | if (upperStr.equals("")) { |
| | | throw new ErrorException(formula + "里面的公式没有函数"); |
| | | } |
| | | //然后获取最外面括号里面的值,再根据","分割 |
| | | 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 inner = formula.substring(start + 1, end); |
| | | List<String> arguments = new ArrayList<>(); |
| | | int depth = 0; |
| | | StringBuilder currentArgument = new StringBuilder(); |
| | | for ( |
| | | char c : inner.toCharArray()) { |
| | | if (c == ',' && depth == 0) { |
| | | arguments.add(currentArgument.toString()); |
| | | currentArgument.setLength(0); |
| | | } else { |
| | | if (c == '(') { |
| | | depth++; |
| | | } else if (c == ')') { |
| | | depth--; |
| | | } |
| | | 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("\\b\\d+\\b"); |
| | | Matcher matcher = pattern.matcher(expr); |
| | | StringBuffer sb = new StringBuffer(); |
| | | while (matcher.find()) { |
| | | int index = Integer.parseInt(matcher.group()) - 1; |
| | | if (index < bigDecimalList.size()) { |
| | | matcher.appendReplacement(sb, bigDecimalList.get(index).toString()); |
| | | } else { |
| | | throw new ErrorException("表达式中的下标 " + 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; |
| | | 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; |
| | | case "RANGE": |
| | | finalResult = results.stream().max(BigDecimal::compareTo) |
| | | .orElseThrow(() -> new IllegalArgumentException("无法计算RANGE值")) |
| | | .subtract(results.stream().min(BigDecimal::compareTo) |
| | | .orElseThrow(() -> new IllegalArgumentException("无法计算RANGE值"))); |
| | | break; |
| | | default: |
| | | throw new UnsupportedOperationException("暂不支持函数: " + upperStr); |
| | | } |
| | | System.out.println(results); |
| | | System.out.println("计算结果: " + finalResult); |
| | | |
| | | return finalResult.toString(); |
| | | // 否则:没有公式代表不需要计算,直接提取List里面的数据 |
| | | } else { |
| | | // 这里只会取列表第一个数据 |
| | |
| | | |
| | | public static String getFormatProcessing(String value) { |
| | | value = value.replaceAll("%", ""); |
| | | if(value.contains("=")){ |
| | | if (value.contains("=")) { |
| | | String[] split = value.split("="); |
| | | return split[split.length - 1]; |
| | | } else if(value.contains(":")) { |
| | | } else if (value.contains(":")) { |
| | | String[] split = value.split(":"); |
| | | return split[split.length - 1]; |
| | | } else { |