From f5850a884d966ff46b0c890d7964888e8e42c053 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Fri, 12 Jun 2026 09:38:39 +0800 Subject: [PATCH] =?UTF-8?q?F2-17=E6=A0=B9=E5=9B=A0:=20=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=B5=81=E5=88=86=E9=A1=B5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?(page/perPage=E2=86=92=E5=BC=95=E6=93=8E=20PAGE=5FSTART/PAGE=5F?= =?UTF-8?q?SIZE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FlowController 把 amis page/perPage 原样当过滤条件,而合成搜索流 listSelect 认的是 PAGE_START/PAGE_SIZE,导致永远返回全表(实测 page1=page2,total=266)。 翻译为引擎分页键并剔除原始键;过滤与分页共存(TITLE=放假 266→42 实测)。14 单测。 Co-Authored-By: Claude Opus 4.8 (1M context) --- .../platform/metadata/web/FlowController.java | 40 +++++++++++ .../metadata/web/FlowControllerTest.java | 72 +++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/web/FlowController.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/web/FlowController.java index f5a9501..d412ade 100644 --- a/platform-metadata/src/main/java/com/eip4/platform/metadata/web/FlowController.java +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/web/FlowController.java @@ -62,12 +62,16 @@ public class FlowController { this.flowNameResolver = flowNameResolver; } + /** amis crud 默认每页条数(与前端 amis 默认 perPage 一致)。 */ + private static final int DEFAULT_PER_PAGE = 10; + /** GET 执行:query 参数透传为服务流入参 map(保留原始大小写键,交由流脚本/服务节点消费)。 */ @GetMapping("/{id}") @Operation(summary = "执行服务流(GET):query 参数作为入参 map,返回流输出 JSON。报表 D 类查询入口") public ResponseEntity> executeGet(@PathVariable String id, @RequestParam Map allParams) { Map map = new HashMap<>(allParams); + applyPaging(map); return run(id, map); } @@ -77,9 +81,45 @@ public class FlowController { public ResponseEntity> executePost(@PathVariable String id, @RequestBody(required = false) Map body) { Map map = body == null ? new HashMap<>() : new HashMap<>(body); + applyPaging(map); return run(id, map); } + /** + * 把 amis crud 的字面分页参数 {@code page}/{@code perPage} 翻译成合成搜索流引擎 + * ({@code POJOCRUDService.listSelect})认得的分页键 {@link POJOCRUDService#PAGE_START}/ + * {@link POJOCRUDService#PAGE_SIZE},并从入参 map 中剔除原始 {@code page}/{@code perPage} + * (否则被引擎当作不存在的列名拼进 where,命名/报表流亦同——剔除即可,无副作用)。 + * + *

缺省:page=1,perPage={@value #DEFAULT_PER_PAGE}(amis 默认)。键类型与 PickerController + * 一致放 int({@code (page-1)*perPage} 与 {@code perPage})。其它 query 参数(如 TITLE)原样保留为过滤条件。 + * 仅当显式带 page/perPage 时才翻译,避免给无分页语义的流凭空注入分页键。 + */ + private void applyPaging(Map map) { + boolean hasPage = map.containsKey("page"); + boolean hasPerPage = map.containsKey("perPage"); + if (!hasPage && !hasPerPage) { + return; + } + int page = parsePositiveInt(map.remove("page"), 1); + int perPage = parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE); + map.put(POJOCRUDService.PAGE_START, (page - 1) * perPage); + map.put(POJOCRUDService.PAGE_SIZE, perPage); + } + + /** 解析分页参数为正整数;缺省/空白/非法/非正 → 回退默认值(page/perPage 至少为 1)。 */ + private static int parsePositiveInt(Object raw, int fallback) { + if (raw == null) { + return fallback; + } + try { + int v = Integer.parseInt(String.valueOf(raw).trim()); + return v > 0 ? v : fallback; + } catch (NumberFormatException e) { + return fallback; + } + } + /** * 统一执行 + 响应封装(F2-14 三级解析)。 * diff --git a/platform-metadata/src/test/java/com/eip4/platform/metadata/web/FlowControllerTest.java b/platform-metadata/src/test/java/com/eip4/platform/metadata/web/FlowControllerTest.java index 6220d50..1cd84a3 100644 --- a/platform-metadata/src/test/java/com/eip4/platform/metadata/web/FlowControllerTest.java +++ b/platform-metadata/src/test/java/com/eip4/platform/metadata/web/FlowControllerTest.java @@ -260,4 +260,76 @@ class FlowControllerTest { assertEquals(1, resp.getBody().get("status")); assertNotNull(resp.getBody().get("msg")); } + + @Test + @DisplayName("12. 分页翻译: page/perPage → PAGE_START/PAGE_SIZE,剔除原始键,过滤键保留") + void translatesPagingForSynthesizedSearch() { + String flowId = "PM_PROJECT_SEARCH"; + FlowExcuteService engine = mock(FlowExcuteService.class); + when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>()); + FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId))); + + Map params = new HashMap<>(); + params.put("page", "2"); + params.put("perPage", "2"); + params.put("TITLE", "放假"); // F2-17 #3 过滤行为不可破坏 + controller.executeGet(flowId, params); + + @SuppressWarnings("unchecked") + ArgumentCaptor> mapCap = ArgumentCaptor.forClass(Map.class); + org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId)); + Map sent = mapCap.getValue(); + + // 引擎认的分页键存在且为 int(与 PickerController 一致),PAGE_START=(2-1)*2=2,PAGE_SIZE=2 + assertEquals(2, sent.get(POJOCRUDService.PAGE_START), "PAGE_START=(page-1)*perPage"); + assertEquals(2, sent.get(POJOCRUDService.PAGE_SIZE), "PAGE_SIZE=perPage"); + assertInstanceOf(Integer.class, sent.get(POJOCRUDService.PAGE_START), "分页键须为 int"); + assertInstanceOf(Integer.class, sent.get(POJOCRUDService.PAGE_SIZE), "分页键须为 int"); + // 原始字面键已剔除,避免被引擎当不存在的列名条件 + org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey("page"), "原始 page 应剔除"); + org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey("perPage"), "原始 perPage 应剔除"); + // 过滤键保留 + assertEquals("放假", sent.get("TITLE"), "其它 query 参数仍作过滤条件保留"); + } + + @Test + @DisplayName("13. 分页缺省: 只给 page,perPage 回落 amis 默认 10;page 缺省 1") + void pagingDefaults() { + String flowId = "PM_PROJECT_SEARCH"; + FlowExcuteService engine = mock(FlowExcuteService.class); + when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>()); + FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId))); + + Map params = new HashMap<>(); + params.put("perPage", "5"); // 只给 perPage,page 缺省 1 + controller.executeGet(flowId, params); + + @SuppressWarnings("unchecked") + ArgumentCaptor> mapCap = ArgumentCaptor.forClass(Map.class); + org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId)); + Map sent = mapCap.getValue(); + assertEquals(0, sent.get(POJOCRUDService.PAGE_START), "page 缺省 1 → PAGE_START=0"); + assertEquals(5, sent.get(POJOCRUDService.PAGE_SIZE), "perPage=5"); + } + + @Test + @DisplayName("14. 无分页参数: 不注入分页键(命名/报表流无分页语义时不凭空加键)") + void noPagingKeysWhenAbsent() { + String flowId = "COMMON_REPORT"; + FlowExcuteService engine = mock(FlowExcuteService.class); + when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>()); + FlowController controller = new FlowController(engine, resolverReturning(flowId, namedHit())); + + Map params = new HashMap<>(); + params.put("DEPT", "财务"); + controller.executeGet(flowId, params); + + @SuppressWarnings("unchecked") + ArgumentCaptor> mapCap = ArgumentCaptor.forClass(Map.class); + org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId)); + Map sent = mapCap.getValue(); + org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey(POJOCRUDService.PAGE_START)); + org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey(POJOCRUDService.PAGE_SIZE)); + assertEquals("财务", sent.get("DEPT"), "过滤键保留"); + } }