weaver-hrm-salary/src/com/engine/salary/service/impl/FormulaRunServiceImpl.java

125 lines
4.9 KiB
Java
Raw Normal View History

2022-04-15 13:54:47 +08:00
package com.engine.salary.service.impl;
import com.engine.core.impl.Service;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salaryformula.po.FormulaVar;
2022-04-27 17:20:33 +08:00
import com.engine.salary.enums.salaryformula.ReferenceTypeEnum;
2022-04-20 19:23:16 +08:00
import com.engine.salary.formlua.entity.parameter.DataType;
2022-04-15 13:54:47 +08:00
import com.engine.salary.service.FormulaRunService;
2022-04-29 14:56:33 +08:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
2022-04-18 14:45:12 +08:00
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import lombok.extern.slf4j.Slf4j;
2022-04-29 14:56:33 +08:00
import org.apache.commons.lang3.StringUtils;
2022-05-07 15:20:39 +08:00
import org.apache.commons.lang3.math.NumberUtils;
2022-04-29 14:56:33 +08:00
import weaver.conn.RecordSet;
2022-08-03 10:08:43 +08:00
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;
2022-04-15 13:54:47 +08:00
2022-04-20 11:17:25 +08:00
import java.math.BigDecimal;
2022-04-15 13:54:47 +08:00
import java.util.List;
2022-04-18 14:45:12 +08:00
@Slf4j
2022-04-15 13:54:47 +08:00
public class FormulaRunServiceImpl extends Service implements FormulaRunService {
2022-04-24 17:42:32 +08:00
2022-04-29 14:56:33 +08:00
private static ExpressRunner runner = new ExpressRunner(true, false);
private static final ObjectMapper objectMapper = new ObjectMapper();
2022-04-27 17:20:33 +08:00
2022-08-03 10:08:43 +08:00
private final BaseBean baseBean = new BaseBean();
private final Boolean isLog = "true".equals(baseBean.getPropValue("hrmSalary", "log"));
2022-04-24 17:42:32 +08:00
2022-04-15 13:54:47 +08:00
@Override
2022-05-08 12:34:05 +08:00
public Object run(ExpressFormula expressFormula, List<FormulaVar> formulaVars, DataCollectionEmployee simpleEmployee) throws Exception {
2022-04-15 13:54:47 +08:00
2022-04-27 17:20:33 +08:00
if (ReferenceTypeEnum.parseByValue(expressFormula.getReferenceType()) == ReferenceTypeEnum.FORMULA) {
return runFormula(expressFormula, formulaVars);
}
2022-04-29 14:56:33 +08:00
if (ReferenceTypeEnum.parseByValue(expressFormula.getReferenceType()) == ReferenceTypeEnum.SQL) {
return runSQL(expressFormula, formulaVars);
}
log.error("express execute fail, {} not in ReferenceTypeEnum ", expressFormula.getReferenceType());
return StringUtils.EMPTY;
}
private String runSQL(ExpressFormula expressFormula, List<FormulaVar> formulaVars) {
2022-08-03 10:08:43 +08:00
if (isLog) {
2022-11-04 10:42:05 +08:00
log.info("SQL ExpressFormula {}", expressFormula);
2022-08-03 10:08:43 +08:00
}
//解析配置,获取返回值、数据源
2022-05-08 12:34:05 +08:00
String extendParam = expressFormula.getExtendParam();
String sqlReturnKey = "";
2022-08-03 10:08:43 +08:00
String datasourceId = "";
2022-04-29 14:56:33 +08:00
try {
2022-05-08 12:34:05 +08:00
JsonNode jsonNode = objectMapper.readTree(extendParam);
2022-08-03 10:08:43 +08:00
//返回值配置
2022-05-08 12:34:05 +08:00
JsonNode sqlReturnKeyNode = jsonNode.get("sqlReturnKey");
if (sqlReturnKeyNode != null) {
2022-08-11 14:12:37 +08:00
sqlReturnKey = sqlReturnKeyNode.asText().trim();
2022-04-29 14:56:33 +08:00
}
2022-08-03 10:08:43 +08:00
//数据源配置
JsonNode datasourceNode = jsonNode.get("datasource");
if (datasourceNode != null) {
JsonNode datasourceIdNode = datasourceNode.get("datasourceId");
if (datasourceIdNode != null) {
datasourceId = datasourceIdNode.asText();
}
}
2022-05-08 12:34:05 +08:00
} catch (JsonProcessingException e) {
log.error("express execute fail, sql extendParam parse fail", e);
}
2022-04-27 17:20:33 +08:00
2022-08-03 10:08:43 +08:00
//解析sql
String sql = expressFormula.getFormulaRunScript();
2022-05-08 12:34:05 +08:00
for (FormulaVar formulaVar : formulaVars) {
2022-07-04 13:44:17 +08:00
sql = sql.replaceAll(formulaVar.getFieldId(), "'" + formulaVar.getContent() + "'");
2022-05-08 12:34:05 +08:00
}
2022-08-03 10:08:43 +08:00
if (isLog) {
2022-11-04 10:42:05 +08:00
log.info("ExpressFormula sql run {}", sql);
2022-08-03 10:08:43 +08:00
}
//外部数据源
if (StringUtils.isNotBlank(datasourceId)) {
RecordSetDataSource rs = new RecordSetDataSource(datasourceId);
if (rs.executeSql(sql)) {
if (rs.next()) {
return rs.getString(sqlReturnKey);
}
}
} else {
RecordSet rs = new RecordSet();
if (rs.execute(sql)) {
if (rs.next()) {
return rs.getString(sqlReturnKey);
}
2022-04-29 14:56:33 +08:00
}
}
return StringUtils.EMPTY;
2022-04-27 17:20:33 +08:00
}
2022-05-08 12:34:05 +08:00
private Object runFormula(ExpressFormula expressFormula, List<FormulaVar> formulaVars) throws Exception {
2022-08-03 10:08:43 +08:00
if (isLog) {
2022-11-04 10:42:05 +08:00
log.info("FORMULA ExpressFormula {} {}", expressFormula.getFormula(), expressFormula.getFormulaRunScript());
2022-08-03 10:08:43 +08:00
}
2022-05-08 12:34:05 +08:00
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
formulaVars.forEach(v -> {
2022-11-04 10:42:05 +08:00
if (isLog) {
log.info("FORMULA formulaVar {} - {}", v.getFieldId(), v.getContent());
}
2022-05-08 12:34:05 +08:00
if (DataType.NUMBER.equals(v.getFieldType()) && NumberUtils.isCreatable(v.getContent())) {
context.put(v.getFieldId(), new BigDecimal(v.getContent()));
} else {
context.put(v.getFieldId(), v.getContent());
}
});
String formula = expressFormula.getFormulaRunScript();
return runner.execute(formula, context, null, true, false);
2022-04-15 13:54:47 +08:00
}
}