Merge branch 'test'

This commit is contained in:
caojia
2026-11-17 16:20:11 +08:00
8 changed files with 188 additions and 24 deletions
@@ -104,6 +104,12 @@ class ProfitReportAreaFilterTest
String emptySql = statement.getBoundSql(query).getSql();
assertFalse(emptySql.contains("join ctr_customer c"));
assertTrue(emptySql.contains("and 1 = 0"));
query.setAreaCustomerIds(null);
query.setProductKeyword("EXP");
String productSql = statement.getBoundSql(query).getSql();
assertTrue(productSql.contains("p.product_code like concat('%', ?, '%')"));
assertTrue(productSql.contains("p.product_name like concat('%', ?, '%')"));
}
}
@@ -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(),
@@ -111,11 +111,12 @@ public class ExcelUtil<T>
*/
private Map<String, CellStyle> cellStyleCache = new HashMap<String, CellStyle>();
/**
* Excel sheet最大行数,默认65536
*/
/** 通用导出的默认每个工作表数据行数;单工作表 XLSX 导出使用 XLSX 实际上限。 */
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 文件,返回文件名。 */
public AjaxResult exportExcel()
{
@@ -824,9 +847,14 @@ public class ExcelUtil<T>
* 创建写入数据到Sheet
*/
public void writeSheet()
{
writeSheet(sheetSize);
}
private void writeSheet(int rowsPerSheet)
{
// 取出一共有多少个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++)
{
createSheet(sheetNo, index);
@@ -855,7 +883,7 @@ public class ExcelUtil<T>
}
if (Type.EXPORT.equals(type))
{
fillExcelData(index);
fillExcelData(index, rowsPerSheet);
addStatisticsRow();
}
}
@@ -869,8 +897,13 @@ public class ExcelUtil<T>
@SuppressWarnings("unchecked")
public void fillExcelData(int index)
{
int startNo = index * sheetSize;
int endNo = Math.min(startNo + sheetSize, list.size());
fillExcelData(index, sheetSize);
}
private void fillExcelData(int index, int rowsPerSheet)
{
int startNo = index * rowsPerSheet;
int endNo = Math.min(startNo + rowsPerSheet, list.size());
int currentRowNum = rownum + 1; // 从标题行后开始
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;
}
/**
* 获取子列表最大数
*/
@@ -37,6 +37,8 @@ public class PrcProfitReportQuery extends BaseEntity
/** 客户名称拆分后的多值查询条件。 */
private List<String> customerNames;
private String belongSalerCode;
/** 产品代码或产品名称的统一模糊查询关键字。 */
private String productKeyword;
private String productCode;
private String productName;
private String countryCode;
@@ -171,6 +171,10 @@ public class PrcProfitReportServiceImpl implements IPrcProfitReportService
}
query.setCustomerArea(query.getCustomerArea().trim());
query.setProfitRule(rule.name());
if (query.getBelongSalerCode() != null)
{
query.setBelongSalerCode(query.getBelongSalerCode().trim());
}
query.setOrderNos(splitFilterValues(query.getOrderNo(), "订单号"));
query.setCustomerCodes(splitFilterValues(query.getCustomerCode(), "客户代码"));
query.setCustomerNames(splitFilterValues(query.getCustomerName(), "客户名称"));
@@ -85,6 +85,12 @@ public class PrcProfitReportExportRunner
put(taskId, userId, "RUNNING", 5, "正在查询订单", null, 0, fileName, null);
List<PrcProfitReportOrderRow> rows = reportService.selectOrderRows(query);
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);
if (ProfitAreaRule.GUANGZHOU.name().equals(query.getProfitRule()))
{
@@ -95,7 +101,7 @@ public class PrcProfitReportExportRunner
file = dir.resolve(taskId + ".xlsx").normalize();
try (OutputStream output = Files.newOutputStream(file))
{
util.exportExcelToStream(rows, "订单毛利报表", output);
util.exportExcelToSingleSheetStream(rows, "订单毛利报表", output);
}
put(taskId, userId, "SUCCESS", 100, "导出完成,正在下载", null, rows.size(), fileName,
REL_DIR + "/" + taskId + ".xlsx");
@@ -197,12 +197,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
and p.country_code &lt;=&gt; #{countryCode}
and i.currency &lt;=&gt; #{currency}
<if test="belongSalerCode != null and belongSalerCode != ''">
and i.belong_saler_code = #{belongSalerCode}
and (i.belong_saler_code = #{belongSalerCode}
or p.belong_saler_code = #{belongSalerCode}
or i.belong_saler like concat('%', #{belongSalerCode}, '%')
or p.belong_saler like concat('%', #{belongSalerCode}, '%'))
</if>
</when>
<otherwise>
<if test="belongSalerCode != null and belongSalerCode != ''">
and i.belong_saler_code = #{belongSalerCode}
and (i.belong_saler_code = #{belongSalerCode}
or p.belong_saler_code = #{belongSalerCode}
or i.belong_saler like concat('%', #{belongSalerCode}, '%')
or p.belong_saler like concat('%', #{belongSalerCode}, '%'))
</if>
<if test="productCode != null and productCode != ''">
and p.product_code = #{productCode}
@@ -215,6 +221,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</if>
</otherwise>
</choose>
<if test="productKeyword != null and productKeyword != ''">
and (p.product_code like concat('%', #{productKeyword}, '%')
or p.product_name like concat('%', #{productKeyword}, '%'))
</if>
<if test="productName != null and productName != ''">
and p.product_name like concat('%', #{productName}, '%')
</if>
+7 -14
View File
@@ -16,7 +16,7 @@
</div>
</Teleport>
<el-card shadow="never" class="search-card">
<el-form :model="query" label-width="88px" @submit.prevent>
<el-form :model="query" label-width="120px" @submit.prevent>
<el-row :gutter="16">
<el-col :xs="24" :sm="12" :lg="8">
<el-form-item label="客户区域" class="filter-item">
@@ -52,18 +52,13 @@
</el-form-item>
</el-col>
<el-col :xs="24" :sm="12" :lg="8">
<el-form-item label="业务员编码" class="filter-item">
<el-input v-model="query.belongSalerCode" clearable placeholder="输入业务员编码" class="filter-control" @keyup.enter="handleSearch" />
<el-form-item label="业务员编码/姓名" class="filter-item">
<el-input v-model="query.belongSalerCode" clearable placeholder="输入业务员编码或姓名" class="filter-control" @keyup.enter="handleSearch" />
</el-form-item>
</el-col>
<el-col :xs="24" :sm="12" :lg="8">
<el-form-item label="产品代码" class="filter-item">
<el-input v-model="query.productCode" clearable placeholder="输入产品代码" class="filter-control" @keyup.enter="handleSearch" />
</el-form-item>
</el-col>
<el-col :xs="24" :sm="12" :lg="8">
<el-form-item label="产品名称" class="filter-item">
<el-input v-model="query.productName" clearable placeholder="输入产品名称" class="filter-control" @keyup.enter="handleSearch" />
<el-form-item label="产品代码/名称" class="filter-item">
<el-input v-model="query.productKeyword" clearable placeholder="输入产品代码或名称" class="filter-control" @keyup.enter="handleSearch" />
</el-form-item>
</el-col>
<el-col :xs="24" :sm="12" :lg="8">
@@ -282,8 +277,7 @@ const query = reactive({
orderNo: '',
customerCode: '',
belongSalerCode: '',
productCode: '',
productName: '',
productKeyword: '',
countryName: '',
countryCode: '',
currency: ''
@@ -599,8 +593,7 @@ watch(() => [
query.orderNo,
query.customerCode,
query.belongSalerCode,
query.productCode,
query.productName,
query.productKeyword,
query.countryName,
query.countryCode,
query.currency,