⚡ 优化毛利分析订单查询
This commit is contained in:
@@ -10,6 +10,11 @@ oss:
|
||||
|
||||
# 项目相关配置
|
||||
bms:
|
||||
# 毛利报表区域客户 ID 过滤:默认关闭,完成生产计划和耗时对比后再启用。
|
||||
profit-report:
|
||||
customer-id-filter-enabled: ${BMS_PROFIT_REPORT_CUSTOMER_ID_FILTER_ENABLED:false}
|
||||
# 超过此客户数时自动回退客户表关联;小于等于 0 时停用,上限为 5000。
|
||||
max-customer-ids: ${BMS_PROFIT_REPORT_MAX_CUSTOMER_IDS:1000}
|
||||
# 名称
|
||||
name: BMS
|
||||
# 版本
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package com.bms.report;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import com.bms.price.domain.PrcProfitReportExport;
|
||||
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
||||
import com.bms.price.domain.vo.PrcProfitReportProductRow;
|
||||
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||
import com.bms.price.domain.vo.PrcProfitReportSummary;
|
||||
import com.bms.price.mapper.PrcProfitReportMapper;
|
||||
import com.bms.price.service.impl.PrcProfitReportServiceImpl;
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
|
||||
class ProfitReportAreaFilterTest
|
||||
{
|
||||
@AfterEach
|
||||
void clearPage()
|
||||
{
|
||||
PageHelper.clearPage();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customerLookupPreservesPaginationAndUsesOnlyServerResolvedIds()
|
||||
{
|
||||
AtomicReference<List<Long>> ids = new AtomicReference<>(List.of(2L, 3L));
|
||||
AtomicReference<PrcProfitReportQuery> seen = new AtomicReference<>();
|
||||
Page<?> page = PageHelper.startPage(2, 10);
|
||||
PrcProfitReportMapper mapper = mapper(ids, seen, page);
|
||||
PrcProfitReportServiceImpl service = service(mapper, true, 2);
|
||||
PrcProfitReportQuery query = new PrcProfitReportQuery();
|
||||
query.setCustomerArea(" C ");
|
||||
query.setAreaCustomerIds(List.of(999L));
|
||||
|
||||
service.selectOrderRows(query);
|
||||
assertSame(query, seen.get());
|
||||
assertEquals(List.of(2L, 3L), query.getAreaCustomerIds());
|
||||
assertSame(page, PageHelper.getLocalPage());
|
||||
|
||||
ids.set(List.of());
|
||||
service.selectSummary(query);
|
||||
assertEquals(List.of(), query.getAreaCustomerIds());
|
||||
|
||||
ids.set(List.of(1L, 2L, 3L));
|
||||
service.selectProductRows(query);
|
||||
assertNull(query.getAreaCustomerIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customerLookupFailureClearsPageContext()
|
||||
{
|
||||
PrcProfitReportMapper mapper = (PrcProfitReportMapper) Proxy.newProxyInstance(
|
||||
getClass().getClassLoader(), new Class<?>[] { PrcProfitReportMapper.class },
|
||||
(proxy, method, args) -> { throw new IllegalStateException("lookup failed"); });
|
||||
PrcProfitReportServiceImpl service = service(mapper, true, 2);
|
||||
PrcProfitReportQuery query = new PrcProfitReportQuery();
|
||||
query.setCustomerArea("C");
|
||||
PageHelper.startPage(1, 10);
|
||||
assertThrows(IllegalStateException.class, () -> service.selectOrderRows(query));
|
||||
assertNull(PageHelper.getLocalPage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sqlUsesAreaJoinOrCustomerIdsAndEmptyIdsMatchNothing() throws Exception
|
||||
{
|
||||
Configuration config = new Configuration();
|
||||
for (Class<?> type : new Class<?>[] { PrcProfitReportExport.class, PrcProfitReportOrderRow.class,
|
||||
PrcProfitReportProductRow.class, PrcProfitReportSummary.class })
|
||||
{
|
||||
config.getTypeAliasRegistry().registerAlias(type);
|
||||
}
|
||||
String resource = "mapper/price/PrcProfitReportMapper.xml";
|
||||
try (InputStream input = getClass().getClassLoader().getResourceAsStream(resource))
|
||||
{
|
||||
assertNotNull(input);
|
||||
new XMLMapperBuilder(input, config, resource, config.getSqlFragments()).parse();
|
||||
}
|
||||
PrcProfitReportQuery query = new PrcProfitReportQuery();
|
||||
query.setCustomerArea("C");
|
||||
for (String id : List.of("selectOrderRows", "selectProductRows", "selectSummary"))
|
||||
{
|
||||
var statement = config.getMappedStatement("com.bms.price.mapper.PrcProfitReportMapper." + id);
|
||||
query.setAreaCustomerIds(null);
|
||||
String joinSql = statement.getBoundSql(query).getSql();
|
||||
assertTrue(joinSql.contains("join ctr_customer c"));
|
||||
assertTrue(joinSql.contains("c.customer_area = ?"));
|
||||
assertTrue(joinSql.contains("c.del_flag = '0'"));
|
||||
|
||||
query.setAreaCustomerIds(List.of(2L, 3L));
|
||||
String inSql = statement.getBoundSql(query).getSql();
|
||||
assertFalse(inSql.contains("join ctr_customer c"));
|
||||
assertTrue(inSql.contains("p.customer_id in"));
|
||||
|
||||
query.setAreaCustomerIds(List.of());
|
||||
String emptySql = statement.getBoundSql(query).getSql();
|
||||
assertFalse(emptySql.contains("join ctr_customer c"));
|
||||
assertTrue(emptySql.contains("and 1 = 0"));
|
||||
}
|
||||
}
|
||||
|
||||
private PrcProfitReportServiceImpl service(PrcProfitReportMapper mapper, boolean enabled, int max)
|
||||
{
|
||||
PrcProfitReportServiceImpl service = new PrcProfitReportServiceImpl();
|
||||
ReflectionTestUtils.setField(service, "profitReportMapper", mapper);
|
||||
ReflectionTestUtils.setField(service, "customerIdFilterEnabled", enabled);
|
||||
ReflectionTestUtils.setField(service, "maxCustomerIds", max);
|
||||
return service;
|
||||
}
|
||||
|
||||
private PrcProfitReportMapper mapper(AtomicReference<List<Long>> ids,
|
||||
AtomicReference<PrcProfitReportQuery> seen, Page<?> page)
|
||||
{
|
||||
return (PrcProfitReportMapper) Proxy.newProxyInstance(getClass().getClassLoader(),
|
||||
new Class<?>[] { PrcProfitReportMapper.class }, (proxy, method, args) -> {
|
||||
if (method.getName().equals("selectAreaCustomerIds"))
|
||||
{
|
||||
assertNull(PageHelper.getLocalPage());
|
||||
assertEquals("C", args[0]);
|
||||
assertEquals(3, args[1]);
|
||||
return ids.get();
|
||||
}
|
||||
assertSame(page, PageHelper.getLocalPage());
|
||||
seen.set((PrcProfitReportQuery) args[0]);
|
||||
return List.of();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user