From 7ef1159af3ce25028e34f9ba1957a7ca45471ba1 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 21:26:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=BC=E5=87=BA=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=88=90=E6=9C=AC=E8=AE=A1=E7=AE=97=E6=94=B9=E7=94=A8=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=8A=B6=E6=80=81=E7=BB=9F=E8=AE=A1=E7=BB=B4=E5=BA=A6?= =?UTF-8?q?=EF=BC=8C=E4=B8=8E=E5=85=A8=E5=BA=97=E6=A6=82=E8=A7=88=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CategoryExportExcelServiceImpl: 注入 OrderStatusStatResolver,成本/退款等改用 stat.* 方法计算 - ExportServiceImpl: 同上,buildShopData/createTrendSheet/按平台各统计方法均改用 stat.* 方法 - 税额计算改为按销售额基数(与全店概览一致) - 税前盈亏需再减推广费和刷单费 - CategoryExportExcelServiceImpl 增加 shopId 过滤条件 --- .../impl/CategoryExportExcelServiceImpl.java | 37 +++----- .../service/impl/ExportServiceImpl.java | 95 ++++++++----------- 2 files changed, 52 insertions(+), 80 deletions(-) diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java index debffe0e..cbe7b9a8 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/CategoryExportExcelServiceImpl.java @@ -12,6 +12,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.category.model.po.CategoryPO; import top.xyquan.ecomdmbbackend.modules.ecom.category.service.CategoryService; import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper; import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO; +import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CategoryExportDTO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.vo.CompanyOverviewVO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.service.CategoryExportExcelService; @@ -76,15 +77,15 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic LocalDateTime endDateTime = endDate.atTime(23, 59, 59); BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE; - List autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId); - List refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId); + OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId); + List unionStatusIds = stat.getUnionStatusIds(); LambdaQueryWrapper orderWrapper = new LambdaQueryWrapper<>(); orderWrapper.eq(OrderPO::getCompanyId, companyId) .ge(OrderPO::getOrderTime, startDateTime) .le(OrderPO::getOrderTime, endDateTime); - if (!autoStatusIds.isEmpty()) { - orderWrapper.in(OrderPO::getStatusId, autoStatusIds); + if (!unionStatusIds.isEmpty()) { + orderWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { orderWrapper.isNull(OrderPO::getId); } @@ -159,36 +160,25 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic List categoryOrders = entry.getValue(); BigDecimal salesAmount = categoryOrders.stream() - .map(o -> o.getReceivedAmount() != null ? o.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(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId())) - .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForRefund) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal productCost = categoryOrders.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); BigDecimal shippingCost = categoryOrders.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); BigDecimal otherCost = categoryOrders.stream() - .map(o -> o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); Map categorySalesByShop = categoryOrders.stream() .collect(Collectors.groupingBy(OrderPO::getShopId, - Collectors.reducing(BigDecimal.ZERO, - o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO, - BigDecimal::add))); + Collectors.reducing(BigDecimal.ZERO, stat::receivedForSales, BigDecimal::add))); BigDecimal categoryTotalSales = categorySalesByShop.values().stream() .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalPromotionFee = BigDecimal.ZERO; @@ -205,7 +195,8 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic } BigDecimal totalCost = productCost.add(shippingCost).add(otherCost); - BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount); + BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount) + .subtract(totalPromotionFee).subtract(totalBrushFee); BigDecimal salesTaxAmount = salesAmount.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java index 3fdba226..ca4fea71 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/overview/service/impl/ExportServiceImpl.java @@ -10,7 +10,6 @@ import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.mapper.BrushOrderMapper import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper; import top.xyquan.ecomdmbbackend.modules.ecom.platform.mapper.PlatformMapper; import top.xyquan.ecomdmbbackend.modules.ecom.platform.model.po.PlatformPO; -import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.vo.CompanyOverviewVO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CategoryExportDTO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CompanyExportDTO; import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.PlatformExportDTO; @@ -19,6 +18,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.overview.service.ExportService; import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMapper; 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.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; @@ -88,8 +88,8 @@ public class ExportServiceImpl implements ExportService { .collect(Collectors.toList()); } - List autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId); - List refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId); + OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId); + List unionStatusIds = stat.getUnionStatusIds(); List> shopDataList = new ArrayList<>(); BigDecimal totalSales = BigDecimal.ZERO; @@ -102,7 +102,7 @@ public class ExportServiceImpl implements ExportService { BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE; for (ShopPO shop : shops) { - Map data = buildShopData(shop, dateRange[0], dateRange[1], autoStatusIds, refundStatusIds, companyId, taxRate); + Map data = buildShopData(shop, dateRange[0], dateRange[1], unionStatusIds, stat, companyId, taxRate); shopDataList.add(data); totalSales = totalSales.add((BigDecimal) data.get("salesAmount")); @@ -113,8 +113,8 @@ public class ExportServiceImpl implements ExportService { totalProfitBeforeTax = totalProfitBeforeTax.add((BigDecimal) data.get("totalProfitBeforeTax")); } - // 计算汇总数据 - BigDecimal totalTax = totalProfitBeforeTax.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); + // 计算汇总数据(税额按销售额比例,与全店概览一致) + BigDecimal totalTax = totalSales.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); BigDecimal totalProfitAfterTax = totalProfitBeforeTax.subtract(totalTax); BigDecimal totalCost = totalPromotionFee.add(totalBrushFee); BigDecimal totalRoi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0 @@ -129,8 +129,8 @@ public class ExportServiceImpl implements ExportService { allOrdersWrapper.eq(OrderPO::getCompanyId, companyId) .ge(OrderPO::getOrderTime, startDateTime) .le(OrderPO::getOrderTime, endDateTime); - if (!autoStatusIds.isEmpty()) { - allOrdersWrapper.in(OrderPO::getStatusId, autoStatusIds); + if (!unionStatusIds.isEmpty()) { + allOrdersWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { allOrdersWrapper.isNull(OrderPO::getId); } @@ -153,7 +153,7 @@ public class ExportServiceImpl implements ExportService { if (("trend".equals(exportType) || "all".equals(exportType)) && !ordersByDate.isEmpty()) { Sheet trendSheet = workbook.createSheet("销售趋势"); - createTrendSheet(trendSheet, dateRangeLabel, ordersByDate, dateRange[0], dateRange[1]); + createTrendSheet(trendSheet, dateRangeLabel, ordersByDate, dateRange[0], dateRange[1], stat); } try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { @@ -302,7 +302,8 @@ public class ExportServiceImpl implements ExportService { private void createTrendSheet(Sheet sheet, String dateRangeLabel, Map> ordersByDate, - LocalDate startDate, LocalDate endDate) { + LocalDate startDate, LocalDate endDate, + OrderStatusStatSets stat) { CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook()); CellStyle dataStyle = createDataStyle(sheet.getWorkbook()); @@ -327,10 +328,10 @@ public class ExportServiceImpl implements ExportService { List dayOrders = ordersByDate.getOrDefault(current, Collections.emptyList()); BigDecimal salesAmount = dayOrders.stream() - .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); - int orderCount = dayOrders.size(); + int orderCount = (int) dayOrders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); BigDecimal avgOrderValue = orderCount > 0 ? salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; @@ -352,7 +353,7 @@ public class ExportServiceImpl implements ExportService { // ==================== 数据构建方法 ==================== private Map buildShopData(ShopPO shop, LocalDate startDate, LocalDate endDate, - List autoStatusIds, List refundStatusIds, Long companyId, + List unionStatusIds, OrderStatusStatSets stat, Long companyId, BigDecimal taxRate) { LocalDateTime startDateTime = startDate.atStartOfDay(); LocalDateTime endDateTime = endDate.atTime(23, 59, 59); @@ -362,8 +363,8 @@ public class ExportServiceImpl implements ExportService { .eq(OrderPO::getShopId, shop.getId()) .ge(OrderPO::getOrderTime, startDateTime) .le(OrderPO::getOrderTime, endDateTime); - if (!autoStatusIds.isEmpty()) { - orderWrapper.in(OrderPO::getStatusId, autoStatusIds); + if (!unionStatusIds.isEmpty()) { + orderWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { orderWrapper.isNull(OrderPO::getId); } @@ -404,42 +405,29 @@ public class ExportServiceImpl implements ExportService { .mapToInt(t -> t.getTargetOrderCount() != null ? t.getTargetOrderCount() : 0) .sum(); - // 计算各项指标 + // 计算各项指标(与全店概览:按订单状态各统计维度) BigDecimal salesAmount = orders.stream() - .map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO) + .map(stat::receivedForSales) .reduce(BigDecimal.ZERO, BigDecimal::add); - int orderCount = orders.size(); + int orderCount = (int) orders.stream().filter(o -> stat.includesSales(o.getStatusId())).count(); BigDecimal avgOrderValue = orderCount > 0 ? salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal refundAmount = orders.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); BigDecimal shippingCost = orders.stream() - .map(o -> { - BigDecimal c = o.getShippingCost() != null ? o.getShippingCost() : BigDecimal.ZERO; - Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; - return c.multiply(new BigDecimal(qty)); - }) + .map(stat::freightCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal productCostTotal = orders.stream() - .map(o -> { - BigDecimal c = o.getProductCost() != null ? o.getProductCost() : BigDecimal.ZERO; - Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; - return c.multiply(new BigDecimal(qty)); - }) + .map(stat::productCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal otherCostTotal = orders.stream() - .map(o -> { - BigDecimal c = o.getOtherCost() != null ? o.getOtherCost() : BigDecimal.ZERO; - Integer qty = o.getQuantity() != null ? o.getQuantity() : 1; - return c.multiply(new BigDecimal(qty)); - }) + .map(stat::otherCostForStats) .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal promotionFee = promotionFees.stream() @@ -451,8 +439,9 @@ public class ExportServiceImpl implements ExportService { .reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalCost = productCostTotal.add(shippingCost).add(otherCostTotal); - BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount); - BigDecimal tax = profitBeforeTax.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); + BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount) + .subtract(promotionFee).subtract(brushFee); + BigDecimal tax = salesAmount.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); BigDecimal profitAfterTax = profitBeforeTax.subtract(tax); BigDecimal feeRatio = salesAmount.compareTo(BigDecimal.ZERO) > 0 ? promotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP) : BigDecimal.ZERO; @@ -578,8 +567,8 @@ public class ExportServiceImpl implements ExportService { LocalDateTime endDateTime = endDate.atTime(23, 59, 59); BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE; - List autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId); - List refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId); + OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId); + List unionStatusIds = stat.getUnionStatusIds(); // 查询所有店铺并按平台分组 List allShops = shopService.list( @@ -626,8 +615,8 @@ public class ExportServiceImpl implements ExportService { orderWrapper.eq(OrderPO::getCompanyId, companyId) .ge(OrderPO::getOrderTime, startDateTime) .le(OrderPO::getOrderTime, endDateTime); - if (!autoStatusIds.isEmpty()) { - orderWrapper.in(OrderPO::getStatusId, autoStatusIds); + if (!unionStatusIds.isEmpty()) { + orderWrapper.in(OrderPO::getStatusId, unionStatusIds); } else { orderWrapper.isNull(OrderPO::getId); } @@ -679,37 +668,28 @@ public class ExportServiceImpl implements ExportService { 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); @@ -738,7 +718,8 @@ public class ExportServiceImpl implements ExportService { ? totalSalesAmount.divide(new BigDecimal(totalOrderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost); - BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount); + BigDecimal profitBeforeTax = totalSalesAmount.subtract(totalCost).subtract(totalRefundAmount) + .subtract(totalPromotionFee).subtract(totalBrushFee); BigDecimal salesTaxAmount = totalSalesAmount.multiply(taxRate).setScale(2, RoundingMode.HALF_UP); BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount); BigDecimal roi = totalPromotionFee.compareTo(BigDecimal.ZERO) > 0