完善计算公式

This commit is contained in:
2023-09-14 21:00:28 +08:00
parent 9948234777
commit d6ee75650e
10 changed files with 212 additions and 3 deletions

View File

@@ -127,6 +127,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-core -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@@ -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<T> extends JpaRepository<T, String>, JpaSpecificationExecutor<T> {
public default T get(String id){
public default T get(String id) {
return findById(id).orElseThrow(() -> new RuntimeException("数据不存在: " + id));
}
public default List<T> findByOrganizationId(String organizationId) {
return findAll((root, query, cb) -> {
try {
Path<Object> path = root.get("organizationId");
return cb.equal(path, organizationId);
} catch (Exception e) {
return cb.and();
}
});
}
;
}

View File

@@ -10,5 +10,10 @@ public class CoalParameterDefDto extends OrgCommonDto {
@Comment("上级名称")
private String parentName;
@Comment("类型 0 手动输入 1 自动计算" )
private String inputType;
@Comment("计算公式" )
private String formula;
}

View File

@@ -9,4 +9,12 @@ public class CreateCoalParameterDefDto extends OrgCommonDto {
@Comment("上级名称")
private String parentName;
@Comment("类型 0 手动输入 1 自动计算" )
private String inputType;
@Comment("计算公式" )
private String formula;
}

View File

@@ -11,4 +11,12 @@ public class UpdateCoalParameterDefDto extends OrgCommonDto {
@Comment("上级名称")
private String parentName;
@Comment("类型 0 手动输入 1 自动计算" )
private String inputType;
@Comment("计算公式" )
private String formula;
}

View File

@@ -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;
}

View File

@@ -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<CoalParameterDefEntity,
}
public CoalParameterDefDto create(CreateCoalParameterDefDto request) {
@@ -52,17 +63,104 @@ public class CoalParameterDefService extends BaseService<CoalParameterDefEntity,
this.repository.save(entity);
updateFormula(entity);
return getById(entity.getId());
}
private void updateFormula(CoalParameterDefEntity entity) {
DefaultDirectedGraph<Object, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
List<CoalParameterDefEntity> all = this.repository.findByOrganizationId(entity.getOrganizationId());
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);
for (CoalParameterDefEntity def : all) {
if (StringUtils.equalsIgnoreCase(def.getInputType(), "1")) {
Iterable<String> 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<Object, DefaultEdge> 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<CoalParameterDefEntity,
this.repository.deleteAllById(request.getIds());
List<CoalParameterDefEntity> allDef = this.repository.findByOrganizationId(Ctx.currentUser().getOrganizationId());
Optional<CoalParameterDefEntity> type1 = allDef.stream().filter(x -> StringUtils.equalsIgnoreCase(x.getInputType(), "1")).findAny();
type1.ifPresent(this::updateFormula);
}
@@ -125,7 +229,6 @@ public class CoalParameterDefService extends BaseService<CoalParameterDefEntity,
}
}
}

View File

@@ -57,6 +57,9 @@ public class DictionaryService extends BaseService<DictionaryEntity, DictionaryR
public DictionaryTreeDto tree(DictTreeRequest request) {
DictionaryEntity dict = this.repository.findByCode(request.getCode());
if (dict == null) {
throw new BizException("数据字典 %s 不存在".formatted(request.getCode()));
}
boolean flatten = StringUtils.equalsIgnoreCase(dict.getComponentType(), "1");
DictionaryTreeDto ans = this.mapper.toDictTree(dict);
if (dict.getDictType().equalsIgnoreCase("1")) {

View File

@@ -91,5 +91,20 @@
"name": "原煤"
}
]
},
{
"code": "coalParameter.inputType",
"name": "煤参数录入方式",
"item": [
{
"code": "0",
"name": "手工录入"
},
{
"code": "1",
"name": "自动计算"
}
]
}
]

View File

@@ -0,0 +1,22 @@
package scripts.dict
import cn.lihongjie.coal.base.dto.CommonQuery
import cn.lihongjie.coal.coalInfo.controller.CoalInfoController
import cn.lihongjie.coal.coalInfo.entity.CoalInfoEntity
import org.springframework.context.ApplicationContext
ApplicationContext ioc = ioc;
def controller = ioc.getBean(CoalInfoController.class)
return controller.list(new CommonQuery()).content.collect {
def entity = new CoalInfoEntity()
entity.id = it.id
entity.code = it.code
entity.name = it.supplierName + "-" + it.name
return entity;
}