mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
fix(statistics): 修复退款金额统计只取单个退款状态的问题,改为取公司下所有is_refund=1状态
影响范围: - ShopOverviewStatisticsServiceImpl(店铺概览) - CompanyOverviewServiceImpl(公司概览) - ProductOverviewServiceImpl(商品概览) - ExportServiceImpl(导出服务) - ShopServiceImpl(店铺服务) 根因:getRefundStatusId() 用 LIMIT 1 只取第一条退款状态,漏统计其他退款状态(is_refund=1)的订单
This commit is contained in:
+28
-28
@@ -139,7 +139,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
|
||||
|
||||
// 获取退款状态ID
|
||||
Long refundStatusId = getRefundStatusId();
|
||||
List<Long> refundStatusIds = getRefundStatusIds();
|
||||
|
||||
// 计算上月同期数据(用于增长百分比)
|
||||
LocalDate lastMonthStartDate = startDate.minusMonths(1);
|
||||
@@ -211,26 +211,26 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
|
||||
// 8. 计算店铺效率象限(ROI vs 净利)
|
||||
List<CompanyOverviewVO.ShopEfficiencyQuadrant> shopEfficiencyQuadrants = calculateShopEfficiencyQuadrants(
|
||||
shops, orders, promotionFees, brushOrders, refundStatusId);
|
||||
shops, orders, promotionFees, brushOrders, refundStatusIds);
|
||||
overviewVO.setShopEfficiencyQuadrants(shopEfficiencyQuadrants);
|
||||
|
||||
// 9. 多店铺数据横向对比
|
||||
CompanyOverviewVO.ShopOverview shopOverview = calculateShopComparisons(
|
||||
shops, orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
|
||||
shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
||||
overviewVO.setShopComparisons(shopOverview);
|
||||
|
||||
// 10. 多品类数据横向对比
|
||||
CompanyOverviewVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
|
||||
orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
|
||||
orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
||||
overviewVO.setCategoryComparisons(categoryOverview);
|
||||
|
||||
// 11. 平台维度汇总
|
||||
CompanyOverviewVO.PlatformOverview platformOverview = calculatePlatformSummary(
|
||||
shops, orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
|
||||
shops, orders, promotionFees, brushOrders, refundStatusIds, companyId, startDate, endDate);
|
||||
overviewVO.setPlatformSummary(platformOverview);
|
||||
|
||||
// 12. 汇总ROI和总税后盈亏
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId);
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusIds);
|
||||
overviewVO.setTotalROI(totalROI);
|
||||
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
|
||||
|
||||
@@ -366,7 +366,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
*/
|
||||
private List<CompanyOverviewVO.ShopEfficiencyQuadrant> calculateShopEfficiencyQuadrants(
|
||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
List<CompanyOverviewVO.ShopEfficiencyQuadrant> quadrants = new ArrayList<>();
|
||||
|
||||
// 按店铺分组数据
|
||||
@@ -390,14 +390,14 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal shopRefund = shopOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.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, refundStatusId);
|
||||
BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds);
|
||||
BigDecimal netProfit = calculateShopNetProfit(profit);
|
||||
|
||||
allROIs.add(roi);
|
||||
@@ -421,14 +421,14 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal shopRefund = shopOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.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, refundStatusId);
|
||||
BigDecimal profit = calculateShopProfit(shopOrders, refundStatusIds);
|
||||
BigDecimal netProfit = calculateShopNetProfit(profit);
|
||||
|
||||
int quadrant;
|
||||
@@ -459,7 +459,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
*/
|
||||
private CompanyOverviewVO.ShopOverview calculateShopComparisons(
|
||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId, Long companyId, LocalDate startDate, LocalDate endDate) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId, LocalDate startDate, LocalDate endDate) {
|
||||
List<CompanyOverviewVO.ShopComparison> comparisons = new ArrayList<>();
|
||||
|
||||
// 按店铺分组数据
|
||||
@@ -502,7 +502,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
int orderCount = shopOrders.size();
|
||||
|
||||
BigDecimal refundAmount = shopOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -703,13 +703,13 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
/**
|
||||
* 计算店铺利润(扣除退款)
|
||||
*/
|
||||
private BigDecimal calculateShopProfit(List<OrderPO> orders, Long refundStatusId) {
|
||||
private BigDecimal calculateShopProfit(List<OrderPO> orders, List<Long> 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 -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -779,12 +779,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
* 计算汇总ROI(新公式):(销售额 - 退款金额)/ 推广费用
|
||||
*/
|
||||
private BigDecimal calculateTotalROI(List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
BigDecimal totalSales = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalRefund = orders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal netSales = totalSales.subtract(totalRefund);
|
||||
@@ -797,14 +797,14 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
/**
|
||||
* 获取退款状态ID(通过 is_refund 字段查询)
|
||||
*/
|
||||
private Long getRefundStatusId() {
|
||||
private List<Long> getRefundStatusIds() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.last("LIMIT 1");
|
||||
OrderStatusPO status = orderStatusService.getOne(wrapper);
|
||||
return status != null ? status.getId() : null;
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -813,7 +813,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
*/
|
||||
private CompanyOverviewVO.CategoryOverview calculateCategoryComparisons(
|
||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId, Long companyId,
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
// Query all categories to build categoryId -> categoryName map
|
||||
@@ -875,7 +875,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
int orderCount = categoryOrders.size();
|
||||
|
||||
BigDecimal refundAmount = categoryOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -1056,7 +1056,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
*/
|
||||
private CompanyOverviewVO.PlatformOverview calculatePlatformSummary(
|
||||
List<ShopPO> shops, List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId, Long companyId,
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds, Long companyId,
|
||||
LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
List<CompanyOverviewVO.PlatformSummary> platformSummaries = new ArrayList<>();
|
||||
@@ -1140,7 +1140,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
|
||||
// 退款金额
|
||||
BigDecimal shopRefund = shopOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
||||
@@ -1263,7 +1263,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
if (!shopsWithoutPlatform.isEmpty()) {
|
||||
CompanyOverviewVO.PlatformSummary otherSummary = calculateOtherPlatformSummary(
|
||||
shopsWithoutPlatform, ordersByShop, promotionFeesByShop, brushOrdersByShop,
|
||||
targetsByShop, refundStatusId, taxRate);
|
||||
targetsByShop, refundStatusIds, taxRate);
|
||||
if (otherSummary.getOrderCount() > 0) {
|
||||
platformSummaries.add(otherSummary);
|
||||
}
|
||||
@@ -1365,7 +1365,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
Map<Long, List<PromotionFeePO>> promotionFeesByShop,
|
||||
Map<Long, List<BrushOrderPO>> brushOrdersByShop,
|
||||
Map<Long, List<ShopMonthlyTargetPO>> targetsByShop,
|
||||
Long refundStatusId,
|
||||
List<Long> refundStatusIds,
|
||||
BigDecimal taxRate) {
|
||||
|
||||
BigDecimal totalSalesAmount = BigDecimal.ZERO;
|
||||
@@ -1392,7 +1392,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
|
||||
totalOrderCount += shopOrders.size();
|
||||
|
||||
BigDecimal shopRefund = shopOrders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
||||
|
||||
+17
-17
@@ -104,7 +104,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
}
|
||||
|
||||
List<Long> autoStatusIds = getAutoStatusIds(companyId);
|
||||
Long refundStatusId = getRefundStatusId(companyId);
|
||||
List<Long> refundStatusIds = getRefundStatusIds(companyId);
|
||||
|
||||
List<Map<String, Object>> shopDataList = new ArrayList<>();
|
||||
BigDecimal totalSales = BigDecimal.ZERO;
|
||||
@@ -117,7 +117,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, refundStatusId, companyId, taxRate);
|
||||
Map<String, Object> data = buildShopData(shop, dateRange[0], dateRange[1], autoStatusIds, refundStatusIds, companyId, taxRate);
|
||||
shopDataList.add(data);
|
||||
|
||||
totalSales = totalSales.add((BigDecimal) data.get("salesAmount"));
|
||||
@@ -236,7 +236,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE;
|
||||
|
||||
List<Long> autoStatusIds = getAutoStatusIds(companyId);
|
||||
Long refundStatusId = getRefundStatusId(companyId);
|
||||
List<Long> refundStatusIds = getRefundStatusIds(companyId);
|
||||
|
||||
// 查询订单
|
||||
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
|
||||
@@ -317,7 +317,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
int orderCount = categoryOrders.size();
|
||||
BigDecimal refundAmount = categoryOrders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal productCost = categoryOrders.stream()
|
||||
@@ -674,7 +674,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
BrushOrderMapper brushOrderMapper,
|
||||
Long companyId, Long shopId,
|
||||
LocalDate startDate, LocalDate endDate,
|
||||
Long refundStatusId) {
|
||||
List<Long> refundStatusIds) {
|
||||
CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
|
||||
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
|
||||
|
||||
@@ -707,7 +707,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
BigDecimal refundAmount = productOrders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -1041,7 +1041,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
// ==================== 数据构建方法 ====================
|
||||
|
||||
private Map<String, Object> buildShopData(ShopPO shop, LocalDate startDate, LocalDate endDate,
|
||||
List<Long> autoStatusIds, Long refundStatusId, Long companyId,
|
||||
List<Long> autoStatusIds, List<Long> refundStatusIds, Long companyId,
|
||||
BigDecimal taxRate) {
|
||||
LocalDateTime startDateTime = startDate.atStartOfDay();
|
||||
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
|
||||
@@ -1103,7 +1103,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
? salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
|
||||
|
||||
BigDecimal refundAmount = orders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -1200,13 +1200,13 @@ public class ExportServiceImpl implements ExportService {
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Long getRefundStatusId(Long companyId) {
|
||||
OrderStatusPO status = orderStatusService.getOne(
|
||||
new LambdaQueryWrapper<OrderStatusPO>()
|
||||
.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.last("LIMIT 1"));
|
||||
return status != null ? status.getId() : null;
|
||||
private List<Long> getRefundStatusIds(Long companyId) {
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// ==================== Excel 样式工具 ====================
|
||||
@@ -1297,7 +1297,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
BigDecimal taxRate = dto.getTaxRate() != null ? dto.getTaxRate() : DEFAULT_TAX_RATE;
|
||||
|
||||
List<Long> autoStatusIds = getAutoStatusIds(companyId);
|
||||
Long refundStatusId = getRefundStatusId(companyId);
|
||||
List<Long> refundStatusIds = getRefundStatusIds(companyId);
|
||||
|
||||
// 查询所有店铺并按平台分组
|
||||
List<ShopPO> allShops = shopService.list(
|
||||
@@ -1403,7 +1403,7 @@ public class ExportServiceImpl implements ExportService {
|
||||
totalOrderCount += shopOrders.size();
|
||||
|
||||
BigDecimal shopRefund = shopOrders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
totalRefundAmount = totalRefundAmount.add(shopRefund);
|
||||
|
||||
+19
-17
@@ -135,8 +135,8 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
.le(BrushOrderPO::getBrushTime, endDateTime);
|
||||
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
|
||||
|
||||
// 获取退款状态ID
|
||||
Long refundStatusId = getRefundStatusId();
|
||||
// 获取所有退款状态ID列表
|
||||
List<Long> refundStatusIds = getRefundStatusIds();
|
||||
|
||||
// 计算上月同期数据(用于增长百分比)
|
||||
LocalDate lastMonthStartDate = startDate.minusMonths(1);
|
||||
@@ -191,9 +191,9 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
|
||||
// 3.1 退款金额、客单价(概览页与导出汇总表共用)
|
||||
BigDecimal refundAmountAgg = BigDecimal.ZERO;
|
||||
if (refundStatusId != null) {
|
||||
if (!refundStatusIds.isEmpty()) {
|
||||
refundAmountAgg = orders.stream()
|
||||
.filter(o -> refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
@@ -230,7 +230,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
calculateTargetData(overviewVO, companyId, queryDTO.getShopId(), startDate, endDate, totalSalesAmount, totalOrderCount);
|
||||
|
||||
// 8. 汇总ROI和总税后盈亏
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId);
|
||||
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusIds);
|
||||
overviewVO.setTotalROI(totalROI);
|
||||
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
|
||||
|
||||
@@ -242,12 +242,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
|
||||
// 10. 多商品数据横向对比
|
||||
ShopOverviewStatisticsVO.ProductOverview productOverview = calculateProductComparisons(
|
||||
orders, promotionFees, brushOrders, refundStatusId);
|
||||
orders, promotionFees, brushOrders, refundStatusIds);
|
||||
overviewVO.setProductOverview(productOverview);
|
||||
|
||||
// 11. 品类多维度数据对比
|
||||
ShopOverviewStatisticsVO.CategoryOverview categoryOverview = calculateCategoryComparisons(
|
||||
orders, promotionFees, brushOrders, refundStatusId);
|
||||
orders, promotionFees, brushOrders, refundStatusIds);
|
||||
overviewVO.setCategoryOverview(categoryOverview);
|
||||
|
||||
return overviewVO;
|
||||
@@ -329,12 +329,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
* 计算汇总ROI
|
||||
*/
|
||||
private BigDecimal calculateTotalROI(List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
BigDecimal totalSales = orders.stream()
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalRefund = orders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
BigDecimal totalPromotionFee = promotionFees.stream()
|
||||
@@ -368,7 +368,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.ProductOverview calculateProductComparisons(
|
||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
List<ShopOverviewStatisticsVO.ProductComparison> comparisons = new ArrayList<>();
|
||||
|
||||
// 按商品分组订单数据
|
||||
@@ -406,7 +406,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
int orderCount = productOrders.size();
|
||||
|
||||
BigDecimal refundAmount = productOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.filter(order -> !refundStatusIds.isEmpty() && refundStatusIds.contains(order.getStatusId()))
|
||||
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
@@ -661,14 +661,16 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
/**
|
||||
* 获取退款状态ID(通过 is_refund 字段查询)
|
||||
*/
|
||||
private Long getRefundStatusId() {
|
||||
private List<Long> getRefundStatusIds() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.last("LIMIT 1");
|
||||
OrderStatusPO status = orderStatusService.getOne(wrapper);
|
||||
return status != null ? status.getId() : null;
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusService.list(wrapper);
|
||||
return statusList.stream()
|
||||
.map(OrderStatusPO::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
|
||||
@@ -687,7 +689,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
*/
|
||||
private ShopOverviewStatisticsVO.CategoryOverview calculateCategoryComparisons(
|
||||
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
|
||||
List<BrushOrderPO> brushOrders, Long refundStatusId) {
|
||||
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
|
||||
|
||||
// 构建商品ID->品类ID的映射
|
||||
Map<Long, Long> productCategoryMap = new HashMap<>();
|
||||
@@ -738,7 +740,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
|
||||
int orderCount = catOrders.size();
|
||||
|
||||
BigDecimal refundAmount = catOrders.stream()
|
||||
.filter(o -> refundStatusId != null && refundStatusId.equals(o.getStatusId()))
|
||||
.filter(o -> !refundStatusIds.isEmpty() && refundStatusIds.contains(o.getStatusId()))
|
||||
.map(o -> o.getReceivedAmount() != null ? o.getReceivedAmount() : BigDecimal.ZERO)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
|
||||
+10
-11
@@ -96,7 +96,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
}
|
||||
|
||||
// 获取退款状态ID
|
||||
Long refundStatusId = getRefundStatusId();
|
||||
List<Long> refundStatusIds = getRefundStatusIds();
|
||||
|
||||
// 按日期分组订单数据
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate = orders.stream()
|
||||
@@ -141,7 +141,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
|
||||
// 计算退款金额统计
|
||||
ProductOverviewVO.DailyStatistics refundAmountStats = calculateRefundAmountStatistics(
|
||||
ordersByDate, dateList, orders, refundStatusId);
|
||||
ordersByDate, dateList, orders, refundStatusIds);
|
||||
overviewVO.setRefundAmount(refundAmountStats);
|
||||
|
||||
// 计算推广费统计(按商品关联的店铺分摊,这里简化处理:按店铺粒度的推广费均摊到各商品)
|
||||
@@ -307,11 +307,11 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
*/
|
||||
private ProductOverviewVO.DailyStatistics calculateRefundAmountStatistics(
|
||||
Map<LocalDate, List<OrderPO>> ordersByDate, List<LocalDate> dateList,
|
||||
List<OrderPO> allOrders, Long refundStatusId) {
|
||||
List<OrderPO> allOrders, List<Long> refundStatusIds) {
|
||||
ProductOverviewVO.DailyStatistics stats = new ProductOverviewVO.DailyStatistics();
|
||||
|
||||
BigDecimal total = allOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.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);
|
||||
@@ -322,7 +322,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
dailyData.setDate(date);
|
||||
List<OrderPO> dayOrders = ordersByDate.getOrDefault(date, List.of());
|
||||
BigDecimal dayRefund = dayOrders.stream()
|
||||
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
|
||||
.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);
|
||||
@@ -406,16 +406,15 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取退款状态ID(根据 is_refund 字段查找)
|
||||
* 获取退款状态ID列表(根据 is_refund 字段查找)
|
||||
*/
|
||||
private Long getRefundStatusId() {
|
||||
private List<Long> getRefundStatusIds() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
|
||||
.eq(OrderStatusPO::getIsRefund, 1)
|
||||
.select(OrderStatusPO::getId)
|
||||
.last("LIMIT 1");
|
||||
OrderStatusPO refundStatus = orderStatusMapper.selectOne(wrapper);
|
||||
return refundStatus != null ? refundStatus.getId() : null;
|
||||
.select(OrderStatusPO::getId);
|
||||
List<OrderStatusPO> statusList = orderStatusMapper.selectList(wrapper);
|
||||
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user