From ce7e6574a1f9942fc090d989b52aceaa11ba8f3e Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期一, 15 四月 2024 21:54:08 +0800 Subject: [PATCH] 1.得分计算部分调整; 2.大屏得分计算; --- ruoyi-common/src/main/java/com/ruoyi/common/utils/CalculateUtil.java | 40 +++++++++++++++++++++++++++++++++------- 1 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/CalculateUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/CalculateUtil.java index 300eb79..585ee1e 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/CalculateUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/CalculateUtil.java @@ -1,18 +1,22 @@ package com.ruoyi.common.utils; -import org.apache.commons.jexl3.*; - import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.jexl3.JexlBuilder; +import org.apache.commons.jexl3.JexlContext; +import org.apache.commons.jexl3.JexlEngine; +import org.apache.commons.jexl3.JexlExpression; +import org.apache.commons.jexl3.MapContext; /** * @author mitao * @date 2024/3/27 */ public class CalculateUtil { - public static Map<String,Integer> getFieldsAndValue(String rule) { + + public static Map<String, Integer> getFieldsAndValue(String rule) { // 正则表达式模式,匹配形如 "fieldName:value" 的字符串 Pattern pattern = Pattern.compile("\\b(\\w+)_(\\d+)\\b"); Matcher matcher = pattern.matcher(rule); @@ -21,11 +25,12 @@ while (matcher.find()) { String fieldName = matcher.group(1); int value = Integer.parseInt(matcher.group(2)); - map.put(fieldName+"_"+value, value); + map.put(fieldName + "_" + value, value); } return map; } - public static double calculate(String expression,Map<String,Object> value) { + + public static double calculate(String expression, Map<String, Object> value) { expression = formatExpression(expression); // 创建 JEXL 引擎 JexlEngine jexl = new JexlBuilder().create(); @@ -35,8 +40,24 @@ // 创建上下文 JexlContext context = new MapContext(); for (Map.Entry<String, Object> stringObjectEntry : value.entrySet()) { - context.set(stringObjectEntry.getKey(),stringObjectEntry.getValue()); + context.set(stringObjectEntry.getKey(), stringObjectEntry.getValue()); } + // 执行计算 + Object result = exp.evaluate(context); + + // 输出结果 + System.out.println("Result: " + result); + return Double.parseDouble(result.toString()); + } + + public static double calculate(String expression) { + // 创建 JEXL 引擎 + JexlEngine jexl = new JexlBuilder().create(); + + // 创建表达式对象 + JexlExpression exp = jexl.createExpression(expression); + // 创建上下文 + JexlContext context = new MapContext(); // 执行计算 Object result = exp.evaluate(context); @@ -51,7 +72,12 @@ .replaceAll("÷", "/") .replaceAll("(", "(") .replaceAll(")", ")") - .replaceAll("+","+") + .replaceAll("+", "+") .replaceAll("-", "-"); } + + public static void main(String[] args) { + String string = "field_3 * field_2"; + System.out.println(getFieldsAndValue(string)); + } } -- Gitblit v1.7.1