活动表达式
This commit is contained in:
parent
b19280f77f
commit
09f1056783
@ -103,11 +103,16 @@
|
|||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-log</artifactId>
|
<artifactId>ruoyi-common-log</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.udojava</groupId>
|
||||||
|
<artifactId>EvalEx</artifactId>
|
||||||
|
<version>2.7</version>
|
||||||
|
</dependency>
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.apache.poi</groupId>-->
|
<!-- <groupId>org.apache.poi</groupId>-->
|
||||||
<!-- <artifactId>poi-ooxml</artifactId>-->
|
<!-- <artifactId>poi-ooxml</artifactId>-->
|
||||||
<!-- <version>3.9</version>-->
|
<!-- <version>3.9</version>-->
|
||||||
<!-- </dependency>-->
|
<!-- </dependency>-->
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -1,50 +1,49 @@
|
|||||||
package com.xhpc.order.service.impl;
|
package com.xhpc.order.service.impl;
|
||||||
|
|
||||||
import org.springframework.expression.Expression;
|
import com.udojava.evalex.Expression;
|
||||||
import org.springframework.expression.ExpressionParser;
|
|
||||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StreamTokenizer;
|
import java.io.StreamTokenizer;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Calc {
|
public class Calc {
|
||||||
|
|
||||||
|
private static String regex = "^[a-zA-Z]+$";
|
||||||
|
private static Pattern pattern = Pattern.compile(regex);
|
||||||
|
|
||||||
public static void main(String[] argv) {
|
public static void main(String[] argv) {
|
||||||
|
|
||||||
test("-1 + 2", 1);
|
Map<String, Double> map = new HashMap<>();
|
||||||
test("2 * (3 * (4 + 5)) / 9 / 6", 1);
|
map.put("T", Double.valueOf(0.7));
|
||||||
// test("max(1, 2 + 3)", 5);
|
String ruleExpression = "T + 10%";
|
||||||
// test("min(3 - 1, 1)", 1);
|
double rs = eval(ruleExpression, map);
|
||||||
|
System.out.println(rs);
|
||||||
|
System.out.println(0.7 + 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double eval(String ruleExpression, Map<String, Double> params) {
|
||||||
|
|
||||||
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);
|
|
||||||
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("[()*/\\-+?]");
|
String[] split = ruleExpression.split("[()*/\\-+?]");
|
||||||
for (String splitValue : split) {
|
for (String splitValue : split) {
|
||||||
System.out.println(splitValue);
|
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) {
|
private static void test(String s, double x) {
|
||||||
@ -78,7 +77,6 @@ public class Calc {
|
|||||||
* mulop = "*" | "/"
|
* mulop = "*" | "/"
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* @param s the string to be evaluated.
|
|
||||||
* @see <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser">
|
* @see <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser">
|
||||||
* Recursive descent parser</a>.
|
* 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);
|
log.error(">>INVALID order [{}] detected. MUST check the system or [{}].<<", orderNo, gunkey);
|
||||||
}
|
}
|
||||||
String resultStr = "6815".concat(req.get("seqhex").toString()).concat("0040").concat(orderNo);
|
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(ServiceResult.HEX_00);
|
||||||
resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
|
resultStr = resultStr.concat(CRCCalculator.calcCrc(resultStr));
|
||||||
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
|
return new ServiceResult(HexUtils.toBytes(resultStr), ServiceResult.OK);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user