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

204 lines
7.6 KiB
Java
Raw Normal View History

2022-04-14 10:03:49 +08:00
package com.engine.salary.service.impl;
import com.engine.core.impl.Service;
2022-04-18 14:45:12 +08:00
import com.engine.salary.constant.SalaryFormulaFieldConstant;
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-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;
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-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-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-04-14 20:46:17 +08:00
import java.util.*;
2022-04-18 14:45:12 +08:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 {
private FormulaMapper getFormulaMapper() {
return MapperProxyFactory.getProxy(FormulaMapper.class);
}
private FormulaVarMapper getFormulaVarMapper() {
return MapperProxyFactory.getProxy(FormulaVarMapper.class);
}
@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));
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());
} 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 13:33:44 +08:00
if ("sql".equals(param.getReferenceType())) {
param.setFormula(param.getFormula().replaceAll("", "select"));
}
2022-04-18 14:45:12 +08:00
//公式参数与公式内容是否相符
2022-04-18 15:38:27 +08:00
// String paramFormula = param.getFormula();
// int end = matchStr(paramFormula);
// if(!Objects.equals(end,param.getParameters())){
// throw new SalaryRunTimeException("参数不匹配");
// }
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-04-24 10:47:38 +08:00
formulaPO.setId(IdGenerator.generate());
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());
formulaPO.setExtendParam(param.getExtendParam());
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;
2022-04-14 20:46:17 +08:00
List<FormulaVar> parameters = param.getParameters();
2022-04-18 14:45:12 +08:00
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-04-14 20:46:17 +08:00
po.setFormulaId(formulaPO.getId());
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-04-27 13:33:44 +08:00
private static int matchStr(String str) {
2022-04-18 15:38:27 +08:00
Pattern salaryPattern = SalaryFormulaFieldConstant.SALARY_PATTERN;
Matcher m = salaryPattern.matcher(str);
int count = 0;
while (m.find()) {
count++;
}
return count;
}
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
}