feat: 公司概览导出支持自定义税率,增加商品成本/其他成本列

CompanyExportDTO / ShopExportDTO: 加 taxRate 字段
ExportServiceImpl:
  - buildShopData 加 taxRate 参数,汇总税额用参数税率
  - 明细表头: 成本运费 → 商品成本/运费/其他成本三列
  - 税额表头动态: 4%税额 → X%税额
前端: 导出请求加 taxRate 参数
This commit is contained in:
小蟑螂
2026-04-15 22:54:44 +08:00
parent 03cf396aea
commit 0caa8e8e95
4 changed files with 49 additions and 20 deletions
@@ -3,8 +3,10 @@ package top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
@@ -12,6 +14,7 @@ import java.util.List;
* 公司概览导出DTO
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "公司概览导出DTO")
public class CompanyExportDTO {
@@ -25,4 +28,7 @@ public class CompanyExportDTO {
@Schema(description = "导出类型:summary-汇总 / detail-明细 / all-全部,默认 all", example = "all")
private String exportType = "all";
@Schema(description = "税率,默认 0.04", example = "0.04")
private BigDecimal taxRate = new BigDecimal("0.04");
}
@@ -4,8 +4,10 @@ import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
@@ -13,6 +15,7 @@ import java.util.List;
* 店铺概览导出DTO
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@Schema(description = "店铺概览导出DTO")
public class ShopExportDTO {
@@ -27,4 +30,7 @@ public class ShopExportDTO {
@Schema(description = "导出类型:summary-汇总 / product-商品明细 / trend-趋势 / all-全部,默认 all", example = "all")
private String exportType = "all";
@Schema(description = "税率,默认 0.04", example = "0.04")
private BigDecimal taxRate = new BigDecimal("0.04");
}
@@ -43,7 +43,7 @@ import java.util.stream.Collectors;
@Service
public class ExportServiceImpl implements ExportService {
private static final BigDecimal SALES_TAX_RATE = new BigDecimal("0.04");
private static final BigDecimal DEFAULT_TAX_RATE = new BigDecimal("0.04");
private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private final CurrentUserContext currentUserContext;
@@ -98,8 +98,10 @@ public class ExportServiceImpl implements ExportService {
BigDecimal totalBrushFee = BigDecimal.ZERO;
BigDecimal totalProfitBeforeTax = BigDecimal.ZERO;
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);
Map<String, Object> data = buildShopData(shop, dateRange[0], dateRange[1], autoStatusIds, refundStatusId, companyId, taxRate);
shopDataList.add(data);
totalSales = totalSales.add((BigDecimal) data.get("salesAmount"));
@@ -110,7 +112,7 @@ public class ExportServiceImpl implements ExportService {
}
// 计算汇总数据
BigDecimal totalTax = totalProfitBeforeTax.multiply(SALES_TAX_RATE).setScale(2, RoundingMode.HALF_UP);
BigDecimal totalTax = totalProfitBeforeTax.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
BigDecimal totalProfitAfterTax = totalProfitBeforeTax.subtract(totalTax);
BigDecimal totalCost = totalPromotionFee.add(totalBrushFee);
BigDecimal totalRoi = totalCost.compareTo(BigDecimal.ZERO) > 0
@@ -144,7 +146,7 @@ public class ExportServiceImpl implements ExportService {
String exportType = dto.getExportType() == null ? "all" : dto.getExportType();
if ("detail".equals(exportType) || "all".equals(exportType)) {
Sheet detailSheet = workbook.createSheet("店铺明细");
createCompanyDetailSheet(detailSheet, dateRangeLabel, shopDataList);
createCompanyDetailSheet(detailSheet, dateRangeLabel, shopDataList, taxRate);
}
if (("trend".equals(exportType) || "all".equals(exportType)) && !ordersByDate.isEmpty()) {
@@ -264,7 +266,7 @@ public class ExportServiceImpl implements ExportService {
}
}
private void createCompanyDetailSheet(Sheet sheet, String dateRangeLabel, List<Map<String, Object>> shopDataList) {
private void createCompanyDetailSheet(Sheet sheet, String dateRangeLabel, List<Map<String, Object>> shopDataList, BigDecimal taxRate) {
Row titleRow = sheet.createRow(0);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("店铺明细 | " + dateRangeLabel);
@@ -274,11 +276,12 @@ public class ExportServiceImpl implements ExportService {
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
// 列顺序与 Dashboard 表格一致
String taxLabel = (taxRate.multiply(new BigDecimal("100")).setScale(0, RoundingMode.HALF_UP)).toPlainString() + "%税额";
String[] headers = {
"店铺名称", "目标单量", "单量达成率(%)", "目标额", "销售达成率(%)",
"实际销售额", "单量", "客单价",
"成本运费", "退款金额", "推广费", "刷单费用", "费比",
"税前盈亏", "4%税额", "ROI", "税后盈亏"
"商品成本", "运费", "其他成本", "退款金额", "推广费", "刷单费用", "费比",
"税前盈亏", taxLabel, "ROI", "税后盈亏"
};
Row headerRow = sheet.createRow(1);
@@ -299,15 +302,17 @@ public class ExportServiceImpl implements ExportService {
setCell(row, 5, objToDouble(data.get("salesAmount")), dataStyle);
setCell(row, 6, objToDouble(data.get("orderCount")), dataStyle);
setCell(row, 7, objToDouble(data.get("avgOrderValue")), dataStyle);
setCell(row, 8, objToDouble(data.get("shippingCost")), dataStyle);
setCell(row, 9, objToDouble(data.get("refundAmount")), dataStyle);
setCell(row, 10, objToDouble(data.get("totalPromotionFee")), dataStyle);
setCell(row, 11, objToDouble(data.get("brushFee")), dataStyle);
setCell(row, 12, objToDouble(data.get("feeRatio")), dataStyle);
setCell(row, 13, objToDouble(data.get("profitBeforeTax")), dataStyle);
setCell(row, 14, objToDouble(data.get("salesTaxAmount")), dataStyle);
setCell(row, 15, objToDouble(data.get("roi")), dataStyle);
setCell(row, 16, objToDouble(data.get("profitAfterTax")), dataStyle);
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("profitBeforeTax")), 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);
}
for (int i = 0; i < headers.length; i++) {
@@ -461,7 +466,7 @@ public class ExportServiceImpl implements ExportService {
BigDecimal totalCost = productCost.add(shippingCost);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal tax = profitBeforeTax.multiply(SALES_TAX_RATE).setScale(2, RoundingMode.HALF_UP);
BigDecimal tax = profitBeforeTax.multiply(DEFAULT_TAX_RATE).setScale(2, RoundingMode.HALF_UP);
BigDecimal profitAfterTax = profitBeforeTax.subtract(tax);
// 按商品分摊推广费和刷单费(按销售额占比)
@@ -762,7 +767,8 @@ 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, Long refundStatusId, Long companyId,
BigDecimal taxRate) {
LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atTime(23, 59, 59);
@@ -843,6 +849,14 @@ public class ExportServiceImpl implements ExportService {
})
.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));
})
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal promotionFee = promotionFees.stream()
.map(f -> f.getFeeAmount() != null ? f.getFeeAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -851,9 +865,9 @@ public class ExportServiceImpl implements ExportService {
.map(b -> b.getBrushAmount() != null ? b.getBrushAmount() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalCost = productCostTotal.add(shippingCost);
BigDecimal totalCost = productCostTotal.add(shippingCost).add(otherCostTotal);
BigDecimal profitBeforeTax = salesAmount.subtract(totalCost).subtract(refundAmount);
BigDecimal tax = profitBeforeTax.multiply(SALES_TAX_RATE).setScale(2, RoundingMode.HALF_UP);
BigDecimal tax = profitBeforeTax.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;
@@ -877,7 +891,9 @@ public class ExportServiceImpl implements ExportService {
data.put("brushFee", brushFee);
data.put("feeRatio", feeRatio);
data.put("refundAmount", refundAmount);
data.put("productCost", productCostTotal);
data.put("shippingCost", shippingCost);
data.put("otherCost", otherCostTotal);
data.put("totalProfitBeforeTax", profitBeforeTax); // 对齐 ShopComparison
data.put("salesTaxAmount", tax); // 对齐 ShopComparison
data.put("profitAfterTax", profitAfterTax);
@@ -567,6 +567,7 @@ const handleExportCompanyOverview = async () => {
endDate.format('YYYY-MM-DD'),
],
shopIds,
taxRate: taxRate.value,
},
filename,
);