mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 11:09:19 +08:00
refactor: 移除工作台多维分析、销售额排行与区间对比
This commit is contained in:
-21
@@ -10,8 +10,6 @@ import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.service.BiWorkbenchService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.dto.WorkbenchQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchFinanceSliceVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchMultiAnalysisPanelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchPeriodCompareSliceVO;
|
||||
|
||||
/**
|
||||
* BI 工作台查询:订单指标来自 Mongo 日汇聚,推广/刷单来自 MySQL。
|
||||
@@ -31,23 +29,4 @@ public class BiWorkbenchController {
|
||||
public ApiResponse<WorkbenchFinanceSliceVO> finance(@Valid @RequestBody WorkbenchQueryDTO queryDTO) {
|
||||
return ApiResponse.success("获取经营概览成功", biWorkbenchService.financeSlice(queryDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/multi-analysis")
|
||||
@RequirePermission(value = "bi:workbench:read", description = "查询工作台多维分析", allowSuper = true)
|
||||
public ApiResponse<WorkbenchMultiAnalysisPanelVO> multiAnalysis(@Valid @RequestBody WorkbenchQueryDTO queryDTO) {
|
||||
return ApiResponse.success("获取多维分析成功", biWorkbenchService.multiAnalysis(queryDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/sales-rank")
|
||||
@RequirePermission(value = "bi:workbench:read", description = "查询工作台销售额排行", allowSuper = true)
|
||||
public ApiResponse<WorkbenchMultiAnalysisPanelVO> salesRank(@Valid @RequestBody WorkbenchQueryDTO queryDTO) {
|
||||
return ApiResponse.success("获取销售额排行成功", biWorkbenchService.salesRank(queryDTO));
|
||||
}
|
||||
|
||||
@PostMapping("/period-compare/slice")
|
||||
@RequirePermission(value = "bi:workbench:read", description = "查询工作台区间对比", allowSuper = true)
|
||||
public ApiResponse<WorkbenchPeriodCompareSliceVO> periodCompareSlice(
|
||||
@Valid @RequestBody WorkbenchQueryDTO queryDTO) {
|
||||
return ApiResponse.success("获取区间对比数据成功", biWorkbenchService.periodCompareSlice(queryDTO));
|
||||
}
|
||||
}
|
||||
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 金额字段为「元」保留两位小数;前端可自行换算为「万」展示。
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WorkbenchDimRowVO {
|
||||
|
||||
private String key;
|
||||
|
||||
private String name;
|
||||
|
||||
private BigDecimal salesYuan;
|
||||
|
||||
private BigDecimal grossProfitYuan;
|
||||
|
||||
private BigDecimal netProfitYuan;
|
||||
|
||||
private BigDecimal refundYuan;
|
||||
|
||||
private BigDecimal grossMarginPct;
|
||||
|
||||
private BigDecimal netMarginPct;
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WorkbenchMultiAnalysisPanelVO {
|
||||
|
||||
private List<WorkbenchDimRowVO> platform;
|
||||
|
||||
private List<WorkbenchDimRowVO> shop;
|
||||
|
||||
private List<WorkbenchDimRowVO> category;
|
||||
|
||||
private List<WorkbenchDimRowVO> product;
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.model.vo.CompanyOverviewVO;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 工作台「区间对比」单段日期聚合:与底部店铺汇总表「合计」行同源({@link CompanyOverviewVO.ShopSummary})。
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class WorkbenchPeriodCompareSliceVO {
|
||||
|
||||
private LocalDate startDate;
|
||||
|
||||
private LocalDate endDate;
|
||||
|
||||
private CompanyOverviewVO.ShopSummary total;
|
||||
}
|
||||
-8
@@ -4,8 +4,6 @@ import top.xyquan.ecomdmbbackend.modules.bi.overview.model.vo.CompanyOverviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.dto.WorkbenchExportDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.dto.WorkbenchQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchFinanceSliceVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchMultiAnalysisPanelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchPeriodCompareSliceVO;
|
||||
|
||||
/**
|
||||
* BI 工作台:读 Mongo 订单日汇聚 + MySQL 推广/刷单。
|
||||
@@ -14,12 +12,6 @@ public interface BiWorkbenchService {
|
||||
|
||||
WorkbenchFinanceSliceVO financeSlice(WorkbenchQueryDTO queryDTO);
|
||||
|
||||
WorkbenchMultiAnalysisPanelVO multiAnalysis(WorkbenchQueryDTO queryDTO);
|
||||
|
||||
WorkbenchMultiAnalysisPanelVO salesRank(WorkbenchQueryDTO queryDTO);
|
||||
|
||||
WorkbenchPeriodCompareSliceVO periodCompareSlice(WorkbenchQueryDTO queryDTO);
|
||||
|
||||
/** 与 finance 四表同口径,供导出使用 */
|
||||
CompanyOverviewVO.WorkbenchTableBundle loadSummaryTables(WorkbenchExportDTO dto);
|
||||
}
|
||||
|
||||
+5
-267
@@ -9,20 +9,13 @@ import top.xyquan.ecomdmbbackend.modules.ec.order.status.support.OrderStatusStat
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.model.vo.CompanyOverviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.overview.service.CompanyOverviewService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.platform.model.enums.PlatformCode;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.category.model.po.CategoryPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.category.service.CategoryService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.service.ShopService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.dto.WorkbenchQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchFinanceSliceVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchPeriodCompareSliceVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchCostSliceVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchDimRowVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchFinancePanelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchKpiItemVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchMultiAnalysisPanelVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.model.vo.WorkbenchTrendPointVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.query.BiOrderStatQueryService;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.workbench.service.BiWorkbenchService;
|
||||
@@ -38,11 +31,9 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -50,14 +41,11 @@ import java.util.stream.Collectors;
|
||||
* <p>
|
||||
* 口径约定:
|
||||
* <ul>
|
||||
* <li>「净销售额、成本、退款、多维行、趋势」等:Mongo 存成交/退款分桶,读侧净销售 = 成交 − 退款;
|
||||
* <li>「净销售额、成本、退款、趋势」等:Mongo 存成交/退款分桶,读侧净销售 = 成交 − 退款;
|
||||
* 成交不含已取消/未知;成本按状态开关计入。</li>
|
||||
* <li>「成交金额」为同店铺、同时间范围内<strong>全部订单状态</strong>的商家实收合计(由汇总表 KPI 提供)。</li>
|
||||
* <li>推广费、刷单费为区间汇总;多维行与趋势中的「净利润」按销售额占比将区间推广、刷单、税额<strong>分摊</strong>到各维度或各日,
|
||||
* 与 {@link #totalsFromOrderRows} 得到的区间总毛利润、总税额自洽。</li>
|
||||
* <li>「成交金额」为同店铺、同时间范围内成交状态商家实收合计。</li>
|
||||
* <li>推广费、刷单费为区间汇总;趋势中的「净利润」按销售额占比将区间推广、刷单、税额分摊到各日。</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* 分 Tab 输出:{@link #financeSlice}、{@link #multiAnalysis}、{@link #salesRank}。
|
||||
*/
|
||||
@Service
|
||||
public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
@@ -70,8 +58,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final ShopService shopService;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
private final ProductMapper productMapper;
|
||||
private final CategoryService categoryService;
|
||||
private final CompanyOverviewService companyOverviewService;
|
||||
private final BiOrderStatQueryService orderStatQueryService;
|
||||
private final BiWorkbenchFeeLoader feeLoader;
|
||||
@@ -79,16 +65,12 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
public BiWorkbenchServiceImpl(CurrentUserContext currentUserContext,
|
||||
ShopService shopService,
|
||||
OrderStatusStatResolver orderStatusStatResolver,
|
||||
ProductMapper productMapper,
|
||||
CategoryService categoryService,
|
||||
CompanyOverviewService companyOverviewService,
|
||||
BiOrderStatQueryService orderStatQueryService,
|
||||
BiWorkbenchFeeLoader feeLoader) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.shopService = shopService;
|
||||
this.orderStatusStatResolver = orderStatusStatResolver;
|
||||
this.productMapper = productMapper;
|
||||
this.categoryService = categoryService;
|
||||
this.companyOverviewService = companyOverviewService;
|
||||
this.orderStatQueryService = orderStatQueryService;
|
||||
this.feeLoader = feeLoader;
|
||||
@@ -120,52 +102,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
return WorkbenchFinanceSliceVO.builder().finance(finance).summaryTables(summaryTables).build();
|
||||
}
|
||||
|
||||
/** 多维分析 Tab:SQL 聚合 + 内存 rollup */
|
||||
@Override
|
||||
public WorkbenchMultiAnalysisPanelVO multiAnalysis(WorkbenchQueryDTO queryDTO) {
|
||||
ShopContext ctx = resolveShopContext(queryDTO);
|
||||
if (ctx.shops().isEmpty() || ctx.stat().isUnionEmpty()) {
|
||||
return emptyMultiPanel();
|
||||
}
|
||||
SqlAggBundle agg = loadSqlAgg(ctx);
|
||||
Totals cur = totalsFromOrderRows(agg.orderRows(), agg.promoTotal(), agg.brushTotal(), ctx.taxRate());
|
||||
return buildMultiPanel(aggregateDimensionsFromSql(agg.orderRows(), ctx), cur);
|
||||
}
|
||||
|
||||
/** 销售额排行 Tab:与多维同源,按销售额降序 */
|
||||
@Override
|
||||
public WorkbenchMultiAnalysisPanelVO salesRank(WorkbenchQueryDTO queryDTO) {
|
||||
return buildSalesRankPanel(multiAnalysis(queryDTO));
|
||||
}
|
||||
|
||||
/**
|
||||
* 区间对比单段:只返回该段日期下店铺维表「合计」行,供前端与另一段日期并排对比;不组装完整工作台。
|
||||
*/
|
||||
@Override
|
||||
public WorkbenchPeriodCompareSliceVO periodCompareSlice(WorkbenchQueryDTO queryDTO) {
|
||||
BigDecimal taxRate = queryDTO.getTaxRate() != null ? queryDTO.getTaxRate() : DEFAULT_TAX_RATE;
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
DateRange range = parseDateRange(queryDTO);
|
||||
OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId);
|
||||
if (stat.isUnionEmpty()) {
|
||||
return WorkbenchPeriodCompareSliceVO.builder()
|
||||
.startDate(range.startDate()).endDate(range.endDate())
|
||||
.total(new CompanyOverviewVO.ShopSummary()).build();
|
||||
}
|
||||
List<ShopPO> shops = loadFilteredShops(companyId, queryDTO.getPlatformCode(), queryDTO.getShopId());
|
||||
ShopContext ctx = new ShopContext(taxRate, companyId, range, stat, shops,
|
||||
shops.stream().map(ShopPO::getId).filter(Objects::nonNull).toList(),
|
||||
shops.stream().collect(Collectors.toMap(ShopPO::getId, s -> s, (a, b) -> a)));
|
||||
SqlAggBundle agg = loadSqlAgg(ctx);
|
||||
CompanyOverviewVO.WorkbenchTableBundle bundle = companyOverviewService.buildWorkbenchSummaryTablesFromAgg(
|
||||
taxRate, companyId, shops, agg.orderRows(), agg.promotionRows(), agg.brushRows(),
|
||||
range.startDate(), range.endDate(), agg.skuRows());
|
||||
CompanyOverviewVO.ShopSummary total = bundle.getShopTable() != null && bundle.getShopTable().getTotal() != null
|
||||
? bundle.getShopTable().getTotal() : new CompanyOverviewVO.ShopSummary();
|
||||
return WorkbenchPeriodCompareSliceVO.builder()
|
||||
.startDate(range.startDate()).endDate(range.endDate()).total(total).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompanyOverviewVO.WorkbenchTableBundle loadSummaryTables(WorkbenchExportDTO dto) {
|
||||
WorkbenchQueryDTO query = new WorkbenchQueryDTO();
|
||||
@@ -209,15 +145,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
.build();
|
||||
}
|
||||
|
||||
private WorkbenchMultiAnalysisPanelVO emptyMultiPanel() {
|
||||
return WorkbenchMultiAnalysisPanelVO.builder()
|
||||
.platform(Collections.emptyList())
|
||||
.shop(Collections.emptyList())
|
||||
.category(Collections.emptyList())
|
||||
.product(Collections.emptyList())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** 解析筛选上下文:公司、税率、日期、店铺列表等 */
|
||||
private ShopContext resolveShopContext(WorkbenchQueryDTO queryDTO) {
|
||||
BigDecimal taxRate = queryDTO.getTaxRate() != null ? queryDTO.getTaxRate() : DEFAULT_TAX_RATE;
|
||||
@@ -292,63 +219,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
.build();
|
||||
}
|
||||
|
||||
private DimensionAggMaps aggregateDimensionsFromSql(List<OverviewAggRows.ShopProductRow> rows, ShopContext ctx) {
|
||||
Map<String, Agg> platformAgg = new HashMap<>();
|
||||
Map<String, Agg> shopAgg = new HashMap<>();
|
||||
Map<String, Agg> categoryAgg = new HashMap<>();
|
||||
Map<String, Agg> productAgg = new HashMap<>();
|
||||
Set<Long> productIds = new HashSet<>();
|
||||
Set<Long> categoryIds = new HashSet<>();
|
||||
|
||||
for (OverviewAggRows.ShopProductRow row : rows) {
|
||||
String platformKey = blankPlatformKey(row.getPlatformCode());
|
||||
long shopKey = row.getShopId() != null ? row.getShopId() : 0L;
|
||||
long productId = row.getProductId() != null && row.getProductId() > 0 ? row.getProductId() : -1L;
|
||||
long categoryKey = productId <= 0 ? -1L
|
||||
: (row.getCategoryId() == null || row.getCategoryId() == 0 ? 0L : row.getCategoryId());
|
||||
platformAgg.computeIfAbsent(platformKey, k -> new Agg()).addFromAggRow(row);
|
||||
shopAgg.computeIfAbsent(String.valueOf(shopKey), k -> new Agg()).addFromAggRow(row);
|
||||
categoryAgg.computeIfAbsent(String.valueOf(categoryKey), k -> new Agg()).addFromAggRow(row);
|
||||
productAgg.computeIfAbsent(String.valueOf(productId), k -> new Agg()).addFromAggRow(row);
|
||||
if (productId > 0) {
|
||||
productIds.add(productId);
|
||||
}
|
||||
if (categoryKey > 0) {
|
||||
categoryIds.add(categoryKey);
|
||||
}
|
||||
}
|
||||
Map<Long, ProductPO> productById = loadProducts(productIds);
|
||||
Map<Long, String> categoryNames = loadCategoryNamesByIds(categoryIds);
|
||||
return new DimensionAggMaps(platformAgg, shopAgg, categoryAgg, productAgg,
|
||||
loadPlatformNames(ctx.shops()), categoryNames, productById, ctx.shopById());
|
||||
}
|
||||
|
||||
private Totals totalsFromOrderRows(List<OverviewAggRows.ShopProductRow> rows,
|
||||
BigDecimal promotion, BigDecimal brush, BigDecimal taxRate) {
|
||||
BigDecimal sales = BigDecimal.ZERO, refund = BigDecimal.ZERO;
|
||||
BigDecimal pc = BigDecimal.ZERO, fc = BigDecimal.ZERO, oc = BigDecimal.ZERO;
|
||||
BigDecimal dealAmount = BigDecimal.ZERO;
|
||||
int orderCount = 0;
|
||||
int salesQty = 0;
|
||||
int dealOrderCount = 0;
|
||||
int dealQuantitySum = 0;
|
||||
for (OverviewAggRows.ShopProductRow row : rows) {
|
||||
sales = sales.add(nz(row.getSalesAmount()));
|
||||
refund = refund.add(nz(row.getRefundAmount()));
|
||||
pc = pc.add(nz(row.getProductCost()));
|
||||
fc = fc.add(nz(row.getFreightCost()));
|
||||
oc = oc.add(nz(row.getOtherCost()));
|
||||
dealAmount = dealAmount.add(nz(row.getDealAmount()));
|
||||
orderCount += row.getOrderCount() != null ? row.getOrderCount().intValue() : 0;
|
||||
salesQty += row.getSalesQuantitySum() != null ? row.getSalesQuantitySum().intValue() : 0;
|
||||
dealOrderCount += row.getDealOrderCount() != null ? row.getDealOrderCount().intValue() : 0;
|
||||
dealQuantitySum += row.getDealQuantitySum() != null ? row.getDealQuantitySum().intValue() : 0;
|
||||
}
|
||||
// 注意:商品粒度单量跨商品会重复计;多维/排行仅用销售额等金额字段,KPI 走店铺日文档
|
||||
return buildTotals(sales, refund, pc, fc, oc, promotion, brush, taxRate, orderCount, salesQty,
|
||||
dealAmount, dealOrderCount, dealQuantitySum);
|
||||
}
|
||||
|
||||
private Totals buildTotals(BigDecimal sales, BigDecimal refund, BigDecimal pc, BigDecimal fc, BigDecimal oc,
|
||||
BigDecimal promotion, BigDecimal brush, BigDecimal taxRate,
|
||||
int orderCount, int salesQuantitySum,
|
||||
@@ -411,14 +281,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
return list;
|
||||
}
|
||||
|
||||
private Map<Long, String> loadCategoryNamesByIds(Set<Long> categoryIds) {
|
||||
if (categoryIds.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return categoryService.list(new LambdaQueryWrapper<CategoryPO>().in(CategoryPO::getId, categoryIds))
|
||||
.stream().collect(Collectors.toMap(CategoryPO::getId, CategoryPO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private static BigDecimal nz(BigDecimal v) {
|
||||
return v != null ? v : BigDecimal.ZERO;
|
||||
}
|
||||
@@ -438,44 +300,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
}
|
||||
}
|
||||
|
||||
private WorkbenchMultiAnalysisPanelVO buildMultiPanel(DimensionAggMaps aggs, Totals cur) {
|
||||
BigDecimal salesAllocBase = cur.totalSales();
|
||||
return WorkbenchMultiAnalysisPanelVO.builder()
|
||||
.platform(buildDimRows(aggs.platformAgg(),
|
||||
k -> aggs.platformNames().getOrDefault(k, PlatformCode.labelOf(k)),
|
||||
salesAllocBase, cur.promotion(), cur.brush(), cur.tax()))
|
||||
.shop(buildDimRows(aggs.shopAgg(), k -> {
|
||||
ShopPO s = aggs.shopById().get(Long.parseLong(k));
|
||||
return s != null ? s.getName() : "未知店铺";
|
||||
}, salesAllocBase, cur.promotion(), cur.brush(), cur.tax()))
|
||||
.category(buildDimRows(aggs.categoryAgg(), k -> {
|
||||
if ("0".equals(k)) {
|
||||
return "未分类";
|
||||
}
|
||||
if ("-1".equals(k)) {
|
||||
return "其他品类";
|
||||
}
|
||||
return aggs.categoryNames().getOrDefault(Long.parseLong(k), "类目" + k);
|
||||
}, salesAllocBase, cur.promotion(), cur.brush(), cur.tax()))
|
||||
.product(buildDimRows(aggs.productAgg(), k -> {
|
||||
if (k.startsWith("-") || "0".equals(k)) {
|
||||
return "未匹配商品";
|
||||
}
|
||||
ProductPO p = aggs.productById().get(Long.parseLong(k));
|
||||
return p != null ? p.getName() : ("商品" + k);
|
||||
}, salesAllocBase, cur.promotion(), cur.brush(), cur.tax()))
|
||||
.build();
|
||||
}
|
||||
|
||||
private WorkbenchMultiAnalysisPanelVO buildSalesRankPanel(WorkbenchMultiAnalysisPanelVO multi) {
|
||||
return WorkbenchMultiAnalysisPanelVO.builder()
|
||||
.platform(sortByDealAmountDesc(multi.getPlatform()))
|
||||
.shop(sortByDealAmountDesc(multi.getShop()))
|
||||
.category(sortByDealAmountDesc(multi.getCategory()))
|
||||
.product(sortByDealAmountDesc(multi.getProduct()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按公司 + 可选平台/单店筛选店铺列表;指定 {@code shopId} 时校验归属与平台一致性。
|
||||
*/
|
||||
@@ -499,22 +323,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
return shopService.list(w);
|
||||
}
|
||||
|
||||
/** 按商品 id 批量加载主数据,供类目解析与商品名称展示 */
|
||||
private Map<Long, ProductPO> loadProducts(Iterable<Long> productIds) {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
for (Long id : productIds) {
|
||||
if (id != null && id > 0) {
|
||||
ids.add(id);
|
||||
}
|
||||
}
|
||||
ids = ids.stream().distinct().toList();
|
||||
if (ids.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
List<ProductPO> list = productMapper.selectList(new LambdaQueryWrapper<ProductPO>().in(ProductPO::getId, ids));
|
||||
return list.stream().collect(Collectors.toMap(ProductPO::getId, p -> p, (a, b) -> a));
|
||||
}
|
||||
|
||||
/** 从店铺收集平台 code,映射为展示名称 */
|
||||
private Map<String, String> loadPlatformNames(List<ShopPO> shops) {
|
||||
return shops.stream()
|
||||
@@ -528,65 +336,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
return platformCode != null && !platformCode.isBlank() ? platformCode : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 将「字符串 key → 聚合桶」转为多维表格行:每行毛利润 = 净销售额−成本;
|
||||
* 推广费、刷单费、税额均按该行净销售额占 {@code salesTotalCompany} 比例分摊;
|
||||
* 净利润 = 毛利润 − 分摊推广 − 分摊刷单 − 分摊税。
|
||||
*/
|
||||
private List<WorkbenchDimRowVO> buildDimRows(Map<String, Agg> aggs,
|
||||
java.util.function.Function<String, String> nameFn,
|
||||
BigDecimal salesTotalCompany,
|
||||
BigDecimal promoTotal,
|
||||
BigDecimal brushTotal,
|
||||
BigDecimal taxTotal) {
|
||||
List<WorkbenchDimRowVO> rows = new ArrayList<>();
|
||||
for (Map.Entry<String, Agg> e : aggs.entrySet()) {
|
||||
Agg a = e.getValue();
|
||||
if (a.sales.compareTo(BigDecimal.ZERO) <= 0 && a.refund.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
continue;
|
||||
}
|
||||
BigDecimal gross = a.sales.subtract(a.cost);
|
||||
BigDecimal promoB = allocShare(a.sales, salesTotalCompany, promoTotal);
|
||||
BigDecimal brushB = allocShare(a.sales, salesTotalCompany, brushTotal);
|
||||
BigDecimal taxB = allocShare(a.sales, salesTotalCompany.max(BigDecimal.ONE), taxTotal);
|
||||
if (salesTotalCompany.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
taxB = BigDecimal.ZERO;
|
||||
}
|
||||
BigDecimal net = gross.subtract(promoB).subtract(brushB).subtract(taxB);
|
||||
|
||||
BigDecimal salesYuan = yuan2(a.sales);
|
||||
BigDecimal grossYuan = yuan2(gross);
|
||||
BigDecimal netYuan = yuan2(net);
|
||||
BigDecimal refundYuan = yuan2(a.refund);
|
||||
|
||||
BigDecimal grossMarginPct = safePct(gross, a.sales);
|
||||
BigDecimal netMarginPct = safePct(net, a.sales);
|
||||
|
||||
rows.add(WorkbenchDimRowVO.builder()
|
||||
.key(e.getKey())
|
||||
.name(nameFn.apply(e.getKey()))
|
||||
.salesYuan(salesYuan)
|
||||
.grossProfitYuan(grossYuan)
|
||||
.netProfitYuan(netYuan)
|
||||
.refundYuan(refundYuan)
|
||||
.grossMarginPct(grossMarginPct)
|
||||
.netMarginPct(netMarginPct)
|
||||
.build());
|
||||
}
|
||||
rows.sort(Comparator.comparing(WorkbenchDimRowVO::getSalesYuan, Comparator.nullsFirst(BigDecimal::compareTo)).reversed());
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** 按净销售额降序复制排序,用于销售额排行 */
|
||||
private List<WorkbenchDimRowVO> sortByDealAmountDesc(List<WorkbenchDimRowVO> in) {
|
||||
if (in == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return in.stream()
|
||||
.sorted(Comparator.comparing(WorkbenchDimRowVO::getSalesYuan, Comparator.nullsFirst(BigDecimal::compareTo)).reversed())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 比例分摊:{@code amount * part / total},四舍五入到 4 位小数;总额或金额为 0 时返回 0。
|
||||
* 用于将区间总推广、刷单、税额拆到各维度行或各日趋势点。
|
||||
@@ -602,7 +351,7 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 各平台净销售额占比:与多维分析「平台」维度同源订单聚合;分母为当前筛选下公司净销售额合计。
|
||||
* 各平台净销售额占比;分母为当前筛选下公司净销售额合计。
|
||||
*/
|
||||
private List<WorkbenchCostSliceVO> buildPlatformSalesSlices(Map<String, Agg> platformAgg,
|
||||
Map<String, String> platformNames,
|
||||
@@ -836,17 +585,6 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
private record ComparePeriodBundle(DateRange lastRange, DateRange yoyRange, Totals prev, Totals yoy) {
|
||||
}
|
||||
|
||||
/** 四维聚合桶 + 展示名称 */
|
||||
private record DimensionAggMaps(Map<String, Agg> platformAgg,
|
||||
Map<String, Agg> shopAgg,
|
||||
Map<String, Agg> categoryAgg,
|
||||
Map<String, Agg> productAgg,
|
||||
Map<String, String> platformNames,
|
||||
Map<Long, String> categoryNames,
|
||||
Map<Long, ProductPO> productById,
|
||||
Map<Long, ShopPO> shopById) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 单时间段店铺财务 KPI 总量(与汇总表 + 推广/刷单区间一致)。
|
||||
*/
|
||||
@@ -872,7 +610,7 @@ public class BiWorkbenchServiceImpl implements BiWorkbenchService {
|
||||
BigDecimal dealPiecePrice) {
|
||||
}
|
||||
|
||||
/** 多维或按日聚合用的累加桶:累计销售额、退款、成本 */
|
||||
/** 平台占比等聚合用的累加桶:累计销售额、退款、成本 */
|
||||
private static final class Agg {
|
||||
BigDecimal sales = BigDecimal.ZERO;
|
||||
BigDecimal refund = BigDecimal.ZERO;
|
||||
|
||||
Reference in New Issue
Block a user