feat: 公司概览新增各品类多维度经营明细对比表

- CompanyOverviewVO: 新增CategoryComparison品类对比数据VO
- CompanyOverviewServiceImpl: 新增calculateCategoryComparisons()按品类汇总订单数据
  - 按商品categoryId分组,未分配品类的归入其他品类
  - 推广费/刷单费按品类各店铺销售额占比分摊
- types.vo.ts: 新增CategoryComparison接口
- company-overview/index.vue: 新增品类经营明细对比表格
This commit is contained in:
小蟑螂
2026-04-20 20:08:55 +08:00
parent dd26732237
commit b270b7818a
4 changed files with 472 additions and 2 deletions
@@ -73,6 +73,12 @@ public class CompanyOverviewVO {
@Schema(description = "多店铺数据横向对比")
private List<ShopComparison> shopComparisons;
/**
* 多品类数据横向对比
*/
@Schema(description = "多品类数据横向对比")
private List<CategoryComparison> categoryComparisons;
/**
* 汇总ROI
*/
@@ -213,6 +219,68 @@ public class CompanyOverviewVO {
@Schema(description = "销售额4%税额", example = "2000.00")
private BigDecimal salesTaxAmount;
@Schema(description = "税后盈亏", example = "30000.00")
private BigDecimal profitAfterTax;
@Schema(description = "ROI", example = "1.5")
private BigDecimal roi;
}
/**
* 品类对比数据
*/
@Data
@Schema(description = "品类对比数据")
public static class CategoryComparison {
@Schema(description = "品类ID(未分配为null)", example = "1")
private Long categoryId;
@Schema(description = "品类名称(未分配为\"未分配品类\")", example = "手机数码")
private String categoryName;
@Schema(description = "销售额", example = "50000.00")
private BigDecimal salesAmount;
@Schema(description = "单量", example = "500")
private Integer orderCount;
@Schema(description = "客单价", example = "100.00")
private BigDecimal averageOrderValue;
@Schema(description = "商品成本", example = "3000.00")
private BigDecimal productCost;
@Schema(description = "运费", example = "1000.00")
private BigDecimal shippingCost;
@Schema(description = "其他成本", example = "1000.00")
private BigDecimal otherCost;
@Schema(description = "退款金额", example = "1000.00")
private BigDecimal refundAmount;
@Schema(description = "推广费", example = "5000.00")
private BigDecimal totalPromotionFee;
@Schema(description = "刷单费用", example = "2000.00")
private BigDecimal brushFee;
@Schema(description = "费比", example = "0.10")
private BigDecimal feeRatio;
@Schema(description = "税前盈亏", example = "35000.00")
private BigDecimal totalProfitBeforeTax;
@Schema(description = "销售额4%税额", example = "2000.00")
private BigDecimal salesTaxAmount;
@Schema(description = "税后盈亏", example = "30000.00")
private BigDecimal profitAfterTax;
@@ -18,6 +18,10 @@ import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.model.po.ShopMo
import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.vo.CompanyOverviewVO;
import top.xyquan.ecomdmbbackend.modules.ecom.overview.service.CompanyOverviewService;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.service.ShopService;
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.product.mapper.ProductMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.math.BigDecimal;
@@ -46,6 +50,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
private final ShopService shopService;
private final OrderStatusService orderStatusService;
private final ProductMapper productMapper;
private final CategoryService categoryService;
public CompanyOverviewServiceImpl(CurrentUserContext currentUserContext,
OrderMapper orderMapper,
@@ -53,7 +59,9 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
BrushOrderMapper brushOrderMapper,
ShopMonthlyTargetMapper shopMonthlyTargetMapper,
ShopService shopService,
OrderStatusService orderStatusService) {
OrderStatusService orderStatusService,
ProductMapper productMapper,
CategoryService categoryService) {
this.currentUserContext = currentUserContext;
this.orderMapper = orderMapper;
this.promotionFeeMapper = promotionFeeMapper;
@@ -61,6 +69,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
this.shopService = shopService;
this.orderStatusService = orderStatusService;
this.productMapper = productMapper;
this.categoryService = categoryService;
}
@Override
@@ -204,7 +214,12 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
shops, orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
overviewVO.setShopComparisons(shopComparisons);
// 10. 汇总ROI和总税后盈亏
// 10. 多品类数据横向对比
List<CompanyOverviewVO.CategoryComparison> categoryComparisons = calculateCategoryComparisons(
orders, promotionFees, brushOrders, refundStatusId, companyId, startDate, endDate);
overviewVO.setCategoryComparisons(categoryComparisons);
// 11. 汇总ROI和总税后盈亏
BigDecimal totalROI = calculateTotalROI(orders, promotionFees, brushOrders, refundStatusId);
overviewVO.setTotalROI(totalROI);
overviewVO.setTotalProfitAfterTax(totalNetProfitAfterTax);
@@ -701,6 +716,172 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
return status != null ? status.getId() : null;
}
/**
* Calculate category comparison data
*/
private List<CompanyOverviewVO.CategoryComparison> calculateCategoryComparisons(
List<OrderPO> orders, List<PromotionFeePO> promotionFees,
List<BrushOrderPO> brushOrders, Long refundStatusId, Long companyId,
LocalDate startDate, LocalDate endDate) {
// Query all categories to build categoryId -> categoryName map
LambdaQueryWrapper<CategoryPO> categoryWrapper = new LambdaQueryWrapper<>();
categoryWrapper.eq(CategoryPO::getCompanyId, companyId);
List<CategoryPO> allCategories = categoryService.list(categoryWrapper);
Map<Long, String> categoryNameMap = allCategories.stream()
.collect(Collectors.toMap(CategoryPO::getId, CategoryPO::getName));
// Collect all product IDs involved and batch query their category info
List<Long> productIds = orders.stream()
.map(OrderPO::getProductId)
.filter(id -> id != null)
.distinct()
.collect(Collectors.toList());
Map<Long, Long> productCategoryMap = new java.util.HashMap<>();
if (!productIds.isEmpty()) {
LambdaQueryWrapper<ProductPO> productWrapper = new LambdaQueryWrapper<>();
productWrapper.in(ProductPO::getId, productIds).select(ProductPO::getId, ProductPO::getCategoryId);
List<ProductPO> products = productMapper.selectList(productWrapper);
for (ProductPO product : products) {
productCategoryMap.put(product.getId(), product.getCategoryId());
}
}
// Group orders by category
Map<Long, List<OrderPO>> ordersByCategory = orders.stream()
.collect(Collectors.groupingBy(order -> {
Long productId = order.getProductId();
if (productId == null) return -1L;
Long categoryId = productCategoryMap.get(productId);
return categoryId != null ? categoryId : -1L;
}));
// Aggregate promotion fees and brush fees by shop
Map<Long, BigDecimal> shopTotalPromotionFeeMap = promotionFees.stream()
.collect(Collectors.groupingBy(
PromotionFeePO::getShopId,
Collectors.reducing(BigDecimal.ZERO,
fee -> fee.getFeeAmount() != null ? fee.getFeeAmount() : BigDecimal.ZERO,
BigDecimal::add)));
Map<Long, BigDecimal> shopTotalBrushFeeMap = brushOrders.stream()
.collect(Collectors.groupingBy(
BrushOrderPO::getShopId,
Collectors.reducing(BigDecimal.ZERO,
brush -> brush.getBrushAmount() != null ? brush.getBrushAmount() : BigDecimal.ZERO,
BigDecimal::add)));
List<CompanyOverviewVO.CategoryComparison> comparisons = new ArrayList<>();
for (Map.Entry<Long, List<OrderPO>> entry : ordersByCategory.entrySet()) {
Long categoryId = entry.getKey();
List<OrderPO> categoryOrders = entry.getValue();
BigDecimal salesAmount = categoryOrders.stream()
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
int orderCount = categoryOrders.size();
BigDecimal refundAmount = categoryOrders.stream()
.filter(order -> refundStatusId != null && refundStatusId.equals(order.getStatusId()))
.map(order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO)
.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));
})
.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));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal otherCost = categoryOrders.stream()
.map(order -> {
BigDecimal cost = order.getOtherCost() != null ? order.getOtherCost() : BigDecimal.ZERO;
return cost;
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
Map<Long, BigDecimal> categorySalesByShop = categoryOrders.stream()
.collect(Collectors.groupingBy(
OrderPO::getShopId,
Collectors.reducing(BigDecimal.ZERO,
order -> order.getReceivedAmount() != null ? order.getReceivedAmount() : BigDecimal.ZERO,
BigDecimal::add)));
BigDecimal categoryTotalSales = categorySalesByShop.values().stream()
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalPromotionFee = BigDecimal.ZERO;
BigDecimal totalBrushFee = BigDecimal.ZERO;
if (categoryTotalSales.compareTo(BigDecimal.ZERO) > 0) {
for (Map.Entry<Long, BigDecimal> shopEntry : categorySalesByShop.entrySet()) {
Long shopId = shopEntry.getKey();
BigDecimal shopCategorySales = shopEntry.getValue();
BigDecimal ratio = shopCategorySales.divide(categoryTotalSales, 6, RoundingMode.HALF_UP);
BigDecimal shopPromoFee = shopTotalPromotionFeeMap.getOrDefault(shopId, BigDecimal.ZERO);
BigDecimal shopBrushFee = shopTotalBrushFeeMap.getOrDefault(shopId, BigDecimal.ZERO);
totalPromotionFee = totalPromotionFee.add(shopPromoFee.multiply(ratio));
totalBrushFee = totalBrushFee.add(shopBrushFee.multiply(ratio));
}
}
BigDecimal totalCost = productCost.add(shippingCost).add(otherCost);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal salesTaxAmount = salesAmount.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTaxAmount);
BigDecimal roi = BigDecimal.ZERO;
if (totalPromotionFee.compareTo(BigDecimal.ZERO) > 0) {
roi = salesAmount.subtract(refundAmount).divide(totalPromotionFee, 4, RoundingMode.HALF_UP);
}
BigDecimal feeRatio = BigDecimal.ZERO;
if (salesAmount.compareTo(BigDecimal.ZERO) > 0) {
feeRatio = totalPromotionFee.divide(salesAmount, 4, RoundingMode.HALF_UP);
}
BigDecimal averageOrderValue = BigDecimal.ZERO;
if (orderCount > 0) {
averageOrderValue = salesAmount.divide(new BigDecimal(orderCount), 2, RoundingMode.HALF_UP);
}
String categoryName = categoryId == -1L ? "其他品类" : categoryNameMap.getOrDefault(categoryId, "Unknown");
CompanyOverviewVO.CategoryComparison comparison = new CompanyOverviewVO.CategoryComparison();
comparison.setCategoryId(categoryId == -1L ? null : categoryId);
comparison.setCategoryName(categoryName);
comparison.setSalesAmount(salesAmount);
comparison.setOrderCount(orderCount);
comparison.setAverageOrderValue(averageOrderValue);
comparison.setProductCost(productCost);
comparison.setShippingCost(shippingCost);
comparison.setOtherCost(otherCost);
comparison.setRefundAmount(refundAmount);
comparison.setTotalPromotionFee(totalPromotionFee);
comparison.setBrushFee(totalBrushFee);
comparison.setFeeRatio(feeRatio);
comparison.setTotalProfitBeforeTax(profitBeforeTax);
comparison.setSalesTaxAmount(salesTaxAmount);
comparison.setProfitAfterTax(profitAfterTax);
comparison.setRoi(roi);
comparisons.add(comparison);
}
comparisons.sort(Comparator.comparing(CompanyOverviewVO.CategoryComparison::getSalesAmount).reversed());
return comparisons;
}
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrderStatusPO::getCompanyId, companyId)