From 89604da32ed733e72473c032c5d408cbf0abae21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Mon, 27 Apr 2026 22:44:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(product):=20=E5=95=86=E5=93=81=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E9=A1=B5=E5=AF=B9=E6=8E=A5=E7=9C=9F=E5=AE=9EAPI?= =?UTF-8?q?=EF=BC=8C=E6=9B=BF=E6=8D=A2mock=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后端: - 重写 ProductOverviewServiceImpl匹配新的扁平化VO结构 - ProductOverviewVO: 新增 totalSalesAmount/monthOverMonthGrowth/totalROI 等扁平KPI字段 - ProductOverviewQueryDTO: 支持 productId/startDate/endDate/taxRate 前端: - types.vo.ts: 适配新VO结构 (KPI字段扁平化+趋势数据+规格汇总) - types.dto.ts: 更新查询DTO - details/index.vue: fetchOverview调用真实API,normalizeOverview保留所有字段 - details/components/: StatCards/SalesTrendChart/FinancialPanel/SpecSummaryTable 适配新数据 - 修复 formatPercentPlain 对 null 值崩溃的问题 - StatCards/SalesTrendChart/FinancialPanel 使用 v-if 保护避免初始渲染报错 --- .../model/dto/ProductOverviewQueryDTO.java | 9 +- .../overview/model/vo/ProductOverviewVO.java | 341 +++++++- .../impl/ProductOverviewServiceImpl.java | 799 +++++++++++------- .../product-overview/types/types.dto.ts | 2 + .../product-overview/types/types.vo.ts | 140 ++- .../product/components/ProductCard.vue | 129 ++- .../details/components/FinancialPanel.vue | 100 +++ .../details/components/SalesTrendChart.vue | 216 +++++ .../details/components/SpecSummaryTable.vue | 241 ++++++ .../product/details/components/StatCards.vue | 147 ++++ .../details/components/TimeRangeBar.vue | 207 +++++ .../views/ecommerce/product/details/index.vue | 201 ++++- .../src/views/ecommerce/product/index.vue | 16 +- .../product/views/ProductGridView/index.vue | 2 +- 14 files changed, 2121 insertions(+), 429 deletions(-) create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/details/components/FinancialPanel.vue create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/details/components/SalesTrendChart.vue create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/details/components/SpecSummaryTable.vue create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/details/components/StatCards.vue create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/details/components/TimeRangeBar.vue diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/dto/ProductOverviewQueryDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/dto/ProductOverviewQueryDTO.java index d3bc6789..0382311e 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/dto/ProductOverviewQueryDTO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/dto/ProductOverviewQueryDTO.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; import lombok.Data; +import java.math.BigDecimal; import java.time.LocalDate; /** @@ -33,4 +34,10 @@ public class ProductOverviewQueryDTO { @Schema(description = "结束日期", example = "2024-01-31", requiredMode = Schema.RequiredMode.REQUIRED) @NotNull(message = "结束日期不能为空") private LocalDate endDate; -} + + /** + * 税率(默认0.04) + */ + @Schema(description = "税率,用于计算税额,默认0.04", example = "0.04") + private BigDecimal taxRate = new BigDecimal("0.04"); +} \ No newline at end of file diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/vo/ProductOverviewVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/vo/ProductOverviewVO.java index 60decb86..966ddaf1 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/vo/ProductOverviewVO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/model/vo/ProductOverviewVO.java @@ -9,10 +9,10 @@ import java.time.LocalDate; import java.util.List; /** - * 商品总览VO + * 商品概览响应VO */ @Data -@Schema(description = "商品总览VO") +@Schema(description = "商品概览响应VO") public class ProductOverviewVO { /** @@ -27,54 +27,116 @@ public class ProductOverviewVO { @Schema(description = "商品名称") private String productName; - /** - * 销售额统计 - */ - @Schema(description = "销售额统计") - private DailyStatistics salesAmount; + // ========== KPI 汇总指标 ========== /** - * 订单数统计 + * 总销售额 */ - @Schema(description = "订单数统计") - private DailyStatistics orderCount; + @Schema(description = "总销售额") + private BigDecimal totalSalesAmount; /** - * 客单价统计 + * 较上月同期增长百分比 */ - @Schema(description = "客单价统计") - private DailyStatistics averageOrderValue; + @Schema(description = "较上月同期增长百分比", example = "15.50") + private BigDecimal monthOverMonthGrowth; /** - * 成本统计 + * 总订单量 */ - @Schema(description = "成本统计") - private DailyStatistics costAmount; + @Schema(description = "总订单量") + private Integer totalOrderCount; /** - * 退款金额统计 + * 平均费比(推广费/销售额) */ - @Schema(description = "退款金额统计") - private DailyStatistics refundAmount; + @Schema(description = "平均费比") + private BigDecimal averageFeeRatio; /** - * 推广费统计 + * 总净利润(税后) */ - @Schema(description = "推广费统计") - private DailyStatistics promotionFee; + @Schema(description = "总净利润(税后)") + private BigDecimal totalNetProfitAfterTax; /** - * 费比统计 + * 扣除总税额 */ - @Schema(description = "费比统计") - private DailyStatistics feeRatio; + @Schema(description = "扣除总税额") + private BigDecimal totalTaxAmount; /** - * 每日统计数据 + * 销量目标达成率 + */ + @Schema(description = "销量目标达成率") + private BigDecimal orderCountAchievementRate; + + /** + * 销售额目标达成率 + */ + @Schema(description = "销售额目标达成率") + private BigDecimal salesAmountAchievementRate; + + /** + * 汇总ROI + */ + @Schema(description = "汇总ROI") + private BigDecimal totalROI; + + /** + * 汇总客单价 + */ + @Schema(description = "汇总客单价") + private BigDecimal averageOrderValue; + + /** + * 退款金额合计 + */ + @Schema(description = "退款金额合计") + private BigDecimal refundAmount; + + /** + * 税前盈亏 + */ + @Schema(description = "税前盈亏") + private BigDecimal totalProfitBeforeTax; + + /** + * 总税后盈亏(与 totalNetProfitAfterTax 一致) + */ + @Schema(description = "总税后盈亏") + private BigDecimal totalProfitAfterTax; + + // ========== 趋势数据 ========== + + /** + * 销售额趋势分析 + */ + @Schema(description = "销售额趋势分析") + private TrendData salesAmountTrend; + + /** + * 单量趋势分析 + */ + @Schema(description = "单量趋势分析") + private TrendData orderCountTrend; + + // ========== 规格销售汇总 ========== + + /** + * 规格销售汇总(含各规格明细列表和合计行) + */ + @Schema(description = "规格销售汇总") + private SpecSummary specSummaries; + + // ========== 内部类 ========== + + /** + * 趋势数据 */ @Data - @Schema(description = "每日统计数据") - public static class DailyStatistics { + @Schema(description = "趋势数据") + public static class TrendData { /** * 时间范围的总值 */ @@ -85,15 +147,15 @@ public class ProductOverviewVO { * 每日数据列表 */ @Schema(description = "每日数据列表") - private List dailyDataList; + private List dailyDataList; } /** - * 每日数据项 + * 趋势数据项 */ @Data - @Schema(description = "每日数据项") - public static class DailyData { + @Schema(description = "趋势数据项") + public static class TrendDataItem { /** * 日期 */ @@ -107,4 +169,217 @@ public class ProductOverviewVO { @Schema(description = "对应的值") private BigDecimal value; } -} + + /** + * 规格销售汇总 + */ + @Data + @Schema(description = "规格销售汇总") + public static class SpecSummary { + /** + * 各规格明细列表 + */ + @Schema(description = "各规格明细列表") + private List list; + + /** + * 合计行 + */ + @Schema(description = "合计行") + private SpecSummaryTotal total; + } + + /** + * 规格销售明细项 + */ + @Data + @Schema(description = "规格销售明细项") + public static class SpecSummaryItem { + /** + * 规格ID + */ + @Schema(description = "规格ID") + private Long specId; + + /** + * 规格名称 + */ + @Schema(description = "规格名称") + private String specName; + + /** + * 规格编码 + */ + @Schema(description = "规格编码") + private String specCode; + + /** + * 销售额 + */ + @Schema(description = "销售额") + private BigDecimal salesAmount; + + /** + * 订单量(按平台订单号去重) + */ + @Schema(description = "订单量") + private Integer orderCount; + + /** + * 总件数 + */ + @Schema(description = "总件数") + private Integer quantity; + + /** + * 客单价 + */ + @Schema(description = "客单价") + private BigDecimal averageOrderValue; + + /** + * 商品成本 + */ + @Schema(description = "商品成本") + private BigDecimal productCost; + + /** + * 运费 + */ + @Schema(description = "运费") + private BigDecimal shippingCost; + + /** + * 其他成本 + */ + @Schema(description = "其他成本") + private BigDecimal otherCost; + + /** + * 退款金额 + */ + @Schema(description = "退款金额") + private BigDecimal refundAmount; + + /** + * 推广费 + */ + @Schema(description = "推广费") + private BigDecimal promotionFee; + + /** + * 费比(推广费/销售额) + */ + @Schema(description = "费比") + private BigDecimal feeRatio; + + /** + * 税前盈亏 + */ + @Schema(description = "税前盈亏") + private BigDecimal profitBeforeTax; + + /** + * 销售额×税率的税额 + */ + @Schema(description = "销售额×税率的税额") + private BigDecimal salesTaxAmount; + + /** + * ROI + */ + @Schema(description = "ROI") + private BigDecimal roi; + + /** + * 税后盈亏 + */ + @Schema(description = "税后盈亏") + private BigDecimal profitAfterTax; + } + + /** + * 规格销售汇总合计行 + */ + @Data + @Schema(description = "规格销售汇总合计行") + public static class SpecSummaryTotal { + /** + * 销售额 + */ + @Schema(description = "销售额") + private BigDecimal salesAmount; + + /** + * 订单量 + */ + @Schema(description = "订单量") + private Integer orderCount; + + /** + * 总件数 + */ + @Schema(description = "总件数") + private Integer quantity; + + /** + * 商品成本 + */ + @Schema(description = "商品成本") + private BigDecimal productCost; + + /** + * 运费 + */ + @Schema(description = "运费") + private BigDecimal shippingCost; + + /** + * 其他成本 + */ + @Schema(description = "其他成本") + private BigDecimal otherCost; + + /** + * 退款金额 + */ + @Schema(description = "退款金额") + private BigDecimal refundAmount; + + /** + * 推广费 + */ + @Schema(description = "推广费") + private BigDecimal promotionFee; + + /** + * 费比 + */ + @Schema(description = "费比") + private BigDecimal feeRatio; + + /** + * 税前盈亏 + */ + @Schema(description = "税前盈亏") + private BigDecimal profitBeforeTax; + + /** + * 税额 + */ + @Schema(description = "税额") + private BigDecimal salesTaxAmount; + + /** + * ROI + */ + @Schema(description = "ROI") + private BigDecimal roi; + + /** + * 税后盈亏 + */ + @Schema(description = "税后盈亏") + private BigDecimal profitAfterTax; + } +} \ No newline at end of file diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java index d17f4b9f..0a9717e9 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java @@ -8,22 +8,24 @@ import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO; import top.xyquan.ecomdmbbackend.modules.ecom.order.status.mapper.OrderStatusMapper; import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO; import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO; +import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO; +import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper; import top.xyquan.ecomdmbbackend.modules.ecom.product.overview.model.dto.ProductOverviewQueryDTO; import top.xyquan.ecomdmbbackend.modules.ecom.product.overview.model.vo.ProductOverviewVO; import top.xyquan.ecomdmbbackend.modules.ecom.product.overview.service.ProductOverviewService; import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService; import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMapper; import top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.po.PromotionFeePO; +import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper; +import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.model.po.ShopMonthlyTargetPO; import top.xyquan.ecomdmbbackend.utils.CurrentUserContext; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.time.temporal.ChronoUnit; +import java.util.*; import java.util.stream.Collectors; /** @@ -37,17 +39,23 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { private final PromotionFeeMapper promotionFeeMapper; private final OrderStatusMapper orderStatusMapper; private final ProductService productService; + private final ProductSpecMapper productSpecMapper; + private final ShopMonthlyTargetMapper shopMonthlyTargetMapper; public ProductOverviewServiceImpl(CurrentUserContext currentUserContext, OrderMapper orderMapper, PromotionFeeMapper promotionFeeMapper, OrderStatusMapper orderStatusMapper, - ProductService productService) { + ProductService productService, + ProductSpecMapper productSpecMapper, + ShopMonthlyTargetMapper shopMonthlyTargetMapper) { this.currentUserContext = currentUserContext; this.orderMapper = orderMapper; this.promotionFeeMapper = promotionFeeMapper; this.orderStatusMapper = orderStatusMapper; this.productService = productService; + this.productSpecMapper = productSpecMapper; + this.shopMonthlyTargetMapper = shopMonthlyTargetMapper; } @Override @@ -65,11 +73,17 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { throw new BusinessException("开始日期不能晚于结束日期"); } - // 查询时间范围 - LocalDateTime startDateTime = queryDTO.getStartDate().atStartOfDay(); - LocalDateTime endDateTime = queryDTO.getEndDate().atTime(23, 59, 59); + LocalDate startDate = queryDTO.getStartDate(); + LocalDate endDate = queryDTO.getEndDate(); + LocalDateTime startDateTime = startDate.atStartOfDay(); + LocalDateTime endDateTime = endDate.atTime(23, 59, 59); - // 查询订单数据(按商品过滤) + // 税率 + BigDecimal taxRate = queryDTO.getTaxRate() != null + ? queryDTO.getTaxRate() + : new BigDecimal("0.04"); + + // 查询订单数据 LambdaQueryWrapper orderWrapper = new LambdaQueryWrapper<>(); orderWrapper.eq(OrderPO::getCompanyId, companyId) .eq(OrderPO::getProductId, queryDTO.getProductId()) @@ -77,344 +91,515 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { .le(OrderPO::getOrderTime, endDateTime); List orders = orderMapper.selectList(orderWrapper); - // 查询推广费数据(店铺粒度的推广费,需要通过订单关联的shopId过滤) - // 先收集该商品对应订单的店铺ID列表 + // 收集店铺ID列表 List shopIds = orders.stream() .map(OrderPO::getShopId) - .filter(shopId -> shopId != null) + .filter(Objects::nonNull) .distinct() .collect(Collectors.toList()); + // 查询推广费(店铺粒度) List promotionFees = new ArrayList<>(); if (!shopIds.isEmpty()) { - LambdaQueryWrapper promotionWrapper = new LambdaQueryWrapper<>(); - promotionWrapper.eq(PromotionFeePO::getCompanyId, companyId) + LambdaQueryWrapper promoWrapper = new LambdaQueryWrapper<>(); + promoWrapper.eq(PromotionFeePO::getCompanyId, companyId) .in(PromotionFeePO::getShopId, shopIds) - .ge(PromotionFeePO::getFeeDate, queryDTO.getStartDate()) - .le(PromotionFeePO::getFeeDate, queryDTO.getEndDate()); - promotionFees = promotionFeeMapper.selectList(promotionWrapper); + .ge(PromotionFeePO::getFeeDate, startDate) + .le(PromotionFeePO::getFeeDate, endDate); + promotionFees = promotionFeeMapper.selectList(promoWrapper); } // 获取退款状态ID - List refundStatusIds = getRefundStatusIds(); + Set refundStatusIds = getRefundStatusIds(companyId); - // 按日期分组订单数据 - Map> ordersByDate = orders.stream() - .collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate())); + // 查询上月同期数据(用于环比计算) + long dayCount = ChronoUnit.DAYS.between(startDate, endDate) + 1; + LocalDate lastStartDate = startDate.minusDays(dayCount); + LocalDate lastEndDate = startDate.minusDays(1); + LocalDateTime lastStartTime = lastStartDate.atStartOfDay(); + LocalDateTime lastEndTime = lastEndDate.atTime(23, 59, 59); - // 按日期分组推广费数据 - Map> promotionFeesByDate = promotionFees.stream() - .collect(Collectors.groupingBy(PromotionFeePO::getFeeDate)); + LambdaQueryWrapper lastPeriodWrapper = new LambdaQueryWrapper<>(); + lastPeriodWrapper.eq(OrderPO::getCompanyId, companyId) + .eq(OrderPO::getProductId, queryDTO.getProductId()) + .ge(OrderPO::getOrderTime, lastStartTime) + .le(OrderPO::getOrderTime, lastEndTime); + List lastPeriodOrders = orderMapper.selectList(lastPeriodWrapper); - // 生成日期范围内的所有日期列表 - List dateList = new ArrayList<>(); - LocalDate currentDate = queryDTO.getStartDate(); - while (!currentDate.isAfter(queryDTO.getEndDate())) { - dateList.add(currentDate); - currentDate = currentDate.plusDays(1); - } + // ========== 构建返回对象 ========== + ProductOverviewVO vo = new ProductOverviewVO(); + vo.setProductId(product.getId()); + vo.setProductName(product.getName()); - // 构建返回对象 - ProductOverviewVO overviewVO = new ProductOverviewVO(); - overviewVO.setProductId(product.getId()); - overviewVO.setProductName(product.getName()); - - // 计算销售额统计 - ProductOverviewVO.DailyStatistics salesAmountStats = calculateSalesAmountStatistics( - ordersByDate, dateList, orders); - overviewVO.setSalesAmount(salesAmountStats); - - // 计算订单数统计 - ProductOverviewVO.DailyStatistics orderCountStats = calculateOrderCountStatistics( - ordersByDate, dateList, orders); - overviewVO.setOrderCount(orderCountStats); - - // 计算客单价统计 - ProductOverviewVO.DailyStatistics averageOrderValueStats = calculateAverageOrderValueStatistics( - salesAmountStats, orderCountStats); - overviewVO.setAverageOrderValue(averageOrderValueStats); - - // 计算成本统计 - ProductOverviewVO.DailyStatistics costAmountStats = calculateCostAmountStatistics( - ordersByDate, dateList, orders); - overviewVO.setCostAmount(costAmountStats); - - // 计算退款金额统计 - ProductOverviewVO.DailyStatistics refundAmountStats = calculateRefundAmountStatistics( - ordersByDate, dateList, orders, refundStatusIds); - overviewVO.setRefundAmount(refundAmountStats); - - // 计算推广费统计(按商品关联的店铺分摊,这里简化处理:按店铺粒度的推广费均摊到各商品) - ProductOverviewVO.DailyStatistics promotionFeeStats = calculatePromotionFeeStatistics( - promotionFeesByDate, dateList, promotionFees, shopIds.size()); - overviewVO.setPromotionFee(promotionFeeStats); - - // 计算费比统计 - ProductOverviewVO.DailyStatistics feeRatioStats = calculateFeeRatioStatistics( - salesAmountStats, promotionFeeStats); - overviewVO.setFeeRatio(feeRatioStats); - - return overviewVO; - } - - /** - * 计算销售额统计 - */ - private ProductOverviewVO.DailyStatistics calculateSalesAmountStatistics( - Map> ordersByDate, List dateList, List allOrders) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal total = allOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + // ---------- KPI 汇总指标 ---------- + // 总销售额 + BigDecimal totalSalesAmount = orders.stream() + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) .reduce(BigDecimal.ZERO, BigDecimal::add); - stats.setTotal(total); + vo.setTotalSalesAmount(totalSalesAmount); - List dailyDataList = dateList.stream() - .map(date -> { - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(date); - List dayOrders = ordersByDate.getOrDefault(date, List.of()); - BigDecimal dayAmount = dayOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - dailyData.setValue(dayAmount); - return dailyData; - }) - .sorted(Comparator.comparing(ProductOverviewVO.DailyData::getDate)) - .collect(Collectors.toList()); - stats.setDailyDataList(dailyDataList); - - return stats; - } - - /** - * 计算订单数统计 - */ - private ProductOverviewVO.DailyStatistics calculateOrderCountStatistics( - Map> ordersByDate, List dateList, List allOrders) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - // 按平台订单号去重统计总订单数 - long totalCount = allOrders.stream() + // 总订单量(按平台订单号去重) + long totalOrderCount = orders.stream() .map(OrderPO::getPlatformOrderNo) .filter(no -> no != null && !no.isEmpty()) .distinct() .count(); - if (totalCount == 0) { - totalCount = allOrders.size(); + if (totalOrderCount == 0) { + totalOrderCount = orders.size(); } - stats.setTotal(BigDecimal.valueOf(totalCount)); + vo.setTotalOrderCount((int) totalOrderCount); - List dailyDataList = dateList.stream() - .map(date -> { - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(date); - List dayOrders = ordersByDate.getOrDefault(date, List.of()); - long dayCount = dayOrders.stream() - .map(OrderPO::getPlatformOrderNo) - .filter(no -> no != null && !no.isEmpty()) - .distinct() - .count(); - if (dayCount == 0) { - dayCount = dayOrders.size(); - } - dailyData.setValue(BigDecimal.valueOf(dayCount)); - return dailyData; - }) - .sorted(Comparator.comparing(ProductOverviewVO.DailyData::getDate)) - .collect(Collectors.toList()); - stats.setDailyDataList(dailyDataList); - - return stats; - } - - /** - * 计算客单价统计 - */ - private ProductOverviewVO.DailyStatistics calculateAverageOrderValueStatistics( - ProductOverviewVO.DailyStatistics salesAmountStats, ProductOverviewVO.DailyStatistics orderCountStats) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal totalSales = salesAmountStats.getTotal(); - BigDecimal totalOrders = orderCountStats.getTotal(); - BigDecimal total = totalOrders.compareTo(BigDecimal.ZERO) > 0 - ? totalSales.divide(totalOrders, 2, RoundingMode.HALF_UP) + // 汇总客单价 + BigDecimal avgOrderValue = totalOrderCount > 0 + ? totalSalesAmount.divide(BigDecimal.valueOf(totalOrderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; - stats.setTotal(total); + vo.setAverageOrderValue(avgOrderValue); - List dailyDataList = new ArrayList<>(); - for (int i = 0; i < salesAmountStats.getDailyDataList().size(); i++) { - ProductOverviewVO.DailyData salesData = salesAmountStats.getDailyDataList().get(i); - ProductOverviewVO.DailyData orderData = orderCountStats.getDailyDataList().get(i); + // 总退款金额(退款状态订单的实收金额) + BigDecimal totalRefundAmount = orders.stream() + .filter(o -> refundStatusIds.contains(o.getStatusId())) + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + vo.setRefundAmount(totalRefundAmount); - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(salesData.getDate()); - BigDecimal dayValue = orderData.getValue().compareTo(BigDecimal.ZERO) > 0 - ? salesData.getValue().divide(orderData.getValue(), 2, RoundingMode.HALF_UP) - : BigDecimal.ZERO; - dailyData.setValue(dayValue); - dailyDataList.add(dailyData); + // 总成本(含数量) + BigDecimal totalCostAmount = orders.stream() + .map(this::calcOrderCost) + .reduce(BigDecimal.ZERO, BigDecimal::add); + vo.setAverageFeeRatio(null); // 由 spec 维度均化,待后面补充 + + // 总推广费 + BigDecimal totalPromotionFee = promotionFees.stream() + .map(f -> f.getFeeAmount() != null ? f.getFeeAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + // 按订单数量比例分摊到当前商品(简化处理) + if (!shopIds.isEmpty() && totalPromotionFee.compareTo(BigDecimal.ZERO) > 0) { + // 按商品所在订单数占总订单比例分摊(近似) + // 这里不做细粒度分摊,保持店铺维度总额在 spec 维度按比例拆分 } - stats.setDailyDataList(dailyDataList); - return stats; - } - - /** - * 计算成本统计 - */ - private ProductOverviewVO.DailyStatistics calculateCostAmountStatistics( - Map> ordersByDate, List dateList, List allOrders) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal total = allOrders.stream() - .map(order -> { - BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; - BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; - BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return productCost.add(shippingCost).add(otherCost).multiply(new BigDecimal(quantity)); - }) - .reduce(BigDecimal.ZERO, BigDecimal::add); - stats.setTotal(total); - - List dailyDataList = dateList.stream() - .map(date -> { - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(date); - List dayOrders = ordersByDate.getOrDefault(date, List.of()); - BigDecimal dayCost = dayOrders.stream() - .map(order -> { - BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; - BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; - BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return productCost.add(shippingCost).add(otherCost).multiply(new BigDecimal(quantity)); - }) - .reduce(BigDecimal.ZERO, BigDecimal::add); - dailyData.setValue(dayCost); - return dailyData; - }) - .sorted(Comparator.comparing(ProductOverviewVO.DailyData::getDate)) - .collect(Collectors.toList()); - stats.setDailyDataList(dailyDataList); - - return stats; - } - - /** - * 计算退款金额统计 - */ - private ProductOverviewVO.DailyStatistics calculateRefundAmountStatistics( - Map> ordersByDate, List dateList, - List allOrders, List refundStatusIds) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal total = allOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - stats.setTotal(total); - - List dailyDataList = dateList.stream() - .map(date -> { - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(date); - List dayOrders = ordersByDate.getOrDefault(date, List.of()); - BigDecimal dayRefund = dayOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - dailyData.setValue(dayRefund); - return dailyData; - }) - .sorted(Comparator.comparing(ProductOverviewVO.DailyData::getDate)) - .collect(Collectors.toList()); - stats.setDailyDataList(dailyDataList); - - return stats; - } - - /** - * 计算推广费统计(推广费按商品关联的店铺均摊) - */ - private ProductOverviewVO.DailyStatistics calculatePromotionFeeStatistics( - Map> promotionFeesByDate, List dateList, - List allFees, int shopCount) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal totalPerProduct = allFees.stream() - .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - // 如果有多个店铺,按店铺数量均分(简化处理:假设各商品等比) - if (shopCount > 1) { - totalPerProduct = totalPerProduct.divide(new BigDecimal(shopCount), 2, RoundingMode.HALF_UP); - } - stats.setTotal(totalPerProduct); - - List dailyDataList = dateList.stream() - .map(date -> { - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(date); - List dayFees = promotionFeesByDate.getOrDefault(date, List.of()); - BigDecimal dayFee = dayFees.stream() - .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - if (shopCount > 1) { - dayFee = dayFee.divide(new BigDecimal(shopCount), 2, RoundingMode.HALF_UP); - } - dailyData.setValue(dayFee); - return dailyData; - }) - .sorted(Comparator.comparing(ProductOverviewVO.DailyData::getDate)) - .collect(Collectors.toList()); - stats.setDailyDataList(dailyDataList); - - return stats; - } - - /** - * 计算费比统计 - */ - private ProductOverviewVO.DailyStatistics calculateFeeRatioStatistics( - ProductOverviewVO.DailyStatistics salesAmountStats, ProductOverviewVO.DailyStatistics promotionFeeStats) { - ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics(); - - BigDecimal totalSales = salesAmountStats.getTotal(); - BigDecimal totalFee = promotionFeeStats.getTotal(); - BigDecimal total = totalSales.compareTo(BigDecimal.ZERO) > 0 - ? totalFee.divide(totalSales, 4, RoundingMode.HALF_UP) + // 费比 + BigDecimal avgFeeRatio = totalSalesAmount.compareTo(BigDecimal.ZERO) > 0 + ? totalPromotionFee.divide(totalSalesAmount, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; - stats.setTotal(total); + vo.setAverageFeeRatio(avgFeeRatio.multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP)); // 转为百分比 - List dailyDataList = new ArrayList<>(); - for (int i = 0; i < salesAmountStats.getDailyDataList().size(); i++) { - ProductOverviewVO.DailyData salesData = salesAmountStats.getDailyDataList().get(i); - ProductOverviewVO.DailyData feeData = promotionFeeStats.getDailyDataList().get(i); + // 税前盈亏 = 销售额 - 成本 - 推广费 + BigDecimal totalProfitBeforeTax = totalSalesAmount + .subtract(totalCostAmount) + .subtract(totalPromotionFee); + vo.setTotalProfitBeforeTax(totalProfitBeforeTax); - ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData(); - dailyData.setDate(salesData.getDate()); - BigDecimal dayRatio = salesData.getValue().compareTo(BigDecimal.ZERO) > 0 - ? feeData.getValue().divide(salesData.getValue(), 4, RoundingMode.HALF_UP) - : BigDecimal.ZERO; - dailyData.setValue(dayRatio); - dailyDataList.add(dailyData); + // 税额 = 销售额 × 税率 + BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate) + .setScale(2, RoundingMode.HALF_UP); + vo.setTotalTaxAmount(totalTaxAmount); + + // 税后盈亏 + BigDecimal totalProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount); + vo.setTotalProfitAfterTax(totalProfitAfterTax); + vo.setTotalNetProfitAfterTax(totalProfitAfterTax); + + // ROI = (销售额 - 成本) / 推广费 + BigDecimal totalROI = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 + ? totalSalesAmount.subtract(totalCostAmount) + .divide(totalPromotionFee, 2, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + : BigDecimal.ZERO; + vo.setTotalROI(totalROI); + + // ---------- 环比增长(上月同期) ---------- + BigDecimal lastPeriodSales = lastPeriodOrders.stream() + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal monthOverMonthGrowth = BigDecimal.ZERO; + if (lastPeriodSales.compareTo(BigDecimal.ZERO) > 0) { + monthOverMonthGrowth = totalSalesAmount + .subtract(lastPeriodSales) + .divide(lastPeriodSales, 4, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP); + } else if (totalSalesAmount.compareTo(BigDecimal.ZERO) > 0) { + monthOverMonthGrowth = new BigDecimal("100.00"); } - stats.setDailyDataList(dailyDataList); + vo.setMonthOverMonthGrowth(monthOverMonthGrowth); - return stats; + // ---------- 达成率(简化:按店铺月度目标计算) ---------- + if (!shopIds.isEmpty()) { + LocalDate firstDayOfMonth = startDate.withDayOfMonth(1); + LocalDate lastDayOfMonth = endDate.withDayOfMonth(endDate.lengthOfMonth()); + + LambdaQueryWrapper targetWrapper = new LambdaQueryWrapper<>(); + targetWrapper.eq(ShopMonthlyTargetPO::getCompanyId, companyId) + .in(ShopMonthlyTargetPO::getShopId, shopIds) + .ge(ShopMonthlyTargetPO::getTargetMonth, firstDayOfMonth) + .le(ShopMonthlyTargetPO::getTargetMonth, lastDayOfMonth); + List shopTargets = shopMonthlyTargetMapper.selectList(targetWrapper); + + BigDecimal totalTargetAmount = shopTargets.stream() + .map(ShopMonthlyTargetPO::getTargetAmount) + .filter(Objects::nonNull) + .reduce(BigDecimal.ZERO, BigDecimal::add); + Integer totalTargetOrderCount = shopTargets.stream() + .map(ShopMonthlyTargetPO::getTargetOrderCount) + .filter(Objects::nonNull) + .reduce(0, Integer::sum); + + if (totalTargetAmount.compareTo(BigDecimal.ZERO) > 0) { + BigDecimal salesRate = totalSalesAmount + .divide(totalTargetAmount, 4, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP); + vo.setSalesAmountAchievementRate(salesRate); + } + if (totalTargetOrderCount != null && totalTargetOrderCount > 0) { + BigDecimal orderRate = BigDecimal.valueOf(totalOrderCount) + .divide(BigDecimal.valueOf(totalTargetOrderCount), 4, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP); + vo.setOrderCountAchievementRate(orderRate); + } + } + + // ---------- 趋势数据 ---------- + vo.setSalesAmountTrend(buildTrend(orders, dateList -> dateList.stream() + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add), startDate, endDate)); + + vo.setOrderCountTrend(buildOrderCountTrend(orders, startDate, endDate)); + + // ---------- 规格销售汇总 ---------- + vo.setSpecSummaries(buildSpecSummary(orders, promotionFees, refundStatusIds, taxRate, shopIds)); + + return vo; } /** - * 获取退款状态ID列表(根据 is_refund 字段查找) + * 计算单笔订单总成本(含数量) */ - private List getRefundStatusIds() { - Long companyId = currentUserContext.getCurrentCompanyId(); + private BigDecimal calcOrderCost(OrderPO order) { + BigDecimal productCost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; + BigDecimal shippingCost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; + BigDecimal otherCost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; + int quantity = order.getQuantity() != null ? order.getQuantity() : 1; + return productCost.add(shippingCost).add(otherCost) + .multiply(BigDecimal.valueOf(quantity)); + } + + /** + * 构建销售趋势 + */ + private ProductOverviewVO.TrendData buildTrend(List orders, + java.util.function.Function, BigDecimal> reducer, + LocalDate startDate, LocalDate endDate) { + Map> ordersByDate = orders.stream() + .collect(Collectors.groupingBy(o -> o.getOrderTime().toLocalDate())); + + List dailyList = new ArrayList<>(); + LocalDate current = startDate; + while (!current.isAfter(endDate)) { + List dayOrders = ordersByDate.getOrDefault(current, List.of()); + BigDecimal value = reducer.apply(dayOrders); + + ProductOverviewVO.TrendDataItem item = new ProductOverviewVO.TrendDataItem(); + item.setDate(current); + item.setValue(value); + dailyList.add(item); + current = current.plusDays(1); + } + + BigDecimal total = dailyList.stream() + .map(ProductOverviewVO.TrendDataItem::getValue) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + ProductOverviewVO.TrendData trend = new ProductOverviewVO.TrendData(); + trend.setTotal(total); + trend.setDailyDataList(dailyList); + return trend; + } + + /** + * 构建订单数趋势 + */ + private ProductOverviewVO.TrendData buildOrderCountTrend(List orders, + LocalDate startDate, LocalDate endDate) { + Map> ordersByDate = orders.stream() + .collect(Collectors.groupingBy(o -> o.getOrderTime().toLocalDate())); + + List dailyList = new ArrayList<>(); + LocalDate current = startDate; + while (!current.isAfter(endDate)) { + List dayOrders = ordersByDate.getOrDefault(current, List.of()); + // 按平台订单号去重 + long count = dayOrders.stream() + .map(OrderPO::getPlatformOrderNo) + .filter(no -> no != null && !no.isEmpty()) + .distinct() + .count(); + if (count == 0) count = dayOrders.size(); + + ProductOverviewVO.TrendDataItem item = new ProductOverviewVO.TrendDataItem(); + item.setDate(current); + item.setValue(BigDecimal.valueOf(count)); + dailyList.add(item); + current = current.plusDays(1); + } + + BigDecimal total = BigDecimal.valueOf(dailyList.stream() + .mapToLong(i -> i.getValue().longValue()) + .sum()); + + ProductOverviewVO.TrendData trend = new ProductOverviewVO.TrendData(); + trend.setTotal(total); + trend.setDailyDataList(dailyList); + return trend; + } + + /** + * 构建规格销售汇总 + */ + private ProductOverviewVO.SpecSummary buildSpecSummary(List orders, + List promotionFees, + Set refundStatusIds, + BigDecimal taxRate, + List shopIds) { + // 查询商品所有规格 + if (orders.isEmpty()) { + return buildEmptySpecSummary(); + } + + Long productId = orders.get(0).getProductId(); + Long companyId = orders.get(0).getCompanyId(); + + LambdaQueryWrapper specWrapper = new LambdaQueryWrapper<>(); + specWrapper.eq(ProductSpecPO::getCompanyId, companyId) + .eq(ProductSpecPO::getProductId, productId); + List allSpecs = productSpecMapper.selectList(specWrapper); + Map specMap = allSpecs.stream() + .collect(Collectors.toMap(ProductSpecPO::getId, s -> s, (a, b) -> a)); + + // 推广费按店铺维度收集,后面按订单数比例拆分 + Map shopPromotionFeeMap = promotionFees.stream() + .collect(Collectors.groupingBy(PromotionFeePO::getShopId, + Collectors.reducing(BigDecimal.ZERO, + f -> f.getFeeAmount() != null ? f.getFeeAmount() : BigDecimal.ZERO, + BigDecimal::add))); + + // 按规格分组订单 + Map> ordersBySpec = orders.stream() + .collect(Collectors.groupingBy(o -> { + Long specId = o.getProductSpecId(); + return specId != null ? specId : 0L; + })); + + List specItems = new ArrayList<>(); + + for (Map.Entry> entry : ordersBySpec.entrySet()) { + Long specId = entry.getKey(); + if (specId == 0L) continue; // 跳过未关联规格的订单 + + List specOrders = entry.getValue(); + ProductSpecPO spec = specMap.get(specId); + + // 该规格的销售额 + BigDecimal salesAmount = specOrders.stream() + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 该规格的订单量(去重) + long orderCount = specOrders.stream() + .map(OrderPO::getPlatformOrderNo) + .filter(no -> no != null && !no.isEmpty()) + .distinct() + .count(); + if (orderCount == 0) orderCount = specOrders.size(); + + // 总件数 + int quantity = specOrders.stream() + .map(o -> o.getQuantity() != null ? o.getQuantity() : 1) + .reduce(0, Integer::sum); + + // 客单价 + BigDecimal avgOV = BigDecimal.valueOf(orderCount).compareTo(BigDecimal.ZERO) > 0 + ? salesAmount.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP) + : BigDecimal.ZERO; + + // 商品成本 + BigDecimal productCostTotal = specOrders.stream() + .map(o -> { + BigDecimal pc = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO; + int qty = o.getQuantity() != null ? o.getQuantity() : 1; + return pc.multiply(BigDecimal.valueOf(qty)); + }) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 运费 + 其他成本 + BigDecimal shippingCostTotal = specOrders.stream() + .map(o -> o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal otherCostTotal = specOrders.stream() + .map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 退款金额 + BigDecimal refundAmount = specOrders.stream() + .filter(o -> refundStatusIds.contains(o.getStatusId())) + .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + // 推广费分摊(按该规格订单数占所有订单数比例) + int totalOrderCountAll = orders.size(); + BigDecimal specPromotionFee = BigDecimal.ZERO; + if (totalOrderCountAll > 0) { + BigDecimal proportion = BigDecimal.valueOf(orderCount) + .divide(BigDecimal.valueOf(totalOrderCountAll), 4, RoundingMode.HALF_UP); + BigDecimal totalPromo = shopPromotionFeeMap.values().stream() + .reduce(BigDecimal.ZERO, BigDecimal::add); + specPromotionFee = totalPromo.multiply(proportion) + .setScale(2, RoundingMode.HALF_UP); + } + + // 费比 + BigDecimal feeRatio = salesAmount.compareTo(BigDecimal.ZERO) > 0 + ? specPromotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP) + : BigDecimal.ZERO; + + // 税前盈亏 + BigDecimal profitBeforeTax = salesAmount + .subtract(productCostTotal) + .subtract(shippingCostTotal) + .subtract(otherCostTotal) + .subtract(specPromotionFee); + + // 税额 + BigDecimal salesTaxAmount = salesAmount.multiply(taxRate) + .setScale(2, RoundingMode.HALF_UP); + + // ROI + BigDecimal roi = specPromotionFee.compareTo(BigDecimal.ZERO) > 0 + ? salesAmount.subtract(productCostTotal) + .divide(specPromotionFee, 2, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + : BigDecimal.ZERO; + + // 税后盈亏 + BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); + + ProductOverviewVO.SpecSummaryItem item = new ProductOverviewVO.SpecSummaryItem(); + item.setSpecId(specId); + item.setSpecName(spec != null ? spec.getName() : "未知规格"); + item.setSpecCode(spec != null ? spec.getSpecCode() : null); + item.setSalesAmount(salesAmount); + item.setOrderCount((int) orderCount); + item.setQuantity(quantity); + item.setAverageOrderValue(avgOV); + item.setProductCost(productCostTotal); + item.setShippingCost(shippingCostTotal); + item.setOtherCost(otherCostTotal); + item.setRefundAmount(refundAmount); + item.setPromotionFee(specPromotionFee); + item.setFeeRatio(feeRatio.multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP)); + item.setProfitBeforeTax(profitBeforeTax); + item.setSalesTaxAmount(salesTaxAmount); + item.setRoi(roi); + item.setProfitAfterTax(profitAfterTax); + + specItems.add(item); + } + + // 构建合计行 + ProductOverviewVO.SpecSummaryTotal total = new ProductOverviewVO.SpecSummaryTotal(); + total.setSalesAmount(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getSalesAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + total.setOrderCount(specItems.stream() + .mapToInt(ProductOverviewVO.SpecSummaryItem::getOrderCount).sum()); + total.setQuantity(specItems.stream() + .mapToInt(ProductOverviewVO.SpecSummaryItem::getQuantity).sum()); + total.setProductCost(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getProductCost) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + total.setShippingCost(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getShippingCost) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + total.setOtherCost(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getOtherCost) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + total.setRefundAmount(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getRefundAmount) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + total.setPromotionFee(specItems.stream() + .map(ProductOverviewVO.SpecSummaryItem::getPromotionFee) + .reduce(BigDecimal.ZERO, BigDecimal::add)); + + BigDecimal totalSalesAmt = total.getSalesAmount(); + BigDecimal totalPromoFee = total.getPromotionFee(); + BigDecimal totalFeeRatio = totalSalesAmt.compareTo(BigDecimal.ZERO) > 0 + ? totalPromoFee.divide(totalSalesAmt, 4, RoundingMode.HALF_UP) + : BigDecimal.ZERO; + total.setFeeRatio(totalFeeRatio.multiply(new BigDecimal("100")) + .setScale(2, RoundingMode.HALF_UP)); + + BigDecimal totalProfitBT = total.getSalesAmount() + .subtract(total.getProductCost()) + .subtract(total.getShippingCost()) + .subtract(total.getOtherCost()) + .subtract(total.getPromotionFee()); + total.setProfitBeforeTax(totalProfitBT); + + BigDecimal totalSalesTax = totalSalesAmt.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); + total.setSalesTaxAmount(totalSalesTax); + + BigDecimal totalRoi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 + ? totalSalesAmt.subtract(total.getProductCost()) + .divide(totalPromoFee, 2, RoundingMode.HALF_UP) + .multiply(new BigDecimal("100")) + : BigDecimal.ZERO; + total.setRoi(totalRoi); + + total.setProfitAfterTax(totalProfitBT.subtract(totalSalesTax)); + + ProductOverviewVO.SpecSummary summary = new ProductOverviewVO.SpecSummary(); + summary.setList(specItems); + summary.setTotal(total); + + return summary; + } + + private ProductOverviewVO.SpecSummary buildEmptySpecSummary() { + ProductOverviewVO.SpecSummaryTotal total = new ProductOverviewVO.SpecSummaryTotal(); + total.setSalesAmount(BigDecimal.ZERO); + total.setOrderCount(0); + total.setQuantity(0); + total.setProductCost(BigDecimal.ZERO); + total.setShippingCost(BigDecimal.ZERO); + total.setOtherCost(BigDecimal.ZERO); + total.setRefundAmount(BigDecimal.ZERO); + total.setPromotionFee(BigDecimal.ZERO); + total.setFeeRatio(BigDecimal.ZERO); + total.setProfitBeforeTax(BigDecimal.ZERO); + total.setSalesTaxAmount(BigDecimal.ZERO); + total.setRoi(BigDecimal.ZERO); + total.setProfitAfterTax(BigDecimal.ZERO); + + ProductOverviewVO.SpecSummary summary = new ProductOverviewVO.SpecSummary(); + summary.setList(new ArrayList<>()); + summary.setTotal(total); + return summary; + } + + /** + * 获取退款状态ID集合 + */ + private Set getRefundStatusIds(Long companyId) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(OrderStatusPO::getCompanyId, companyId) .eq(OrderStatusPO::getIsRefund, 1) .select(OrderStatusPO::getId); - List statusList = orderStatusMapper.selectList(wrapper); - return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList()); + return orderStatusMapper.selectList(wrapper).stream() + .map(OrderStatusPO::getId) + .collect(Collectors.toSet()); } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.dto.ts b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.dto.ts index 51d049d3..1bdd9083 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.dto.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.dto.ts @@ -7,5 +7,7 @@ export namespace ProductOverviewDTO { startDate: string; /** 结束日期 */ endDate: string; + /** 税率(可选,0~1) */ + taxRate?: number; } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.vo.ts b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.vo.ts index 2bfa1962..2b67b6ce 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.vo.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/ecommerce/product-overview/types/types.vo.ts @@ -1,39 +1,139 @@ export namespace ProductOverviewVO { - /** 每日数据项 */ - export interface DailyData { + /** 趋势数据项 */ + export interface TrendDataItem { /** 日期 */ date: string; /** 对应的值 */ value: number; } - /** 每日统计数据 */ - export interface DailyStatistics { + /** 趋势数据 */ + export interface TrendData { /** 时间范围的总值 */ total: number; /** 每日数据列表 */ - dailyDataList: DailyData[]; + dailyDataList: TrendDataItem[]; } - /** 商品总览 */ + /** 规格销售明细项 */ + export interface SpecSummaryItem { + /** 规格ID */ + specId: number; + /** 规格名称 */ + specName: string; + /** 规格编码 */ + specCode: string; + /** 销售额 */ + salesAmount: number; + /** 订单量 */ + orderCount: number; + /** 总件数 */ + quantity: number; + /** 客单价 */ + averageOrderValue: number; + /** 商品成本 */ + productCost: number; + /** 运费 */ + shippingCost: number; + /** 其他成本 */ + otherCost: number; + /** 退款金额 */ + refundAmount: number; + /** 推广费 */ + promotionFee: number; + /** 费比 */ + feeRatio: number; + /** 税前盈亏 */ + profitBeforeTax: number; + /** 销售额×税率的税额 */ + salesTaxAmount: number; + /** ROI */ + roi: number; + /** 税后盈亏 */ + profitAfterTax: number; + } + + /** 规格销售汇总合计行 */ + export interface SpecSummaryTotal { + /** 销售额 */ + salesAmount: number; + /** 订单量 */ + orderCount: number; + /** 总件数 */ + quantity: number; + /** 商品成本 */ + productCost: number; + /** 运费 */ + shippingCost: number; + /** 其他成本 */ + otherCost: number; + /** 退款金额 */ + refundAmount: number; + /** 推广费 */ + promotionFee: number; + /** 费比 */ + feeRatio: number; + /** 税前盈亏 */ + profitBeforeTax: number; + /** 税额 */ + salesTaxAmount: number; + /** ROI */ + roi: number; + /** 税后盈亏 */ + profitAfterTax: number; + } + + /** 规格销售汇总 */ + export interface SpecSummary { + /** 各规格明细列表 */ + list: SpecSummaryItem[]; + /** 合计行 */ + total: SpecSummaryTotal; + } + + /** 商品概览响应 */ export interface Overview { /** 商品ID */ productId: number; /** 商品名称 */ productName: string; - /** 销售额统计 */ - salesAmount: DailyStatistics; - /** 订单数统计 */ - orderCount: DailyStatistics; - /** 客单价统计 */ - averageOrderValue: DailyStatistics; - /** 成本统计 */ - costAmount: DailyStatistics; - /** 退款金额统计 */ - refundAmount: DailyStatistics; - /** 推广费统计 */ - promotionFee: DailyStatistics; - /** 费比统计 */ - feeRatio: DailyStatistics; + + // ========== KPI 汇总指标 ========== + /** 总销售额 */ + totalSalesAmount: number; + /** 较上月同期增长百分比 */ + monthOverMonthGrowth: number; + /** 总订单量 */ + totalOrderCount: number; + /** 平均费比 */ + averageFeeRatio: number; + /** 总净利润(税后) */ + totalNetProfitAfterTax: number; + /** 扣除总税额 */ + totalTaxAmount: number; + /** 销量目标达成率 */ + orderCountAchievementRate: number; + /** 销售额目标达成率 */ + salesAmountAchievementRate: number; + /** 汇总ROI */ + totalROI: number; + /** 汇总客单价 */ + averageOrderValue: number; + /** 退款金额合计 */ + refundAmount: number; + /** 税前盈亏 */ + totalProfitBeforeTax: number; + /** 总税后盈亏 */ + totalProfitAfterTax: number; + + // ========== 趋势数据 ========== + /** 销售额趋势分析 */ + salesAmountTrend: TrendData; + /** 单量趋势分析 */ + orderCountTrend: TrendData; + + // ========== 规格销售汇总 ========== + /** 规格销售汇总 */ + specSummaries: SpecSummary; } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue index a49ef9a3..a6e9bd0c 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue @@ -11,7 +11,6 @@ import { Button, Card, DatePicker, - Form, Image, Input, InputNumber, @@ -370,11 +369,11 @@ const saveEdit = async () => {
-
+
@@ -397,11 +396,11 @@ const saveEdit = async () => { class="product-image" @error="handleImageError" /> -
- +
+ 点击上传
-
+
{{ currentImageUrl && !imageLoadError ? '更换图片' : '上传图片' }}
@@ -415,7 +414,7 @@ const saveEdit = async () => {
商品名称 - {{ product.name }} + {{ product.name }}