Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
XiaoCong16 opened this issue Apr 18, 2024 · 1 comment

Comments

@XiaoCong16
Copy link

@DaTa
public class CalcDTO {
//主表达式
private String mainExpress;
//关联表达式
private Map<String,String> paramExpress;
//参数值
private Map<String, Object> 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<String, String> entry : calcDTO.getParamExpress().entrySet()) {
        runner.addMacro(entry.getKey(), entry.getValue());
    }

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

    List<String> 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<String,String> paramExpress = new HashMap<>();
paramExpress.put("合计","{资本净额}*{核心资本}-{附属资本}");
calcDTO.setParamExpress(paramExpress);
Map<String,Object> 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);

}
@taokan
Copy link
Contributor

taokan commented Jun 16, 2024

是不支持,你们在服务端包装一下把 "{资本净额} * {核心资本} - {附属资本}" -> qlexpress "a * b - c"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants