mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
feat(product): 商品详情页对接真实API,替换mock数据
后端: - 重写 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 保护避免初始渲染报错
This commit is contained in:
+8
-1
@@ -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");
|
||||
}
|
||||
+308
-33
@@ -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<DailyData> dailyDataList;
|
||||
private List<TrendDataItem> 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<SpecSummaryItem> 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;
|
||||
}
|
||||
}
|
||||
+492
-307
@@ -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<OrderPO> 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<OrderPO> orders = orderMapper.selectList(orderWrapper);
|
||||
|
||||
// 查询推广费数据(店铺粒度的推广费,需要通过订单关联的shopId过滤)
|
||||
// 先收集该商品对应订单的店铺ID列表
|
||||
// 收集店铺ID列表
|
||||
List<Long> shopIds = orders.stream()
|
||||
.map(OrderPO::getShopId)
|
||||
.filter(shopId -> shopId != null)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 查询推广费(店铺粒度)
|
||||
List<PromotionFeePO> promotionFees = new ArrayList<>();
|
||||
if (!shopIds.isEmpty()) {
|
||||
LambdaQueryWrapper<PromotionFeePO> promotionWrapper = new LambdaQueryWrapper<>();
|
||||
promotionWrapper.eq(PromotionFeePO::getCompanyId, companyId)
|
||||
LambdaQueryWrapper<PromotionFeePO> 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<Long> refundStatusIds = getRefundStatusIds();
|
||||
Set<Long> refundStatusIds = getRefundStatusIds(companyId);
|
||||
|
||||
// 按日期分组订单数据
|
||||
Map<LocalDate, List<OrderPO>> 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<LocalDate, List<PromotionFeePO>> promotionFeesByDate = promotionFees.stream()
|
||||
.collect(Collectors.groupingBy(PromotionFeePO::getFeeDate));
|
||||
LambdaQueryWrapper<OrderPO> lastPeriodWrapper = new LambdaQueryWrapper<>();
|
||||
lastPeriodWrapper.eq(OrderPO::getCompanyId, companyId)
|
||||
.eq(OrderPO::getProductId, queryDTO.getProductId())
|
||||
.ge(OrderPO::getOrderTime, lastStartTime)
|
||||
.le(OrderPO::getOrderTime, lastEndTime);
|
||||
List<OrderPO> lastPeriodOrders = orderMapper.selectList(lastPeriodWrapper);
|
||||
|
||||
// 生成日期范围内的所有日期列表
|
||||
List<LocalDate> 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<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> 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<ProductOverviewVO.DailyData> dailyDataList = dateList.stream()
|
||||
.map(date -> {
|
||||
ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData();
|
||||
dailyData.setDate(date);
|
||||
List<OrderPO> 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<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> 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<ProductOverviewVO.DailyData> dailyDataList = dateList.stream()
|
||||
.map(date -> {
|
||||
ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData();
|
||||
dailyData.setDate(date);
|
||||
List<OrderPO> 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<ProductOverviewVO.DailyData> 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<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList, List<OrderPO> 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<ProductOverviewVO.DailyData> dailyDataList = dateList.stream()
|
||||
.map(date -> {
|
||||
ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData();
|
||||
dailyData.setDate(date);
|
||||
List<OrderPO> 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<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList,
|
||||
List<OrderPO> allOrders, List<Long> 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<ProductOverviewVO.DailyData> dailyDataList = dateList.stream()
|
||||
.map(date -> {
|
||||
ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData();
|
||||
dailyData.setDate(date);
|
||||
List<OrderPO> 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<LocalDate, List<PromotionFeePO>> promotionFeesByDate, List<LocalDate> dateList,
|
||||
List<PromotionFeePO> 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<ProductOverviewVO.DailyData> dailyDataList = dateList.stream()
|
||||
.map(date -> {
|
||||
ProductOverviewVO.DailyData dailyData = new ProductOverviewVO.DailyData();
|
||||
dailyData.setDate(date);
|
||||
List<PromotionFeePO> 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<ProductOverviewVO.DailyData> 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<ShopMonthlyTargetPO> targetWrapper = new LambdaQueryWrapper<>();
|
||||
targetWrapper.eq(ShopMonthlyTargetPO::getCompanyId, companyId)
|
||||
.in(ShopMonthlyTargetPO::getShopId, shopIds)
|
||||
.ge(ShopMonthlyTargetPO::getTargetMonth, firstDayOfMonth)
|
||||
.le(ShopMonthlyTargetPO::getTargetMonth, lastDayOfMonth);
|
||||
List<ShopMonthlyTargetPO> 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<Long> 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<OrderPO> orders,
|
||||
java.util.function.Function<List<OrderPO>, BigDecimal> reducer,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
.collect(Collectors.groupingBy(o -> o.getOrderTime().toLocalDate()));
|
||||
|
||||
List<ProductOverviewVO.TrendDataItem> dailyList = new ArrayList<>();
|
||||
LocalDate current = startDate;
|
||||
while (!current.isAfter(endDate)) {
|
||||
List<OrderPO> 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<OrderPO> orders,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
.collect(Collectors.groupingBy(o -> o.getOrderTime().toLocalDate()));
|
||||
|
||||
List<ProductOverviewVO.TrendDataItem> dailyList = new ArrayList<>();
|
||||
LocalDate current = startDate;
|
||||
while (!current.isAfter(endDate)) {
|
||||
List<OrderPO> 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<OrderPO> orders,
|
||||
List<PromotionFeePO> promotionFees,
|
||||
Set<Long> refundStatusIds,
|
||||
BigDecimal taxRate,
|
||||
List<Long> shopIds) {
|
||||
// 查询商品所有规格
|
||||
if (orders.isEmpty()) {
|
||||
return buildEmptySpecSummary();
|
||||
}
|
||||
|
||||
Long productId = orders.get(0).getProductId();
|
||||
Long companyId = orders.get(0).getCompanyId();
|
||||
|
||||
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
|
||||
specWrapper.eq(ProductSpecPO::getCompanyId, companyId)
|
||||
.eq(ProductSpecPO::getProductId, productId);
|
||||
List<ProductSpecPO> allSpecs = productSpecMapper.selectList(specWrapper);
|
||||
Map<Long, ProductSpecPO> specMap = allSpecs.stream()
|
||||
.collect(Collectors.toMap(ProductSpecPO::getId, s -> s, (a, b) -> a));
|
||||
|
||||
// 推广费按店铺维度收集,后面按订单数比例拆分
|
||||
Map<Long, BigDecimal> shopPromotionFeeMap = promotionFees.stream()
|
||||
.collect(Collectors.groupingBy(PromotionFeePO::getShopId,
|
||||
Collectors.reducing(BigDecimal.ZERO,
|
||||
f -> f.getFeeAmount() != null ? f.getFeeAmount() : BigDecimal.ZERO,
|
||||
BigDecimal::add)));
|
||||
|
||||
// 按规格分组订单
|
||||
Map<Long, List<OrderPO>> ordersBySpec = orders.stream()
|
||||
.collect(Collectors.groupingBy(o -> {
|
||||
Long specId = o.getProductSpecId();
|
||||
return specId != null ? specId : 0L;
|
||||
}));
|
||||
|
||||
List<ProductOverviewVO.SpecSummaryItem> specItems = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<Long, List<OrderPO>> entry : ordersBySpec.entrySet()) {
|
||||
Long specId = entry.getKey();
|
||||
if (specId == 0L) continue; // 跳过未关联规格的订单
|
||||
|
||||
List<OrderPO> 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<Long> getRefundStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusMapper.selectList(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
return orderStatusMapper.selectList(wrapper).stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user