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)
@@ -25,6 +25,8 @@ export namespace CompanyOverviewVO {
shopEfficiencyQuadrants?: ShopEfficiencyQuadrant[];
/** 多店铺数据横向对比(16维度) */
shopComparisons?: ShopComparison[];
/** 多品类数据横向对比 */
categoryComparisons?: CategoryComparison[];
/** 汇总ROI */
totalROI?: number;
/** 总税后盈亏 */
@@ -110,4 +112,40 @@ export namespace CompanyOverviewVO {
/** ROI */
roi?: number;
}
/** 品类对比数据 */
export interface CategoryComparison {
/** 品类ID(未分配为null) */
categoryId?: number;
/** 品类名称(未分配为"未分配品类") */
categoryName: string;
/** 销售额 */
salesAmount: number;
/** 单量 */
orderCount?: number;
/** 客单价 */
averageOrderValue?: number;
/** 商品成本 */
productCost?: number;
/** 运费 */
shippingCost?: number;
/** 其他成本 */
otherCost?: number;
/** 退款金额 */
refundAmount?: number;
/** 推广费 */
totalPromotionFee?: number;
/** 刷单费用 */
brushFee?: number;
/** 费比 */
feeRatio?: number;
/** 税前盈亏 */
totalProfitBeforeTax?: number;
/** 销售额4%税额 */
salesTaxAmount?: number;
/** 税后盈亏 */
profitAfterTax?: number;
/** ROI */
roi?: number;
}
}
@@ -1200,6 +1200,189 @@ const handleExportSelectedShops = async () => {
</table>
</div>
</div>
<!-- 第四层:品类多维度经营明细对比 -->
<div
class="overflow-hidden rounded-2xl border border-slate-200 dark:border-[hsl(240deg_3.7%_22%)] bg-white/90 dark:bg-[hsl(222.34deg_10.43%_12.27%)] shadow-[0_4px_12px_rgba(0,0,0,0.03)] dark:shadow-none backdrop-blur-[10px]"
>
<div
class="flex flex-col items-start justify-between gap-4 border-b border-slate-100 dark:border-b-[hsl(240deg_3.7%_22%)] p-8 md:flex-row md:items-center"
>
<div>
<h3 class="text-xl font-black text-slate-800 dark:text-[hsl(0deg_0%_95%)]">
各品类多维度经营明细对比
</h3>
<p class="mt-1 text-xs font-bold text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
包含:规模、成本及最终损益所有维度
</p>
</div>
</div>
<div class="table-container overflow-x-auto">
<table class="w-full whitespace-nowrap text-left text-sm">
<thead
class="bg-slate-50 dark:bg-[hsl(240deg_3.7%_15.9%)] text-xs font-black uppercase text-slate-500 dark:text-[hsl(240deg_5%_64.9%)]"
>
<tr>
<th
rowspan="2"
class="sticky-col border-b p-6 text-base font-black text-slate-900 dark:text-[hsl(0deg_0%_95%)] border-r-2 border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] bg-white dark:bg-[hsl(222.34deg_10.43%_12.27%)] shadow-sm dark:shadow-none"
>
品类名称
</th>
<th
colspan="3"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] dark:border-[hsl(240deg_3.7%_22%)] bg-slate-100/50 dark:bg-[hsl(240deg_3.7%_22%/50%)] p-3 text-center"
>
经营规模维度
</th>
<th
colspan="5"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] dark:border-[hsl(240deg_3.7%_22%)] bg-orange-50/50 dark:bg-[hsl(24deg_9.8%_15%/50%)] p-3 text-center text-orange-700 dark:text-orange-400"
>
成本费用维度
</th>
<th
colspan="4"
class="border-b border-l border-slate-200 dark:border-b-[hsl(240deg_3.7%_22%)] dark:border-l-[hsl(240deg_3.7%_22%)] dark:border-[hsl(240deg_3.7%_22%)] bg-emerald-50/50 dark:bg-[hsl(142deg_71.8%_29%/20%)] p-3 text-center text-emerald-700 dark:text-emerald-400"
>
盈利分析维度
</th>
</tr>
<tr class="bg-slate-50/80 dark:bg-[hsl(240deg_3.7%_15.9%/80)] text-sm">
<th
class="border-b border-l px-6 py-4 font-black text-slate-900 dark:text-[hsl(0deg_0%_95%)]"
>
实际销售额
</th>
<th class="border-b px-6 py-4">单量</th>
<th class="border-b px-6 py-4">客单价</th>
<th class="border-b border-l px-6 py-4">商品成本</th>
<th class="border-b px-6 py-4">运费</th>
<th class="border-b px-6 py-4">其他成本</th>
<th class="border-b px-6 py-4 font-bold text-rose-600 dark:text-rose-400">
退款金额
</th>
<th class="border-b px-6 py-4">推广费</th>
<th class="border-b px-6 py-4 text-orange-600 dark:text-orange-400">刷单费用</th>
<th class="border-b px-6 py-4 text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">费比 %</th>
<th class="border-b border-l px-6 py-4 font-bold">
税前盈亏
</th>
<th class="border-b px-6 py-4 italic text-rose-500 dark:text-rose-400">
{{ (taxRate * 100).toFixed(0) }}%税额
</th>
<th class="border-b px-6 py-4 font-black text-indigo-700 dark:text-indigo-400">
ROI
</th>
<th
class="border-b bg-emerald-600 dark:bg-emerald-600 px-6 py-4 font-black text-white"
>
税后盈亏
</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100 dark:divide-[hsl(240deg_3.7%_22%)]">
<tr
v-for="cat in overviewData?.categoryComparisons"
:key="cat.categoryId ?? 'uncategorized'"
class="transition-colors hover:bg-blue-50/50 dark:hover:bg-[hsl(217.2deg_32.6%_17.5%/30%)] dark:bg-[hsl(217.2deg_32.6%_17.5%/50%)]"
>
<td
class="sticky-col border-r-2 border-slate-100 dark:border-[hsl(240deg_3.7%_22%)] bg-white dark:bg-[hsl(222.34deg_10.43%_12.27%)] p-6 font-bold shadow-sm dark:shadow-none"
>
<span
class="text-blue-600 dark:text-blue-400 transition-colors hover:text-blue-300 hover:underline"
>
{{ cat.categoryName }}
</span>
</td>
<td class="p-6 font-black">
{{ formatAmount(cat.salesAmount) }}
</td>
<td class="p-6 font-medium">
{{ cat.orderCount?.toLocaleString('zh-CN') || '-' }}
</td>
<td class="p-6 font-medium">
{{
cat.averageOrderValue
? formatAmount(cat.averageOrderValue)
: '-'
}}
</td>
<td class="p-6">
{{
cat.productCost ? formatAmount(cat.productCost) : '-'
}}
</td>
<td class="p-6">
{{
cat.shippingCost ? formatAmount(cat.shippingCost) : '-'
}}
</td>
<td class="p-6">
{{
cat.otherCost ? formatAmount(cat.otherCost) : '-'
}}
</td>
<td class="p-6 font-bold text-rose-600 dark:text-rose-400">
{{
cat.refundAmount ? formatAmount(cat.refundAmount) : '-'
}}
</td>
<td class="p-6">
{{
cat.totalPromotionFee
? formatAmount(cat.totalPromotionFee)
: '-'
}}
</td>
<td class="p-6 text-orange-600 dark:text-orange-400">
{{ cat.brushFee ? formatAmount(cat.brushFee) : '-' }}
</td>
<td class="p-6 text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
{{
cat.feeRatio !== undefined
? formatPercentPlain(cat.feeRatio)
: '-'
}}
</td>
<td class="p-6 font-bold">
{{
cat.totalProfitBeforeTax
? formatAmount(cat.totalProfitBeforeTax)
: '-'
}}
</td>
<td class="p-6 italic text-slate-400 dark:text-[hsl(240deg_5%_64.9%)]">
{{
cat.salesTaxAmount
? formatAmount(cat.salesTaxAmount)
: '-'
}}
</td>
<td class="p-6 font-black text-indigo-700 dark:text-indigo-400">
{{ cat.roi?.toFixed(2) || '-' }}
</td>
<td
class="bg-emerald-50 dark:bg-[hsl(142deg_71.8%_29%/20%)] p-6 text-base font-black text-emerald-600"
>
{{
cat.profitAfterTax
? formatAmount(cat.profitAfterTax)
: '-'
}}
</td>
</tr>
<tr v-if="!overviewData?.categoryComparisons?.length">
<td colspan="12" class="p-8 text-center text-gray-400">
暂无数据
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</Spin>
</Page>