diff --git a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java index b7fb4333..c5950b40 100644 --- a/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java +++ b/src/main/java/cn/lihongjie/coal/coalWashingDailyAnalysis/entity/CoalWashingDailyAnalysisEntity.java @@ -1,6 +1,9 @@ package cn.lihongjie.coal.coalWashingDailyAnalysis.entity; import cn.lihongjie.coal.base.entity.OrgCommonEntity; +import cn.lihongjie.coal.exception.BizException; + +import io.vavr.Function3; import jakarta.persistence.*; @@ -100,6 +103,21 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { inputItems.sort( Comparator.comparing(x -> ObjectUtils.defaultIfNull(x.getTime(), LocalTime.MIN))); + for(int i = 1; i < inputItems.size(); i++) { + var c = inputItems.get(i); + var p = inputItems.get(i - 1); + + if (c.getTime() == null || p.getTime() == null){ + throw new BizException("时间不能为空"); + } + + if (c.getTime().equals(p.getTime())){ + throw new BizException("时间重复 " + c.getTime()); + } + } + + + for (int i = 0; i < inputItems.size(); i++) { CoalWashingDailyAnalysisItemVo coalWashingDailyAnalysisItemVo = inputItems.get(i); if (StringUtils.equals(type, "2") && i == inputItems.size() - 1) { @@ -112,186 +130,100 @@ public class CoalWashingDailyAnalysisEntity extends OrgCommonEntity { } Double prevNumber = ObjectUtils.defaultIfNull(initTotalNumber, 0.0); - // 总的权重 - Double totalWeight1 = 0.0; - Double totalWeight2 = 0.0; - // 大堆平均之和 - Double avgSum1 = 0.0; - Double avgSum2 = 0.0; - for (CoalWashingDailyAnalysisItemVo inputItem : inputItems) { + Function3< + List, + Integer, + CoalWashingDailyAnalysisItemVo, + Object> + getWeight = + (list, index, item) -> { + Double p = null; + if (index == 0) { - double currWeight = - inputItem.getTotalNumber() == null - ? 1 - : Double.max(1, inputItem.getTotalNumber() - prevNumber); + p = null; - if (inputItem.getAvgDdp1() != null) { - totalWeight1 += currWeight; + } else { + p = list.get(index - 1).getTotalNumber(); + } - avgSum1 += ObjectUtils.defaultIfNull(inputItem.getAvgDdp1(), 0.0) * currWeight; - } - try { + Double c = list.get(index).getTotalNumber(); - inputItem.setRollingAvgDdp1(avgSum1 / totalWeight1); - } catch (Exception e) { + if (p == null || c == null) { + return 1; + } - } + return Math.max(1, c - p); + }; + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getAvgDdp1, + getWeight, + (x, v) -> x.setRollingAvgDdp1(v == null ? null : v.doubleValue())); - if (inputItem.getAvgDdp2() != null) { - totalWeight2 += currWeight; - avgSum2 += ObjectUtils.defaultIfNull(inputItem.getAvgDdp2(), 0.0) * currWeight; - } - try { - inputItem.setRollingAvgDdp2(avgSum2 / totalWeight2); - } catch (Exception e) { + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getAvgDdp2, + getWeight, + (x, v) -> x.setRollingAvgDdp2(v == null ? null : v.doubleValue())); - } - } + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getFddp1, + getWeight, + (x, v) -> x.setFddAvgP1(v == null ? null : v.doubleValue())); - for (int i = 0; i < inputItems.size(); i++) { - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setFddAvgP1(0.0); + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getFddp2, + getWeight, + (x, v) -> x.setFddAvgP2(v == null ? null : v.doubleValue())); - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getFddp1() == null) { - nullCount++; - } else { - item.setFddAvgP1( - ObjectUtils.defaultIfNull(inputItems.get(j).getFddp1(), 0.0) - + item.getFddAvgP1()); - } - } - if (i - nullCount + 1 > 0) { - item.setFddAvgP1(item.getFddAvgP1() / (double) (i - nullCount + 1)); - } else { - item.setFddAvgP1(null); - } - } + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getSysDdp1, + getWeight, + (x, v) -> x.setSysDdAvgP1(v == null ? null : v.doubleValue())); - for (int i = 0; i < inputItems.size(); i++) { - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setFddAvgP2(0.0); - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getFddp2() == null) { - nullCount++; - } else { + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getSysDdp2, + getWeight, + (x, v) -> x.setSysDdAvgP2(v == null ? null : v.doubleValue())); - item.setFddAvgP2( - ObjectUtils.defaultIfNull(inputItems.get(j).getFddp2(), 0.0) - + item.getFddAvgP2()); - } - } - if (i - nullCount + 1 > 0) { - item.setFddAvgP2(item.getFddAvgP2() / (double) (i - nullCount + 1)); - } else { - item.setFddAvgP2(null); - } - } - for (int i = 0; i < inputItems.size(); i++) { + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getDdp1, + getWeight, + (x, v) -> x.setDdAvgP1(v == null ? null : v.doubleValue())); - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setSysDdAvgP1(0.0); - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getSysDdp1() == null) { - nullCount++; - } else { - item.setSysDdAvgP1( - ObjectUtils.defaultIfNull(inputItems.get(j).getSysDdp1(), 0.0) - + item.getSysDdAvgP1()); - } - } - if (i - nullCount + 1 > 0) { + cn.lihongjie.coal.common.CollectionUtils.rollingAvg( + inputItems, + Comparator.comparing(CoalWashingDailyAnalysisItemVo::getTime), + CoalWashingDailyAnalysisItemVo::getDdp2, + getWeight, + (x, v) -> x.setDdAvgP2(v == null ? null : v.doubleValue())); - item.setSysDdAvgP1(item.getSysDdAvgP1() / (double) (i - nullCount + 1)); - } else { - item.setSysDdAvgP1(null); - } - } - for (int i = 0; i < inputItems.size(); i++) { - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setSysDdAvgP2(0.0); - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getSysDdp2() == null) { - nullCount++; - } else { - item.setSysDdAvgP2( - ObjectUtils.defaultIfNull(inputItems.get(j).getSysDdp2(), 0.0) - + item.getSysDdAvgP2()); - } - } - if (i - nullCount + 1 > 0) { - - item.setSysDdAvgP2(item.getSysDdAvgP2() / (double) (i - nullCount + 1)); - } else { - item.setSysDdAvgP2(null); - } - } - - for (int i = 0; i < inputItems.size(); i++) { - - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setDdAvgP1(0.0); - - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getDdp1() == null) { - nullCount++; - } else { - - item.setDdAvgP1( - ObjectUtils.defaultIfNull(inputItems.get(j).getDdp1(), 0.0) - + item.getDdAvgP1()); - } - } - if (i - nullCount + 1 > 0) { - - item.setDdAvgP1(item.getDdAvgP1() / (double) (i - nullCount + 1)); - } else { - item.setDdAvgP1(null); - } - } - - for (int i = 0; i < inputItems.size(); i++) { - - CoalWashingDailyAnalysisItemVo item = inputItems.get(i); - item.setDdAvgP2(0.0); - - int nullCount = 0; - for (int j = 0; j <= i; j++) { - if (inputItems.get(j).getDdp2() == null) { - nullCount++; - } else { - - item.setDdAvgP2( - ObjectUtils.defaultIfNull(inputItems.get(j).getDdp2(), 0.0) - + item.getDdAvgP2()); - } - } - if (i - nullCount + 1 > 0) { - - item.setDdAvgP2(item.getDdAvgP2() / (double) (i - nullCount + 1)); - } else { - item.setDdAvgP2(null); - } - } } } diff --git a/src/main/java/cn/lihongjie/coal/common/CollectionUtils.java b/src/main/java/cn/lihongjie/coal/common/CollectionUtils.java index 32754e53..d05f505a 100644 --- a/src/main/java/cn/lihongjie/coal/common/CollectionUtils.java +++ b/src/main/java/cn/lihongjie/coal/common/CollectionUtils.java @@ -1,5 +1,6 @@ package cn.lihongjie.coal.common; +import io.vavr.Function3; import io.vavr.Tuple2; import lombok.experimental.UtilityClass; @@ -7,6 +8,7 @@ import lombok.experimental.UtilityClass; import org.apache.commons.lang3.ObjectUtils; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.*; import java.util.function.BiConsumer; import java.util.function.BiFunction; @@ -113,14 +115,14 @@ public class CollectionUtils { Comparator comparator, Function getVal, BiConsumer setAvg) { - rollingAvg(src, comparator, getVal, t -> 1, setAvg); + rollingAvg(src, comparator, getVal, (a,b, c) -> 1, setAvg); } public static void rollingAvg( List src, Comparator comparator, Function getVal, - Function getWeight, + Function3, Integer, T, Object> getWeight, BiConsumer setAvg) { if (org.apache.commons.collections4.CollectionUtils.isEmpty(src)) { @@ -138,7 +140,7 @@ public class CollectionUtils { T t = list.get(i); BigDecimal value = toBigDecimal(getVal.apply(t)); - BigDecimal weight = toBigDecimal(getWeight.apply(t)); + BigDecimal weight = toBigDecimal(getWeight.apply(list, i, t)); if (value != null) { @@ -152,7 +154,7 @@ public class CollectionUtils { setAvg.accept(t, null); } else { - setAvg.accept(t, valueSum.divide(weightSum)); + setAvg.accept(t, valueSum.divide(weightSum, 6, RoundingMode.HALF_UP)); } } }