package com.changhong.epc.count.service.system.performer.impl; import com.changhong.epc.bean.tenant.system.ErrSystemMsg; import com.changhong.epc.count.service.exception.IEMRuntimeMasterException; import com.changhong.epc.count.service.system.performer.IFormulaHandle; import com.googlecode.aviator.AviatorEvaluator; import com.googlecode.aviator.exception.ExpressionRuntimeException; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; /** * 公式解析-抽象类 */ @Slf4j @Service("formulaHandle") @Scope("prototype") public class FormulaHandle implements IFormulaHandle { private FunctionParam functionParam; private Map env; /** * 错误信息 */ @Getter private List errSystemMsgs = new ArrayList<>(); /** * @param * @return * */ @Override public Object execute(FunctionParam param){ functionParam = param; // 1. 准备参数 env = functionParam.getEnv(); Object exeExpression = exeExpression(); // 4. 返回结果 return exeExpression; } @Override public Object exeResult(Object execute, String formula) { if(StringUtils.isBlank(formula)){ return execute; } log.debug("计算系统变量后再次处理:值{},公式{}", execute, formula); String[] formulas = formula.trim().split("\\s+"); if("IN".equalsIgnoreCase(formulas[0])){ String[] vals = formulas[1].split(","); return ObjectUtil.or(Objects.toString(execute, ""), vals); } String lr = execute + " " + formula; Object res = AviatorEvaluator.execute(lr); return res; } private Object exeExpression() { log.debug("公式表达式\t:{}", functionParam.getExpression()); log.debug("公式上下文\t:{}", JSONTool.toJson(env)); Object val = null; try { val = AviatorEvaluator.execute(functionParam.getExpression(), env); }catch (ExpressionRuntimeException e){ log.error(e.getMessage(), e); if(e.getCause() instanceof IEMRuntimeMasterException){ errSystemMsgs.add(((IEMRuntimeMasterException) e.getCause()).getErrSystemMsg()); } } log.debug("公式计算结果\t:{}", val); return val; } @Override public T result(Class c) { // TODO Auto-generated method stub return null; } @Override public Boolean resultBoolean(Object execute) { log.debug("转换为boolean结果:{}", execute); if(execute instanceof Boolean){ return (Boolean) execute; }else if(execute instanceof String){ return execute.equals("true") ? true : false; } log.debug("未知类型无法转化为 boolean--> " + execute); return false; } @Override public Double resultDouble(Object execute) { if(execute instanceof Double){ return (Double) execute; }else if(execute instanceof String){ return Double.valueOf((String)execute); }else if(execute instanceof Integer){ return Double.valueOf((Integer)execute); }else if(execute instanceof Long){ return Double.valueOf((Long)execute); } log.debug("未知类型无法转化为 Double--> " + execute); return 0D; } }