diff --git a/src/main/java/mjkf/xinke/main/controller/PublicController.java b/src/main/java/mjkf/xinke/main/controller/PublicController.java index 5759a2c..5abca16 100644 --- a/src/main/java/mjkf/xinke/main/controller/PublicController.java +++ b/src/main/java/mjkf/xinke/main/controller/PublicController.java @@ -76,12 +76,17 @@ public class PublicController { var currentData = pricePublishService.list(query); - LambdaQueryWrapper queryPrevious = pricePublishService.getQuery(year - 1, month, null, name, spec, PricePublishType.CURRENT); - queryPrevious.isNotNull(PricePublish::getMaterialId); // 材料编号禁止为空 - var previousData = pricePublishService.list(queryPrevious); + var queryYoy = pricePublishService.getQuery(year - 1, month, null, name, spec, PricePublishType.CURRENT); + queryYoy.isNotNull(PricePublish::getMaterialId); // 材料编号禁止为空 + var yoyData = pricePublishService.list(queryYoy); - // 计算同比, 组合 - var result = PublicResponse.list(currentData, previousData, region); + var momDate = LocalDate.of(year, month, 1).plusMonths(1); + var queryMom = pricePublishService.getQuery(momDate.getYear(), momDate.getMonthValue(), null, name, spec, PricePublishType.CURRENT); + queryMom.isNotNull(PricePublish::getMaterialId); + var momData = pricePublishService.list(queryMom); + + // 计算同比、环比 组合 + var result = PublicResponse.list(currentData, yoyData, momData, region); return FuHttpResponse.Builder().dataResponse(result).build(); } diff --git a/src/main/java/mjkf/xinke/main/model/vo/PublicResponse.java b/src/main/java/mjkf/xinke/main/model/vo/PublicResponse.java index d32a625..8f348a1 100644 --- a/src/main/java/mjkf/xinke/main/model/vo/PublicResponse.java +++ b/src/main/java/mjkf/xinke/main/model/vo/PublicResponse.java @@ -67,17 +67,24 @@ public class PublicResponse { private Float yoy; - static public List list(List currentData, List previousData, String region) { - var previousPriceMap = previousData.stream().collect(Collectors.toMap( + private Float mom; + + static public List list(List currentData, List yoyData, List momData, String region) { + var yoyPriceMap = yoyData.stream().collect(Collectors.toMap( PricePublish::getMaterialId, item -> item.getRegionPrice(region), (v1, v2) -> (BigDecimal)v2 )); - var result = currentData.stream().map(item -> new PublicResponse(item, previousPriceMap, region)).collect(Collectors.toList()); + var momPriceMap = momData.stream().collect(Collectors.toMap( + PricePublish::getMaterialId, + item -> item.getRegionPrice(region), + (v1, v2) -> (BigDecimal)v2 + )); + var result = currentData.stream().map(item -> new PublicResponse(item, yoyPriceMap, momPriceMap, region)).collect(Collectors.toList()); return result; } - public PublicResponse(PricePublish item, Map previousPriceMap, String region) { + public PublicResponse(PricePublish item, Map yoyPriceMap, Map momPriceMap, String region) { this.updateUserId = item.getUpdateUserId(); this.updateUserName = item.getUpdateUserName(); this.updateTime = item.getUpdateTime(); @@ -99,11 +106,18 @@ public class PublicResponse { this.price = item.getRegionPrice(region); - var previousPrice = previousPriceMap.getOrDefault(this.materialId, BigDecimal.valueOf(0)); - if (previousPrice.intValue() == 0) { + var yoyPrice = yoyPriceMap.getOrDefault(this.materialId, BigDecimal.valueOf(0)); + if (yoyPrice.intValue() == 0) { this.yoy = 0f; } else { - this.yoy = (this.price.floatValue() - previousPrice.floatValue()) / previousPrice.floatValue(); + this.yoy = (this.price.floatValue() - yoyPrice.floatValue()) / yoyPrice.floatValue(); + } + + var momPrice = momPriceMap.getOrDefault(this.materialId, BigDecimal.valueOf(0)); + if (momPrice.intValue() == 0) { + this.mom = 0f; + } else { + this.mom = (this.price.floatValue() - momPrice.floatValue()) / momPrice.floatValue(); } } }