feat(ecom): 产品概况增加总成本字段及按自动统计状态过滤订单

- CompanyOverviewVO 新增 totalCost 字段(商品成本+运费+其他成本)
- ProductOverviewServiceImpl 查询订单时仅统计 autoStatistics=1 的订单状态
- 同步前端 types.vo.ts 及各组件展示总成本
- SQL 迁移文件整理至 sql/migration/ 目录
This commit is contained in:
小蟑螂
2026-05-05 12:06:09 +08:00
parent 17bbae66a1
commit 44662a4790
14 changed files with 116 additions and 51 deletions
@@ -285,6 +285,9 @@ public class CompanyOverviewVO {
@Schema(description = "其他成本", example = "1000.00")
private BigDecimal otherCost;
@Schema(description = "总成本(商品成本+运费+其他成本)", example = "5000.00")
private BigDecimal totalCost;
@Schema(description = "退款金额", example = "1000.00")
private BigDecimal refundAmount;
@@ -344,6 +347,8 @@ public class CompanyOverviewVO {
@Schema(description = "其他成本", example = "1000.00")
private BigDecimal otherCost;
@Schema(description = "总成本(商品成本+运费+其他成本)", example = "5000.00")
private BigDecimal totalCost;
@Schema(description = "退款金额", example = "1000.00")
private BigDecimal refundAmount;
@@ -360,11 +365,9 @@ public class CompanyOverviewVO {
@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;
@@ -414,6 +417,9 @@ public class CompanyOverviewVO {
@Schema(description = "其他成本", example = "3200.00")
private BigDecimal otherCost;
@Schema(description = "总成本(商品成本+运费+其他成本)", example = "167700.00")
private BigDecimal totalCost;
@Schema(description = "退款金额", example = "12600.00")
private BigDecimal refundAmount;
@@ -231,6 +231,7 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
data.setProductCost(productCost);
data.setShippingCost(shippingCost);
data.setOtherCost(otherCost);
data.setTotalCost(totalCost);
data.setRefundAmount(refundAmount);
data.setTotalPromotionFee(totalPromotionFee);
data.setBrushFee(totalBrushFee);
@@ -272,18 +273,18 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
String taxLabel = taxRate.multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP).toPlainString() + "%税额";
String[] headers = {
"品类名称", "销售额", "单量", "客单价",
"商品成本", "运费", "其他成本", "总成本", "退款金额", "推广费", "刷单费用",
"费比", "税前盈亏", taxLabel, "税后盈亏", "ROI"
};
Row titleRow = sheet.createRow(0);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("品类明细 | " + dateRangeLabel);
titleCell.setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 13));
String taxLabel = taxRate.multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP).toPlainString() + "%税额";
String[] headers = {
"品类名称", "销售额", "单量", "客单价",
"商品成本", "运费", "其他成本", "退款金额", "推广费", "刷单费用",
"费比", "税前盈亏", taxLabel, "税后盈亏", "ROI"
};
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
Row headerRow = sheet.createRow(1);
for (int i = 0; i < headers.length; i++) {
@@ -302,14 +303,15 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
setCell(row, 4, objToDouble(data.getProductCost()), dataStyle);
setCell(row, 5, objToDouble(data.getShippingCost()), dataStyle);
setCell(row, 6, objToDouble(data.getOtherCost()), dataStyle);
setCell(row, 7, objToDouble(data.getRefundAmount()), dataStyle);
setCell(row, 8, objToDouble(data.getTotalPromotionFee()), dataStyle);
setCell(row, 9, objToDouble(data.getBrushFee()), dataStyle);
setCell(row, 10, objToDouble(data.getFeeRatio()), dataStyle);
setCell(row, 11, objToDouble(data.getTotalProfitBeforeTax()), dataStyle);
setCell(row, 12, objToDouble(data.getSalesTaxAmount()), dataStyle);
setCell(row, 13, objToDouble(data.getProfitAfterTax()), dataStyle);
setCell(row, 14, objToDouble(data.getRoi()), dataStyle);
setCell(row, 7, objToDouble(data.getTotalCost()), dataStyle);
setCell(row, 8, objToDouble(data.getRefundAmount()), dataStyle);
setCell(row, 9, objToDouble(data.getTotalPromotionFee()), dataStyle);
setCell(row, 10, objToDouble(data.getBrushFee()), dataStyle);
setCell(row, 11, objToDouble(data.getFeeRatio()), dataStyle);
setCell(row, 12, objToDouble(data.getTotalProfitBeforeTax()), dataStyle);
setCell(row, 13, objToDouble(data.getSalesTaxAmount()), dataStyle);
setCell(row, 14, objToDouble(data.getProfitAfterTax()), dataStyle);
setCell(row, 15, objToDouble(data.getRoi()), dataStyle);
}
for (int i = 0; i < headers.length; i++) {
@@ -616,6 +616,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
comparison.setProductCost(productCost);
comparison.setShippingCost(shippingCost);
comparison.setOtherCost(otherCost);
comparison.setTotalCost(productCost.add(shippingCost).add(otherCost));
comparison.setRefundAmount(refundAmount);
comparison.setTotalPromotionFee(totalPromotionFee);
comparison.setBrushFee(brushFee);
@@ -702,6 +703,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setTotalCost(totalCost);
total.setRefundAmount(totalRefund);
total.setTotalPromotionFee(totalPromoFee);
total.setBrushFee(totalBrushFee);
@@ -991,6 +993,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
comparison.setProductCost(productCost);
comparison.setShippingCost(shippingCost);
comparison.setOtherCost(otherCost);
comparison.setTotalCost(totalCost);
comparison.setRefundAmount(refundAmount);
comparison.setTotalPromotionFee(totalPromotionFee);
comparison.setBrushFee(totalBrushFee);
@@ -1061,6 +1064,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setTotalCost(totalCost);
total.setRefundAmount(totalRefund);
total.setTotalPromotionFee(totalPromoFee);
total.setBrushFee(totalBrushFee);
@@ -1279,6 +1283,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
platformSummary.setProductCost(totalProductCost);
platformSummary.setShippingCost(totalShippingCost);
platformSummary.setOtherCost(totalOtherCost);
platformSummary.setTotalCost(totalCost);
platformSummary.setRefundAmount(totalRefundAmount);
platformSummary.setTotalPromotionFee(totalPromotionFee);
platformSummary.setBrushFee(totalBrushFee);
@@ -1377,6 +1382,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
total.setProductCost(totalProductCost);
total.setShippingCost(totalShippingCost);
total.setOtherCost(totalOtherCost);
total.setTotalCost(totalCost);
total.setRefundAmount(totalRefundAmount);
total.setTotalPromotionFee(totalPromotionFee);
total.setBrushFee(totalBrushFee);
@@ -1507,6 +1513,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
summary.setProductCost(totalProductCost);
summary.setShippingCost(totalShippingCost);
summary.setOtherCost(totalOtherCost);
summary.setTotalCost(totalCost);
summary.setRefundAmount(totalRefundAmount);
summary.setTotalPromotionFee(totalPromotionFee);
summary.setBrushFee(totalBrushFee);
@@ -235,18 +235,18 @@ public class ExportServiceImpl implements ExportService {
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("店铺明细 | " + dateRangeLabel);
titleCell.setCellStyle(createTitleStyle(sheet.getWorkbook()));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 17));
CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
// 列顺序与 Dashboard 表格一致
String taxLabel = (taxRate.multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP)).toPlainString() + "%税额";
String[] headers = {
"店铺名称", "目标单量", "单量达成率(%)", "目标额", "销售达成率(%)",
"实际销售额", "单量", "客单价",
"商品成本", "运费", "其他成本", "退款金额", "推广费", "刷单费用", "费比",
"商品成本", "运费", "其他成本", "总成本", "退款金额", "推广费", "刷单费用", "费比",
"税前盈亏", taxLabel, "ROI", "税后盈亏"
};
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
// 列顺序与 Dashboard 表格一致
Row headerRow = sheet.createRow(1);
for (int i = 0; i < headers.length; i++) {
@@ -269,14 +269,15 @@ public class ExportServiceImpl implements ExportService {
setCell(row, 8, objToDouble(data.get("productCost")), dataStyle);
setCell(row, 9, objToDouble(data.get("shippingCost")), dataStyle);
setCell(row, 10, objToDouble(data.get("otherCost")), dataStyle);
setCell(row, 11, objToDouble(data.get("refundAmount")), dataStyle);
setCell(row, 12, objToDouble(data.get("totalPromotionFee")), dataStyle);
setCell(row, 13, objToDouble(data.get("brushFee")), dataStyle);
setCell(row, 14, objToDouble(data.get("feeRatio")), dataStyle);
setCell(row, 15, objToDouble(data.get("totalProfitBeforeTax")), dataStyle);
setCell(row, 16, objToDouble(data.get("salesTaxAmount")), dataStyle);
setCell(row, 17, objToDouble(data.get("roi")), dataStyle);
setCell(row, 18, objToDouble(data.get("profitAfterTax")), dataStyle);
setCell(row, 11, objToDouble(data.get("totalCost")), dataStyle);
setCell(row, 12, objToDouble(data.get("refundAmount")), dataStyle);
setCell(row, 13, objToDouble(data.get("totalPromotionFee")), dataStyle);
setCell(row, 14, objToDouble(data.get("brushFee")), dataStyle);
setCell(row, 15, objToDouble(data.get("feeRatio")), dataStyle);
setCell(row, 16, objToDouble(data.get("totalProfitBeforeTax")), dataStyle);
setCell(row, 17, objToDouble(data.get("salesTaxAmount")), dataStyle);
setCell(row, 18, objToDouble(data.get("roi")), dataStyle);
setCell(row, 19, objToDouble(data.get("profitAfterTax")), dataStyle);
}
for (int i = 0; i < headers.length; i++) {
@@ -478,6 +479,7 @@ public class ExportServiceImpl implements ExportService {
data.put("productCost", productCostTotal);
data.put("shippingCost", shippingCost);
data.put("otherCost", otherCostTotal);
data.put("totalCost", totalCost);
data.put("totalProfitBeforeTax", profitBeforeTax); // 对齐 ShopComparison
data.put("salesTaxAmount", tax); // 对齐 ShopComparison
data.put("profitAfterTax", profitAfterTax);
@@ -768,6 +770,7 @@ public class ExportServiceImpl implements ExportService {
data.put("productCost", totalProductCost);
data.put("shippingCost", totalShippingCost);
data.put("otherCost", totalOtherCost);
data.put("totalCost", totalCost);
data.put("refundAmount", totalRefundAmount);
data.put("totalPromotionFee", totalPromotionFee);
data.put("brushFee", totalBrushFee);
@@ -807,15 +810,14 @@ public class ExportServiceImpl implements ExportService {
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("平台维度汇总 | " + dateRangeLabel);
titleCell.setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 17));
String taxLabel = taxRate.multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP).toPlainString() + "%税额";
String[] headers = {
"平台名称", "目标单量", "单量达成率(%)", "目标额", "销售达成率(%)",
"实际销售额", "单量", "客单价",
"商品成本", "运费", "其他成本", "退款金额", "推广费", "刷单费用", "费比",
"商品成本", "运费", "其他成本", "总成本", "退款金额", "推广费", "刷单费用", "费比",
"税前盈亏", taxLabel, "ROI", "税后盈亏"
};
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
Row headerRow = sheet.createRow(1);
for (int i = 0; i < headers.length; i++) {
@@ -838,14 +840,15 @@ public class ExportServiceImpl implements ExportService {
setCell(row, 8, objToDouble(data.get("productCost")), dataStyle);
setCell(row, 9, objToDouble(data.get("shippingCost")), dataStyle);
setCell(row, 10, objToDouble(data.get("otherCost")), dataStyle);
setCell(row, 11, objToDouble(data.get("refundAmount")), dataStyle);
setCell(row, 12, objToDouble(data.get("totalPromotionFee")), dataStyle);
setCell(row, 13, objToDouble(data.get("brushFee")), dataStyle);
setCell(row, 14, objToPct(data.get("feeRatio")), dataStyle);
setCell(row, 15, objToDouble(data.get("totalProfitBeforeTax")), dataStyle);
setCell(row, 16, objToDouble(data.get("salesTaxAmount")), dataStyle);
setCell(row, 17, objToDouble(data.get("roi")), dataStyle);
setCell(row, 18, objToDouble(data.get("profitAfterTax")), dataStyle);
setCell(row, 11, objToDouble(data.get("totalCost")), dataStyle);
setCell(row, 12, objToDouble(data.get("refundAmount")), dataStyle);
setCell(row, 13, objToDouble(data.get("totalPromotionFee")), dataStyle);
setCell(row, 14, objToDouble(data.get("brushFee")), dataStyle);
setCell(row, 15, objToPct(data.get("feeRatio")), dataStyle);
setCell(row, 16, objToDouble(data.get("totalProfitBeforeTax")), dataStyle);
setCell(row, 17, objToDouble(data.get("salesTaxAmount")), dataStyle);
setCell(row, 18, objToDouble(data.get("roi")), dataStyle);
setCell(row, 19, objToDouble(data.get("profitAfterTax")), dataStyle);
}
for (int i = 0; i < headers.length; i++) {
@@ -563,7 +563,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
@@ -894,7 +894,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
: BigDecimal.ZERO;
BigDecimal totalCost = totalProductCost.add(totalShippingCost).add(totalOtherCost);
BigDecimal profitBeforeTax = totalSales.subtract(totalCost).subtract(totalRefund);
BigDecimal salesTax = totalSales.multiply(new BigDecimal("0.04"));
BigDecimal salesTax = totalSales.multiply(taxRate);
BigDecimal profitAfterTax = profitBeforeTax.subtract(salesTax);
BigDecimal roi = totalPromoFee.compareTo(BigDecimal.ZERO) > 0
? totalSales.subtract(totalRefund).divide(totalPromoFee, 4, RoundingMode.HALF_UP)
@@ -83,6 +83,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
? queryDTO.getTaxRate()
: new BigDecimal("0.04");
List<Long> autoStatisticsStatusIds = getAutoStatisticsStatusIds(companyId);
// 查询订单数据
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
orderWrapper.eq(OrderPO::getCompanyId, companyId)
@@ -90,6 +92,11 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, startDateTime)
.le(OrderPO::getOrderTime, endDateTime);
if (!autoStatisticsStatusIds.isEmpty()) {
orderWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
} else {
orderWrapper.isNull(OrderPO::getId);
}
List<OrderPO> orders = orderMapper.selectList(orderWrapper);
// 收集店铺ID列表
@@ -126,6 +133,11 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, lastStartTime)
.le(OrderPO::getOrderTime, lastEndTime);
if (!autoStatisticsStatusIds.isEmpty()) {
lastPeriodWrapper.in(OrderPO::getStatusId, autoStatisticsStatusIds);
} else {
lastPeriodWrapper.isNull(OrderPO::getId);
}
List<OrderPO> lastPeriodOrders = orderMapper.selectList(lastPeriodWrapper);
// ========== 构建返回对象 ==========
@@ -602,6 +614,19 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
return summary;
}
/**
* 获取参与统计的订单状态ID列表
*/
private List<Long> getAutoStatisticsStatusIds(Long companyId) {
LambdaQueryWrapper<OrderStatusPO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrderStatusPO::getCompanyId, companyId)
.eq(OrderStatusPO::getAutoStatistics, 1)
.select(OrderStatusPO::getId);
return orderStatusMapper.selectList(wrapper).stream()
.map(OrderStatusPO::getId)
.collect(Collectors.toList());
}
/**
* 获取退款状态ID集合
*/