From 65739ecfac5a0b2f363f86efeacf607e6ac771e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Wed, 6 May 2026 00:27:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ecom):=20CompanyOverviewServiceImpl=20?= =?UTF-8?q?=E5=85=A8=E9=9D=A2=E6=8E=A5=E5=85=A5=20OrderStatusStatResolver?= =?UTF-8?q?=20=E5=A4=9A=E7=BB=B4=E5=BA=A6=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用 OrderStatusStatSets 替换原有的 getAutoStatisticsStatusIds / getRefundStatusIds 逻辑 - 所有销售额/成本/退款计算改为 stat.receivedForSales / stat.totalCostForStats / stat.receivedForRefund 等方法 - 订单计数改用 stat.includesSales(statusId) 过滤,与单店概览口径一致 - 税额计算统一改用 this.taxRate 变量,移除硬编码的 0.04 - 删除 calculateShopTotalCost / calculateShopROI / calculateShopNetProfit 重载等冗余方法 --- .../impl/CompanyOverviewServiceImpl.java | 309 ++++++------------ 1 file changed, 98 insertions(+), 211 deletions(-) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java index c2acbdbf..29c69dd8 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CompanyOverviewServiceImpl.java @@ -12,6 +12,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CompanyOverview import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.model.po.BrushOrderPO; import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO; import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver; +import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets; import top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.po.PromotionFeePO; import top.xyquan.ecomdmbbackend.modules.ecom.shop.model.po.ShopPO; import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper; @@ -108,7 +109,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { LocalDateTime startDateTime = startDate.atStartOfDay(); LocalDateTime endDateTime = endDate.atTime(23, 59, 59); - List autoStatisticsStatusIds = getAutoStatisticsStatusIds(companyId); + OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId); + List unionStatusIds = stat.getUnionStatusIds(); // 查询所有店铺 LambdaQueryWrapper shopWrapper = new LambdaQueryWrapper<>(); @@ -123,8 +125,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { orderWrapper.eq(OrderPO::getCompanyId, companyId) .ge(OrderPO::getOrderTime, startDateTime) .le(OrderPO::getOrderTime, endDateTime); - if (!autoStatisticsStatusIds.isEmpty()) { - orderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds); + if (!unionStatusIds.isEmpty()) { + orderWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { orderWrapper.isNull(OrderPO::getId); } @@ -144,9 +146,6 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { .le(BrushOrderPO::getBrushTime, endDateTime); List brushOrders = brushOrderMapper.selectList(brushWrapper); - // 获取退款状态ID - List refundStatusIds = getRefundStatusIds(); - // 计算上月同期数据(用于增长百分比) LocalDate lastMonthStartDate = startDate.minusMonths(1); LocalDate lastMonthEndDate = endDate.minusMonths(1); @@ -157,8 +156,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { lastMonthOrderWrapper.eq(OrderPO::getCompanyId, companyId) .ge(OrderPO::getOrderTime, lastMonthStartDateTime) .le(OrderPO::getOrderTime, lastMonthEndDateTime); - if (!autoStatisticsStatusIds.isEmpty()) { - lastMonthOrderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds); + if (!unionStatusIds.isEmpty()) { + lastMonthOrderWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { lastMonthOrderWrapper.isNull(OrderPO::getId); } @@ -167,15 +166,15 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { // 构建返回对象 CompanyOverviewVO overviewVO = new CompanyOverviewVO(); - // 1. 计算全店总销售额 + // 1. 计算全店总销售额(按状态「计入销售额」维度,与单店概览一致) BigDecimal totalSalesAmount = orders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); overviewVO.setTotalSalesAmount(totalSalesAmount); // 2. 计算较上月同期增长百分比 BigDecimal lastMonthSalesAmount = lastMonthOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal monthOverMonthGrowth = BigDecimal.ZERO; if (lastMonthSalesAmount.compareTo(BigDecimal.ZERO) > 0) { @@ -187,8 +186,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } overviewVO.setMonthOverMonthGrowth(monthOverMonthGrowth); - // 3. 计算全店总单量 - int totalOrderCount = orders.size(); + // 3. 计算全店总单量(按状态「计入销售额」维度计单) + int totalOrderCount = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); overviewVO.setTotalOrderCount(totalOrderCount); // 4. 计算总推广费支出 @@ -204,45 +203,45 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } overviewVO.setAverageFeeRatio(averageFeeRatio); - // 6. 计算全店总净利(税后)和总税额 - BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders); - BigDecimal totalTaxAmount = totalProfitBeforeTax.multiply(taxRate); + // 6. 计算全店总净利(税后)和总税额(利润口径与单店一致;税额按销售额比例) + BigDecimal totalProfitBeforeTax = calculateTotalProfit(orders, stat); + BigDecimal totalTaxAmount = totalSalesAmount.multiply(taxRate); BigDecimal totalNetProfitAfterTax = totalProfitBeforeTax.subtract(totalTaxAmount); overviewVO.setTotalNetProfitAfterTax(totalNetProfitAfterTax); overviewVO.setTotalTaxAmount(totalTaxAmount); // 7. 计算各店铺销售占比排行 - List shopSalesRatios = calculateShopSalesRatios(shops, orders, totalSalesAmount); + List shopSalesRatios = calculateShopSalesRatios(shops, orders, totalSalesAmount, stat); overviewVO.setShopSalesRatios(shopSalesRatios); // 8. 计算销售额与单量趋势 - CompanyOverviewVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate); - CompanyOverviewVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate); + CompanyOverviewVO.TrendData salesAmountTrend = calculateSalesAmountTrend(orders, startDate, endDate, stat); + CompanyOverviewVO.TrendData orderCountTrend = calculateOrderCountTrend(orders, startDate, endDate, stat); overviewVO.setSalesAmountTrend(salesAmountTrend); overviewVO.setOrderCountTrend(orderCountTrend); // 9. 计算店铺效率象限(ROI vs 净利) List shopEfficiencyQuadrants = calculateShopEfficiencyQuadrants( - shops, orders, promotionFees, brushOrders, refundStatusIds); + shops, orders, promotionFees, brushOrders, stat); overviewVO.setShopEfficiencyQuadrants(shopEfficiencyQuadrants); // 9. 多店铺数据横向汇总 CompanyOverviewVO.ShopOverview shopOverview = calculateShopComparisons( - shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate); + shops, orders, promotionFees, brushOrders, stat, companyId, startDate, endDate); overviewVO.setShopComparisons(shopOverview); // 10. 多品类数据横向汇总 CompanyOverviewVO.CategoryOverview categoryOverview = calculateCategoryComparisons( - orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate); + orders, promotionFees, brushOrders, stat, companyId, startDate, endDate); overviewVO.setCategoryComparisons(categoryOverview); // 11. 平台维度汇总 CompanyOverviewVO.PlatformOverview platformOverview = calculatePlatformSummary( - shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate); + shops, orders, promotionFees, brushOrders, stat, companyId, startDate, endDate); overviewVO.setPlatformSummary(platformOverview); // 12. 汇总ROI和总税后盈亏 - BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusIds); + BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, stat); overviewVO.setTotalROI(totalROI); overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax); @@ -322,19 +321,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } /** - * 计算总利润(税前) + * 计算总利润(税前):与单店概览一致,按订单状态各维度计入销售额与各项成本 */ - private BigDecimal calculateTotalProfit(List orders) { + private BigDecimal calculateTotalProfit(List orders, OrderStatusStatSets stat) { return orders.stream() - .map(order -> { - BigDecimal receivedAmount = order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO; - 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; - BigDecimal totalCost = productCost.add(shippingCost).add(otherCost).multiply(new BigDecimal(quantity)); - return receivedAmount.subtract(totalCost); - }) + .map(order -> stat.receivedForSales(order).subtract(stat.totalCostForStats(order))) .reduce(BigDecimal.ZERO, BigDecimal::add); } @@ -342,13 +333,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { * 计算各店铺销售占比排行 */ private List calculateShopSalesRatios( - List shops, List orders, BigDecimal totalSalesAmount) { + List shops, List orders, BigDecimal totalSalesAmount, OrderStatusStatSets stat) { Map shopSalesMap = orders.stream() .collect(Collectors.groupingBy( OrderPO::getShopId, Collectors.reducing( BigDecimal.ZERO, - order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO, + stat::receivedForSales, BigDecimal::add))); List ratios = new ArrayList<>(); @@ -378,7 +369,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { */ private List calculateShopEfficiencyQuadrants( List shops, List orders, List promotionFees, - List brushOrders, List refundStatusIds) { + List brushOrders, OrderStatusStatSets stat) { List quadrants = new ArrayList<>(); // 按店铺分组数据 @@ -399,18 +390,17 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); BigDecimal shopSales = shopOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal shopRefund = shopOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal shopPromotionFee = shopPromotionFees.stream() .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); - BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds); - BigDecimal netProfit = calculateShopNetProfit(profit); + BigDecimal profit = calculateShopProfit(shopOrders, stat); + BigDecimal netProfit = calculateShopNetProfit(profit, shopSales); allROIs.add(roi); allNetProfits.add(netProfit); @@ -430,18 +420,17 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); BigDecimal shopSales = shopOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal shopRefund = shopOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal shopPromotionFee = shopPromotionFees.stream() .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal roi = calculateSalesROI(shopSales.subtract(shopRefund), shopPromotionFee); - BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds); - BigDecimal netProfit = calculateShopNetProfit(profit); + BigDecimal profit = calculateShopProfit(shopOrders, stat); + BigDecimal netProfit = calculateShopNetProfit(profit, shopSales); int quadrant; if (roi.compareTo(avgROI) >= 0 && netProfit.compareTo(avgNetProfit) >= 0) { @@ -471,7 +460,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { */ private CompanyOverviewVO.ShopOverview calculateShopComparisons( List shops, List orders, List promotionFees, - List brushOrders, List refundStatusIds, Long companyId, LocalDate startDate, LocalDate endDate) { + List brushOrders, OrderStatusStatSets stat, Long companyId, LocalDate startDate, LocalDate endDate) { List comparisons = new ArrayList<>(); // 按店铺分组数据 @@ -506,16 +495,15 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); List shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>()); - // 计算各项指标 + // 计算各项指标(与单店概览:按订单状态统计维度) BigDecimal salesAmount = shopOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); - int orderCount = shopOrders.size(); + int orderCount = (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); BigDecimal refundAmount = shopOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal brushFee = shopBrushOrders.stream() @@ -528,33 +516,21 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { // 计算商品成本 BigDecimal productCost = shopOrders.stream() - .map(order -> { - BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::productCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); // 计算运费成本 BigDecimal shippingCost = shopOrders.stream() - .map(order -> { - BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::freightCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); // 计算其他成本 BigDecimal otherCost = shopOrders.stream() - .map(order -> { - BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); - BigDecimal totalCost = calculateShopTotalCost(shopOrders); - BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount); + BigDecimal orderLineCost = productCost.add(shippingCost).add(otherCost); + BigDecimal profitBeforeTax = salesAmount.subtract(orderLineCost).subtract(refundAmount); // 销售额4%税额 BigDecimal salesTaxAmount = salesAmount.multiply(taxRate); @@ -673,7 +649,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { : BigDecimal.ZERO; BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund); - BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04")); + BigDecimal salesTax = totalSales.multiply(taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -715,60 +691,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } /** - * 计算店铺利润(扣除退款) + * 店铺维度税前利润(与品类/单店口径一致:计入销售额的实收 − 各成本维度 − 计入退款的实收) */ - private BigDecimal calculateShopProfit(List orders, List refundStatusIds) { - BigDecimal totalSales = orders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - BigDecimal refundAmount = orders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - BigDecimal totalCost = calculateShopTotalCost(orders); - return totalSales.subtract(totalCost).subtract(refundAmount); - } - - /** - * 计算店铺总成本(订单成本) - */ - private BigDecimal calculateShopTotalCost(List orders) { - return orders.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); - } - - /** - * 计算店铺总成本(推广费+刷单费) - */ - private BigDecimal calculateShopTotalCost(List promotionFees, List brushOrders) { - BigDecimal promotionFee = promotionFees.stream() - .map(fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - BigDecimal brushFee = brushOrders.stream() - .map(brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO) - .reduce(BigDecimal.ZERO, BigDecimal::add); - - return promotionFee.add(brushFee); - } - - /** - * 计算店铺ROI - */ - private BigDecimal calculateShopROI(BigDecimal profit, BigDecimal totalCost) { - if (totalCost.compareTo(BigDecimal.ZERO) <= 0) { - return BigDecimal.ZERO; - } - return profit.divide(totalCost, 4, RoundingMode.HALF_UP); + private BigDecimal calculateShopProfit(List orders, OrderStatusStatSets stat) { + BigDecimal salesAmount = orders.stream().map(stat::receivedForSales).reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal orderLineCost = orders.stream().map(stat::totalCostForStats).reduce(BigDecimal.ZERO, BigDecimal::add); + BigDecimal refundAmount = orders.stream().map(stat::receivedForRefund).reduce(BigDecimal.ZERO, BigDecimal::add); + return salesAmount.subtract(orderLineCost).subtract(refundAmount); } /** @@ -782,24 +711,23 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { } /** - * 计算店铺净利(税后) + * 店铺净利(税后):税额按「计入销售额」的销售额比例,与单店概览一致 */ - private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax) { - BigDecimal taxAmount = profitBeforeTax.multiply(taxRate); - return profitBeforeTax.subtract(taxAmount); + private BigDecimal calculateShopNetProfit(BigDecimal profitBeforeTax, BigDecimal salesAmountForTax) { + BigDecimal salesAmount = salesAmountForTax != null ? salesAmountForTax : BigDecimal.ZERO; + return profitBeforeTax.subtract(salesAmount.multiply(taxRate)); } /** * 计算汇总ROI(新公式):(销售额 - 退款金额)/ 推广费用 */ private BigDecimal calculateTotalROI(List orders, List promotionFees, - List brushOrders, List refundStatusIds) { + List brushOrders, OrderStatusStatSets stat) { BigDecimal totalSales = orders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalRefund = orders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal netSales = totalSales.subtract(totalRefund); BigDecimal totalPromotionFee = promotionFees.stream() @@ -808,13 +736,6 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return calculateSalesROI(netSales, totalPromotionFee); } - /** - * 获取退款状态ID(通过 is_refund 字段查询) - */ - private List getRefundStatusIds() { - return orderStatusStatResolver.resolve(currentUserContext.getCurrentCompanyId()).getRefundStatusIds(); - } - /** 订单行上的 product_spec_id → 商品 id */ private Map orderSpecIdToProductIdMap(List orders) { List specIds = orders.stream() @@ -840,7 +761,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { */ private CompanyOverviewVO.CategoryOverview calculateCategoryComparisons( List orders, List promotionFees, - List brushOrders, List refundStatusIds, Long companyId, + List brushOrders, OrderStatusStatSets stat, Long companyId, LocalDate startDate, LocalDate endDate) { // Query all categories to build categoryId -> categoryName map @@ -896,45 +817,31 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List categoryOrders = entry.getValue(); BigDecimal salesAmount = categoryOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); - int orderCount = categoryOrders.size(); + int orderCount = (int) categoryOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); BigDecimal refundAmount = categoryOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal productCost = categoryOrders.stream() - .map(order -> { - BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::productCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal shippingCost = categoryOrders.stream() - .map(order -> { - BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::freightCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal otherCost = categoryOrders.stream() - .map(order -> { - BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO; - return cost; - }) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); Map categorySalesByShop = categoryOrders.stream() .collect(Collectors.groupingBy( OrderPO::getShopId, - Collectors.reducing(BigDecimal.ZERO, - order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO, - BigDecimal::add))); + Collectors.reducing(BigDecimal.ZERO, stat::receivedForSales, BigDecimal::add))); BigDecimal categoryTotalSales = categorySalesByShop.values().stream() .reduce(BigDecimal.ZERO, BigDecimal::add); @@ -1039,7 +946,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { : BigDecimal.ZERO; BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund); - BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04")); + BigDecimal salesTax = totalSales.multiply(taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0 ? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP) @@ -1069,16 +976,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return total; } - private List getAutoStatisticsStatusIds(Long companyId) { - return orderStatusStatResolver.resolve(companyId).getUnionStatusIds(); - } - /** * 计算平台维度汇总 */ private CompanyOverviewVO.PlatformOverview calculatePlatformSummary( List shops, List orders, List promotionFees, - List brushOrders, List refundStatusIds, Long companyId, + List brushOrders, OrderStatusStatSets stat, Long companyId, LocalDate startDate, LocalDate endDate) { List platformSummaries = new ArrayList<>(); @@ -1151,45 +1054,36 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopBrushOrders = brushOrdersByShop.getOrDefault(shop.getId(), new ArrayList<>()); List shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>()); - // 销售额 + // 销售额(按状态「计入销售额」) BigDecimal shopSales = shopOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); totalSalesAmount = totalSalesAmount.add(shopSales); - // 订单数 - totalOrderCount += shopOrders.size(); + // 订单数(按状态「计入销售额」计单) + totalOrderCount += (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); - // 退款金额 + // 退款金额(按状态「计入退款」) BigDecimal shopRefund = shopOrders.stream() - .filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId())) - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); totalRefundAmount = totalRefundAmount.add(shopRefund); // 商品成本 BigDecimal shopProductCost = shopOrders.stream() - .map(order -> { - BigDecimal cost = order.getProductCost() != null ? order.getProductCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::productCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalProductCost = totalProductCost.add(shopProductCost); // 运费成本 BigDecimal shopShippingCost = shopOrders.stream() - .map(order -> { - BigDecimal cost = order.getShippingCost() != null ? order.getShippingCost() : BigDecimal.ZERO; - Integer quantity = order.getQuantity() != null ? order.getQuantity() : 1; - return cost.multiply(new BigDecimal(quantity)); - }) + .map(stat::freightCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalShippingCost = totalShippingCost.add(shopShippingCost); // 其他成本 BigDecimal shopOtherCost = shopOrders.stream() - .map(order -> order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalOtherCost = totalOtherCost.add(shopOtherCost); @@ -1286,7 +1180,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { if (!shopsWithoutPlatform.isEmpty()) { CompanyOverviewVO.PlatformSummary otherSummary = calculateOtherPlatformSummary( shopsWithoutPlatform, ordersByShop, promotionFeesByShop, brushOrdersByShop, - targetsByShop, refundStatusIds, taxRate); + targetsByShop, stat, taxRate); if (otherSummary.getOrderCount() > 0) { platformSummaries.add(otherSummary); } @@ -1338,8 +1232,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { : BigDecimal.ZERO; BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount); - BigDecimal taxRate = new BigDecimal("0.04"); - BigDecimal salesTax = totalSalesAmount.multiply(taxRate); + BigDecimal salesTax = totalSalesAmount.multiply(this.taxRate); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax); BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 ? totalSalesAmount.subtract(totalRefundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP) @@ -1389,7 +1282,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { Map> promotionFeesByShop, Map> brushOrdersByShop, Map> targetsByShop, - List refundStatusIds, + OrderStatusStatSets stat, BigDecimal taxRate) { BigDecimal totalSalesAmount = BigDecimal.ZERO; @@ -1410,37 +1303,28 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { List shopTargets = targetsByShop.getOrDefault(shop.getId(), new ArrayList<>()); BigDecimal shopSales = shopOrders.stream() - .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); totalSalesAmount = totalSalesAmount.add(shopSales); - totalOrderCount += shopOrders.size(); + totalOrderCount += (int) shopOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); BigDecimal shopRefund = shopOrders.stream() - .filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId())) - .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); totalRefundAmount = totalRefundAmount.add(shopRefund); BigDecimal shopProductCost = shopOrders.stream() - .map(o -> { - BigDecimal c = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO; - int qty = o.getQuantity() != null ? o.getQuantity() : 1; - return c.multiply(new BigDecimal(qty)); - }) + .map(stat::productCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalProductCost = totalProductCost.add(shopProductCost); BigDecimal shopShippingCost = shopOrders.stream() - .map(o -> { - BigDecimal c = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO; - int qty = o.getQuantity() != null ? o.getQuantity() : 1; - return c.multiply(new BigDecimal(qty)); - }) + .map(stat::freightCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalShippingCost = totalShippingCost.add(shopShippingCost); BigDecimal shopOtherCost = shopOrders.stream() - .map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); totalOtherCost = totalOtherCost.add(shopOtherCost); @@ -1522,12 +1406,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return platform != null ? platform.getPlatformName() : null; } - private CompanyOverviewVO.TrendData calculateSalesAmountTrend(List orders, LocalDate startDate, LocalDate endDate) { + private CompanyOverviewVO.TrendData calculateSalesAmountTrend(List orders, LocalDate startDate, + LocalDate endDate, OrderStatusStatSets stat) { CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData(); Map> ordersByDate = orders.stream() .collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate())); BigDecimal total = orders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); trendData.setTotal(total); List dateList = new ArrayList<>(); @@ -1542,7 +1427,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { item.setDate(date); List dayOrders = ordersByDate.getOrDefault(date, List.of()); BigDecimal dayTotal = dayOrders.stream() - .map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); item.setValue(dayTotal); return item; @@ -1553,11 +1438,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { return trendData; } - private CompanyOverviewVO.TrendData calculateOrderCountTrend(List orders, LocalDate startDate, LocalDate endDate) { + private CompanyOverviewVO.TrendData calculateOrderCountTrend(List orders, LocalDate startDate, + LocalDate endDate, OrderStatusStatSets stat) { CompanyOverviewVO.TrendData trendData = new CompanyOverviewVO.TrendData(); Map> ordersByDate = orders.stream() .collect(Collectors.groupingBy(order -> order.getOrderTime().toLocalDate())); - int total = orders.size(); + int total = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); trendData.setTotal(new BigDecimal(total)); List dateList = new ArrayList<>(); LocalDate currentDate = startDate; @@ -1570,7 +1456,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService { CompanyOverviewVO.TrendDataItem item = new CompanyOverviewVO.TrendDataItem(); item.setDate(date); List dayOrders = ordersByDate.getOrDefault(date, List.of()); - item.setValue(new BigDecimal(dayOrders.size())); + int dayCount = (int) dayOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); + item.setValue(new BigDecimal(dayCount)); return item; }) .sorted(Comparator.comparing(CompanyOverviewVO.TrendDataItem::getDate))