diff --git a/pom.xml b/pom.xml index 1c0389c4..067e5605 100644 --- a/pom.xml +++ b/pom.xml @@ -127,6 +127,14 @@ org.springframework.boot spring-boot-starter-actuator + + + + org.jgrapht + jgrapht-core + 1.5.2 + + com.google.guava guava diff --git a/src/main/java/cn/lihongjie/coal/base/dao/BaseRepository.java b/src/main/java/cn/lihongjie/coal/base/dao/BaseRepository.java index edfe1b07..35b62389 100644 --- a/src/main/java/cn/lihongjie/coal/base/dao/BaseRepository.java +++ b/src/main/java/cn/lihongjie/coal/base/dao/BaseRepository.java @@ -1,15 +1,34 @@ package cn.lihongjie.coal.base.dao; +import jakarta.persistence.criteria.Path; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.NoRepositoryBean; +import java.util.List; + @NoRepositoryBean public interface BaseRepository extends JpaRepository, JpaSpecificationExecutor { - public default T get(String id){ + public default T get(String id) { return findById(id).orElseThrow(() -> new RuntimeException("数据不存在: " + id)); } + + public default List findByOrganizationId(String organizationId) { + return findAll((root, query, cb) -> { + try { + + Path path = root.get("organizationId"); + return cb.equal(path, organizationId); + } catch (Exception e) { + return cb.and(); + } + }); + + + } + + ; } diff --git a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CoalParameterDefDto.java b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CoalParameterDefDto.java index 3877e335..3d3941b5 100644 --- a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CoalParameterDefDto.java +++ b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CoalParameterDefDto.java @@ -10,5 +10,10 @@ public class CoalParameterDefDto extends OrgCommonDto { @Comment("上级名称") private String parentName; + @Comment("类型 0 手动输入 1 自动计算" ) + private String inputType; + + @Comment("计算公式" ) + private String formula; } diff --git a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CreateCoalParameterDefDto.java b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CreateCoalParameterDefDto.java index cd9ab0bd..db488fc1 100644 --- a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CreateCoalParameterDefDto.java +++ b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/CreateCoalParameterDefDto.java @@ -9,4 +9,12 @@ public class CreateCoalParameterDefDto extends OrgCommonDto { @Comment("上级名称") private String parentName; + + + @Comment("类型 0 手动输入 1 自动计算" ) + private String inputType; + + + @Comment("计算公式" ) + private String formula; } diff --git a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/UpdateCoalParameterDefDto.java b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/UpdateCoalParameterDefDto.java index a850e4d9..a73fd26f 100644 --- a/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/UpdateCoalParameterDefDto.java +++ b/src/main/java/cn/lihongjie/coal/coalParameterDef/dto/UpdateCoalParameterDefDto.java @@ -11,4 +11,12 @@ public class UpdateCoalParameterDefDto extends OrgCommonDto { @Comment("上级名称") private String parentName; + + @Comment("类型 0 手动输入 1 自动计算" ) + private String inputType; + + + @Comment("计算公式" ) + private String formula; + } diff --git a/src/main/java/cn/lihongjie/coal/coalParameterDef/entity/CoalParameterDefEntity.java b/src/main/java/cn/lihongjie/coal/coalParameterDef/entity/CoalParameterDefEntity.java index 421c7582..67660fc5 100644 --- a/src/main/java/cn/lihongjie/coal/coalParameterDef/entity/CoalParameterDefEntity.java +++ b/src/main/java/cn/lihongjie/coal/coalParameterDef/entity/CoalParameterDefEntity.java @@ -14,5 +14,23 @@ public class CoalParameterDefEntity extends OrgCommonEntity { private String parentName; + @Comment("类型 0 手动输入 1 自动计算" ) + private String inputType; + + + @Comment("计算公式" ) + private String formula; + + @Comment("解析后的公式" ) + private String formula0; + + + @Comment("依赖的字段" ) + private String dependents; + + @Comment("计算优先级" ) + private Integer priority; + + } diff --git a/src/main/java/cn/lihongjie/coal/coalParameterDef/service/CoalParameterDefService.java b/src/main/java/cn/lihongjie/coal/coalParameterDef/service/CoalParameterDefService.java index a8e3e8e3..cdcc922c 100644 --- a/src/main/java/cn/lihongjie/coal/coalParameterDef/service/CoalParameterDefService.java +++ b/src/main/java/cn/lihongjie/coal/coalParameterDef/service/CoalParameterDefService.java @@ -9,13 +9,19 @@ import cn.lihongjie.coal.coalParameterDef.dto.UpdateCoalParameterDefDto; 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.organization.service.OrganizationService; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Splitter; import jakarta.annotation.PostConstruct; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jgrapht.graph.DefaultDirectedGraph; +import org.jgrapht.graph.DefaultEdge; +import org.jgrapht.traverse.TopologicalOrderIterator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.core.io.ClassPathResource; @@ -25,7 +31,10 @@ 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 @Slf4j @@ -45,6 +54,8 @@ public class CoalParameterDefService extends BaseService graph = new DefaultDirectedGraph<>(DefaultEdge.class); + List all = this.repository.findByOrganizationId(entity.getOrganizationId()); + + String formula = entity.getFormula(); + + String dependent = ""; + if (StringUtils.isNotEmpty(formula)) { + + List 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); + + + for (CoalParameterDefEntity def : all) { + + if (StringUtils.equalsIgnoreCase(def.getInputType(), "1")) { + + + Iterable dependents = Splitter.on(",").trimResults().omitEmptyStrings().split(def.getDependents()); + + + for (String d : dependents) { + + graph.addVertex(def.getCode()); + graph.addVertex(d); + + + graph.addEdge(d, def.getCode()); + + } + } + + } + + + TopologicalOrderIterator iterator = new TopologicalOrderIterator<>(graph); + + int order = 0; + while (iterator.hasNext()) { + Object next = (String) iterator.next(); + + for (CoalParameterDefEntity coalParameterDefEntity : all) { + + if (StringUtils.equalsIgnoreCase(coalParameterDefEntity.getCode(), next + "")) { + coalParameterDefEntity.setPriority(order++); + break; + + } + } + + + + } + + + this.repository.saveAll(all); + + + } + public CoalParameterDefDto update(UpdateCoalParameterDefDto request) { CoalParameterDefEntity entity = this.repository.get(request.getId()); this.mapper.updateEntity(entity, request); this.repository.save(entity); - + updateFormula(entity); return getById(entity.getId()); } @@ -71,6 +169,12 @@ public class CoalParameterDefService extends BaseService allDef = this.repository.findByOrganizationId(Ctx.currentUser().getOrganizationId()); + + Optional type1 = allDef.stream().filter(x -> StringUtils.equalsIgnoreCase(x.getInputType(), "1")).findAny(); + + type1.ifPresent(this::updateFormula); + } @@ -125,7 +229,6 @@ public class CoalParameterDefService extends BaseService