diff --git a/bms-back/bms-admin/src/main/resources/application.yml b/bms-back/bms-admin/src/main/resources/application.yml index 71489d73..e4668fe9 100644 --- a/bms-back/bms-admin/src/main/resources/application.yml +++ b/bms-back/bms-admin/src/main/resources/application.yml @@ -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 # 版本 diff --git a/bms-back/bms-admin/src/test/java/com/bms/report/ProfitReportAreaFilterTest.java b/bms-back/bms-admin/src/test/java/com/bms/report/ProfitReportAreaFilterTest.java new file mode 100644 index 00000000..fbbfeadd --- /dev/null +++ b/bms-back/bms-admin/src/test/java/com/bms/report/ProfitReportAreaFilterTest.java @@ -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> ids = new AtomicReference<>(List.of(2L, 3L)); + AtomicReference 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> ids, + AtomicReference 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(); + }); + } +} diff --git a/bms-back/bms-system/src/main/java/com/bms/price/domain/vo/PrcProfitReportQuery.java b/bms-back/bms-system/src/main/java/com/bms/price/domain/vo/PrcProfitReportQuery.java index 651bd1aa..b27caf1c 100644 --- a/bms-back/bms-system/src/main/java/com/bms/price/domain/vo/PrcProfitReportQuery.java +++ b/bms-back/bms-system/src/main/java/com/bms/price/domain/vo/PrcProfitReportQuery.java @@ -26,6 +26,8 @@ public class PrcProfitReportQuery extends BaseEntity private List customerCodes; /** 按客户当前区域筛选。 */ private String customerArea; + /** 服务端解析的区域客户 ID;null 表示使用客户表关联,空列表表示无匹配客户。 */ + private List areaCustomerIds; /** 服务端根据客户区域确定的计算规则。 */ private String profitRule; private String customerName; diff --git a/bms-back/bms-system/src/main/java/com/bms/price/mapper/PrcProfitReportMapper.java b/bms-back/bms-system/src/main/java/com/bms/price/mapper/PrcProfitReportMapper.java index 484908b8..74f8309c 100644 --- a/bms-back/bms-system/src/main/java/com/bms/price/mapper/PrcProfitReportMapper.java +++ b/bms-back/bms-system/src/main/java/com/bms/price/mapper/PrcProfitReportMapper.java @@ -11,6 +11,8 @@ import com.bms.price.domain.vo.PrcProfitReportSummary; /** 毛利报表 mapper;金额事实取自 prc_income_bill。 */ public interface PrcProfitReportMapper { + List selectAreaCustomerIds(@Param("customerArea") String customerArea, @Param("limit") int limit); + List selectOrderRows(PrcProfitReportQuery query); List selectProductRows(PrcProfitReportQuery query); diff --git a/bms-back/bms-system/src/main/java/com/bms/price/service/impl/PrcProfitReportServiceImpl.java b/bms-back/bms-system/src/main/java/com/bms/price/service/impl/PrcProfitReportServiceImpl.java index 594f6d6d..7f870995 100644 --- a/bms-back/bms-system/src/main/java/com/bms/price/service/impl/PrcProfitReportServiceImpl.java +++ b/bms-back/bms-system/src/main/java/com/bms/price/service/impl/PrcProfitReportServiceImpl.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; // 提成功能暂停使用:import java.math.BigDecimal; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; // 提成功能暂停使用:import org.springframework.transaction.annotation.Transactional; import com.bms.common.exception.ServiceException; @@ -23,16 +24,27 @@ import com.bms.price.domain.vo.PrcProfitReportSummary; import com.bms.price.enums.ProfitAreaRule; import com.bms.price.mapper.PrcProfitReportMapper; import com.bms.price.service.IPrcProfitReportService; +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** 毛利报表:以 prc_income_bill 为金额事实表,不关联应付模块。 */ @Service public class PrcProfitReportServiceImpl implements IPrcProfitReportService { private static final int MULTI_VALUE_FILTER_LIMIT = 2000; - + private static final int MAX_AREA_CUSTOMER_IDS = 5000; + private static final Logger log = LoggerFactory.getLogger(PrcProfitReportServiceImpl.class); @Autowired private PrcProfitReportMapper profitReportMapper; + @Value("${bms.profit-report.customer-id-filter-enabled:false}") + private boolean customerIdFilterEnabled; + + @Value("${bms.profit-report.max-customer-ids:1000}") + private int maxCustomerIds; + @Autowired private BmsCustomerMapper customerMapper; @@ -59,8 +71,35 @@ public class PrcProfitReportServiceImpl implements IPrcProfitReportService @Override public List selectOrderRows(PrcProfitReportQuery query) { - normalizeMultiValueFilters(query); - return profitReportMapper.selectOrderRows(query); + log.info("selectOrderRows 开始, query={}", query); + long methodStart = System.currentTimeMillis(); + try { + // ---- normalizeMultiValueFilters 单独计时 ---- + long normalizeStart = System.currentTimeMillis(); + normalizeMultiValueFilters(query); + long normalizeCost = System.currentTimeMillis() - normalizeStart; + log.debug("normalizeMultiValueFilters 完成, 耗时={}ms, query={}", normalizeCost, query); + + // ---- 数据库查询单独计时 ---- + long dbStart = System.currentTimeMillis(); + List prcProfitReportOrderRows = profitReportMapper.selectOrderRows(query); + long dbCost = System.currentTimeMillis() - dbStart; + + long totalCost = System.currentTimeMillis() - methodStart; + int size = prcProfitReportOrderRows == null ? 0 : prcProfitReportOrderRows.size(); + + log.info("selectOrderRows 结束, 结果条数={}, normalize耗时={}ms, db耗时={}ms, 总耗时={}ms", + size, normalizeCost, dbCost, totalCost); + + if (log.isDebugEnabled() && prcProfitReportOrderRows != null && !prcProfitReportOrderRows.isEmpty()) { + log.debug("selectOrderRows 首条数据={}", prcProfitReportOrderRows.get(0)); + } + return prcProfitReportOrderRows; + } catch (Exception e) { + long totalCost = System.currentTimeMillis() - methodStart; + log.error("selectOrderRows 异常, 总耗时={}ms, query={}", totalCost, query, e); + throw e; + } } @Override @@ -115,6 +154,8 @@ public class PrcProfitReportServiceImpl implements IPrcProfitReportService { throw new ServiceException("请选择客户区域"); } + // 区域客户 ID 只能由服务端生成,不能信任请求中传入的值。 + query.setAreaCustomerIds(null); if ("COMMISSION_MISSING".equals(query.getDetailType())) { throw new ServiceException("提成功能已暂停使用"); @@ -133,6 +174,43 @@ public class PrcProfitReportServiceImpl implements IPrcProfitReportService query.setOrderNos(splitFilterValues(query.getOrderNo(), "订单号")); query.setCustomerCodes(splitFilterValues(query.getCustomerCode(), "客户代码")); query.setCustomerNames(splitFilterValues(query.getCustomerName(), "客户名称")); + resolveAreaCustomerIds(query); + } + + private void resolveAreaCustomerIds(PrcProfitReportQuery query) + { + if (!customerIdFilterEnabled || maxCustomerIds <= 0) + { + return; + } + + // startPage 已由 Controller 设置;客户查询不能消耗订单列表的分页。 + Page page = PageHelper.getLocalPage(); + PageHelper.clearPage(); + boolean resolved = false; + try + { + int threshold = Math.min(maxCustomerIds, MAX_AREA_CUSTOMER_IDS); + int limit = threshold + 1; + List customerIds = profitReportMapper.selectAreaCustomerIds(query.getCustomerArea(), limit); + if (customerIds == null) + { + throw new ServiceException("查询区域客户失败"); + } + if (customerIds.size() <= threshold) + { + query.setAreaCustomerIds(customerIds); + } + // 超过阈值保留 null,回退原客户表关联,避免生成过长的 IN 列表。 + resolved = true; + } + finally + { + if (resolved && page != null) + { + PageHelper.setLocalPage(page); + } + } } private List splitFilterValues(String rawValue, String fieldName) diff --git a/bms-back/bms-system/src/main/resources/mapper/price/PrcProfitReportMapper.xml b/bms-back/bms-system/src/main/resources/mapper/price/PrcProfitReportMapper.xml index a1093f80..42df2d13 100644 --- a/bms-back/bms-system/src/main/resources/mapper/price/PrcProfitReportMapper.xml +++ b/bms-back/bms-system/src/main/resources/mapper/price/PrcProfitReportMapper.xml @@ -116,10 +116,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" --> + + from prc_income_bill i inner join prc_order_bill p on p.bill_id = i.bill_id - + left join ctr_customer c on c.customer_id = p.customer_id and c.del_flag = '0' and (p.billing_fail_status is null or p.billing_fail_status not in (1, 2, 3)) - - and c.customer_area = #{customerArea} - + + + and p.customer_id in + + #{customerId} + + + + and 1 = 0 + + + and c.customer_area = #{customerArea} + + and p.biz_date >= #{beginBizDate} @@ -251,7 +269,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and p.ar_bill_status = #{arBillStatus} - order by p.biz_date desc, p.bill_id desc