alibaba / alibaba/QLExpress

应该如何处理,主要是"{资本净额}*{核心资本}-{附属资本}"这个表达式它不支持,而我又要使用{}这个符号去进行计算

Open
#322 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.6k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

@Data
public class CalcDTO {
//主表达式
private String mainExpress;
//关联表达式
private Map paramExpress;
//参数值
private Map variables;
}

public class Calculator {

/**
* 计算表达式逻辑
*
* @param calcDTO
* @param isCache 是否缓存数据
* @return
* @throws Exception
*/
public static BigDecimal calculate(CalcDTO calcDTO, boolean isCache) throws Exception {
ExpressRunner runner = new ExpressRunner();
String expressStr = calcDTO.getMainExpress();

for (Map.Entry entry : calcDTO.getParamExpress().entrySet()) {
runner.addMacro(entry.getKey(), entry.getValue());
}

DefaultContext context = new DefaultContext();
for (Map.Entry entry : calcDTO.getVariables().entrySet()) {
context.put(entry.getKey(), entry.getValue());
}

List errorList = new ArrayList<>();
Object execute = runner.execute(expressStr, context, errorList, false, true);
return convertToBigDecimal(execute);
}

/**
* 将给定的Object实例安全地转换为BigDecimal类型。
* 支持常见的数值类型及其包装类,以及String类型。
* 对于不支持的类型或无法转换的情况,返回null。
*
* @param obj 待转换的Object实例
* @return 转换后的BigDecimal实例,或null
*/
private static BigDecimal convertToBigDecimal(Object obj) {
if (obj == null) {
return null;
}

try {
if (obj instanceof BigDecimal) {
return (BigDecimal) obj;
} else if (obj instanceof Number) {
return new BigDecimal(((Number) obj).doubleValue());
} else if (obj instanceof String) {
return new BigDecimal((String) obj);
} else {
// 不支持的类型,返回null
return null;
}
} catch (NumberFormatException e) {
// 字符串转换时可能抛出此异常,返回null
return null;
}
}
}

@Test
public void t2() throws Exception {
CalcDTO calcDTO = new CalcDTO();
Map paramExpress = new HashMap<>();
paramExpress.put("合计","{资本净额}*{核心资本}-{附属资本}");
calcDTO.setParamExpress(paramExpress);
Map variables = new HashMap<>();
variables.put("{资本净额}",Integer.valueOf("9998"));
variables.put("{核心资本}",Integer.valueOf("9998"));
variables.put("{附属资本}",Integer.valueOf("9998"));
calcDTO.setVariables(variables);
calcDTO.setMainExpress("合计");
BigDecimal calculate = Calculator.calculate(calcDTO, false);
System.out.println(calculate);

}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the provided Calculator.calculate entry point and the t2 reproducer, focusing on how ExpressRunner handles the macro expression and variable names containing braces. Confirm the current failure and determine what behavior is expected for the `{资本净额}*{核心资本}-{附属资本}` expression; done means the reproducer evaluates successfully without breaking existing expression handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.