2022-04-14 10:03:49 +08:00
|
|
|
|
package com.engine.salary.service.impl;
|
|
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import com.engine.common.util.ServiceUtil;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import com.engine.core.impl.Service;
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
|
2022-04-14 20:46:17 +08:00
|
|
|
|
import com.engine.salary.entity.salaryformula.ExpressFormula;
|
|
|
|
|
|
import com.engine.salary.entity.salaryformula.param.SalaryFormulaSaveParam;
|
|
|
|
|
|
import com.engine.salary.entity.salaryformula.po.FormulaPO;
|
|
|
|
|
|
import com.engine.salary.entity.salaryformula.po.FormulaVar;
|
2022-04-27 17:20:33 +08:00
|
|
|
|
import com.engine.salary.enums.salaryformula.ReferenceTypeEnum;
|
|
|
|
|
|
import com.engine.salary.enums.salaryformula.ReturnTypeEnum;
|
|
|
|
|
|
import com.engine.salary.enums.salaryformula.ValidateTypeEnum;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import com.engine.salary.exception.SalaryRunTimeException;
|
|
|
|
|
|
import com.engine.salary.mapper.formula.FormulaMapper;
|
|
|
|
|
|
import com.engine.salary.mapper.formula.FormulaVarMapper;
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import com.engine.salary.service.FormulaRunService;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import com.engine.salary.service.SalaryFormulaService;
|
|
|
|
|
|
import com.engine.salary.util.db.MapperProxyFactory;
|
2022-04-14 20:46:17 +08:00
|
|
|
|
import com.engine.salary.util.valid.RuntimeTypeEnum;
|
|
|
|
|
|
import com.engine.salary.util.valid.ValidUtil;
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import com.google.common.collect.Lists;
|
2022-04-24 10:47:38 +08:00
|
|
|
|
import dm.jdbc.util.IdGenerator;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2022-04-14 20:46:17 +08:00
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import org.springframework.beans.BeanUtils;
|
2022-05-08 12:34:05 +08:00
|
|
|
|
import weaver.hrm.User;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
|
2022-04-14 20:46:17 +08:00
|
|
|
|
import java.util.*;
|
2022-04-14 10:03:49 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 薪酬管理公式编辑器
|
|
|
|
|
|
* <p>Copyright: Copyright (c) 2022</p>
|
|
|
|
|
|
* <p>Company: 泛微软件</p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author qiantao
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class SalaryFormulaServiceImpl extends Service implements SalaryFormulaService {
|
2022-05-08 12:34:05 +08:00
|
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
2022-04-14 10:03:49 +08:00
|
|
|
|
|
|
|
|
|
|
private FormulaMapper getFormulaMapper() {
|
|
|
|
|
|
return MapperProxyFactory.getProxy(FormulaMapper.class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private FormulaVarMapper getFormulaVarMapper() {
|
|
|
|
|
|
return MapperProxyFactory.getProxy(FormulaVarMapper.class);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
private FormulaRunService getFormulaRunService(User user) {
|
|
|
|
|
|
return (FormulaRunService) ServiceUtil.getService(FormulaRunServiceImpl.class, user);
|
|
|
|
|
|
}
|
2022-04-14 10:03:49 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public List<ExpressFormula> listExpressFormula(Collection<Long> formulaIds) {
|
|
|
|
|
|
formulaIds = formulaIds.stream().filter(id -> id != null && id > 0).collect(Collectors.toList());
|
|
|
|
|
|
if (CollectionUtils.isEmpty(formulaIds)) {
|
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 当前租户自己新建的公式
|
|
|
|
|
|
List<FormulaPO> expressFormulas = getFormulaMapper().listByIds(Lists.newArrayList(formulaIds));
|
|
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
return expressFormulas.stream().filter(Objects::nonNull).map(m -> {
|
|
|
|
|
|
ExpressFormula expressFormula = new ExpressFormula();
|
|
|
|
|
|
BeanUtils.copyProperties(m, expressFormula);
|
|
|
|
|
|
List<FormulaVar> formulaVarPOS = getFormulaVarMapper().listSome(FormulaVar.builder().formulaId(m.getId()).build());
|
|
|
|
|
|
expressFormula.setParameters(formulaVarPOS);
|
|
|
|
|
|
return expressFormula;
|
|
|
|
|
|
}).collect(Collectors.toList());
|
2022-04-14 10:03:49 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.info("获取公示详情失败", e);
|
|
|
|
|
|
throw new SalaryRunTimeException("获取公示详情失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public ExpressFormula getExpressFormula(Long formulaId) {
|
|
|
|
|
|
if (formulaId == null || formulaId <= 0) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 当前租户自己新建的公式
|
|
|
|
|
|
FormulaPO formulaPO = getFormulaMapper().getById(formulaId);
|
|
|
|
|
|
ExpressFormula expressFormula = new ExpressFormula();
|
|
|
|
|
|
BeanUtils.copyProperties(formulaPO, expressFormula);
|
|
|
|
|
|
List<FormulaVar> formulaVarPOS = getFormulaVarMapper().listSome(FormulaVar.builder().formulaId(formulaId).build());
|
|
|
|
|
|
expressFormula.setParameters(formulaVarPOS);
|
|
|
|
|
|
|
|
|
|
|
|
return expressFormula;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.info("获取公示详情失败", e);
|
|
|
|
|
|
throw new SalaryRunTimeException("获取公示详情失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-14 20:46:17 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-04-16 16:49:22 +08:00
|
|
|
|
public FormulaPO save(SalaryFormulaSaveParam param) {
|
2022-04-18 14:45:12 +08:00
|
|
|
|
ValidUtil.doValidator(param);
|
2022-04-27 17:20:33 +08:00
|
|
|
|
if (ReferenceTypeEnum.parseByValue(param.getReferenceType()) == null) {
|
|
|
|
|
|
throw new SalaryRunTimeException("引用类型异常");
|
2022-04-27 13:33:44 +08:00
|
|
|
|
}
|
2022-04-27 17:20:33 +08:00
|
|
|
|
if (ReturnTypeEnum.parseByValue(param.getReturnType()) == null) {
|
|
|
|
|
|
throw new SalaryRunTimeException("返回类型异常");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ValidateTypeEnum.parseByValue(param.getValidateType()) == null) {
|
|
|
|
|
|
throw new SalaryRunTimeException("校验类型异常");
|
|
|
|
|
|
}
|
|
|
|
|
|
//todo 公式参数与公式内容是否相符
|
2022-05-07 15:20:39 +08:00
|
|
|
|
List<FormulaVar> parameters = param.getParameters();
|
2022-06-30 11:10:46 +08:00
|
|
|
|
//防止参数名和字段名呈现一对多的问题
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(parameters)) {
|
|
|
|
|
|
List<String> notRepeatingNameSize = parameters.stream().map(FormulaVar::getFieldName).distinct().collect(Collectors.toList());
|
|
|
|
|
|
if (notRepeatingNameSize.size() < parameters.size()) {
|
|
|
|
|
|
throw new SalaryRunTimeException("公式参数配置异常!参数名称重复,请清空公式内容后,再重新打开编辑");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-27 17:20:33 +08:00
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
String extendParam = param.getExtendParam();
|
2022-04-27 17:20:33 +08:00
|
|
|
|
if (ReferenceTypeEnum.parseByValue(param.getReferenceType()) == ReferenceTypeEnum.SQL) {
|
2022-05-08 12:34:05 +08:00
|
|
|
|
|
|
|
|
|
|
if (extendParam == null) {
|
|
|
|
|
|
throw new SalaryRunTimeException("未设置SQL返回值");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String sqlReturnKey = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readTree(extendParam);
|
|
|
|
|
|
JsonNode sqlReturnKeyNode = jsonNode.get("sqlReturnKey");
|
|
|
|
|
|
if (sqlReturnKeyNode != null) {
|
|
|
|
|
|
sqlReturnKey = sqlReturnKeyNode.asText();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
|
log.error("express execute fail, sql extendParam parse fail", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(sqlReturnKey)) {
|
|
|
|
|
|
throw new SalaryRunTimeException("未设置SQL返回值");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-27 17:20:33 +08:00
|
|
|
|
//将select因XSS过滤造成的异常字符转换回来
|
|
|
|
|
|
param.setFormula(param.getFormula().replaceAll("select", "select"));
|
2022-08-04 11:45:26 +08:00
|
|
|
|
param.setFormula(param.getFormula().replaceAll("SELECT", "select"));
|
2022-07-28 14:17:30 +08:00
|
|
|
|
param.setFormula(param.getFormula().replaceAll("join", "join"));
|
2022-08-04 11:45:26 +08:00
|
|
|
|
param.setFormula(param.getFormula().replaceAll("JOIN ", "join"));
|
2022-06-28 16:08:20 +08:00
|
|
|
|
param.setFormula(param.getFormula().replaceAll("and", "and"));
|
|
|
|
|
|
param.setFormula(param.getFormula().replaceAll("or", "or"));
|
|
|
|
|
|
param.setFormula(param.getFormula().replaceAll("in", "in"));
|
|
|
|
|
|
param.setFormula(param.getFormula().replaceAll("like", "like"));
|
2022-08-04 11:45:26 +08:00
|
|
|
|
param.setFormula(param.getFormula().replaceAll("LIKE ", "like"));
|
2022-04-27 17:20:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
//试运行公式
|
2022-07-25 15:01:10 +08:00
|
|
|
|
checkRun(param);
|
2022-05-08 12:34:05 +08:00
|
|
|
|
|
2022-04-14 20:46:17 +08:00
|
|
|
|
FormulaPO formulaPO = new FormulaPO();
|
2022-04-16 16:49:22 +08:00
|
|
|
|
String formula = param.getFormula();
|
2022-05-06 13:22:16 +08:00
|
|
|
|
long formulaId = IdGenerator.generate();
|
|
|
|
|
|
formulaPO.setId(formulaId);
|
2022-04-14 20:46:17 +08:00
|
|
|
|
formulaPO.setName(param.getName());
|
|
|
|
|
|
formulaPO.setDescription(param.getDescription());
|
|
|
|
|
|
formulaPO.setModule(param.getModule());
|
|
|
|
|
|
formulaPO.setUseFor(param.getUseFor());
|
|
|
|
|
|
formulaPO.setReferenceType(param.getReferenceType());
|
|
|
|
|
|
formulaPO.setReturnType(param.getReturnType());
|
|
|
|
|
|
formulaPO.setValidateType(param.getValidateType());
|
2022-05-08 12:34:05 +08:00
|
|
|
|
formulaPO.setExtendParam(extendParam);
|
2022-04-16 16:49:22 +08:00
|
|
|
|
formulaPO.setFormula(formula);
|
2022-04-14 20:46:17 +08:00
|
|
|
|
formulaPO.setDeleteType(NumberUtils.INTEGER_ZERO);
|
|
|
|
|
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
|
|
formulaPO.setCreateTime(now);
|
|
|
|
|
|
formulaPO.setUpdateTime(now);
|
|
|
|
|
|
formulaPO.setCreator((long) user.getUID());
|
|
|
|
|
|
|
2022-04-18 14:45:12 +08:00
|
|
|
|
/*
|
|
|
|
|
|
公式内容以如下显示方式存储
|
|
|
|
|
|
{薪资项目.输入项1}+{薪资项目.输入项2}
|
|
|
|
|
|
转换为实际的运行脚本
|
|
|
|
|
|
*/
|
|
|
|
|
|
String formulaRunScript = formula;
|
|
|
|
|
|
for (FormulaVar po : parameters) {
|
|
|
|
|
|
formulaRunScript = formulaRunScript.replace(po.getFieldName(), po.getFieldId());
|
|
|
|
|
|
}
|
|
|
|
|
|
formulaPO.setFormulaRunScript(formulaRunScript);
|
|
|
|
|
|
getFormulaMapper().insertIgnoreNull(formulaPO);
|
2022-04-16 16:49:22 +08:00
|
|
|
|
|
2022-04-18 14:45:12 +08:00
|
|
|
|
for (FormulaVar po : parameters) {
|
2022-04-24 10:47:38 +08:00
|
|
|
|
po.setId(IdGenerator.generate());
|
2022-05-06 13:22:16 +08:00
|
|
|
|
po.setFormulaId(formulaId);
|
2022-04-14 20:46:17 +08:00
|
|
|
|
po.setDeleteType(NumberUtils.INTEGER_ZERO);
|
|
|
|
|
|
po.setCreator((long) user.getUID());
|
|
|
|
|
|
po.setCreateTime(now);
|
|
|
|
|
|
po.setUpdateTime(now);
|
|
|
|
|
|
getFormulaVarMapper().insertIgnoreNull(po);
|
2022-04-18 14:45:12 +08:00
|
|
|
|
}
|
2022-04-16 16:49:22 +08:00
|
|
|
|
return formulaPO;
|
2022-04-14 20:46:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-08 12:34:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 模拟运行公式
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param param
|
|
|
|
|
|
*/
|
|
|
|
|
|
private void checkRun(SalaryFormulaSaveParam param) {
|
|
|
|
|
|
//返回类型
|
|
|
|
|
|
String returnType = param.getReturnType();
|
|
|
|
|
|
ReturnTypeEnum returnTypeEnum = ReturnTypeEnum.parseByValue(returnType);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
公式内容以如下显示方式存储
|
|
|
|
|
|
{薪资项目.输入项1}+{薪资项目.输入项2}
|
|
|
|
|
|
转换为实际的运行脚本
|
|
|
|
|
|
*/
|
|
|
|
|
|
String formulaRunScript = param.getFormula();
|
|
|
|
|
|
|
|
|
|
|
|
List<FormulaVar> parameters = param.getParameters();
|
|
|
|
|
|
for (int i = 0; i < parameters.size(); i++) {
|
|
|
|
|
|
FormulaVar po = parameters.get(i);
|
|
|
|
|
|
formulaRunScript = formulaRunScript.replace(po.getFieldName(), po.getFieldId());
|
|
|
|
|
|
po.setContent(String.valueOf(i + 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
//验证公式是否可运行
|
2022-06-16 17:55:26 +08:00
|
|
|
|
if (ReferenceTypeEnum.parseByValue(param.getReferenceType()) == ReferenceTypeEnum.FORMULA) {
|
|
|
|
|
|
ExpressFormula test = ExpressFormula.builder().formulaRunScript(formulaRunScript).extendParam(param.getExtendParam()).referenceType(param.getReferenceType()).build();
|
|
|
|
|
|
Object run = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
run = getFormulaRunService(user).run(test, parameters, DataCollectionEmployee.builder().employeeId((long) user.getUID()).build());
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("express execute fail ", e);
|
2022-06-30 11:10:46 +08:00
|
|
|
|
throw new SalaryRunTimeException("公式模拟运行出错,请检查公式配置!", e);
|
2022-06-16 17:55:26 +08:00
|
|
|
|
}
|
2022-05-08 12:34:05 +08:00
|
|
|
|
|
2022-06-16 17:55:26 +08:00
|
|
|
|
if (run != null && StringUtils.isNotBlank(String.valueOf(run)) && returnTypeEnum == ReturnTypeEnum.NUMBER) {
|
|
|
|
|
|
//返回结果不是数字
|
|
|
|
|
|
if (!NumberUtils.isCreatable(String.valueOf(run))) {
|
|
|
|
|
|
throw new SalaryRunTimeException("返回结果不是数值");
|
|
|
|
|
|
}
|
2022-05-08 12:34:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-25 15:01:10 +08:00
|
|
|
|
|
|
|
|
|
|
if (ReferenceTypeEnum.parseByValue(param.getReferenceType()) == ReferenceTypeEnum.SQL) {
|
|
|
|
|
|
if (formulaRunScript.contains(";") || formulaRunScript.contains("--")) {
|
|
|
|
|
|
throw new SalaryRunTimeException("SQL配置异常,请去除';'或者'--'");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-05-08 12:34:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-14 20:46:17 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-04-16 16:49:22 +08:00
|
|
|
|
public FormulaPO update(SalaryFormulaSaveParam param) {
|
2022-04-14 20:46:17 +08:00
|
|
|
|
|
|
|
|
|
|
ValidUtil.doValidator(param, RuntimeTypeEnum.UPDATE);
|
|
|
|
|
|
|
|
|
|
|
|
Long id = param.getId();
|
|
|
|
|
|
FormulaPO formulaPO = getFormulaMapper().getById(id);
|
|
|
|
|
|
if (formulaPO == null) {
|
|
|
|
|
|
throw new SalaryRunTimeException("公式不存在或已删除");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
formulaPO.setName(param.getName());
|
|
|
|
|
|
formulaPO.setDescription(param.getDescription());
|
|
|
|
|
|
formulaPO.setModule(param.getModule());
|
|
|
|
|
|
formulaPO.setUseFor(param.getUseFor());
|
|
|
|
|
|
formulaPO.setReferenceType(param.getReferenceType());
|
|
|
|
|
|
formulaPO.setReturnType(param.getReturnType());
|
|
|
|
|
|
formulaPO.setValidateType(param.getValidateType());
|
|
|
|
|
|
formulaPO.setExtendParam(param.getExtendParam());
|
|
|
|
|
|
formulaPO.setFormula(param.getFormula());
|
|
|
|
|
|
formulaPO.setDeleteType(NumberUtils.INTEGER_ZERO);
|
|
|
|
|
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
|
|
formulaPO.setUpdateTime(now);
|
|
|
|
|
|
formulaPO.setCreator((long) user.getUID());
|
|
|
|
|
|
getFormulaMapper().updateIgnoreNull(formulaPO);
|
|
|
|
|
|
|
|
|
|
|
|
List<FormulaVar> parameters = param.getParameters();
|
|
|
|
|
|
//删除公式下的变量
|
|
|
|
|
|
getFormulaVarMapper().deleteByFormulaId(id);
|
|
|
|
|
|
parameters.forEach(po -> {
|
|
|
|
|
|
po.setCreator((long) user.getUID());
|
|
|
|
|
|
po.setCreateTime(now);
|
|
|
|
|
|
po.setUpdateTime(now);
|
|
|
|
|
|
getFormulaVarMapper().insertIgnoreNull(po);
|
|
|
|
|
|
});
|
2022-04-16 16:49:22 +08:00
|
|
|
|
return formulaPO;
|
2022-04-14 20:46:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-14 10:03:49 +08:00
|
|
|
|
}
|