✨ 毛利分析订单导出合并为单个工作表
This commit is contained in:
@@ -9,6 +9,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -16,6 +17,9 @@ import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
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 org.apache.poi.ss.usermodel.Row;
|
||||
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.core.redis.RedisCache;
|
||||
import com.bms.common.exception.ServiceException;
|
||||
import com.bms.common.utils.poi.ExcelUtil;
|
||||
import com.bms.price.domain.PrcProfitReportExportTask;
|
||||
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
||||
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||
@@ -139,6 +144,41 @@ class ProfitReportAsyncExportTest
|
||||
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
|
||||
void cleanupRemovesOnlyExpiredExportFiles() throws Exception
|
||||
{
|
||||
@@ -195,6 +235,64 @@ class ProfitReportAsyncExportTest
|
||||
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)
|
||||
{
|
||||
return (IPrcProfitReportService) Proxy.newProxyInstance(ProfitReportAsyncExportTest.class.getClassLoader(),
|
||||
|
||||
Reference in New Issue
Block a user