Commit 223c2e6f authored by lixuan's avatar lixuan

feat: 需求

parent f6a66148
Pipeline #147208 failed with stages
in 0 seconds
......@@ -435,96 +435,132 @@ public class HouseResourceServiceImpl implements HouseResourceService {
}
}
/** 行业分类固定顺序,用于并发查询各行业的年销售合计。 */
private static final List<String> INDUSTRY_CLASSIFICATIONS = Arrays.asList(
"工业", "建筑业", "批发", "零售", "住宿", "餐饮", "服务业", "其他");
@Override
public BusinessEntityStatisticsDetail businessEntityStatistics(HouseResourcePageQuery query) {
List<HouseResourcePage> list = houseResourceMapper.selectProfileGraph(query);
BusinessEntityStatisticsDetail detail = new BusinessEntityStatisticsDetail();
if (CollectionUtils.isEmpty(list)) {
return new BusinessEntityStatisticsDetail();
}
BusinessEntityStatisticsDetail businessEntityStatisticsDetail = new BusinessEntityStatisticsDetail();
CompletableFuture<Long> lyCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 1).count(), executor);
CompletableFuture<Long> jdCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 4).count(), executor);
CompletableFuture<Long> jqCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 5).count(), executor);
CompletableFuture<Long> zhtCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 6).count(), executor);
CompletableFuture<Long> scCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 7).count(), executor);
CompletableFuture<Integer> sjLyCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 1).collect(Collectors.groupingBy(HouseResourcePage::getThree)).size(), executor);
CompletableFuture<Integer> sjJdCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 4).collect(Collectors.groupingBy(HouseResourcePage::getThree)).size(), executor);
CompletableFuture<Integer> sjJqCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 5).collect(Collectors.groupingBy(HouseResourcePage::getThree)).size(), executor);
CompletableFuture<Integer> sjZhtCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 6).collect(Collectors.groupingBy(HouseResourcePage::getThree)).size(), executor);
CompletableFuture<Integer> sjScCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getType() == 7).collect(Collectors.groupingBy(HouseResourcePage::getThree)).size(), executor);
CompletableFuture<Long> zlCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceAttribute() == 0).count(), executor);
CompletableFuture<Long> zyCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceAttribute() == 2).count(), executor);
CompletableFuture<Long> qtCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceAttribute() == 1).count(), executor);
CompletableFuture<Long> zaiyingCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceType() == 0).count(), executor);
CompletableFuture<Long> xzCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceType() == 1).count(), executor);
CompletableFuture<Long> gkCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceType() == 2).count(), executor);
CompletableFuture<Long> sbCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceType() == 3).count(), executor);
CompletableFuture<Long> zyzyCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> x.getHouseResourceType() == 4).count(), executor);
CompletableFuture<BigDecimal> xsCountFuture = CompletableFuture.supplyAsync(() -> list.stream().map(HouseResourcePage::getIncome).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add), executor);
CompletableFuture<BigDecimal> ssCountFuture = CompletableFuture.supplyAsync(() -> list.stream().map(HouseResourcePage::getTaxCount).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add), executor);
CompletableFuture<Long> gsCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> Objects.nonNull(x.getGs()) && x.getGs().equals(Boolean.TRUE)).count(), executor);
CompletableFuture<BigDecimal> gsSsCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> Objects.nonNull(x.getGs()) && x.getGs().equals(Boolean.TRUE)).map(HouseResourcePage::getTaxCount).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add), executor);
CompletableFuture<BigDecimal> gsXsCountFuture = CompletableFuture.supplyAsync(() -> list.stream().filter(x -> Objects.nonNull(x.getGs()) && x.getGs().equals(Boolean.TRUE)).map(HouseResourcePage::getIncome).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add), executor);
List<BusinessEntityInfo> businessEntityInfos = businessEntityInfoMapper.selectByIdList(list.stream().map(HouseResourcePage::getBusinessEntityInfoId).collect(Collectors.toList()));
businessEntityStatisticsDetail.setZtCount(businessEntityInfos.size());
List<String> industries = Arrays.asList("工业", "建筑业", "批发", "零售", "住宿", "餐饮", "服务业", "其他");
Map<String, CompletableFuture<BigDecimal>> industrySellFutures = industries.stream()
.collect(Collectors.toMap(industry -> industry,
industry -> CompletableFuture.supplyAsync(() -> getIndustrySellCount(businessEntityInfos, industry, businessEntitySellMapper), executor)));
businessEntityStatisticsDetail.setLyCount(lyCountFuture.join());
businessEntityStatisticsDetail.setJdCount(jdCountFuture.join());
businessEntityStatisticsDetail.setJqCount(jqCountFuture.join());
businessEntityStatisticsDetail.setZhtCount(zhtCountFuture.join());
businessEntityStatisticsDetail.setScCount(scCountFuture.join());
businessEntityStatisticsDetail.setSjLyCount(sjLyCountFuture.join());
businessEntityStatisticsDetail.setSjJdCount(sjJdCountFuture.join());
businessEntityStatisticsDetail.setSjJqCount(sjJqCountFuture.join());
businessEntityStatisticsDetail.setSjZhtCount(sjZhtCountFuture.join());
businessEntityStatisticsDetail.setSjScCount(sjScCountFuture.join());
businessEntityStatisticsDetail.setZlCount(zlCountFuture.join());
businessEntityStatisticsDetail.setZyCount(zyCountFuture.join());
businessEntityStatisticsDetail.setQtCount(qtCountFuture.join());
businessEntityStatisticsDetail.setZaiyingCount(zaiyingCountFuture.join());
businessEntityStatisticsDetail.setXzCount(xzCountFuture.join());
businessEntityStatisticsDetail.setGkCount(gkCountFuture.join());
businessEntityStatisticsDetail.setSbCount(sbCountFuture.join());
businessEntityStatisticsDetail.setZyzyCount(zyzyCountFuture.join());
businessEntityStatisticsDetail.setXsCount(xsCountFuture.join());
businessEntityStatisticsDetail.setSsCount(ssCountFuture.join());
businessEntityStatisticsDetail.setGsCount(gsCountFuture.join());
businessEntityStatisticsDetail.setGsSsCount(gsSsCountFuture.join());
businessEntityStatisticsDetail.setGsXsCount(gsXsCountFuture.join());
businessEntityStatisticsDetail.setGyCount(industrySellFutures.get("工业").join());
businessEntityStatisticsDetail.setJzyCount(industrySellFutures.get("建筑业").join());
businessEntityStatisticsDetail.setPfCount(industrySellFutures.get("批发").join());
businessEntityStatisticsDetail.setLsCount(industrySellFutures.get("零售").join());
businessEntityStatisticsDetail.setZsCount(industrySellFutures.get("住宿").join());
businessEntityStatisticsDetail.setCyCount(industrySellFutures.get("餐饮").join());
businessEntityStatisticsDetail.setFwyCount(industrySellFutures.get("服务业").join());
businessEntityStatisticsDetail.setQtjjfzCount(industrySellFutures.get("其他").join());
businessEntityStatisticsDetail.setJjzl(businessEntityStatisticsDetail.getGyCount()
.add(businessEntityStatisticsDetail.getJzyCount())
.add(businessEntityStatisticsDetail.getPfCount())
.add(businessEntityStatisticsDetail.getLsCount())
.add(businessEntityStatisticsDetail.getZsCount())
.add(businessEntityStatisticsDetail.getCyCount())
.add(businessEntityStatisticsDetail.getFwyCount())
.add(businessEntityStatisticsDetail.getQtjjfzCount()));
return businessEntityStatisticsDetail;
return detail;
}
// 原实现起了 20+ 个 CompletableFuture 各扫一遍 list,纯内存操作没有并发收益,
// 反而让口径分散。改为单遍累加:type / 房源属性 / 房源类型 / 收入税收 / 规上统计全部一次走完。
long lyCount = 0, jdCount = 0, jqCount = 0, zhtCount = 0, scCount = 0;
Set<String> sjLy = new HashSet<>();
Set<String> sjJd = new HashSet<>();
Set<String> sjJq = new HashSet<>();
Set<String> sjZht = new HashSet<>();
Set<String> sjSc = new HashSet<>();
long zlCount = 0, zyCount = 0, qtCount = 0;
long zaiyingCount = 0, xzCount = 0, gkCount = 0, sbCount = 0, zyzyCount = 0;
BigDecimal xsSum = BigDecimal.ZERO;
BigDecimal ssSum = BigDecimal.ZERO;
long gsCount = 0;
BigDecimal gsSsSum = BigDecimal.ZERO;
BigDecimal gsXsSum = BigDecimal.ZERO;
for (HouseResourcePage x : list) {
String three = x.getThree();
switch (x.getType()) {
case 1: lyCount++; sjLy.add(three); break;
case 4: jdCount++; sjJd.add(three); break;
case 5: jqCount++; sjJq.add(three); break;
case 6: zhtCount++; sjZht.add(three); break;
case 7: scCount++; sjSc.add(three); break;
default: break;
}
switch (x.getHouseResourceAttribute()) {
case 0: zlCount++; break;
case 1: qtCount++; break;
case 2: zyCount++; break;
default: break;
}
switch (x.getHouseResourceType()) {
case 0: zaiyingCount++; break;
case 1: xzCount++; break;
case 2: gkCount++; break;
case 3: sbCount++; break;
case 4: zyzyCount++; break;
default: break;
}
if (x.getIncome() != null) xsSum = xsSum.add(x.getIncome());
if (x.getTaxCount() != null) ssSum = ssSum.add(x.getTaxCount());
if (Boolean.TRUE.equals(x.getGs())) {
gsCount++;
if (x.getTaxCount() != null) gsSsSum = gsSsSum.add(x.getTaxCount());
if (x.getIncome() != null) gsXsSum = gsXsSum.add(x.getIncome());
}
}
detail.setLyCount(lyCount);
detail.setJdCount(jdCount);
detail.setJqCount(jqCount);
detail.setZhtCount(zhtCount);
detail.setScCount(scCount);
detail.setSjLyCount(sjLy.size());
detail.setSjJdCount(sjJd.size());
detail.setSjJqCount(sjJq.size());
detail.setSjZhtCount(sjZht.size());
detail.setSjScCount(sjSc.size());
detail.setZlCount(zlCount);
detail.setZyCount(zyCount);
detail.setQtCount(qtCount);
detail.setZaiyingCount(zaiyingCount);
detail.setXzCount(xzCount);
detail.setGkCount(gkCount);
detail.setSbCount(sbCount);
detail.setZyzyCount(zyzyCount);
detail.setXsCount(xsSum);
detail.setSsCount(ssSum);
detail.setGsCount(gsCount);
detail.setGsSsCount(gsSsSum);
detail.setGsXsCount(gsXsSum);
// 主体数量
List<String> entityIds = list.stream()
.map(HouseResourcePage::getBusinessEntityInfoId)
.collect(Collectors.toList());
List<BusinessEntityInfo> businessEntityInfos = businessEntityInfoMapper.selectByIdList(entityIds);
detail.setZtCount(businessEntityInfos.size());
// 行业销售:每个行业是一次独立的 DB 查询,保留并发
Map<String, BigDecimal> industrySums = computeIndustrySells(businessEntityInfos);
detail.setGyCount(industrySums.getOrDefault("工业", BigDecimal.ZERO));
detail.setJzyCount(industrySums.getOrDefault("建筑业", BigDecimal.ZERO));
detail.setPfCount(industrySums.getOrDefault("批发", BigDecimal.ZERO));
detail.setLsCount(industrySums.getOrDefault("零售", BigDecimal.ZERO));
detail.setZsCount(industrySums.getOrDefault("住宿", BigDecimal.ZERO));
detail.setCyCount(industrySums.getOrDefault("餐饮", BigDecimal.ZERO));
detail.setFwyCount(industrySums.getOrDefault("服务业", BigDecimal.ZERO));
detail.setQtjjfzCount(industrySums.getOrDefault("其他", BigDecimal.ZERO));
detail.setJjzl(industrySums.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add));
return detail;
}
/** 按行业并发查询每个分类下的年销售合计。 */
private Map<String, BigDecimal> computeIndustrySells(List<BusinessEntityInfo> infos) {
Map<String, CompletableFuture<BigDecimal>> futures = INDUSTRY_CLASSIFICATIONS.stream()
.collect(Collectors.toMap(
Function.identity(),
industry -> CompletableFuture.supplyAsync(
() -> getIndustrySellCount(infos, industry, businessEntitySellMapper),
executor)));
Map<String, BigDecimal> result = new LinkedHashMap<>();
futures.forEach((k, f) -> result.put(k, f.join()));
return result;
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment