package com.changhong.epc.count.service.count.execution.impl; import com.changhong.epc.bean.count.MasterInfo; import com.changhong.epc.bean.tenant.norm.AppStanderDef; import com.changhong.epc.bean.tenant.norm.extend.AppStanderExtend; import com.changhong.epc.bean.tenant.norm.way.EleType; import com.changhong.epc.constter.count.nomr.CountCoreKey; import com.changhong.epc.count.service.count.execution.DefCountCore; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Set; @Slf4j @Service("countCore") public class DefCountCoreImpl implements DefCountCore, CountCoreKey { @Setter @Getter private AppStanderExtend asd; private static final ScriptEngine JS = new ScriptEngineManager().getEngineByName("JavaScript"); /** * 开始计算 * @Title: count * @Description: TODO[开始计算] * @param * @return void 返回类型 * @throws */ public Boolean countCore(AppStanderExtend row, Set all) { asd = row; /* * 1.类型为补助金额 */ if(Objects.equals(row.getEleType(), EleType.JE)){ /* * 1.1如果匹配不成功则继续循环 */ if(countRow(row, all)){ /* * 1.1.1 赋值金额并跳出循环 */ return Boolean.TRUE; } else{ return Boolean.FALSE; } } /* * 2.条件及标准 */ else{ /* * 2.1 匹配不成功则匹配下一行 */ if(countRow(row, all)){ return Boolean.TRUE; } else{ return Boolean.FALSE; } } } protected Boolean countRow(AppStanderDef row, Set all) { boolean success = false; for (MasterInfo mi : all) { // 没有主数据直接忽略 匹配另一个 if(StringUtils.isBlank(mi.getMdCode())){ continue ; } try{ if(compareToVal(mi.getMeVal(), mi.getMdCode(), mi.getMeCode())){ success = Boolean.TRUE; } }catch(IllegalArgumentException ille){ success = false; } // 如果计算成功,直接返回成功 if(success){ break; } } if(success){ return Boolean.TRUE; } log.debug("匹配失败:\n\trow:{}, \n\tmaster:{}", JSONTool.toJson(row), JSONTool.toJson(all)); return Boolean.FALSE; } /** * 匹配维度 * @Title: compareToVal * @Description: [匹配维度] * @param @param valCode 值code * @param @param mdCode 主数据code * @param @param elIden 元素标识符 * @param @return 设定文件 * @return boolean 返回类型 * @throws */ public boolean compareToVal(Object valCode, String mdCode, String meCode){ /* 金额,数据code存在elIden中 */ if(Objects.equals(asd.getEleType(), EleType.JE)){ if(asd.getElIden() == null || Objects.equals("", asd.getElIden())){ return Boolean.TRUE; }else{ return Objects.equals(asd.getElIden(), valCode) && Objects.equals(asd.getMasDefineCode(), mdCode); } }else if(asd.getDefType() == _SYSTEM_MATH){ if(asd.getElIden() == null || Objects.equals("", asd.getElIden())){ return Boolean.TRUE; }else{ return Objects.equals(asd.getElIden(), valCode); } } else{ /* 条件、标准code存在values中 */ return compareApp(valCode, mdCode, meCode); } } private boolean compareApp(Object valCode, String mdCode, String meCode){ /* 公式类型 */ if(asd.getDefType() == CRITERIA_FORMULA_TYPE){ log.debug("公式参数{}, {}, {}", asd.getMasDefineCode(), mdCode, valCode); /* 计算公式 */ return Objects.equals(asd.getMasDefineCode(), mdCode) && eval(getVal(asd.getDefValue(), (String)valCode), Boolean.class); }/* 条件,标准 */ else{ log.debug("条件参数{}, {}, {}, {}", asd.getMasDefineCode(), asd.getDefValue(), mdCode, valCode); Boolean success; /* 补助标准==null 返回true */ if(ObjectUtil.empty(asd.getMasDefineCode())){ success = Boolean.TRUE; }else{ success = findStrIsExist(valCode) && Objects.equals(asd.getMasDefineCode(), mdCode); } /* 补助标准 */ if(isNormType() && !success){ throw new IllegalArgumentException(); } return success; } } private boolean findStrIsExist(Object obj){ if(StringUtils.isBlank(asd.getDefValue())) return true; List strlist = new ArrayList<>(); if(obj instanceof String) strlist.add((String) obj); if(obj instanceof List) strlist.addAll((List)obj); for (String str : strlist) { String[] split = asd.getDefValue().split(","); for (String bz : split) { if(Objects.equals(bz, str)) return true; } } return false; } public boolean isNormType(){ return Objects.equals(asd.getType(), EleType.BZ.toString()); } public static final String getVal(String string, String data){ log.debug(String.format("循环vals:%s", string)); /* 去掉判断类型 */ String stringNotType = string.substring(0, string.lastIndexOf(' ')); /* 去掉主数据code */ return stringNotType.replaceAll(FLAG_SYMBOL, data); } public static final T eval(String formula, Class clazz){ log.debug(String.format("js执行公式:'%s'", formula)); T result = null; try { result = (T) JS.eval(formula); return result; } catch (ScriptException e) { log.error(e.getMessage(), e); return null; }finally { log.debug(String.format("js执行公式:'%s'-执行结果:'%s'%n", formula, Objects.toString(result))); } } }