feat: 新增同比数据
This commit is contained in:
@@ -76,12 +76,17 @@ public class PublicController {
|
||||
|
||||
var currentData = pricePublishService.list(query);
|
||||
|
||||
LambdaQueryWrapper<PricePublish> 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();
|
||||
}
|
||||
|
@@ -67,17 +67,24 @@ public class PublicResponse {
|
||||
|
||||
private Float yoy;
|
||||
|
||||
static public List<PublicResponse> list(List<PricePublish> currentData, List<PricePublish> previousData, String region) {
|
||||
var previousPriceMap = previousData.stream().collect(Collectors.toMap(
|
||||
private Float mom;
|
||||
|
||||
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,
|
||||
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<String, BigDecimal> previousPriceMap, String region) {
|
||||
public PublicResponse(PricePublish item, Map<String, BigDecimal> yoyPriceMap, Map<String, BigDecimal> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user