✨ 毛利分析订单导出合并为单个工作表
This commit is contained in:
@@ -9,6 +9,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -16,6 +17,9 @@ import java.util.TimeZone;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
import javax.xml.stream.XMLInputFactory;
|
||||||
|
import javax.xml.stream.XMLStreamReader;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
@@ -30,6 +34,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|||||||
import com.bms.common.config.BmsConfig;
|
import com.bms.common.config.BmsConfig;
|
||||||
import com.bms.common.core.redis.RedisCache;
|
import com.bms.common.core.redis.RedisCache;
|
||||||
import com.bms.common.exception.ServiceException;
|
import com.bms.common.exception.ServiceException;
|
||||||
|
import com.bms.common.utils.poi.ExcelUtil;
|
||||||
import com.bms.price.domain.PrcProfitReportExportTask;
|
import com.bms.price.domain.PrcProfitReportExportTask;
|
||||||
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
||||||
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||||
@@ -139,6 +144,41 @@ class ProfitReportAsyncExportTest
|
|||||||
assertFalse(Files.exists(tempDir.resolve("download/profitReportExport/" + failedId + ".xlsx")));
|
assertFalse(Files.exists(tempDir.resolve("download/profitReportExport/" + failedId + ".xlsx")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void exportsMoreThanLegacySheetLimitIntoOneSheet() throws Exception
|
||||||
|
{
|
||||||
|
int rowCount = ExcelUtil.sheetSize + 1;
|
||||||
|
List<PrcProfitReportOrderRow> rows = Collections.nCopies(rowCount, row("ORDER-LARGE"));
|
||||||
|
HoldingExecutor executor = new HoldingExecutor();
|
||||||
|
PrcProfitReportExportRunner runner = new PrcProfitReportExportRunner(service(query -> rows),
|
||||||
|
new MemoryRedis(), executor);
|
||||||
|
String taskId = runner.submit(query("C"), 1L);
|
||||||
|
executor.work.run();
|
||||||
|
|
||||||
|
PrcProfitReportExportTask task = runner.getTask(taskId, 1L);
|
||||||
|
assertEquals("SUCCESS", task.getStatus());
|
||||||
|
assertEquals(rowCount, task.getRowCount());
|
||||||
|
Path file = tempDir.resolve("download").resolve(task.getStorageKey());
|
||||||
|
assertEquals(1, countWorkbookSheets(file));
|
||||||
|
assertEquals(rowCount + 1, lastWorksheetRowNumber(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsRowsBeyondTheSingleSheetLimit()
|
||||||
|
{
|
||||||
|
List<PrcProfitReportOrderRow> rows = Collections.nCopies(ExcelUtil.XLSX_MAX_ROW_COUNT, row("ORDER-LARGE"));
|
||||||
|
HoldingExecutor executor = new HoldingExecutor();
|
||||||
|
PrcProfitReportExportRunner runner = new PrcProfitReportExportRunner(service(query -> rows),
|
||||||
|
new MemoryRedis(), executor);
|
||||||
|
String taskId = runner.submit(query("C"), 1L);
|
||||||
|
executor.work.run();
|
||||||
|
|
||||||
|
PrcProfitReportExportTask task = runner.getTask(taskId, 1L);
|
||||||
|
assertEquals("FAIL", task.getStatus());
|
||||||
|
assertTrue(task.getError().contains("单个工作表最多支持1048575条订单"));
|
||||||
|
assertFalse(Files.exists(tempDir.resolve("download/profitReportExport/" + taskId + ".xlsx")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cleanupRemovesOnlyExpiredExportFiles() throws Exception
|
void cleanupRemovesOnlyExpiredExportFiles() throws Exception
|
||||||
{
|
{
|
||||||
@@ -195,6 +235,64 @@ class ProfitReportAsyncExportTest
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int countWorkbookSheets(Path file) throws Exception
|
||||||
|
{
|
||||||
|
try (ZipFile workbook = new ZipFile(file.toFile());
|
||||||
|
InputStream input = workbook.getInputStream(workbook.getEntry("xl/workbook.xml")))
|
||||||
|
{
|
||||||
|
XMLStreamReader reader = xmlInputFactory().createXMLStreamReader(input);
|
||||||
|
int count = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (reader.hasNext())
|
||||||
|
{
|
||||||
|
if (reader.next() == XMLStreamReader.START_ELEMENT && "sheet".equals(reader.getLocalName()))
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int lastWorksheetRowNumber(Path file) throws Exception
|
||||||
|
{
|
||||||
|
try (ZipFile workbook = new ZipFile(file.toFile());
|
||||||
|
InputStream input = workbook.getInputStream(workbook.getEntry("xl/worksheets/sheet1.xml")))
|
||||||
|
{
|
||||||
|
XMLStreamReader reader = xmlInputFactory().createXMLStreamReader(input);
|
||||||
|
int lastRowNumber = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (reader.hasNext())
|
||||||
|
{
|
||||||
|
if (reader.next() == XMLStreamReader.START_ELEMENT && "row".equals(reader.getLocalName()))
|
||||||
|
{
|
||||||
|
lastRowNumber = Integer.parseInt(reader.getAttributeValue(null, "r"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
return lastRowNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static XMLInputFactory xmlInputFactory()
|
||||||
|
{
|
||||||
|
XMLInputFactory factory = XMLInputFactory.newFactory();
|
||||||
|
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
|
||||||
|
factory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
private static IPrcProfitReportService service(Function<PrcProfitReportQuery, List<PrcProfitReportOrderRow>> query)
|
private static IPrcProfitReportService service(Function<PrcProfitReportQuery, List<PrcProfitReportOrderRow>> query)
|
||||||
{
|
{
|
||||||
return (IPrcProfitReportService) Proxy.newProxyInstance(ProfitReportAsyncExportTest.class.getClassLoader(),
|
return (IPrcProfitReportService) Proxy.newProxyInstance(ProfitReportAsyncExportTest.class.getClassLoader(),
|
||||||
|
|||||||
@@ -111,11 +111,12 @@ public class ExcelUtil<T>
|
|||||||
*/
|
*/
|
||||||
private Map<String, CellStyle> cellStyleCache = new HashMap<String, CellStyle>();
|
private Map<String, CellStyle> cellStyleCache = new HashMap<String, CellStyle>();
|
||||||
|
|
||||||
/**
|
/** 通用导出的默认每个工作表数据行数;单工作表 XLSX 导出使用 XLSX 实际上限。 */
|
||||||
* Excel sheet最大行数,默认65536
|
|
||||||
*/
|
|
||||||
public static final int sheetSize = 65536;
|
public static final int sheetSize = 65536;
|
||||||
|
|
||||||
|
/** XLSX 工作表的最大行数。 */
|
||||||
|
public static final int XLSX_MAX_ROW_COUNT = 1_048_576;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工作表名称
|
* 工作表名称
|
||||||
*/
|
*/
|
||||||
@@ -796,6 +797,28 @@ public class ExcelUtil<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 写入单个 XLSX 工作表;超出 Excel 行数上限时返回明确错误。 */
|
||||||
|
public void exportExcelToSingleSheetStream(List<T> list, String sheetName, OutputStream output) throws IOException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.init(list, sheetName, StringUtils.EMPTY, Type.EXPORT);
|
||||||
|
int reservedRows = rownum + 1 + (hasStatisticsColumns() ? 1 : 0);
|
||||||
|
int maxDataRows = XLSX_MAX_ROW_COUNT - reservedRows;
|
||||||
|
if (this.list.size() > maxDataRows)
|
||||||
|
{
|
||||||
|
throw new IOException(StringUtils.format("单个工作表最多支持{}条数据,当前{}条,请缩小查询范围",
|
||||||
|
maxDataRows, this.list.size()));
|
||||||
|
}
|
||||||
|
writeSheet(maxDataRows);
|
||||||
|
wb.write(output);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IOUtils.closeQuietly(wb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** 对 list 数据源生成本地 Excel 文件,返回文件名。 */
|
/** 对 list 数据源生成本地 Excel 文件,返回文件名。 */
|
||||||
public AjaxResult exportExcel()
|
public AjaxResult exportExcel()
|
||||||
{
|
{
|
||||||
@@ -824,9 +847,14 @@ public class ExcelUtil<T>
|
|||||||
* 创建写入数据到Sheet
|
* 创建写入数据到Sheet
|
||||||
*/
|
*/
|
||||||
public void writeSheet()
|
public void writeSheet()
|
||||||
|
{
|
||||||
|
writeSheet(sheetSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeSheet(int rowsPerSheet)
|
||||||
{
|
{
|
||||||
// 取出一共有多少个sheet.
|
// 取出一共有多少个sheet.
|
||||||
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize));
|
int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / rowsPerSheet));
|
||||||
for (int index = 0; index < sheetNo; index++)
|
for (int index = 0; index < sheetNo; index++)
|
||||||
{
|
{
|
||||||
createSheet(sheetNo, index);
|
createSheet(sheetNo, index);
|
||||||
@@ -855,7 +883,7 @@ public class ExcelUtil<T>
|
|||||||
}
|
}
|
||||||
if (Type.EXPORT.equals(type))
|
if (Type.EXPORT.equals(type))
|
||||||
{
|
{
|
||||||
fillExcelData(index);
|
fillExcelData(index, rowsPerSheet);
|
||||||
addStatisticsRow();
|
addStatisticsRow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -869,8 +897,13 @@ public class ExcelUtil<T>
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void fillExcelData(int index)
|
public void fillExcelData(int index)
|
||||||
{
|
{
|
||||||
int startNo = index * sheetSize;
|
fillExcelData(index, sheetSize);
|
||||||
int endNo = Math.min(startNo + sheetSize, list.size());
|
}
|
||||||
|
|
||||||
|
private void fillExcelData(int index, int rowsPerSheet)
|
||||||
|
{
|
||||||
|
int startNo = index * rowsPerSheet;
|
||||||
|
int endNo = Math.min(startNo + rowsPerSheet, list.size());
|
||||||
int currentRowNum = rownum + 1; // 从标题行后开始
|
int currentRowNum = rownum + 1; // 从标题行后开始
|
||||||
|
|
||||||
for (int i = startNo; i < endNo; i++)
|
for (int i = startNo; i < endNo; i++)
|
||||||
@@ -931,6 +964,18 @@ public class ExcelUtil<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasStatisticsColumns()
|
||||||
|
{
|
||||||
|
for (Object[] field : fields)
|
||||||
|
{
|
||||||
|
if (((Excel) field[1]).isStatistics())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取子列表最大数
|
* 获取子列表最大数
|
||||||
*/
|
*/
|
||||||
|
|||||||
+7
-1
@@ -85,6 +85,12 @@ public class PrcProfitReportExportRunner
|
|||||||
put(taskId, userId, "RUNNING", 5, "正在查询订单", null, 0, fileName, null);
|
put(taskId, userId, "RUNNING", 5, "正在查询订单", null, 0, fileName, null);
|
||||||
List<PrcProfitReportOrderRow> rows = reportService.selectOrderRows(query);
|
List<PrcProfitReportOrderRow> rows = reportService.selectOrderRows(query);
|
||||||
put(taskId, userId, "RUNNING", 40, "正在生成 Excel", null, rows.size(), fileName, null);
|
put(taskId, userId, "RUNNING", 40, "正在生成 Excel", null, rows.size(), fileName, null);
|
||||||
|
int maxRows = ExcelUtil.XLSX_MAX_ROW_COUNT - 1;
|
||||||
|
if (rows.size() > maxRows)
|
||||||
|
{
|
||||||
|
throw new ServiceException(String.format("单个工作表最多支持%d条订单,当前%d条,请缩小查询范围",
|
||||||
|
maxRows, rows.size()));
|
||||||
|
}
|
||||||
ExcelUtil<PrcProfitReportOrderRow> util = new ExcelUtil<>(PrcProfitReportOrderRow.class);
|
ExcelUtil<PrcProfitReportOrderRow> util = new ExcelUtil<>(PrcProfitReportOrderRow.class);
|
||||||
if (ProfitAreaRule.GUANGZHOU.name().equals(query.getProfitRule()))
|
if (ProfitAreaRule.GUANGZHOU.name().equals(query.getProfitRule()))
|
||||||
{
|
{
|
||||||
@@ -95,7 +101,7 @@ public class PrcProfitReportExportRunner
|
|||||||
file = dir.resolve(taskId + ".xlsx").normalize();
|
file = dir.resolve(taskId + ".xlsx").normalize();
|
||||||
try (OutputStream output = Files.newOutputStream(file))
|
try (OutputStream output = Files.newOutputStream(file))
|
||||||
{
|
{
|
||||||
util.exportExcelToStream(rows, "订单毛利报表", output);
|
util.exportExcelToSingleSheetStream(rows, "订单毛利报表", output);
|
||||||
}
|
}
|
||||||
put(taskId, userId, "SUCCESS", 100, "导出完成,正在下载", null, rows.size(), fileName,
|
put(taskId, userId, "SUCCESS", 100, "导出完成,正在下载", null, rows.size(), fileName,
|
||||||
REL_DIR + "/" + taskId + ".xlsx");
|
REL_DIR + "/" + taskId + ".xlsx");
|
||||||
|
|||||||
Reference in New Issue
Block a user