diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/CompanyOverviewVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/CompanyOverviewVO.java index 2ecd916e..9701cf61 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/CompanyOverviewVO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/model/vo/CompanyOverviewVO.java @@ -73,6 +73,12 @@ public class CompanyOverviewVO { @Schema(description = "多店铺数据横向对比") private List shopComparisons; + /** + * 多品类数据横向对比 + */ + @Schema(description = "多品类数据横向对比") + private List categoryComparisons; + /** * 汇总ROI */ @@ -213,6 +219,68 @@ public class CompanyOverviewVO { @Schema(description = "销售额4%税额", example = "2000.00") private BigDecimal salesTaxAmount; + @Schema(description = "税后盈亏", example = "30000.00") + private BigDecimal profitAfterTax; + + @Schema(description = "ROI", example = "1.5") + private BigDecimal roi; + } + + /** + * 品类对比数据 + */ + @Data + @Schema(description = "品类对比数据") + public static class CategoryComparison { + @Schema(description = "品类ID(未分配为null)", 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 = "销售额4%税额", example = "2000.00") + private BigDecimal salesTaxAmount; + + @Schema(description = "税后盈亏", example = "30000.00") private BigDecimal profitAfterTax; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java index 1e2ed311..fcd5c610 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java @@ -18,6 +18,10 @@ import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.model.po.ShopMo import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.vo.CompanyOverviewVO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.service.CompanyOverviewService; import top.xyquan.ecomdmbbackend.modules.ecom.shop.service.ShopService; +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.product.mapper.ProductMapper; +import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; import java.math.BigDecimal; @@ -46,6 +50,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { private final ShopMonthlyTargetMapper shopMonthlyTargetMapper; private final ShopService shopService; private final OrderStatusService orderStatusService; + private final ProductMapper productMapper; + private final CategoryService categoryService; public CompanyOverviewServiceImpl(CurrentUserContext currentUserContext, OrderMapper orderMapper, @@ -53,7 +59,9 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { BrushOrderMapper brushOrderMapper, ShopMonthlyTargetMapper shopMonthlyTargetMapper, ShopService shopService, - OrderStatusService orderStatusService) { + OrderStatusService orderStatusService, + ProductMapper productMapper, + CategoryService categoryService) { this.currentUserContext = currentUserContext; this.orderMapper = orderMapper; this.promotionFeeMapper = promotionFeeMapper; @@ -61,6 +69,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { this.shopMonthlyTargetMapper = shopMonthlyTargetMapper; this.shopService = shopService; this.orderStatusService = orderStatusService; + this.productMapper = productMapper; + this.categoryService = categoryService; } @Override @@ -204,7 +214,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { shops, orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate); overviewVO.setShopComparisons(shopComparisons); - // 10. 汇总ROI和总税后盈亏 + // 10. 多品类数据横向对比 + List categoryComparisons = calculateCategoryComparisons( + orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate); + overviewVO.setCategoryComparisons(categoryComparisons); + + // 11. 汇总ROI和总税后盈亏 BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId); overviewVO.setTotalROI(totalROI); overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax); @@ -701,6 +716,172 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return status != null ? status.getId() : null; } + + /** + * Calculate category comparison data + */ + private List calculateCategoryComparisons( + List orders, List promotionFees, + List brushOrders, Long refundStatusId, Long companyId, + LocalDate startDate, LocalDate endDate) { + + // Query all categories to build categoryId -> categoryName map + LambdaQueryWrapper categoryWrapper = new LambdaQueryWrapper<>(); + categoryWrapper.eq(CategoryPO::getCompanyId, companyId); + List allCategories = categoryService.list(categoryWrapper); + Map categoryNameMap = allCategories.stream() + .collect(Collectors.toMap(CategoryPO::getId, CategoryPO::getName)); + + // Collect all product IDs involved and batch query their category info + List productIds = orders.stream() + .map(OrderPO::getProductId) + .filter(id -> id != null) + .distinct() + .collect(Collectors.toList()); + + Map productCategoryMap = new java.util.HashMap<>(); + if (!productIds.isEmpty()) { + LambdaQueryWrapper productWrapper = new LambdaQueryWrapper<>(); + productWrapper.in(ProductPO::getId, productIds).select(ProductPO::getId, ProductPO::getCategoryId); + List products = productMapper.selectList(productWrapper); + for (ProductPO product : products) { + productCategoryMap.put(product.getId(), product.getCategoryId()); + } + } + + // Group orders by category + Map> ordersByCategory = orders.stream() + .collect(Collectors.groupingBy(order -> { + Long productId = order.getProductId(); + if (productId == null) return -1L; + Long categoryId = productCategoryMap.get(productId); + return categoryId != null ? categoryId : -1L; + })); + + // Aggregate promotion fees and brush fees by shop + Map shopTotalPromotionFeeMap = promotionFees.stream() + .collect(Collectors.groupingBy( + PromotionFeePO::getShopId, + Collectors.reducing(BigDecimal.ZERO, + fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO, + BigDecimal::add))); + Map shopTotalBrushFeeMap = brushOrders.stream() + .collect(Collectors.groupingBy( + BrushOrderPO::getShopId, + Collectors.reducing(BigDecimal.ZERO, + brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO, + BigDecimal::add))); + + List comparisons = new ArrayList<>(); + for (Map.Entry> entry : ordersByCategory.entrySet()) { + Long categoryId = entry.getKey(); + List categoryOrders = entry.getValue(); + + BigDecimal salesAmount = categoryOrders.stream() + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + int orderCount = categoryOrders.size(); + + BigDecimal refundAmount = categoryOrders.stream() + .filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId())) + .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal productCost = categoryOrders.stream() + .map(order -> { + BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; + Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; + return cost.multiply(new BigDecimal(quantity)); + }) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal shippingCost = categoryOrders.stream() + .map(order -> { + BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; + Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; + return cost.multiply(new BigDecimal(quantity)); + }) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal otherCost = categoryOrders.stream() + .map(order -> { + BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; + return cost; + }) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + Map categorySalesByShop = categoryOrders.stream() + .collect(Collectors.groupingBy( + OrderPO::getShopId, + Collectors.reducing(BigDecimal.ZERO, + order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO, + BigDecimal::add))); + + BigDecimal categoryTotalSales = categorySalesByShop.values().stream() + .reduce(BigDecimal.ZERO, BigDecimal::add); + + BigDecimal totalPromotionFee = BigDecimal.ZERO; + BigDecimal totalBrushFee = BigDecimal.ZERO; + + if (categoryTotalSales.compareTo(BigDecimal.ZERO) > 0) { + for (Map.Entry shopEntry : categorySalesByShop.entrySet()) { + Long shopId = shopEntry.getKey(); + BigDecimal shopCategorySales = shopEntry.getValue(); + BigDecimal ratio = shopCategorySales.divide(categoryTotalSales, 6, RoundingMode.HALF_UP); + + BigDecimal shopPromoFee = shopTotalPromotionFeeMap.getOrDefault(shopId, BigDecimal.ZERO); + BigDecimal shopBrushFee = shopTotalBrushFeeMap.getOrDefault(shopId, BigDecimal.ZERO); + totalPromotionFee = totalPromotionFee.add(shopPromoFee.multiply(ratio)); + totalBrushFee = totalBrushFee.add(shopBrushFee.multiply(ratio)); + } + } + + 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 = BigDecimal.ZERO; + if (totalPromotionFee.compareTo(BigDecimal.ZERO) > 0) { + roi = salesAmount.subtract(refundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP); + } + + BigDecimal feeRatio = BigDecimal.ZERO; + if (salesAmount.compareTo(BigDecimal.ZERO) > 0) { + feeRatio = totalPromotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP); + } + + BigDecimal averageOrderValue = BigDecimal.ZERO; + if (orderCount > 0) { + averageOrderValue = salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP); + } + + String categoryName = categoryId == -1L ? "其他品类" : categoryNameMap.getOrDefault(categoryId, "Unknown"); + + CompanyOverviewVO.CategoryComparison comparison = new CompanyOverviewVO.CategoryComparison(); + comparison.setCategoryId(categoryId == -1L ? null : categoryId); + comparison.setCategoryName(categoryName); + comparison.setSalesAmount(salesAmount); + comparison.setOrderCount(orderCount); + comparison.setAverageOrderValue(averageOrderValue); + comparison.setProductCost(productCost); + comparison.setShippingCost(shippingCost); + comparison.setOtherCost(otherCost); + comparison.setRefundAmount(refundAmount); + comparison.setTotalPromotionFee(totalPromotionFee); + comparison.setBrushFee(totalBrushFee); + comparison.setFeeRatio(feeRatio); + comparison.setTotalProfitBeforeTax(profitBeforeTax); + comparison.setSalesTaxAmount(salesTaxAmount); + comparison.setProfitAfterTax(profitAfterTax); + comparison.setRoi(roi); + comparisons.add(comparison); + } + + comparisons.sort(Comparator.comparing(CompanyOverviewVO.CategoryComparison::getSalesAmount).reversed()); + return comparisons; + } private List getAutoStatisticsStatusIds(Long companyId) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(OrderStatusPO::getCompanyId, companyId) diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/company-overview/types/types.vo.ts b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/company-overview/types/types.vo.ts index b62ed535..f94f7fc4 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/company-overview/types/types.vo.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/company-overview/types/types.vo.ts @@ -25,6 +25,8 @@ export namespace CompanyOverviewVO { shopEfficiencyQuadrants?: ShopEfficiencyQuadrant[]; /** 多店铺数据横向对比(16维度) */ shopComparisons?: ShopComparison[]; + /** 多品类数据横向对比 */ + categoryComparisons?: CategoryComparison[]; /** 汇总ROI */ totalROI?: number; /** 总税后盈亏 */ @@ -110,4 +112,40 @@ export namespace CompanyOverviewVO { /** ROI */ roi?: number; } + + /** 品类对比数据 */ + export interface CategoryComparison { + /** 品类ID(未分配为null) */ + 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; + } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue index 3df7b692..12773f79 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/company-overview/index.vue @@ -1200,6 +1200,189 @@ const handleExportSelectedShops = async () => { + + +
+
+
+

+ 各品类多维度经营明细对比 +

+

+ 包含:规模、成本及最终损益所有维度 +

+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 品类名称 + + 经营规模维度 + + 成本费用维度 + + 盈利分析维度 +
+ 实际销售额 + 单量客单价商品成本运费其他成本 + 退款金额 + 推广费刷单费用费比 % + 税前盈亏 + + {{ (taxRate * 100).toFixed(0) }}%税额 + + ROI + + 税后盈亏 +
+ + {{ cat.categoryName }} + + + {{ formatAmount(cat.salesAmount) }} + + {{ cat.orderCount?.toLocaleString('zh-CN') || '-' }} + + {{ + cat.averageOrderValue + ? formatAmount(cat.averageOrderValue) + : '-' + }} + + {{ + 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) + : '-' + }} +
+ 暂无数据 +
+
+