fix: 公司导出恢复一行一店铺格式,字段对齐 ShopComparison

- 还原 createCompanyDetailSheet 为行式:一行=一个店铺,列=各指标
- buildShopData key 更名为对齐 ShopComparison(totalPromotionFee/salesTaxAmount/totalProfitBeforeTax)
- 添加 objToDouble/objToPct 工具方法
This commit is contained in:
小蟑螂
2026-03-26 21:58:26 +08:00
parent 4d5f5914e5
commit 60a8b009d1
2 changed files with 58 additions and 112 deletions
@@ -3,8 +3,6 @@ package top.xyquan.ecomdmbbackend.modules.ecom.overview.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.Arrays;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
@@ -101,9 +99,9 @@ public class ExportServiceImpl implements ExportService {
totalSales = totalSales.add((BigDecimal) data.get("salesAmount"));
totalOrders = totalOrders.add(new BigDecimal((Integer) data.get("orderCount")));
totalPromotionFee = totalPromotionFee.add((BigDecimal) data.get("promotionFee"));
totalPromotionFee = totalPromotionFee.add((BigDecimal) data.get("totalPromotionFee"));
totalBrushFee = totalBrushFee.add((BigDecimal) data.get("brushFee"));
totalProfitBeforeTax = totalProfitBeforeTax.add((BigDecimal) data.get("profitBeforeTax"));
totalProfitBeforeTax = totalProfitBeforeTax.add((BigDecimal) data.get("totalProfitBeforeTax"));
}
// 计算汇总数据
@@ -282,122 +280,69 @@ public class ExportServiceImpl implements ExportService {
}
private void createCompanyDetailSheet(Sheet sheet, String dateRangeLabel, List<Map<String, Object>> shopDataList) {
int colCount = shopDataList.size() + 1; // 指标名称列 + 每个店铺一列
CellStyle titleStyle = createTitleStyle(sheet.getWorkbook());
CellStyle headerStyle = createHeaderStyle(sheet.getWorkbook());
CellStyle labelStyle = createLabelStyle(sheet.getWorkbook());
CellStyle dataStyle = createDataStyle(sheet.getWorkbook());
// 标题行
Row titleRow = sheet.createRow(0);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("店铺明细(横向对比) | " + dateRangeLabel);
titleCell.setCellStyle(titleStyle);
sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, colCount - 1));
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[] headers = {
"店铺名称", "目标单量", "单量达成率(%)", "目标额", "销售达成率(%)",
"实际销售额", "单量", "客单价",
"成本运费", "退款金额", "推广费", "刷单费用", "费比",
"税前盈亏", "4%税额", "ROI", "税后盈亏"
};
// 表头:指标名称 | 店铺1 | 店铺2 | ...
Row headerRow = sheet.createRow(1);
Cell firstHeader = headerRow.createCell(0);
firstHeader.setCellValue("指标名称");
firstHeader.setCellStyle(headerStyle);
for (int c = 0; c < shopDataList.size(); c++) {
Cell hCell = headerRow.createCell(c + 1);
hCell.setCellValue((String) shopDataList.get(c).get("shopName"));
hCell.setCellStyle(headerStyle);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerStyle);
}
// 定义所有指标(字段名 -> 标签 -> 取值key -> 是否数值)
List<MetricDef> metrics = Arrays.asList(
new MetricDef("销售额", "salesAmount", true),
new MetricDef("单量", "orderCount", false),
new MetricDef("客单价", "avgOrderValue", true),
new MetricDef("推广费", "promotionFee", true),
new MetricDef("刷单费", "brushFee", true),
new MetricDef("费比", "feeRatio", true, "4"),
new MetricDef("退款金额", "refundAmount", true),
new MetricDef("成本运费", "shippingCost", true),
new MetricDef("税前盈亏", "profitBeforeTax", true),
new MetricDef("税额", "tax", true),
new MetricDef("税后盈亏", "profitAfterTax", true),
new MetricDef("ROI", "roi", true, "4"),
new MetricDef("目标销售额", "targetAmount", true),
new MetricDef("销售额达成率", "salesAchievementRate", true, "2", "%"),
new MetricDef("目标单量", "targetOrderCount", false),
new MetricDef("单量达成率", "orderAchievementRate", true, "2", "%")
);
for (int r = 0; r < metrics.size(); r++) {
MetricDef m = metrics.get(r);
Row row = sheet.createRow(r + 2);
// 第一列:指标名称
Cell labelCell = row.createCell(0);
labelCell.setCellValue(m.label + (m.unit != null ? " (" + m.unit + ")" : ""));
labelCell.setCellStyle(labelStyle);
// 每个店铺的值
for (int c = 0; c < shopDataList.size(); c++) {
Map<String, Object> shop = shopDataList.get(c);
Cell vCell = row.createCell(c + 1);
vCell.setCellStyle(dataStyle);
if (m.isNumeric) {
Object val = shop.get(m.key);
if (val != null) {
double d = 0;
if (val instanceof BigDecimal) {
int scale = m.scale != null ? Integer.parseInt(m.scale) : 2;
d = ((BigDecimal) val).setScale(scale, RoundingMode.HALF_UP).doubleValue();
} else if (val instanceof Integer) {
d = ((Integer) val).doubleValue();
}
vCell.setCellValue(d);
}
} else {
// 非数值类型(如单量、目标单量用整数显示)
Object val = shop.get(m.key);
if (val != null) {
if (val instanceof BigDecimal) {
vCell.setCellValue(((BigDecimal) val).setScale(0, RoundingMode.HALF_UP).doubleValue());
} else if (val instanceof Integer) {
vCell.setCellValue(((Integer) val).doubleValue());
}
}
}
}
int rowIdx = 2;
for (Map<String, Object> data : shopDataList) {
Row row = sheet.createRow(rowIdx++);
setCell(row, 0, (String) data.get("shopName"), dataStyle);
setCell(row, 1, objToDouble(data.get("targetOrderCount")), dataStyle);
setCell(row, 2, objToPct(data.get("orderCountAchievementRate")), dataStyle);
setCell(row, 3, objToDouble(data.get("targetAmount")), dataStyle);
setCell(row, 4, objToPct(data.get("salesAchievementRate")), dataStyle);
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);
}
// 设置列宽
sheet.setColumnWidth(0, 4000);
for (int c = 0; c < shopDataList.size(); c++) {
sheet.setColumnWidth(c + 1, 3200);
for (int i = 0; i < headers.length; i++) {
sheet.setColumnWidth(i, 3200);
}
}
// 内部类:指标定义
private static class MetricDef {
String label;
String key;
boolean isNumeric;
String scale = "2";
String unit;
private double objToDouble(Object val) {
if (val == null) return 0.0;
if (val instanceof BigDecimal) return ((BigDecimal) val).setScale(2, RoundingMode.HALF_UP).doubleValue();
if (val instanceof Integer) return ((Integer) val).doubleValue();
return 0.0;
}
MetricDef(String label, String key, boolean isNumeric) {
this.label = label;
this.key = key;
this.isNumeric = isNumeric;
}
MetricDef(String label, String key, boolean isNumeric, String scale) {
this(label, key, isNumeric);
this.scale = scale;
}
MetricDef(String label, String key, boolean isNumeric, String scale, String unit) {
this(label, key, isNumeric, scale);
this.unit = unit;
}
private double objToPct(Object val) {
if (val == null) return 0.0;
if (val instanceof BigDecimal) return ((BigDecimal) val).setScale(2, RoundingMode.HALF_UP).doubleValue();
if (val instanceof Double) return BigDecimal.valueOf((Double) val).setScale(2, RoundingMode.HALF_UP).doubleValue();
if (val instanceof Integer) return ((Integer) val).doubleValue();
return 0.0;
}
private void createShopSummarySheet(Sheet sheet, String shopName, String dateRangeLabel, Map<String, Object> data) {
@@ -716,19 +661,19 @@ public class ExportServiceImpl implements ExportService {
data.put("salesAmount", salesAmount);
data.put("orderCount", orderCount);
data.put("avgOrderValue", avgOrderValue);
data.put("promotionFee", promotionFee);
data.put("totalPromotionFee", promotionFee); // 对齐 ShopComparison
data.put("brushFee", brushFee);
data.put("feeRatio", feeRatio);
data.put("refundAmount", refundAmount);
data.put("shippingCost", shippingCost);
data.put("profitBeforeTax", profitBeforeTax);
data.put("tax", tax);
data.put("totalProfitBeforeTax", profitBeforeTax); // 对齐 ShopComparison
data.put("salesTaxAmount", tax); // 对齐 ShopComparison
data.put("profitAfterTax", profitAfterTax);
data.put("roi", roi);
data.put("targetAmount", targetAmount);
data.put("targetOrderCount", targetOrderCount);
data.put("salesAchievementRate", salesAchievementRate);
data.put("orderAchievementRate", orderAchievementRate);
data.put("orderCountAchievementRate", orderAchievementRate);
return data;
}