增加计算接口

This commit is contained in:
2023-09-14 21:42:00 +08:00
parent cb1e91361d
commit de17b5cd90
6 changed files with 196 additions and 23 deletions

View File

@@ -2,11 +2,13 @@ package cn.lihongjie.coal.coalAnalysis.dto;
import cn.lihongjie.coal.base.dto.OrgCommonDto;
import cn.lihongjie.coal.coalInfo.entity.CoalInfoEntity;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.DecimalMin;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.util.HashMap;
import java.util.Map;
@Data
public class CoalAnalysisDto extends OrgCommonDto {
@Comment("关联的煤源信息")
@@ -73,4 +75,60 @@ public class CoalAnalysisDto extends OrgCommonDto {
@Comment("参数 20 ")
@DecimalMin(value = "0.1", inclusive = true, message = "参数不能小于0.1")
private Double param20;
public Map<String, Object> toMap() {
HashMap<String, Object> ma = new HashMap<>();
ma.put("param1", param1);
ma.put("param2", param2);
ma.put("param3", param3);
ma.put("param4", param4);
ma.put("param5", param5);
ma.put("param6", param6);
ma.put("param7", param7);
ma.put("param8", param8);
ma.put("param9", param9);
ma.put("param10", param10);
ma.put("param11", param11);
ma.put("param12", param12);
ma.put("param13", param13);
ma.put("param14", param14);
ma.put("param15", param15);
ma.put("param16", param16);
ma.put("param17", param17);
ma.put("param18", param18);
ma.put("param19", param19);
ma.put("param20", param20);
return ma;
}
public void updateFromMap(Map<String, Object> map) {
this.param1 = (Double) map.getOrDefault("param1", null);
this.param2 = (Double) map.getOrDefault("param2", null);
this.param3 = (Double) map.getOrDefault("param3", null);
this.param4 = (Double) map.getOrDefault("param4", null);
this.param5 = (Double) map.getOrDefault("param5", null);
this.param6 = (Double) map.getOrDefault("param6", null);
this.param7 = (Double) map.getOrDefault("param7", null);
this.param8 = (Double) map.getOrDefault("param8", null);
this.param9 = (Double) map.getOrDefault("param9", null);
this.param10 = (Double) map.getOrDefault("param10", null);
this.param11 = (Double) map.getOrDefault("param11", null);
this.param12 = (Double) map.getOrDefault("param12", null);
this.param13 = (Double) map.getOrDefault("param13", null);
this.param14 = (Double) map.getOrDefault("param14", null);
this.param15 = (Double) map.getOrDefault("param15", null);
this.param16 = (Double) map.getOrDefault("param16", null);
this.param17 = (Double) map.getOrDefault("param17", null);
this.param18 = (Double) map.getOrDefault("param18", null);
this.param19 = (Double) map.getOrDefault("param19", null);
this.param20 = (Double) map.getOrDefault("param20", null);
}
}

View File

@@ -9,8 +9,15 @@ import cn.lihongjie.coal.coalAnalysis.dto.UpdateCoalAnalysisDto;
import cn.lihongjie.coal.coalAnalysis.entity.CoalAnalysisEntity;
import cn.lihongjie.coal.coalAnalysis.mapper.CoalAnalysisMapper;
import cn.lihongjie.coal.coalAnalysis.repository.CoalAnalysisRepository;
import java.lang.String;
import cn.lihongjie.coal.coalParameterDef.entity.CoalParameterDefEntity;
import cn.lihongjie.coal.coalParameterDef.repository.CoalParameterDefRepository;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.common.GroovyScriptUtils;
import cn.lihongjie.coal.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
@@ -18,6 +25,11 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
public class CoalAnalysisService extends BaseService<CoalAnalysisEntity, CoalAnalysisRepository> {
@@ -39,6 +51,39 @@ public class CoalAnalysisService extends BaseService<CoalAnalysisEntity, CoalAna
;
}
@Autowired
CoalParameterDefRepository coalParameterDefRepository;
public CoalAnalysisDto calculate(CoalAnalysisDto dto) {
List<CoalParameterDefEntity> parameters = coalParameterDefRepository.findByOrganizationId(Ctx.currentUser().getOrganizationId());
List<CoalParameterDefEntity> toCalculate = parameters.stream().filter(x -> StringUtils.equalsIgnoreCase(x.getInputType(), "1")).sorted(Comparator.comparing(x -> ObjectUtils.defaultIfNull(x.getPriority(), -1))).collect(Collectors.toList());
Map<String, Object> map = dto.toMap();
for (CoalParameterDefEntity def : toCalculate) {
Object exec = GroovyScriptUtils.exec(def.getFormula0(), map);
if (exec!=null && !(exec instanceof Number)) {
throw new BizException("计算结果不是数字");
}
map.put(def.getCode(), exec != null ? NumberUtils.toDouble(exec + "") : null);
}
dto.updateFromMap(map);
return dto;
}
public CoalAnalysisDto update(UpdateCoalAnalysisDto request) {
CoalAnalysisEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);

View File

@@ -12,7 +12,7 @@ public class CoalParameterDefDto extends OrgCommonDto {
private String parentName;
@Comment("类型 0 手动输入 1 自动计算" )
private String inputType;
private String inputTypeName;
@Comment("计算公式" )
private String formula;

View File

@@ -4,6 +4,7 @@ import cn.lihongjie.coal.base.entity.OrgCommonEntity;
import jakarta.persistence.Entity;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
@Entity
@Data
@@ -17,6 +18,14 @@ public class CoalParameterDefEntity extends OrgCommonEntity {
@Comment("类型 0 手动输入 1 自动计算" )
private String inputType;
@Formula("(select i.name\n" +
"from t_dictionary d,\n" +
" t_dictionary_item i\n" +
"where d.id = i.dictionary_id\n" +
" and d.code = 'coalParameter.inputType'\n" +
" and i.code = input_type)")
private String inputTypeName;
@Comment("计算公式" )
private String formula;

View File

@@ -10,6 +10,7 @@ import cn.lihongjie.coal.coalParameterDef.entity.CoalParameterDefEntity;
import cn.lihongjie.coal.coalParameterDef.mapper.CoalParameterDefMapper;
import cn.lihongjie.coal.coalParameterDef.repository.CoalParameterDefRepository;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.common.GroovyScriptUtils;
import cn.lihongjie.coal.organization.service.OrganizationService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -31,9 +32,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
@@ -62,6 +61,12 @@ public class CoalParameterDefService extends BaseService<CoalParameterDefEntity,
CoalParameterDefEntity entity = mapper.toEntity(request);
if (StringUtils.equalsIgnoreCase(entity.getInputType(), "1")) {
GroovyScriptUtils.validate(entity.getFormula());
}
this.repository.save(entity);
updateFormula(entity);
@@ -78,33 +83,16 @@ public class CoalParameterDefService extends BaseService<CoalParameterDefEntity,
String formula = entity.getFormula();
String dependent = "";
if (StringUtils.isNotEmpty(formula)) {
List<CoalParameterDefEntity> sorted = all.stream().sorted(Comparator.comparing((CoalParameterDefEntity x) -> x.getName().length()).reversed()).toList();
for (CoalParameterDefEntity def : sorted) {
if (Objects.equals(def.getId(), entity.getId())) {
continue;
}
if (formula.contains(def.getName())) {
formula = formula.replaceAll(def.getName(), def.getCode());
dependent += def.getCode();
}
}
}
entity.setFormula0(formula);
entity.setDependents(dependent);
entity.setDependents(StringUtils.join(GroovyScriptUtils.variables(formula), ","));
for (CoalParameterDefEntity def : all) {
@@ -159,6 +147,10 @@ public class CoalParameterDefService extends BaseService<CoalParameterDefEntity,
CoalParameterDefEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
if (StringUtils.equalsIgnoreCase(entity.getInputType(), "1")) {
GroovyScriptUtils.validate(entity.getFormula());
}
this.repository.save(entity);
updateFormula(entity);
return getById(entity.getId());

View File

@@ -0,0 +1,69 @@
package cn.lihongjie.coal.common;
import cn.lihongjie.coal.exception.BizException;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.GroovyCodeVisitorAdapter;
import org.codehaus.groovy.ast.builder.AstStringCompiler;
import org.codehaus.groovy.ast.expr.VariableExpression;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@UtilityClass
@Slf4j
public class GroovyScriptUtils {
public static void validate(String formula) {
if (StringUtils.isEmpty(formula)) {
return;
}
AstStringCompiler compiler = new AstStringCompiler();
try {
List<ASTNode> astNodes = compiler.compile(formula);
} catch (Exception e) {
log.info(formula, e);
throw new BizException("无效的计算公式");
}
}
public static List<String> variables(String formula) {
if (StringUtils.isEmpty(formula)) {
return new ArrayList<>();
}
AstStringCompiler compiler = new AstStringCompiler();
return compiler.compile(formula).stream().flatMap(x -> {
ArrayList<String> ans = new ArrayList<>();
x.visit(new GroovyCodeVisitorAdapter() {
@Override
public void visitVariableExpression(VariableExpression expression) {
ans.add(expression.getName());
}
});
return ans.stream();
}).collect(Collectors.toList());
}
public static Object exec(String formula0, Map<String, Object> map) {
GroovyShell shell = new GroovyShell(new Binding(map));
return shell.evaluate(formula0);
}
}