|
|
|
@ -16,9 +16,15 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
|
|
import static hw.tagApi.service.constant.ApiConstants.MAX_RECORDS_PER_EXCEL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 标签Excel工具类
|
|
|
|
@ -27,6 +33,7 @@ import java.util.*;
|
|
|
|
|
public class TagExcelUtil {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(TagExcelUtil.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解析标签数据Excel
|
|
|
|
|
*
|
|
|
|
@ -240,7 +247,7 @@ public class TagExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成标签Excel文件
|
|
|
|
|
* 生成标签Excel文件,支持大数据量分片导出
|
|
|
|
|
*
|
|
|
|
|
* @param records 标签记录列表
|
|
|
|
|
* @param fileName 文件名
|
|
|
|
@ -248,24 +255,12 @@ public class TagExcelUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static String generateTagExcel(List<HwTagRecord> records, String fileName) {
|
|
|
|
|
try {
|
|
|
|
|
// 使用ExcelUtil导出
|
|
|
|
|
ExcelUtil<HwTagRecord> util = new ExcelUtil<>(HwTagRecord.class);
|
|
|
|
|
// 导出Excel并获取结果
|
|
|
|
|
AjaxResult result = util.exportExcel(records, "标签数据");
|
|
|
|
|
if (result != null && HttpStatus.SUCCESS == Integer.parseInt(String.valueOf(result.get(AjaxResult.CODE_TAG)))) {
|
|
|
|
|
String exportFileName = (String) result.get(AjaxResult.MSG_TAG);
|
|
|
|
|
if (exportFileName == null || exportFileName.isEmpty()) {
|
|
|
|
|
throw new RuntimeException("导出文件名不能为空");
|
|
|
|
|
}
|
|
|
|
|
String fileUrl = ApiConstants.BASE_URL + UUID.randomUUID();
|
|
|
|
|
String absoluteFile = ExcelUtil.getAbsoluteFile(exportFileName);
|
|
|
|
|
// 存储文件信息
|
|
|
|
|
saveFileInfo(fileUrl, fileName, absoluteFile);
|
|
|
|
|
log.info("Excel文件生成成功: {}", absoluteFile);
|
|
|
|
|
return fileUrl;
|
|
|
|
|
if (records.size() <= MAX_RECORDS_PER_EXCEL) {
|
|
|
|
|
// 数据量小于等于100,直接导出单个Excel
|
|
|
|
|
return generateSingleExcel(records, fileName);
|
|
|
|
|
} else {
|
|
|
|
|
String errorMsg = result != null ? (String) result.get(AjaxResult.MSG_TAG) : "导出失败";
|
|
|
|
|
throw new RuntimeException("导出Excel失败: " + errorMsg);
|
|
|
|
|
// 数据量大于100,分片导出并压缩
|
|
|
|
|
return generateMultipleExcelAndZip(records, fileName);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成Excel文件失败", e);
|
|
|
|
@ -273,6 +268,88 @@ public class TagExcelUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成单个Excel文件
|
|
|
|
|
*/
|
|
|
|
|
private static String generateSingleExcel(List<HwTagRecord> records, String fileName) {
|
|
|
|
|
ExcelUtil<HwTagRecord> util = new ExcelUtil<>(HwTagRecord.class);
|
|
|
|
|
AjaxResult result = util.exportExcel(records, "团队中心-标签数据总库导出");
|
|
|
|
|
if (result != null && HttpStatus.SUCCESS == Integer.parseInt(String.valueOf(result.get(AjaxResult.CODE_TAG)))) {
|
|
|
|
|
String exportFileName = (String) result.get(AjaxResult.MSG_TAG);
|
|
|
|
|
if (exportFileName == null || exportFileName.isEmpty()) {
|
|
|
|
|
throw new RuntimeException("导出文件名不能为空");
|
|
|
|
|
}
|
|
|
|
|
String fileUrl = ApiConstants.BASE_URL + UUID.randomUUID();
|
|
|
|
|
String absoluteFile = ExcelUtil.getAbsoluteFile(exportFileName);
|
|
|
|
|
saveFileInfo(fileUrl, fileName, absoluteFile);
|
|
|
|
|
log.info("Excel文件生成成功: {}", absoluteFile);
|
|
|
|
|
return fileUrl;
|
|
|
|
|
} else {
|
|
|
|
|
String errorMsg = result != null ? (String) result.get(AjaxResult.MSG_TAG) : "导出失败";
|
|
|
|
|
throw new RuntimeException("导出Excel失败: " + errorMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成多个Excel文件并压缩成zip
|
|
|
|
|
*/
|
|
|
|
|
private static String generateMultipleExcelAndZip(List<HwTagRecord> records, String fileName) {
|
|
|
|
|
try {
|
|
|
|
|
// 计算需要多少个Excel文件
|
|
|
|
|
int totalFiles = (int) Math.ceil((double) records.size() / MAX_RECORDS_PER_EXCEL);
|
|
|
|
|
List<String> excelFiles = new ArrayList<>();
|
|
|
|
|
ExcelUtil<HwTagRecord> util = new ExcelUtil<>(HwTagRecord.class);
|
|
|
|
|
|
|
|
|
|
// 分片生成Excel文件
|
|
|
|
|
for (int i = 0; i < totalFiles; i++) {
|
|
|
|
|
int fromIndex = i * MAX_RECORDS_PER_EXCEL;
|
|
|
|
|
int toIndex = Math.min(fromIndex + MAX_RECORDS_PER_EXCEL, records.size());
|
|
|
|
|
List<HwTagRecord> subList = records.subList(fromIndex, toIndex);
|
|
|
|
|
|
|
|
|
|
// 生成Excel文件
|
|
|
|
|
AjaxResult result = util.exportExcel(subList, "团队中心-标签数据总库导出_" + (i + 1));
|
|
|
|
|
if (result != null && HttpStatus.SUCCESS == Integer.parseInt(String.valueOf(result.get(AjaxResult.CODE_TAG)))) {
|
|
|
|
|
String exportFileName = (String) result.get(AjaxResult.MSG_TAG);
|
|
|
|
|
if (exportFileName != null && !exportFileName.isEmpty()) {
|
|
|
|
|
excelFiles.add(ExcelUtil.getAbsoluteFile(exportFileName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成zip文件名
|
|
|
|
|
String zipFileName = fileName.replace(".xlsx", ".zip");
|
|
|
|
|
String zipFilePath = ExcelUtil.getAbsoluteFile(zipFileName);
|
|
|
|
|
|
|
|
|
|
// 创建zip文件
|
|
|
|
|
try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(Paths.get(zipFilePath)))) {
|
|
|
|
|
for (String excelFile : excelFiles) {
|
|
|
|
|
File fileToZip = new File(excelFile);
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(fileToZip)) {
|
|
|
|
|
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
|
|
|
|
|
zipOut.putNextEntry(zipEntry);
|
|
|
|
|
byte[] bytes = new byte[1024];
|
|
|
|
|
int length;
|
|
|
|
|
while ((length = fis.read(bytes)) >= 0) {
|
|
|
|
|
zipOut.write(bytes, 0, length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 删除临时Excel文件
|
|
|
|
|
fileToZip.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存zip文件信息
|
|
|
|
|
String fileUrl = ApiConstants.BASE_URL + UUID.randomUUID();
|
|
|
|
|
saveFileInfo(fileUrl, zipFileName, zipFilePath);
|
|
|
|
|
log.info("ZIP文件生成成功: {}", zipFilePath);
|
|
|
|
|
return fileUrl;
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成ZIP文件失败", e);
|
|
|
|
|
throw new RuntimeException("生成ZIP文件失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存文件信息到数据库
|
|
|
|
|
*
|
|
|
|
|