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
+1
View File
@@ -36,6 +36,7 @@
## 阶段四:功能补充 ## 阶段四:功能补充
- [x] 4.1 公司概览 Dashboard 页面 ✅ 已存在,添加导出按钮完成 2026-03-26 - [x] 4.1 公司概览 Dashboard 页面 ✅ 已存在,添加导出按钮完成 2026-03-26
- [x] 公司概览导出改为行级复选框选择 ✅ 2026-03-26
- [x] 4.2 操作日志表 + AOP 记录 ✅ 2026-03-26 @OperLog 注解 + AOP 切面 + SQL 脚本已提交 - [x] 4.2 操作日志表 + AOP 记录 ✅ 2026-03-26 @OperLog 注解 + AOP 切面 + SQL 脚本已提交
- [x] 5.5 后端单元测试 - 暂跳过,代码已有基础保障 - [x] 5.5 后端单元测试 - 暂跳过,代码已有基础保障
@@ -3,8 +3,6 @@ package top.xyquan.ecomdmbbackend.modules.ecom.overview.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import java.util.Arrays;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException; import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
@@ -101,9 +99,9 @@ public class ExportServiceImpl implements ExportService {
totalSales = totalSales.add((BigDecimal) data.get("salesAmount")); totalSales = totalSales.add((BigDecimal) data.get("salesAmount"));
totalOrders = totalOrders.add(new BigDecimal((Integer) data.get("orderCount"))); 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")); 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) { 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); Row titleRow = sheet.createRow(0);
Cell titleCell = titleRow.createCell(0); Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("店铺明细(横向对比) | " + dateRangeLabel); titleCell.setCellValue("店铺明细 | " + dateRangeLabel);
titleCell.setCellStyle(titleStyle); titleCell.setCellStyle(createTitleStyle(sheet.getWorkbook()));
sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(0, 0, 0, colCount - 1)); 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); Row headerRow = sheet.createRow(1);
Cell firstHeader = headerRow.createCell(0); for (int i = 0; i < headers.length; i++) {
firstHeader.setCellValue("指标名称"); Cell cell = headerRow.createCell(i);
firstHeader.setCellStyle(headerStyle); cell.setCellValue(headers[i]);
cell.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);
} }
// 定义所有指标(字段名 -> 标签 -> 取值key -> 是否数值) int rowIdx = 2;
List<MetricDef> metrics = Arrays.asList( for (Map<String, Object> data : shopDataList) {
new MetricDef("销售额", "salesAmount", true), Row row = sheet.createRow(rowIdx++);
new MetricDef("单量", "orderCount", false), setCell(row, 0, (String) data.get("shopName"), dataStyle);
new MetricDef("客单价", "avgOrderValue", true), setCell(row, 1, objToDouble(data.get("targetOrderCount")), dataStyle);
new MetricDef("推广费", "promotionFee", true), setCell(row, 2, objToPct(data.get("orderCountAchievementRate")), dataStyle);
new MetricDef("刷单费", "brushFee", true), setCell(row, 3, objToDouble(data.get("targetAmount")), dataStyle);
new MetricDef("费比", "feeRatio", true, "4"), setCell(row, 4, objToPct(data.get("salesAchievementRate")), dataStyle);
new MetricDef("退款金额", "refundAmount", true), setCell(row, 5, objToDouble(data.get("salesAmount")), dataStyle);
new MetricDef("成本运费", "shippingCost", true), setCell(row, 6, objToDouble(data.get("orderCount")), dataStyle);
new MetricDef("税前盈亏", "profitBeforeTax", true), setCell(row, 7, objToDouble(data.get("avgOrderValue")), dataStyle);
new MetricDef("税额", "tax", true), setCell(row, 8, objToDouble(data.get("shippingCost")), dataStyle);
new MetricDef("税后盈亏", "profitAfterTax", true), setCell(row, 9, objToDouble(data.get("refundAmount")), dataStyle);
new MetricDef("ROI", "roi", true, "4"), setCell(row, 10, objToDouble(data.get("totalPromotionFee")), dataStyle);
new MetricDef("目标销售额", "targetAmount", true), setCell(row, 11, objToDouble(data.get("brushFee")), dataStyle);
new MetricDef("销售额达成率", "salesAchievementRate", true, "2", "%"), setCell(row, 12, objToDouble(data.get("feeRatio")), dataStyle);
new MetricDef("目标单量", "targetOrderCount", false), setCell(row, 13, objToDouble(data.get("profitBeforeTax")), dataStyle);
new MetricDef("单量达成率", "orderAchievementRate", true, "2", "%") setCell(row, 14, objToDouble(data.get("salesTaxAmount")), dataStyle);
); setCell(row, 15, objToDouble(data.get("roi")), dataStyle);
setCell(row, 16, objToDouble(data.get("profitAfterTax")), dataStyle);
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());
}
}
}
}
} }
// 设置列宽 for (int i = 0; i < headers.length; i++) {
sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(i, 3200);
for (int c = 0; c < shopDataList.size(); c++) {
sheet.setColumnWidth(c + 1, 3200);
} }
} }
// 内部类:指标定义 private double objToDouble(Object val) {
private static class MetricDef { if (val == null) return 0.0;
String label; if (val instanceof BigDecimal) return ((BigDecimal) val).setScale(2, RoundingMode.HALF_UP).doubleValue();
String key; if (val instanceof Integer) return ((Integer) val).doubleValue();
boolean isNumeric; return 0.0;
String scale = "2"; }
String unit;
MetricDef(String label, String key, boolean isNumeric) { private double objToPct(Object val) {
this.label = label; if (val == null) return 0.0;
this.key = key; if (val instanceof BigDecimal) return ((BigDecimal) val).setScale(2, RoundingMode.HALF_UP).doubleValue();
this.isNumeric = isNumeric; 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;
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 void createShopSummarySheet(Sheet sheet, String shopName, String dateRangeLabel, Map<String, Object> data) { 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("salesAmount", salesAmount);
data.put("orderCount", orderCount); data.put("orderCount", orderCount);
data.put("avgOrderValue", avgOrderValue); data.put("avgOrderValue", avgOrderValue);
data.put("promotionFee", promotionFee); data.put("totalPromotionFee", promotionFee); // 对齐 ShopComparison
data.put("brushFee", brushFee); data.put("brushFee", brushFee);
data.put("feeRatio", feeRatio); data.put("feeRatio", feeRatio);
data.put("refundAmount", refundAmount); data.put("refundAmount", refundAmount);
data.put("shippingCost", shippingCost); data.put("shippingCost", shippingCost);
data.put("profitBeforeTax", profitBeforeTax); data.put("totalProfitBeforeTax", profitBeforeTax); // 对齐 ShopComparison
data.put("tax", tax); data.put("salesTaxAmount", tax); // 对齐 ShopComparison
data.put("profitAfterTax", profitAfterTax); data.put("profitAfterTax", profitAfterTax);
data.put("roi", roi); data.put("roi", roi);
data.put("targetAmount", targetAmount); data.put("targetAmount", targetAmount);
data.put("targetOrderCount", targetOrderCount); data.put("targetOrderCount", targetOrderCount);
data.put("salesAchievementRate", salesAchievementRate); data.put("salesAchievementRate", salesAchievementRate);
data.put("orderAchievementRate", orderAchievementRate); data.put("orderCountAchievementRate", orderAchievementRate);
return data; return data;
} }