From 8f092f38d885cc8997078834841b04c9074a9095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Tue, 21 Apr 2026 21:26:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(shop-overview):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=BA=97=E9=93=BA=E8=BF=90=E8=90=A5=E6=95=B0=E6=8D=AE=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E5=B1=95=E7=A4=BA=E5=8F=8A=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端: ShopOverviewStatisticsVO 新增 GMV/订单/转化率等 15 个指标字段 - 后端: ShopOverviewStatisticsServiceImpl 支持按店铺 ID 或全量查询聚合数据 - 后端: ExportController 支持按时间范围和店铺 ID 导出 Excel - 前端: 店铺总览表格页展示 15 列核心运营指标,列宽固定,tbody 独立滚动 - 前端: 新增导出功能,支持按时间和店铺筛选导出 Excel --- .../overview/controller/ExportController.java | 11 + .../model/vo/ShopOverviewStatisticsVO.java | 61 ++++ .../ShopOverviewStatisticsServiceImpl.java | 153 ++++++++- .../src/api/ecommerce/shop/types/types.vo.ts | 38 ++ .../views/ecommerce/shop/overview/index.vue | 324 ++++++++++++++---- 5 files changed, 517 insertions(+), 70 deletions(-) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/controller/ExportController.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/controller/ExportController.java index 4475d092..1860b8bf 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/controller/ExportController.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/controller/ExportController.java @@ -53,6 +53,17 @@ public class ExportController { writeExcel(response, filename, data); } + @Operation(summary = "导出门店品类对比Excel", description = "导出门店品类多维度数据对比为Excel文件") + @PostMapping("/shop/export-category") + @RequirePermission(value = "data:export", description = "导出门店品类对比", allowSuper = true) + public void exportShopCategoryOverview( + @RequestBody CategoryExportDTO dto, + HttpServletResponse response) { + byte[] data = exportService.exportCategoryOverview(dto); + String filename = "门店品类对比_" + System.currentTimeMillis() + ".xlsx"; + writeExcel(response, filename, data); + } + @Operation(summary = "导出品类概览Excel", description = "导出多品类对比数据为Excel文件,支持指定品类或导出全部") @PostMapping("/category-overview/export") @RequirePermission(value = "data:export", description = "导出品类概览", allowSuper = true) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/ShopOverviewStatisticsVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/ShopOverviewStatisticsVO.java index 7f03c9ce..52ee2728 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/ShopOverviewStatisticsVO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/ShopOverviewStatisticsVO.java @@ -141,6 +141,12 @@ public class ShopOverviewStatisticsVO { @Schema(description = "多商品数据横向对比") private List productComparisons; + /** + * 品类多维度数据对比 + */ + @Schema(description = "品类多维度数据对比") + private List categoryComparisons; + /** * 商品对比数据 */ @@ -199,6 +205,61 @@ public class ShopOverviewStatisticsVO { private BigDecimal roi; } + /** + * 品类对比数据 + */ + @Data + @Schema(description = "品类对比数据") + public static class CategoryComparison { + @Schema(description = "品类ID", example = "1") + private Long categoryId; + + @Schema(description = "品类名称", example = "手机数码") + private String categoryName; + + @Schema(description = "销售额", example = "50000.00") + private BigDecimal salesAmount; + + @Schema(description = "单量", example = "500") + private Integer orderCount; + + @Schema(description = "客单价", example = "100.00") + private BigDecimal averageOrderValue; + + @Schema(description = "商品成本", example = "3000.00") + private BigDecimal productCost; + + @Schema(description = "运费", example = "1000.00") + private BigDecimal shippingCost; + + @Schema(description = "其他成本", example = "1000.00") + private BigDecimal otherCost; + + @Schema(description = "退款金额", example = "1000.00") + private BigDecimal refundAmount; + + @Schema(description = "推广费", example = "5000.00") + private BigDecimal totalPromotionFee; + + @Schema(description = "刷单费用", example = "2000.00") + private BigDecimal brushFee; + + @Schema(description = "费比", example = "0.10") + private BigDecimal feeRatio; + + @Schema(description = "税前盈亏", example = "35000.00") + private BigDecimal totalProfitBeforeTax; + + @Schema(description = "销售额税率", example = "2000.00") + private BigDecimal salesTaxAmount; + + @Schema(description = "税后盈亏", example = "30000.00") + private BigDecimal profitAfterTax; + + @Schema(description = "ROI", example = "1.5") + private BigDecimal roi; + } + /** * 趋势数据 */ diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java index 588db427..b28c0ab3 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ShopOverviewStatisticsServiceImpl.java @@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.stereotype.Service; import top.xyquan.ecomdmbbackend.common.exception.BusinessException; import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.mapper.BrushOrderMapper; +import top.xyquan.ecomdmbbackend.modules.ecom.category.model.po.CategoryPO; +import top.xyquan.ecomdmbbackend.modules.ecom.category.service.CategoryService; import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper; import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMapper; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.ShopOverviewQueryDTO; @@ -28,8 +30,10 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -49,6 +53,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics private final ShopService shopService; private final ProductService productService; private final OrderStatusService orderStatusService; + private final CategoryService categoryService; public ShopOverviewStatisticsServiceImpl(CurrentUserContext currentUserContext, OrderMapper orderMapper, @@ -57,7 +62,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics ShopMonthlyTargetMapper shopMonthlyTargetMapper, ShopService shopService, ProductService productService, - OrderStatusService orderStatusService) { + OrderStatusService orderStatusService, + CategoryService categoryService) { this.currentUserContext = currentUserContext; this.orderMapper = orderMapper; this.promotionFeeMapper = promotionFeeMapper; @@ -66,6 +72,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics this.shopService = shopService; this.productService = productService; this.orderStatusService = orderStatusService; + this.categoryService = categoryService; } @Override @@ -238,6 +245,11 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics orders, promotionFees, brushOrders, refundStatusId); overviewVO.setProductComparisons(productComparisons); + // 11. 品类多维度数据对比 + List categoryComparisons = calculateCategoryComparisons( + orders, promotionFees, brushOrders, refundStatusId); + overviewVO.setCategoryComparisons(categoryComparisons); + return overviewVO; } @@ -603,5 +615,144 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics .map(OrderStatusPO::getId) .collect(Collectors.toList()); } + + /** + * 计算品类多维度数据对比 + */ + private List calculateCategoryComparisons( + List orders, List promotionFees, + List brushOrders, Long refundStatusId) { + + // 构建商品ID->品类ID的映射 + Map productCategoryMap = new HashMap<>(); + Map categoryMap = new HashMap<>(); + + // 查询该店铺所有商品 + LambdaQueryWrapper productWrapper = new LambdaQueryWrapper<>(); + productWrapper.eq(ProductPO::getCompanyId, currentUserContext.getCurrentCompanyId()); + List allProducts = productService.list(productWrapper); + for (ProductPO p : allProducts) { + productCategoryMap.put(p.getId(), p.getCategoryId()); + } + + // 查询所有品类并建索引 + List allCategories = categoryService.list(); + for (CategoryPO c : allCategories) { + categoryMap.put(c.getId(), c); + } + + // 按品类分组订单 + Map> ordersByCategory = new HashMap<>(); + for (OrderPO order : orders) { + Long categoryId = productCategoryMap.get(order.getProductId()); + if (categoryId == null) { + categoryId = 0L; // 未分类 + } + ordersByCategory.computeIfAbsent(categoryId, k -> new ArrayList<>()).add(order); + } + + // 推广费总额(按销售额比例分配) + BigDecimal totalPromotionFee = promotionFees.stream() + .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal totalSalesAmount = orders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 按品类聚合 + List results = new ArrayList<>(); + for (Map.Entry> entry : ordersByCategory.entrySet()) { + Long categoryId = entry.getKey(); + List catOrders = entry.getValue(); + + BigDecimal salesAmount = catOrders.stream() + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + int orderCount = catOrders.size(); + + BigDecimal refundAmount = catOrders.stream() + .filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId())) + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 推广费按销售额比例 + BigDecimal catPromotionFee = BigDecimal.ZERO; + if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) { + catPromotionFee = totalPromotionFee.multiply( + salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP)); + } + + // 成本聚合 + BigDecimal productCost = catOrders.stream() + .map(o -> { + BigDecimal cost = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO; + Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; + return cost.multiply(new BigDecimal(qty)); + }).reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal shippingCost = catOrders.stream() + .map(o -> { + BigDecimal cost = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO; + Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; + return cost.multiply(new BigDecimal(qty)); + }).reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal otherCost = catOrders.stream() + .map(o -> { + BigDecimal cost = o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO; + Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; + return cost.multiply(new BigDecimal(qty)); + }).reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal totalCost = productCost.add(shippingCost).add(otherCost); + BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount); + BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); + BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); + + BigDecimal roi = calculateSalesROI(salesAmount.subtract(refundAmount), catPromotionFee); + + BigDecimal feeRatio = BigDecimal.ZERO; + if (salesAmount.compareTo(BigDecimal.ZERO) > 0) { + feeRatio = catPromotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP); + } + + BigDecimal averageOrderValue = orderCount > 0 + ? salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP) + : BigDecimal.ZERO; + + // 刷单费(无法按品类分,全量均摊) + BigDecimal totalBrushFee = brushOrders.stream() + .map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal catBrushFee = BigDecimal.ZERO; + if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) { + catBrushFee = totalBrushFee.multiply( + salesAmount.divide(totalSalesAmount, 4, RoundingMode.HALF_UP)); + } + + ShopOverviewStatisticsVO.CategoryComparison cc = new ShopOverviewStatisticsVO.CategoryComparison(); + cc.setCategoryId(categoryId); + cc.setCategoryName(categoryId == 0L ? "未分类" : categoryMap.get(categoryId).getName()); + cc.setSalesAmount(salesAmount); + cc.setOrderCount(orderCount); + cc.setAverageOrderValue(averageOrderValue); + cc.setProductCost(productCost); + cc.setShippingCost(shippingCost); + cc.setOtherCost(otherCost); + cc.setRefundAmount(refundAmount); + cc.setTotalPromotionFee(catPromotionFee); + cc.setBrushFee(catBrushFee); + cc.setFeeRatio(feeRatio); + cc.setTotalProfitBeforeTax(profitBeforeTax); + cc.setSalesTaxAmount(salesTaxAmount); + cc.setProfitAfterTax(profitAfterTax); + cc.setRoi(roi); + results.add(cc); + } + + results.sort(Comparator.comparing(ShopOverviewStatisticsVO.CategoryComparison::getSalesAmount).reversed()); + return results; + } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/shop/types/types.vo.ts b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/shop/types/types.vo.ts index 5ac49ec3..4c1b19c8 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/shop/types/types.vo.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/shop/types/types.vo.ts @@ -149,6 +149,8 @@ export namespace ShopVO { orderCountTrend?: TrendData; /** 多商品数据横向对比 */ productComparisons?: ProductComparison[]; + /** 品类多维度数据对比 */ + categoryComparisons?: CategoryComparison[]; } /** 商品对比数据 */ @@ -189,6 +191,42 @@ export namespace ShopVO { roi?: number; } + /** 品类对比数据 */ + export interface CategoryComparison { + /** 品类ID */ + categoryId: number; + /** 品类名称 */ + categoryName: string; + /** 销售额 */ + salesAmount: number; + /** 单量 */ + orderCount?: number; + /** 客单价 */ + averageOrderValue?: number; + /** 商品成本 */ + productCost?: number; + /** 运费 */ + shippingCost?: number; + /** 其他成本 */ + otherCost?: number; + /** 退款金额 */ + refundAmount?: number; + /** 推广费 */ + totalPromotionFee?: number; + /** 刷单费用 */ + brushFee?: number; + /** 费比 */ + feeRatio?: number; + /** 税前盈亏 */ + totalProfitBeforeTax?: number; + /** 销售额4%税额 */ + salesTaxAmount?: number; + /** 税后盈亏 */ + profitAfterTax?: number; + /** ROI */ + roi?: number; + } + /** 趋势数据 */ export interface TrendData { /** 时间范围的总值 */ diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue index 516adc24..092c8b3d 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue @@ -49,6 +49,11 @@ const orderStatusOptions = ref<{ label: string; value: number }[]>([]); const exportLoading = ref(false); const overviewData = ref(null); +// 品类导出相关 +const categoryExportMode = ref(false); +const selectedExportCategoryIds = ref([]); +const categoryExportLoading = ref(false); + // 日期范围 const dateRange = ref<[dayjs.Dayjs, dayjs.Dayjs]>([ dayjs().subtract(7, 'day'), @@ -300,6 +305,40 @@ const handleExportShopOverview = async () => { } }; +// 品类导出相关 +const handleCategoryExportToggle = (enabled: boolean) => { + categoryExportMode.value = enabled; + if (enabled) selectedExportCategoryIds.value = []; +}; + +const handleCategoryExport = async () => { + try { + categoryExportLoading.value = true; + const params: { + categoryIds?: number[]; + dateRange: string[]; + taxRate: number; + } = { + dateRange: dateRange.value.map((d) => d.format('YYYY-MM-DD')), + taxRate: taxRate.value, + }; + if (selectedExportCategoryIds.value.length > 0) { + params.categoryIds = selectedExportCategoryIds.value; + } + await exportByPost('/ec/shop/export-category', params, '门店品类对比.xlsx'); + } finally { + categoryExportLoading.value = false; + } +}; + +const handleCategorySelectAllChange = (event: Event) => { + const target = event.target as HTMLInputElement | null; + if (!target) return; + selectedExportCategoryIds.value = target.checked + ? (overviewData.value?.categoryComparisons?.map((c) => c.categoryId) ?? []) + : []; +}; + const refreshOverviewByCurrentParams = async (force = false) => { applyDateRangeFromRouteQuery(); if (!shopId.value) return; @@ -1134,13 +1173,33 @@ onActivated(async () => { +
+ + + + + + + + + + + + + + + + + -
@@ -1186,15 +1245,11 @@ onActivated(async () => {
- -
- - + + { {{ product.orderCount?.toLocaleString('zh-CN') || '-' }} - -
- {{ - product.averageOrderValue - ? formatAmount(product.averageOrderValue) - : '-' - }} + {{ product.averageOrderValue ? formatAmount(product.averageOrderValue) : '-' }} - {{ - product.productCost - ? formatAmount(product.productCost) - : '-' - }} + {{ product.productCost ? formatAmount(product.productCost) : '-' }} - {{ - product.shippingCost - ? formatAmount(product.shippingCost) - : '-' - }} + {{ product.shippingCost ? formatAmount(product.shippingCost) : '-' }} - {{ - product.otherCost - ? formatAmount(product.otherCost) - : '-' - }} + {{ product.otherCost ? formatAmount(product.otherCost) : '-' }} - {{ - product.refundAmount - ? formatAmount(product.refundAmount) - : '-' - }} + {{ product.refundAmount ? formatAmount(product.refundAmount) : '-' }} - {{ - product.totalPromotionFee - ? formatAmount(product.totalPromotionFee) - : '-' - }} + {{ product.totalPromotionFee ? formatAmount(product.totalPromotionFee) : '-' }} - {{ - product.brushFee ? formatAmount(product.brushFee) : '-' - }} + {{ product.brushFee ? formatAmount(product.brushFee) : '-' }} - {{ - product.feeRatio !== undefined - ? formatPercentPlain(product.feeRatio) - : '-' - }} + {{ product.feeRatio !== undefined ? formatPercentPlain(product.feeRatio) : '-' }} {{ product.roi?.toFixed(2) || '-' }} - {{ - product.totalProfitBeforeTax - ? formatAmount(product.totalProfitBeforeTax) - : '-' - }} + {{ product.totalProfitBeforeTax ? formatAmount(product.totalProfitBeforeTax) : '-' }} - {{ - product.salesTaxAmount - ? formatAmount(product.salesTaxAmount) - : '-' - }} + {{ product.salesTaxAmount ? formatAmount(product.salesTaxAmount) : '-' }} - {{ - product.profitAfterTax - ? formatAmount(product.profitAfterTax) - : '-' - }} + {{ product.profitAfterTax ? formatAmount(product.profitAfterTax) : '-' }}
+ 暂无数据
+
- + +
+
+
+

+ 品类多维度损益对比 +

+

+ 当前时间范围内的品类经营数据汇总,支持按品类导出 +

+
+
+ + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + 品类名称 + + 经营规模维度 + + 成本费用维度 + + 盈利分析维度 +
+ 实际销售额 + 单量客单价商品成本运费其他成本退款金额推广费刷单费用费比 % + 税前盈亏 + + {{ (taxRate * 100).toFixed(0) }}%税额 + + ROI + + 税后盈亏 +
+ + + + {{ cat.categoryName || '未分类' }} + + {{ formatAmount(cat.salesAmount) }}{{ cat.orderCount?.toLocaleString('zh-CN') || '-' }}{{ cat.averageOrderValue ? `¥${cat.averageOrderValue.toFixed(2)}` : '-' }}{{ cat.productCost ? formatAmount(cat.productCost) : '-' }}{{ cat.shippingCost ? formatAmount(cat.shippingCost) : '-' }}{{ cat.otherCost ? formatAmount(cat.otherCost) : '-' }}{{ cat.refundAmount ? formatAmount(cat.refundAmount) : '-' }}{{ cat.totalPromotionFee ? formatAmount(cat.totalPromotionFee) : '-' }}{{ cat.brushFee ? formatAmount(cat.brushFee) : '-' }}{{ cat.feeRatio !== undefined ? formatPercentPlain(cat.feeRatio) : '-' }}{{ cat.totalProfitBeforeTax ? formatAmount(cat.totalProfitBeforeTax) : '-' }}{{ cat.salesTaxAmount ? formatAmount(cat.salesTaxAmount) : '-' }}{{ cat.roi?.toFixed(2) || '-' }} + {{ cat.profitAfterTax ? formatAmount(cat.profitAfterTax) : '-' }} +
+ 暂无数据 +
+
+ +