✨ 毛利分析报表导出接入 OSS 存储

This commit is contained in:
caojia
2026-10-16 17:09:34 +08:00
parent 9e391888f5
commit eb67ccc3cb
4 changed files with 218 additions and 22 deletions
@@ -1,5 +1,7 @@
package com.bms.web.controller.report;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -23,6 +25,9 @@ import com.bms.common.core.page.TableDataInfo;
import com.bms.common.enums.BusinessType;
import com.bms.common.exception.ServiceException;
import com.bms.common.utils.file.FileUtils;
import com.bms.common.utils.file.OssStorage;
import com.bms.common.utils.DateUtils;
import com.bms.common.utils.uuid.IdUtils;
import com.bms.common.utils.poi.ExcelUtil;
// 提成功能暂停使用:import com.bms.price.domain.PrcProfitCommissionRate;
import com.bms.price.domain.PrcProfitReportExport;
@@ -40,6 +45,9 @@ public class PrcProfitReportController extends BaseController
@Autowired
private IPrcProfitReportService profitReportService;
@Autowired
private OssStorage ossStorage;
@PreAuthorize("@ss.hasPermi('report:profit:list')")
@GetMapping("/summary")
public AjaxResult summary(PrcProfitReportQuery query)
@@ -100,9 +108,7 @@ public class PrcProfitReportController extends BaseController
try
{
List<PrcProfitReportOrderRow> rows = profitReportService.selectOrderRows(query);
AjaxResult file = new ExcelUtil<PrcProfitReportOrderRow>(PrcProfitReportOrderRow.class)
.exportExcel(rows, "订单毛利报表");
return finishExport(export, rows.size(), file);
return finishExport(export, rows, PrcProfitReportOrderRow.class, "订单毛利报表");
}
catch (RuntimeException e)
{
@@ -120,9 +126,7 @@ public class PrcProfitReportController extends BaseController
try
{
List<PrcProfitReportProductRow> rows = profitReportService.selectProductRows(query);
AjaxResult file = new ExcelUtil<PrcProfitReportProductRow>(PrcProfitReportProductRow.class)
.exportExcel(rows, "产品毛利报表");
return finishExport(export, rows.size(), file);
return finishExport(export, rows, PrcProfitReportProductRow.class, "产品毛利报表");
}
catch (RuntimeException e)
{
@@ -145,11 +149,22 @@ public class PrcProfitReportController extends BaseController
{
PrcProfitReportExport export = profitReportService.selectExport(exportId, getUsername());
if (export == null || !"SUCCESS".equals(export.getExportStatus())
|| export.getStorageKey() == null || !FileUtils.checkAllowDownload(export.getStorageKey()))
|| export.getStorageKey() == null || export.getStorageKey().isBlank())
{
throw new ServiceException("导出文件不存在或尚未生成成功");
}
if (export.getStorageKey().startsWith("oss://"))
{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
FileUtils.setAttachmentResponseHeader(response, export.getFileName());
ossStorage.download(export.getStorageKey(), response.getOutputStream());
return;
}
if (!FileUtils.checkAllowDownload(export.getStorageKey()))
{
throw new ServiceException("导出文件路径非法");
}
Path downloadRoot = Paths.get(BmsConfig.getDownloadPath()).toAbsolutePath().normalize();
Path filePath = downloadRoot.resolve(export.getStorageKey()).normalize();
if (!filePath.startsWith(downloadRoot) || !Files.isRegularFile(filePath))
@@ -175,21 +190,47 @@ public class PrcProfitReportController extends BaseController
return export;
}
private AjaxResult finishExport(PrcProfitReportExport export, int rowCount, AjaxResult file)
private <T> AjaxResult finishExport(PrcProfitReportExport export, List<T> rows, Class<T> rowType, String sheetName)
{
Object storageKey = file.get("msg");
if (storageKey == null || String.valueOf(storageKey).isBlank())
Path temp = null;
try
{
throw new ServiceException("导出文件生成失败");
temp = Files.createTempFile("profit-report-", ".xlsx");
try (OutputStream output = Files.newOutputStream(temp))
{
new ExcelUtil<T>(rowType).exportExcelToStream(rows, sheetName, output);
}
String objectKey = "report/profit/" + DateUtils.datePath() + "/" + IdUtils.fastSimpleUUID() + ".xlsx";
export.setStorageKey(ossStorage.upload(temp, objectKey,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
export.setRowCount(rows.size());
export.setExportStatus("SUCCESS");
if (profitReportService.completeExport(export) != 1)
{
ossStorage.delete(export.getStorageKey());
throw new ServiceException("保存导出日志失败");
}
return success(export);
}
export.setStorageKey(String.valueOf(storageKey));
export.setRowCount(rowCount);
export.setExportStatus("SUCCESS");
if (profitReportService.completeExport(export) != 1)
catch (IOException e)
{
throw new ServiceException("保存导出日志失败");
logger.error("毛利报表生成或上传失败,exportId={}", export.getExportId(), e);
throw new ServiceException("毛利报表生成或上传失败,请重新导出");
}
finally
{
if (temp != null)
{
try
{
Files.deleteIfExists(temp);
}
catch (IOException e)
{
logger.warn("毛利报表临时文件清理失败,exportId={}", export.getExportId(), e);
}
}
}
return success(export);
}
private void failExport(PrcProfitReportExport export, RuntimeException e)