feat(shop-overview): 添加店铺运营数据表格展示及导出功能

- 后端: ShopOverviewStatisticsVO 新增 GMV/订单/转化率等 15 个指标字段
- 后端: ShopOverviewStatisticsServiceImpl 支持按店铺 ID 或全量查询聚合数据
- 后端: ExportController 支持按时间范围和店铺 ID 导出 Excel
- 前端: 店铺总览表格页展示 15 列核心运营指标,列宽固定,tbody 独立滚动
- 前端: 新增导出功能,支持按时间和店铺筛选导出 Excel
This commit is contained in:
小蟑螂
2026-04-21 21:26:31 +08:00
parent 00e53a20e3
commit 8f092f38d8
5 changed files with 517 additions and 70 deletions
@@ -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)
@@ -141,6 +141,12 @@ public class ShopOverviewStatisticsVO {
@Schema(description = "多商品数据横向对比")
private List<ProductComparison> productComparisons;
/**
* 品类多维度数据对比
*/
@Schema(description = "品类多维度数据对比")
private List<CategoryComparison> 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;
}
/**
* 趋势数据
*/
@@ -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<ShopOverviewStatisticsVO.CategoryComparison> 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<ShopOverviewStatisticsVO.CategoryComparison> calculateCategoryComparisons(
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders, Long refundStatusId) {
// 构建商品ID->品类ID的映射
Map<Long, Long> productCategoryMap = new HashMap<>();
Map<Long, CategoryPO> categoryMap = new HashMap<>();
// 查询该店铺所有商品
LambdaQueryWrapper<ProductPO> productWrapper = new LambdaQueryWrapper<>();
productWrapper.eq(ProductPO::getCompanyId, currentUserContext.getCurrentCompanyId());
List<ProductPO> allProducts = productService.list(productWrapper);
for (ProductPO p : allProducts) {
productCategoryMap.put(p.getId(), p.getCategoryId());
}
// 查询所有品类并建索引
List<CategoryPO> allCategories = categoryService.list();
for (CategoryPO c : allCategories) {
categoryMap.put(c.getId(), c);
}
// 按品类分组订单
Map<Long, List<OrderPO>> 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<ShopOverviewStatisticsVO.CategoryComparison> results = new ArrayList<>();
for (Map.Entry<Long, List<OrderPO>> entry : ordersByCategory.entrySet()) {
Long categoryId = entry.getKey();
List<OrderPO> 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;
}
}