活动表达式
This commit is contained in:
parent
b19280f77f
commit
09f1056783
@ -103,6 +103,11 @@
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-log</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.udojava</groupId>
|
||||
<artifactId>EvalEx</artifactId>
|
||||
<version>2.7</version>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.apache.poi</groupId>-->
|
||||
<!-- <artifactId>poi-ooxml</artifactId>-->
|
||||
|
||||
@ -1,50 +1,49 @@
|
||||
package com.xhpc.order.service.impl;
|
||||
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import com.udojava.evalex.Expression;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StreamTokenizer;
|
||||
import java.io.StringReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Calc {
|
||||
|
||||
private static String regex = "^[a-zA-Z]+$";
|
||||
private static Pattern pattern = Pattern.compile(regex);
|
||||
|
||||
public static void main(String[] argv) {
|
||||
|
||||
test("-1 + 2", 1);
|
||||
test("2 * (3 * (4 + 5)) / 9 / 6", 1);
|
||||
// test("max(1, 2 + 3)", 5);
|
||||
// test("min(3 - 1, 1)", 1);
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("totalPower", new BigDecimal("400000.000000"));
|
||||
StandardEvaluationContext stdContext = new StandardEvaluationContext();
|
||||
stdContext.setVariables(map);
|
||||
String ruleExpression = "#totalPower >= 500000 ? 'e1' : 'e2'";
|
||||
// Evaluate the SpEL expression
|
||||
ExpressionParser parser = new SpelExpressionParser();
|
||||
Expression expression = parser.parseExpression(ruleExpression);
|
||||
String rs = expression.getValue(stdContext, String.class);
|
||||
Map<String, Double> map = new HashMap<>();
|
||||
map.put("T", Double.valueOf(0.7));
|
||||
String ruleExpression = "T + 10%";
|
||||
double rs = eval(ruleExpression, map);
|
||||
System.out.println(rs);
|
||||
ruleExpression = "#totalPower + 500000";
|
||||
expression = parser.parseExpression(ruleExpression);
|
||||
rs = expression.getValue(stdContext, String.class);
|
||||
System.out.println(rs);
|
||||
ruleExpression = "#totalPower - (0.03% * 2%)";
|
||||
// expression = parser.parseExpression(ruleExpression);
|
||||
// rs = expression.getValue(stdContext, String.class);
|
||||
// System.out.println(rs);
|
||||
String[] split = ruleExpression.split("[()*/\\-+?]");
|
||||
for (String splitValue : split) {
|
||||
System.out.println(splitValue);
|
||||
System.out.println(0.7 + 0.1);
|
||||
}
|
||||
|
||||
public static double eval(String ruleExpression, Map<String, Double> params) {
|
||||
|
||||
String[] split = ruleExpression.split("[()*/\\-+?]");
|
||||
for (String splitValue : split) {
|
||||
if (splitValue.endsWith("%")) {
|
||||
BigDecimal rate = new BigDecimal(splitValue.trim().replace("%", ""));
|
||||
ruleExpression = ruleExpression.replace(splitValue, rate.divide(BigDecimal.valueOf(100)).setScale(2,
|
||||
RoundingMode.HALF_UP).toString());
|
||||
} else {
|
||||
if (pattern.matcher(splitValue.trim()).matches()) {
|
||||
ruleExpression = ruleExpression.replace(splitValue, params.get(splitValue.trim()).toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression expression = new Expression(ruleExpression);
|
||||
expression.setPrecision(3);
|
||||
return expression.eval().doubleValue();
|
||||
}
|
||||
|
||||
private static void test(String s, double x) {
|
||||
@ -78,7 +77,6 @@ public class Calc {
|
||||
* mulop = "*" | "/"
|
||||
* </code>
|
||||
*
|
||||
* @param s the string to be evaluated.
|
||||
* @see <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser">
|
||||
* Recursive descent parser</a>.
|
||||
*/
|
||||
|
||||
@ -78,7 +78,7 @@ public class OrderDataLogic implements ServiceLogic {
|
||||
log.error(">>INVALID order [{}] detected. MUST check the system or [{}].<<", orderNo, gunkey);
|
||||
}
|
||||
String resultStr = "6815".concat(req.get("seqhex").toString()).concat("0040").concat(orderNo);
|
||||
if (r != null && r.getCode() == 200) {
|
||||
if ((r != null && r.getCode() == 200) || (r != null && r.getMsg() != null && r.getMsg().contains("重复结算"))) {
|
||||
resultStr = resultStr.concat(ServiceResult.HEX_00);
|
||||
resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
|
||||
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user