feat(coal-washing): 添加日期字段并优化时间比较逻辑

- 在 CoalWashingDailyAnalysisItemVo 中添加 date 字段
-优化 CoalWashingDailyAnalysisEntity 中的时间比较逻辑,考虑日期因素- 更新 CoalWashingDailyAnalysisService 中的相关方法,支持新的日期字段
This commit is contained in:
2025-02-17 21:26:50 +08:00
parent 7e9e996d7d
commit 388818b5af
3 changed files with 92 additions and 67 deletions

View File

@@ -16,6 +16,7 @@ import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.jetbrains.annotations.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -31,12 +32,13 @@ import java.util.stream.Collectors;
@Entity
@Data
@Comment("洗煤报告表")
@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_coal_washing_daily_analysis_org_id", columnList = "organization_id"))
@jakarta.persistence.Table(
indexes =
@jakarta.persistence.Index(
name = "idx_coal_washing_daily_analysis_org_id",
columnList = "organization_id"))
public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity {
@ManyToOne
@Comment("产品信息")
private ProductEntity product;
@@ -144,8 +146,18 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity {
!coalWashingDailyAnalysisItemVo1.isAllEmpty())
.collect(Collectors.toList());
for (CoalWashingDailyAnalysisItemVo inputItem : inputItems) {
if (inputItem.getDate() == null) {
inputItem.setDate(this.date);
}
}
inputItems.sort(
Comparator.comparing(x -> ObjectUtils.defaultIfNull(x.getTime(), LocalTime.MIN)));
Comparator.comparing(
x ->
LocalDateTime.of(
ObjectUtils.defaultIfNull(x.getDate(), this.date),
ObjectUtils.defaultIfNull(x.getTime(), LocalTime.MIN))));
for (int i = 1; i < inputItems.size(); i++) {
var c = inputItems.get(i);
@@ -155,8 +167,8 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity {
throw new BizException("时间不能为空");
}
if (c.getTime().equals(p.getTime())) {
throw new BizException("时间重复 " + c.getTime());
if (c.getTime().equals(p.getTime()) && c.getDate().equals(p.getDate())) {
throw new BizException("时间重复 " + c.getDate() + " " + c.getTime());
}
}
@@ -199,61 +211,70 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity {
};
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getAvgDdp1,
getWeight,
(x, v) -> x.setRollingAvgDdp1(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getAvgDdp2,
getWeight,
(x, v) -> x.setRollingAvgDdp2(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getFddp1,
(x, y, z) -> 1,
(x, v) -> x.setFddAvgP1(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getFddp2,
(x, y, z) -> 1,
(x, v) -> x.setFddAvgP2(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getSysDdp1,
getWeight,
(x, v) -> x.setSysDdAvgP1(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getSysDdp2,
getWeight,
(x, v) -> x.setSysDdAvgP2(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getDdp1,
getWeight,
(x, v) -> x.setDdAvgP1(v == null ? null : v.doubleValue()));
cn.lihongjie.coal.common.CollectionUtils.rollingAvg(
inputItems,
Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime),
getComparing(),
CoalWashingDailyAnalysisItemVo::getDdp2,
getWeight,
(x, v) -> x.setDdAvgP2(v == null ? null : v.doubleValue()));
}
@NotNull
private Comparator<CoalWashingDailyAnalysisItemVo> getComparing() {
return Comparator.comparing(
x ->
LocalDateTime.of(
ObjectUtils.defaultIfNull(x.getDate(), this.date),
ObjectUtils.defaultIfNull(x.getTime(), LocalTime.MIN)));
}
@Override
public void prePersist() {
super.prePersist();

View File

@@ -14,6 +14,7 @@ import lombok.With;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Objects;
@@ -23,6 +24,9 @@ import java.util.Objects;
@AllArgsConstructor
public class CoalWashingDailyAnalysisItemVo {
@Comment("日期")
private LocalDate date;
@Comment("时间")
private LocalTime time;

View File

@@ -15,8 +15,8 @@ import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.common.FreeMakerUtils;
import cn.lihongjie.coal.common.JpaUtils;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.product.service.ProductService;
import com.google.common.base.CaseFormat;
import jakarta.annotation.PostConstruct;
@@ -57,6 +57,8 @@ public class CoalWashingDailyAnalysisService
@PostConstruct
public void init() {}
@Autowired ProductService productService;
public CoalWashingDailyAnalysisDto create(CreateCoalWashingDailyAnalysisDto request) {
CoalWashingDailyAnalysisEntity entity = mapper.toEntity(request);
@@ -71,20 +73,6 @@ public class CoalWashingDailyAnalysisService
return getById(entity.getId());
}
private void validate(CoalWashingDailyAnalysisEntity entity) {
if (CollectionUtils.isNotEmpty(entity.getInputItems())){
entity.getInputItems().forEach(item -> {
if (item.getTime() == null){
throw new BizException("时间不能为空");
}
});
}
}
public CoalWashingDailyAnalysisDto calculate(CreateCoalWashingDailyAnalysisDto request) {
CoalWashingDailyAnalysisEntity entity = mapper.toEntity(request);
@@ -94,12 +82,29 @@ public class CoalWashingDailyAnalysisService
return mapper.toDto(roundMapper.round(entity));
}
@Autowired
ProductService productService;
private void validate(CoalWashingDailyAnalysisEntity entity) {
if (CollectionUtils.isNotEmpty(entity.getInputItems())) {
entity.getInputItems()
.forEach(
item -> {
if (item.getTime() == null) {
throw new BizException("时间不能为空");
}
});
entity.getInputItems()
.forEach(
item -> {
if (item.getDate() == null) {
item.setDate(entity.getDate());
}
});
}
}
public CoalWashingDailyAnalysisDto update(UpdateCoalWashingDailyAnalysisDto request) {
CoalWashingDailyAnalysisEntity entity = this.repository.get(request.getId());
if (this.repository.containArchived(request.getId())) {
@@ -109,7 +114,6 @@ public class CoalWashingDailyAnalysisService
this.mapper.updateEntity(entity, request);
entity.setOrganizationId(Ctx.currentUser().getOrganizationId());
validate(entity);
if (entity.getProduct() != null) {
@@ -162,8 +166,6 @@ public class CoalWashingDailyAnalysisService
public Object report(CoalWashingDailyAnalysisCoalReportRequest request) {
String sql =
FreeMakerUtils.render(
"""
@@ -259,6 +261,4 @@ public class CoalWashingDailyAnalysisService
return new PageImpl<>(ans, PageRequest.of(0, request.getPageSize()), count);
}
}