fix: 导出服务成本计算改用订单状态统计维度,与全店概览保持一致

- CategoryExportExcelServiceImpl: 注入 OrderStatusStatResolver,成本/退款等改用 stat.* 方法计算
- ExportServiceImpl: 同上,buildShopData/createTrendSheet/按平台各统计方法均改用 stat.* 方法
- 税额计算改为按销售额基数(与全店概览一致)
- 税前盈亏需再减推广费和刷单费
- CategoryExportExcelServiceImpl 增加 shopId 过滤条件
This commit is contained in:
小蟑螂
2026-05-06 21:26:07 +08:00
parent 955b29c750
commit 7ef1159af3
2 changed files with 52 additions and 80 deletions
@@ -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<Long> autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId);
List<Long> refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId);
OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId);
List<Long> unionStatusIds = stat.getUnionStatusIds();
LambdaQueryWrapper<OrderPO> 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<OrderPO> 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<Long, BigDecimal> 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
@@ -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<Long> autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId);
List<Long> refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId);
OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId);
List<Long> unionStatusIds = stat.getUnionStatusIds();
List<Map<String, Object>> 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<String, Object> data = buildShopData(shop, dateRange[0], dateRange[1], autoStatusIds, refundStatusIds, companyId, taxRate);
Map<String, Object> 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<LocalDate, List<OrderPO>> 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<OrderPO> 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<String, Object> buildShopData(ShopPO shop, LocalDate startDate, LocalDate endDate,
List<Long> autoStatusIds, List<Long> refundStatusIds, Long companyId,
List<Long> 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<Long> autoStatusIds = exportStatusRangeHelper.getAutoStatusIds(companyId);
List<Long> refundStatusIds = exportStatusRangeHelper.getRefundStatusIds(companyId);
OrderStatusStatSets stat = exportStatusRangeHelper.resolveStat(companyId);
List<Long> unionStatusIds = stat.getUnionStatusIds();
// 查询所有店铺并按平台分组
List<ShopPO> 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<ShopMonthlyTargetPO> 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