evaluate java_用Java进行动态公式处理

这篇博客介绍了如何使用Java进行动态公式处理。通过`ExpressionUtil`类,作者展示了如何根据实体类和JPA注解获取表名,进而从数据库查询公式,并对List中的每个对象计算公式值。文章还详细讲解了`workOutListBean`方法,该方法处理包含公式的bean列表,以及处理单个Map对象的辅助方法。此外,还包括了条件判断和表达式计算的相关逻辑。
摘要由CSDN通过智能技术生成

/*** Created by Teddy on 2017/10/23.*/

public classExpressionUtil {/*** 计算List配置了公式的值

*@parambeanList

*@return

*/

public static List workOutListBean(List extends BaseEntity>beanList){if(CollectionUtils.isEmpty(beanList)){returnCollections.EMPTY_LIST;

}else{

Class extends BaseEntity> beanClass = beanList.get(0).getClass();

Table table= beanClass.getAnnotation(Table.class);//在实体中通过JPA注解获取表名

String tableCode =table.name();if(StringUtil.isEmpty(tableCode)){

System.out.println("无法计算,该bean没有表名信息");

}

List mapList =BeanUtil.convertBeansToMaps(beanList);

FormulaService formulaService= SpringContext.getBean(FormulaService.class);

List formulas =formulaService.queryByTableCode(tableCode);for(Map map : mapList) {

JEP jep=getJEP(map);for(Formula formula : formulas) {

String conditionExpr=formula.getConditionExpr();

String formulaExpr=formula.getFormulaExpr();

String evaluate=formula.getEvaluate();

workOutKey(jep,map,conditionExpr,formulaExpr,evaluate);

}

}returnmapList;

}

}/*** 非动态配置公式方式

*@parammapList

*@paramconditionExpr

*@paramformulaExpr

*@paramevaluate*/

public static void workOutListMap(ListmapList,String conditionExpr,String formulaExpr, String evaluate){for(Map map : mapList) {

JEP jep=getJEP(map);

workOutKey(jep,map,conditionExpr,formulaExpr,evaluate);

}

}/*** 计算出表达式并填充

*@paramjep

*@parammap

*@paramconditionExpr

*@paramformulaExpr

*@paramevaluate*/

private static voidworkOutKey(JEP jep,Map map,String conditionExpr, String formulaExpr, String evaluate){//如果没有条件

if(StringUtil.isEmpty(conditionExpr)){

map.put(evaluate,workOutSingle(jep,formulaExpr));//如果有条件 且条件为true

}else if(workOutBool(jep,conditionExpr)){

map.put(evaluate,workOutSingle(jep,formulaExpr));

}

}/*** 判断条件表达式

*@paramjep

*@paramexpression

*@return

*/

private static booleanworkOutBool(JEP jep,String expression){return (Double)workOutSingle(jep,expression) > 0;

}/*** 计算表达式的值

*@paramjep

*@paramexpression

*@return

*/

private staticObject workOutSingle(JEP jep,String expression){

Object result= null;try { //执行

Node parse =jep.parse(expression);

result=jep.evaluate(parse);

}catch(ParseException e) {throw new BaseRunTimeException("公式表达式解析失败",e);

}if(result == null){throw new BaseRunTimeException("公式表达式解析失败");

}returnresult;

}/*** 获取填充好变量的JEP对象

*@paramparam

*@return

*/

private staticJEP getJEP(Map param){

JEP jep= newJEP();

Set set =param.entrySet();for(Map.Entry entry : set) {

Object entryValue=entry.getValue();

String entryKey=(String) entry.getKey();

jep.addVariable(entryKey, entryValue);

}returnjep;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值