feat: 新增同比数据

This commit is contained in:
han0
2024-11-11 17:26:32 +08:00
parent 007376fbf9
commit 9d136721f8
2 changed files with 31 additions and 12 deletions

View File

@@ -76,12 +76,17 @@ public class PublicController {
var currentData = pricePublishService.list(query); var currentData = pricePublishService.list(query);
LambdaQueryWrapper<PricePublish> queryPrevious = pricePublishService.getQuery(year - 1, month, null, name, spec, PricePublishType.CURRENT); var queryYoy = pricePublishService.getQuery(year - 1, month, null, name, spec, PricePublishType.CURRENT);
queryPrevious.isNotNull(PricePublish::getMaterialId); // 材料编号禁止为空 queryYoy.isNotNull(PricePublish::getMaterialId); // 材料编号禁止为空
var previousData = pricePublishService.list(queryPrevious); var yoyData = pricePublishService.list(queryYoy);
// 计算同比, 组合 var momDate = LocalDate.of(year, month, 1).plusMonths(1);
var result = PublicResponse.list(currentData, previousData, region); 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(); return FuHttpResponse.Builder().dataResponse(result).build();
} }

View File

@@ -67,17 +67,24 @@ public class PublicResponse {
private Float yoy; private Float yoy;
static public List<PublicResponse> list(List<PricePublish> currentData, List<PricePublish> previousData, String region) { private Float mom;
var previousPriceMap = previousData.stream().collect(Collectors.toMap(
static public List<PublicResponse> list(List<PricePublish> currentData, List<PricePublish> yoyData, List<PricePublish> momData, String region) {
var yoyPriceMap = yoyData.stream().collect(Collectors.toMap(
PricePublish::getMaterialId, PricePublish::getMaterialId,
item -> item.getRegionPrice(region), item -> item.getRegionPrice(region),
(v1, v2) -> (BigDecimal)v2 (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; return result;
} }
public PublicResponse(PricePublish item, Map<String, BigDecimal> previousPriceMap, String region) { public PublicResponse(PricePublish item, Map<String, BigDecimal> yoyPriceMap, Map<String, BigDecimal> momPriceMap, String region) {
this.updateUserId = item.getUpdateUserId(); this.updateUserId = item.getUpdateUserId();
this.updateUserName = item.getUpdateUserName(); this.updateUserName = item.getUpdateUserName();
this.updateTime = item.getUpdateTime(); this.updateTime = item.getUpdateTime();
@@ -99,11 +106,18 @@ public class PublicResponse {
this.price = item.getRegionPrice(region); this.price = item.getRegionPrice(region);
var previousPrice = previousPriceMap.getOrDefault(this.materialId, BigDecimal.valueOf(0)); var yoyPrice = yoyPriceMap.getOrDefault(this.materialId, BigDecimal.valueOf(0));
if (previousPrice.intValue() == 0) { if (yoyPrice.intValue() == 0) {
this.yoy = 0f; this.yoy = 0f;
} else { } 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();
} }
} }
} }