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

57 lines
2.0 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-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-27 17:20:33 +08:00
import org.jetbrains.annotations.Nullable;
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
private static ExpressRunner runner;
2022-04-27 17:20:33 +08:00
2022-04-24 17:42:32 +08:00
static {
2022-04-27 17:20:33 +08:00
runner = new ExpressRunner(true, false);
2022-04-24 17:42:32 +08:00
}
2022-04-15 13:54:47 +08:00
@Override
public Object run(ExpressFormula expressFormula, List<FormulaVar> formulaVars, DataCollectionEmployee simpleEmployee) {
2022-04-27 17:20:33 +08:00
if (ReferenceTypeEnum.parseByValue(expressFormula.getReferenceType()) == ReferenceTypeEnum.FORMULA) {
return runFormula(expressFormula, formulaVars);
}
return null;
}
@Nullable
private Object runFormula(ExpressFormula expressFormula, List<FormulaVar> formulaVars) {
2022-04-18 14:45:12 +08:00
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
2022-04-16 16:49:22 +08:00
formulaVars.forEach(v -> {
2022-04-20 19:23:16 +08:00
if (DataType.NUMBER.equals(v.getFieldType())) {
context.put(v.getFieldId(), new BigDecimal(v.getContent()));
} else {
context.put(v.getFieldId(), v.getContent());
}
2022-04-15 13:54:47 +08:00
});
2022-04-18 14:45:12 +08:00
Object execute = null;
try {
2022-04-20 19:23:16 +08:00
String formula = expressFormula.getFormulaRunScript();
2022-04-18 14:45:12 +08:00
execute = runner.execute(formula, context, null, true, false);
} catch (Exception e) {
2022-04-24 17:42:32 +08:00
log.error("express execute fail", e);
2022-04-18 14:45:12 +08:00
}
2022-04-15 13:54:47 +08:00
return execute;
}
}