From ba6e7559fa1ee4ba8b1ce6695ede180a17a973a0 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Thu, 11 Jun 2026 16:52:50 +0800 Subject: [PATCH] =?UTF-8?q?P4-6:=20platform-flow-gateway=20=E5=BE=85?= =?UTF-8?q?=E5=8A=9E=E2=86=92=E4=B8=9A=E5=8A=A1=E9=A1=B5=E6=98=A0=E5=B0=84?= =?UTF-8?q?(tablename=E2=86=92templateId=E2=86=92pageUrl)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 侦察口径:sys_entity+view_templateentity+view_template 两步映射; 多模板优先级 type=7(表单)>1(列表)>其余升序,同 type 按 templateid 字典序取首个 - 新建 PageTemplateService:findTemplateId() 查只读客户库, @Cacheable("pageTemplateByTable") Caffeine TTL=10min/maxSize=2000 - FlowGatewayConfig 启用 @EnableCaching + flowGatewayCacheManager(Caffeine) - TodoItem 增加 pageTemplateId/pageUrl/pageUrlReason; 查不到映射时 pageUrl=null+reason,前端据此禁用跳转(优雅降级) - TodoAggregator 注入 PageTemplateService,aggregate 逐条 fillPageTemplate - PageTemplateMappingTest 10 单测;TodoAggregatorTest 补 @Mock PageTemplateService - 验证:真实用户(829cdcca)3 条待办带出 templateId=7042cb37(PM_SAFE_CLARIFICATION/安全技术交底), 与客户库直查一致;全反应堆 31+ArchUnit 绿 Co-Authored-By: Claude Fable 5 --- platform-flow-gateway/P3网关实测报告.md | 100 +++++++++ platform-flow-gateway/pom.xml | 10 + .../aggregate/PageTemplateService.java | 111 ++++++++++ .../flowgateway/aggregate/TodoAggregator.java | 34 ++- .../flowgateway/aggregate/TodoItem.java | 16 ++ .../flowgateway/client/FlowGatewayConfig.java | 25 +++ .../flowgateway/PageTemplateMappingTest.java | 201 ++++++++++++++++++ .../flowgateway/TodoAggregatorTest.java | 5 + 8 files changed, 498 insertions(+), 4 deletions(-) create mode 100644 platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/PageTemplateService.java create mode 100644 platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/PageTemplateMappingTest.java diff --git a/platform-flow-gateway/P3网关实测报告.md b/platform-flow-gateway/P3网关实测报告.md index 3380c42..dc6ed96 100644 --- a/platform-flow-gateway/P3网关实测报告.md +++ b/platform-flow-gateway/P3网关实测报告.md @@ -182,3 +182,103 @@ platform-app 进程存活(引擎宕机不拖垮单体,明确 503 语义而非 50 - 验证完成后 `kill` 本任务起的 8089 / 8090(8084 未动)。 - 客户库零写入;本地 eip4m0 测试数据为可重复构造的自举工件(bootstrap-datastatus.sql)。 - eip4flow 迁移副本上 1 条真实 complete 已完整登记(实例 20334081 结束)。 + +--- + +## P4-6 增补:待办→业务页映射(tablename → templateId → pageUrl) + +> 日期:2026-06-11 | 任务:P4-6 —— `TodoItem` 增加 pageTemplateId/pageUrl 字段; +> 映射口径侦察;Caffeine 缓存;单测 ≥4;真实待办验证 + +### P4-6.1 映射规则结论(侦察客户库 view_templateentity + view_template + sys_entity) + +**两步映射**: +1. `SYS_DATASTATUS.tablename` → `sys_entity.sys_entity_id`(entityid):`UPPER(tablename)` 归一大小写 +2. `entityid` → `view_templateentity.templateid`(经 `view_template.type` 优先级筛选,取最优一条) + +**多模板优先级规则**(`TemplateType` 枚举语义 + 待办填写场景): + +| 优先级 | type | 名称 | 理由 | +|--------|------|------|------| +| 0(最优) | 7 | EDITFORM 表单模板 | 待办详情 = 填写/审批页面,表单模板最贴近 | +| 1 | 1 | GRID 列表模板 | 无专属表单时退回列表页 | +| 2–5 | 3/4/6/5 | 树/树列表/组合/日历 | 功能性模板,语义次之 | +| 6–7 | 100/101 | 弹窗选择 | 非主页面,兜底 | +| 8 | 200 | 报表模板 | 报表展示,非流程业务页 | + +同 type 多条时按 `templateid` 字典序取首个(稳定可重复)。 + +**数据佐证**(客户库直查): +- `OA_SEALUSEAPPLY`:有 type=7「填写印章使用申请」→ 优先选 type=7 ✅ +- `PM_SAFE_CLARIFICATION`:仅 type=1「安全技术交底」→ 退回 type=1 ✅ +- `PM_CONTRACTACCOUNTS`:仅 type=1「项目分包合同决算」→ type=1 ✅ + +### P4-6.2 实现要点 + +| 文件 | 变更 | +|------|------| +| `pom.xml` | 新增 `spring-boot-starter-cache` + `caffeine` 依赖 | +| `FlowGatewayConfig` | `@EnableCaching` + `flowGatewayCacheManager`(Caffeine,TTL=10min,maxSize=2000) | +| `PageTemplateService`(新建) | `findTemplateId(tableName)` 查客户库两步映射,`@Cacheable("pageTemplateByTable")`;`buildPageUrl()` 静态工具 | +| `TodoItem` | 新增 `pageTemplateId`/`pageUrl`/`pageUrlReason` 三字段 | +| `TodoAggregator` | 注入 `PageTemplateService`;`fillPageTemplate()` 填充新字段;降级写 `pageUrlReason` | + +**降级策略**(优雅降级,前端禁用跳转): +- `businessTable=null`(SYS_DATASTATUS 无映射)→ `pageUrl=null`,`pageUrlReason="businessTable 未映射,无法定位模板"` +- `sys_entity/view_templateentity` 找不到模板 → `pageUrl=null`,`pageUrlReason` 含表名 +- 数据库异常 → `PageTemplateService` 内部 catch,返回 `Optional.empty()`,降级同上 + +### P4-6.3 真实待办验证 + +服务:flowable-service 8089(本地 eip4flow 迁移数据)+ platform-app 8090(local profile,eip4m0) + +用户 `829cdcca-4e07-4e12-9c8e-a3da2c105e31`(wangwu)待办 3 条,全部 `businessKey → PM_SAFE_CLARIFICATION`: + +| taskId | businessKey | businessTable | pageTemplateId | pageUrl | +|--------|-------------|--------------|----------------|---------| +| 20334061 | 4f89dbc4-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... | +| 20333989 | 36e9763f-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... | +| 20329569 | 1ef1559b-... | PM_SAFE_CLARIFICATION | 7042cb37-9581-4dfe-bb9f-18ed0e0b6b53 | /page/7042cb37-... | + +与客户库直查对照:`PM_SAFE_CLARIFICATION → sys_entity_id=8c3c71e9 → templateid=7042cb37(type=1 安全技术交底)` ✅ 完全一致。 + +降级形态:`pageUrl=null + pageUrlReason` 非空 —— 由单测 `PageTemplateMappingTest` 3 个降级用例覆盖。 + +### P4-6.4 单测总计 + +`PageTemplateMappingTest`(10 单测,新增): + +| 用例 | 验证点 | +|------|--------| +| `tableName有映射_返回templateId` | 正常映射路径 | +| `tableName无映射_返回empty` | 数据库 null 结果 | +| `tableName为null_返回empty不查库` | 空保护 | +| `tableName为空串_返回empty不查库` | 空保护 | +| `buildPageUrl_路由格式正确` | `/page/{id}` 格式 | +| `数据库异常_降级返回empty` | 异常静默降级 | +| `聚合_有模板条目pageUrl正确填充` | 聚合层正常路径 | +| `聚合_无模板映射_pageUrl为null且reason含表名` | 模板未找到降级 | +| `聚合_businessTable为null_pageUrl为null且reason含提示` | SYS_DATASTATUS 无映射降级 | +| `聚合_有模板与无模板条目共存_优雅降级` | 混合列表共存 | + +全模块 `mvn test`:**31 单测(platform-flow-gateway)全绿**;`ArchitectureRulesTest` 绿;全反应堆 BUILD SUCCESS。 + +### P4-6.5 数据变更登记 + +| 操作 | 目标库.行 | 性质 | +|------|--------|------| +| 插入本地 wangwu 用户 | eip4m0.sys_user | 本地自举,可重复 | +| 插入 3 条 SYS_DATASTATUS | eip4m0.sys_datastatus(PM_SAFE_CLARIFICATION) | 本地构造,演示映射 | +| 建 view_template / view_templateentity | eip4m0(两张新表) | 本地镜像,可重复构造 | +| 插入 sys_entity + view_template + view_templateentity | eip4m0,PM_SAFE_CLARIFICATION 的镜像数据 | 本地构造 | +| **客户库** | 零写入 | 只读 ✅ | + +### P4-6.6 新坑 + +1. **`mvn spring-boot:run` 用的是 classpath 而非 jar**:依赖模块(platform-flow-gateway)改动后须先 `mvn install` 再重启 platform-app,否则 platform-app 运行时仍加载旧 .class(本次 platform-app 在 flow-gateway 编译之前已启动,导致新字段/新 Service 未生效,初查误认为缓存或代理问题)。 +2. **local profile 无客户库表时 PageTemplateService 静默降级**:view_template/view_templateentity 在本地 eip4m0 需手动建镜像表(最小结构)才能在 local 验证;生产/default profile 直连客户库则无需此步骤。 + +## 七-B、P4-6 收尾 + +- 验证完成后已 `kill` 8089(PID 15942)和 8090(PID 18609)。 +- 客户库零写入;本地 eip4m0 新增镜像表为可重复构造工件。 diff --git a/platform-flow-gateway/pom.xml b/platform-flow-gateway/pom.xml index ffe69c7..90bb530 100644 --- a/platform-flow-gateway/pom.xml +++ b/platform-flow-gateway/pom.xml @@ -38,6 +38,16 @@ springdoc-openapi-starter-webmvc-ui + + + org.springframework.boot + spring-boot-starter-cache + + + com.github.ben-manes.caffeine + caffeine + + org.springframework.boot spring-boot-starter-test diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/PageTemplateService.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/PageTemplateService.java new file mode 100644 index 0000000..bcb7498 --- /dev/null +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/PageTemplateService.java @@ -0,0 +1,111 @@ +package com.eip4.platform.flowgateway.aggregate; + +import java.util.Optional; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +/** + * 业务表名 → 前端页面模板 ID 映射服务。 + * + *

映射口径(侦察客户库 view_templateentity + view_template + sys_entity 得出): + *

    + *
  1. {@code tablename} → {@code sys_entity.sys_entity_id}(entityid)
  2. + *
  3. {@code entityid} → {@code view_templateentity.templateid}(经 {@code view_template.type} 优先级筛选)
  4. + *
+ * + *

多模板优先级规则(由 TemplateType 枚举语义 + 待办填写场景决定): + *

    + *
  • type=7(EDITFORM 表单模板)优先 —— 待办详情对应的填写/审批页面;
  • + *
  • type=1(GRID 列表模板)次之 —— 无专属表单时退回列表页;
  • + *
  • 其余 type 按升序兜底(type=3/4/6 等功能模板);
  • + *
  • type=100(SELECTDIALOG)和 type=200(REPORT)语义与待办无关,但不排除(有表单/列表时自然被压后);
  • + *
  • 同 type 有多条时按 templateid 字典序取首个(稳定可重复)。
  • + *
+ * + *

缓存:模板关联是低频变更配置数据(设计时确定),用 Caffeine 缓存 {@code tablename→templateId} + * 映射结果,避免每次待办渲染都查只读客户库两张关联表。 + * 缓存命名 {@code "pageTemplateByTable"},配置见 {@code FlowGatewayConfig}。 + * + *

优雅降级:任何数据库异常或找不到映射时返回 {@link Optional#empty()},不抛出,由调用方决定降级策略。 + * + *

数据源只读:全程只查 {@code sys_entity} + {@code view_templateentity} + {@code view_template}, + * 不写任何表,对齐客户库只读约定。 + */ +@Service +public class PageTemplateService { + + private static final Logger log = LoggerFactory.getLogger(PageTemplateService.class); + + /** + * 优先级映射:type → 排序权重(越小越优先)。 + * type=7 (EDITFORM) 最优先;type=1 (GRID) 次之;其余按 type 值本身升序。 + * 未在此表中的 type 用 {@code type + 1000} 作兜底权重。 + */ + private static final String BEST_TEMPLATE_SQL = """ + SELECT vte.templateid + FROM sys_entity se + JOIN view_templateentity vte ON vte.entityid = se.sys_entity_id + JOIN view_template vt ON vt.view_template_id = vte.templateid + WHERE UPPER(se.tablename) = UPPER(?) + ORDER BY + CASE vt.type + WHEN 7 THEN 0 + WHEN 1 THEN 1 + WHEN 3 THEN 2 + WHEN 4 THEN 3 + WHEN 6 THEN 4 + WHEN 5 THEN 5 + WHEN 100 THEN 6 + WHEN 101 THEN 7 + WHEN 200 THEN 8 + ELSE vt.type + 1000 + END, + vte.templateid + LIMIT 1 + """; + + private final JdbcTemplate jdbcTemplate; + + public PageTemplateService(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + /** + * 按业务表名查对应的最优前端模板 ID。 + * + *

结果被 Caffeine 缓存(key = tableName 原始值,不区分大小写 —— SQL 内已 UPPER 归一)。 + * 缓存未命中时查只读客户库,任何异常返回 {@link Optional#empty()}。 + * + * @param tableName 业务表名(如 {@code PM_PAYMENTAPPLY}),不区分大小写 + * @return 最优 templateId;找不到或异常时为 {@link Optional#empty()} + */ + @Cacheable(cacheNames = "pageTemplateByTable", unless = "#result.isEmpty()") + public Optional findTemplateId(String tableName) { + if (tableName == null || tableName.isBlank()) { + return Optional.empty(); + } + try { + String id = jdbcTemplate.query(BEST_TEMPLATE_SQL, + rs -> rs.next() ? rs.getString(1) : null, + tableName); + return Optional.ofNullable(id); + } catch (RuntimeException e) { + log.debug("pageTemplate 映射查询降级(tableName={}): {}", tableName, e.getMessage()); + return Optional.empty(); + } + } + + /** + * 按 templateId 构造前端页面 URL(路由形态 {@code /page/{templateId}})。 + * + * @param templateId 非空 templateId + * @return {@code /page/} + */ + public static String buildPageUrl(String templateId) { + return "/page/" + templateId; + } +} diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoAggregator.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoAggregator.java index 09e280e..f6989fa 100644 --- a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoAggregator.java +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoAggregator.java @@ -2,6 +2,7 @@ package com.eip4.platform.flowgateway.aggregate; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import org.springframework.stereotype.Service; @@ -15,21 +16,26 @@ import com.eip4.platform.flowgateway.client.FlowableClient; *

    *
  • 当前用户待办——按 assignee + 候选组(JWT roles)聚合;
  • *
  • 逐条经 {@link BusinessSummaryService} 拼业务摘要(businessKey → 业务表标题字段);
  • - *
  • 拼不到摘要时优雅降级为仅流程字段(由 BusinessSummaryService 内部兜底)。
  • + *
  • 逐条经 {@link PageTemplateService} 填充前端页面模板(businessTable → templateId → pageUrl);
  • + *
  • 拼不到摘要/模板时优雅降级(pageUrl=null + pageUrlReason 说明原因),不拖垮整张列表。
  • *
* *

引擎不可达由 {@link FlowableClient} 抛 {@code FlowServiceUnavailableException}(→ 503), - * 本层不吞;但业务摘要侧缺数据一律降级,不影响流程字段返回。 + * 本层不吞;但业务摘要/模板侧缺数据一律降级,不影响流程字段返回。 */ @Service public class TodoAggregator { private final FlowableClient flowableClient; private final BusinessSummaryService businessSummaryService; + private final PageTemplateService pageTemplateService; - public TodoAggregator(FlowableClient flowableClient, BusinessSummaryService businessSummaryService) { + public TodoAggregator(FlowableClient flowableClient, + BusinessSummaryService businessSummaryService, + PageTemplateService pageTemplateService) { this.flowableClient = flowableClient; this.businessSummaryService = businessSummaryService; + this.pageTemplateService = pageTemplateService; } /** @@ -49,7 +55,7 @@ public class TodoAggregator { return aggregate(tasks); } - /** 逐条拼业务摘要(摘要侧异常已在 BusinessSummaryService 内降级,不在此抛出)。 */ + /** 逐条拼业务摘要 + 页面模板(异常已在各 Service 内降级,不在此抛出)。 */ private List aggregate(List tasks) { List items = new ArrayList<>(tasks == null ? 0 : tasks.size()); if (tasks == null) { @@ -60,8 +66,28 @@ public class TodoAggregator { BusinessSummaryService.Summary summary = businessSummaryService.summarize(t.businessKey); item.businessTable = summary.tableName(); item.businessSummary = summary.fields(); + fillPageTemplate(item); items.add(item); } return items; } + + /** + * 填充 pageTemplateId / pageUrl / pageUrlReason。 + * 依赖链:businessTable(已由业务摘要层填充) → templateId → pageUrl。 + * 任何环节缺失时 pageUrl=null 并写入 reason,前端据此禁用跳转。 + */ + private void fillPageTemplate(TodoItem item) { + if (item.businessTable == null) { + item.pageUrlReason = "businessTable 未映射,无法定位模板"; + return; + } + Optional templateId = pageTemplateService.findTemplateId(item.businessTable); + if (templateId.isEmpty()) { + item.pageUrlReason = "sys_entity/view_templateentity 中找不到表 " + item.businessTable + " 对应的模板"; + return; + } + item.pageTemplateId = templateId.get(); + item.pageUrl = PageTemplateService.buildPageUrl(item.pageTemplateId); + } } diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoItem.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoItem.java index 97b01cb..8b8259f 100644 --- a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoItem.java +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/aggregate/TodoItem.java @@ -34,6 +34,22 @@ public class TodoItem { /** 业务摘要标题字段(loadPOJO 只读取的标题类字段)。拼不到时为空 Map(降级)。 */ public Map businessSummary = Map.of(); + /** + * 前端页面模板 ID(经 sys_entity + view_templateentity + view_template 映射,type=7 表单优先)。 + * 查不到映射时为 null(前端根据 pageUrlReason 决定是否禁用跳转)。 + */ + public String pageTemplateId; + /** + * 前端页面 URL,形态 {@code /page/{templateId}}。 + * 查不到时为 null,前端应禁用跳转按钮。 + */ + public String pageUrl; + /** + * 当 {@code pageUrl} 为 null 时,说明查不到映射的原因(优雅降级辅助信息)。 + * pageUrl 不为 null 时此字段为 null。 + */ + public String pageUrlReason; + static TodoItem fromTask(FlowTaskDto t) { TodoItem item = new TodoItem(); item.taskId = t.id; diff --git a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowGatewayConfig.java b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowGatewayConfig.java index 525db76..e93ba14 100644 --- a/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowGatewayConfig.java +++ b/platform-flow-gateway/src/main/java/com/eip4/platform/flowgateway/client/FlowGatewayConfig.java @@ -1,23 +1,34 @@ package com.eip4.platform.flowgateway.client; import java.time.Duration; +import java.util.List; +import java.util.concurrent.TimeUnit; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.client.ClientHttpRequestFactories; import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.web.client.RestClient; +import com.github.benmanes.caffeine.cache.Caffeine; + /** * 流程网关客户端装配:把 {@link FlowGatewayProperties} 的基址/超时落到一个专用 {@code RestClient}。 * *

用专属 bean 名 {@code flowRestClient},不与应用里可能存在的其他 {@code RestClient} 冲突; * 超时经 {@link ClientHttpRequestFactorySettings} 落到请求工厂(JDK {@code SimpleClientHttpRequestFactory}), * 引擎卡死时按 {@code read-timeout} 抛 {@code SocketTimeoutException} → {@code FlowableClient} 走熔断降级。 + * + *

同时启用 {@link EnableCaching} 并注册 Caffeine 缓存管理器,供 + * {@code PageTemplateService} 缓存 tablename→templateId 映射结果(模板关联低频变更,10 分钟 TTL)。 */ @Configuration +@EnableCaching @EnableConfigurationProperties(FlowGatewayProperties.class) public class FlowGatewayConfig { @@ -36,4 +47,18 @@ public class FlowGatewayConfig { .requestFactory(flowRequestFactory) .build(); } + + /** + * Caffeine 缓存管理器:{@code pageTemplateByTable} 缓存 tablename→templateId 映射。 + * TTL 10 分钟(模板关联是设计时低频配置,无需实时刷新);最大 2000 条(实体总量约 1500,留余量)。 + */ + @Bean + CacheManager flowGatewayCacheManager() { + CaffeineCacheManager mgr = new CaffeineCacheManager(); + mgr.setCacheNames(List.of("pageTemplateByTable")); + mgr.setCaffeine(Caffeine.newBuilder() + .expireAfterWrite(10, TimeUnit.MINUTES) + .maximumSize(2000)); + return mgr; + } } diff --git a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/PageTemplateMappingTest.java b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/PageTemplateMappingTest.java new file mode 100644 index 0000000..02cb42d --- /dev/null +++ b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/PageTemplateMappingTest.java @@ -0,0 +1,201 @@ +package com.eip4.platform.flowgateway; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.ResultSetExtractor; + +import com.eip4.platform.flowgateway.aggregate.BusinessSummaryService; +import com.eip4.platform.flowgateway.aggregate.PageTemplateService; +import com.eip4.platform.flowgateway.aggregate.TodoAggregator; +import com.eip4.platform.flowgateway.aggregate.TodoItem; +import com.eip4.platform.flowgateway.client.FlowTaskDto; +import com.eip4.platform.flowgateway.client.FlowableClient; + +/** + * P4-6 页面模板映射单测: + * 1. tableName 有映射 → pageTemplateId/pageUrl 正确填充 + * 2. tableName 无映射 → pageUrl=null + reason 含表名 + * 3. businessTable=null(业务摘要层未映射)→ pageUrl=null + reason 含提示 + * 4. PageTemplateService.findTemplateId 空 tableName → Optional.empty() + * 5. buildPageUrl 路由格式正确 + * 6. TodoAggregator 聚合:有模板+无模板条目共存(优雅降级) + */ +@ExtendWith(MockitoExtension.class) +class PageTemplateMappingTest { + + // ── PageTemplateService 直接单测 ────────────────────────────────────────── + + @Mock + JdbcTemplate jdbcTemplate; + + @InjectMocks + PageTemplateService pageTemplateService; + + @SuppressWarnings("unchecked") + private void givenTemplateId(String tableName, String templateId) { + when(jdbcTemplate.query(any(String.class), + any(ResultSetExtractor.class), + eq(tableName))).thenReturn(templateId); + } + + @Test + void tableName有映射_返回templateId() { + givenTemplateId("PM_PAYMENTAPPLY", "a18b1f38-073c-4190-bfd5-b95274cf9f31"); + + Optional result = pageTemplateService.findTemplateId("PM_PAYMENTAPPLY"); + + assertThat(result).isPresent(); + assertThat(result.get()).isEqualTo("a18b1f38-073c-4190-bfd5-b95274cf9f31"); + } + + @Test + void tableName无映射_返回empty() { + givenTemplateId("UNKNOWN_TABLE", null); + + Optional result = pageTemplateService.findTemplateId("UNKNOWN_TABLE"); + + assertThat(result).isEmpty(); + } + + @Test + void tableName为null_返回empty不查库() { + Optional result = pageTemplateService.findTemplateId(null); + + assertThat(result).isEmpty(); + } + + @Test + void tableName为空串_返回empty不查库() { + Optional result = pageTemplateService.findTemplateId(" "); + + assertThat(result).isEmpty(); + } + + @Test + void buildPageUrl_路由格式正确() { + String url = PageTemplateService.buildPageUrl("a18b1f38-073c-4190-bfd5-b95274cf9f31"); + + assertThat(url).isEqualTo("/page/a18b1f38-073c-4190-bfd5-b95274cf9f31"); + } + + @Test + void 数据库异常_降级返回empty() { + when(jdbcTemplate.query(any(String.class), any(ResultSetExtractor.class), eq("PM_BADTABLE"))) + .thenThrow(new RuntimeException("连接超时")); + + Optional result = pageTemplateService.findTemplateId("PM_BADTABLE"); + + assertThat(result).isEmpty(); + } + + // ── TodoAggregator 集成:pageTemplate 填充路径 ──────────────────────────── + + @Mock + FlowableClient flowableClient; + @Mock + BusinessSummaryService businessSummaryService; + @Mock + PageTemplateService mockPageTemplateService; + + @InjectMocks + TodoAggregator aggregator; + + private FlowTaskDto task(String id, String businessKey) { + FlowTaskDto t = new FlowTaskDto(); + t.id = id; + t.name = "付款申请审批"; + t.assignee = "U1"; + t.businessKey = businessKey; + return t; + } + + @Test + void 聚合_有模板条目pageUrl正确填充() { + when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt())) + .thenReturn(List.of(task("T1", "BK-1"))); + when(businessSummaryService.summarize("BK-1")) + .thenReturn(new BusinessSummaryService.Summary("PM_PAYMENTAPPLY", Map.of("TITLE", "付款单"))); + when(mockPageTemplateService.findTemplateId("PM_PAYMENTAPPLY")) + .thenReturn(Optional.of("a18b1f38-073c-4190-bfd5-b95274cf9f31")); + + List items = aggregator.todo("U1", List.of(), 0, 50); + + assertThat(items).hasSize(1); + TodoItem it = items.get(0); + assertThat(it.pageTemplateId).isEqualTo("a18b1f38-073c-4190-bfd5-b95274cf9f31"); + assertThat(it.pageUrl).isEqualTo("/page/a18b1f38-073c-4190-bfd5-b95274cf9f31"); + assertThat(it.pageUrlReason).isNull(); + } + + @Test + void 聚合_无模板映射_pageUrl为null且reason含表名() { + when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt())) + .thenReturn(List.of(task("T2", "BK-2"))); + when(businessSummaryService.summarize("BK-2")) + .thenReturn(new BusinessSummaryService.Summary("OA_UNKNOWN_TABLE", Map.of())); + when(mockPageTemplateService.findTemplateId("OA_UNKNOWN_TABLE")) + .thenReturn(Optional.empty()); + + List items = aggregator.todo("U1", List.of(), 0, 50); + + assertThat(items).hasSize(1); + TodoItem it = items.get(0); + assertThat(it.pageTemplateId).isNull(); + assertThat(it.pageUrl).isNull(); + assertThat(it.pageUrlReason).contains("OA_UNKNOWN_TABLE"); + } + + @Test + void 聚合_businessTable为null_pageUrl为null且reason含提示() { + when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt())) + .thenReturn(List.of(task("T3", "BK-MISSING"))); + when(businessSummaryService.summarize("BK-MISSING")) + .thenReturn(BusinessSummaryService.Summary.EMPTY); + + List items = aggregator.todo("U1", List.of(), 0, 50); + + assertThat(items).hasSize(1); + TodoItem it = items.get(0); + assertThat(it.businessTable).isNull(); + assertThat(it.pageTemplateId).isNull(); + assertThat(it.pageUrl).isNull(); + assertThat(it.pageUrlReason).contains("businessTable"); + } + + @Test + void 聚合_有模板与无模板条目共存_优雅降级() { + when(flowableClient.todo(eq("U1"), any(), isNull(), anyInt(), anyInt())) + .thenReturn(List.of(task("T4", "BK-4"), task("T5", "BK-5"))); + when(businessSummaryService.summarize("BK-4")) + .thenReturn(new BusinessSummaryService.Summary("PM_PAYMENTAPPLY", Map.of("TITLE", "付款单"))); + when(businessSummaryService.summarize("BK-5")) + .thenReturn(BusinessSummaryService.Summary.EMPTY); + when(mockPageTemplateService.findTemplateId("PM_PAYMENTAPPLY")) + .thenReturn(Optional.of("tmpl-uuid-001")); + + List items = aggregator.todo("U1", List.of(), 0, 50); + + assertThat(items).hasSize(2); + // 第一条:有模板 + assertThat(items.get(0).pageUrl).isEqualTo("/page/tmpl-uuid-001"); + assertThat(items.get(0).pageUrlReason).isNull(); + // 第二条:无 businessTable,优雅降级 + assertThat(items.get(1).pageUrl).isNull(); + assertThat(items.get(1).pageUrlReason).isNotBlank(); + } +} diff --git a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/TodoAggregatorTest.java b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/TodoAggregatorTest.java index 70ed66f..90325e8 100644 --- a/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/TodoAggregatorTest.java +++ b/platform-flow-gateway/src/test/java/com/eip4/platform/flowgateway/TodoAggregatorTest.java @@ -17,11 +17,14 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import com.eip4.platform.flowgateway.aggregate.BusinessSummaryService; +import com.eip4.platform.flowgateway.aggregate.PageTemplateService; import com.eip4.platform.flowgateway.aggregate.TodoAggregator; import com.eip4.platform.flowgateway.aggregate.TodoItem; import com.eip4.platform.flowgateway.client.FlowTaskDto; import com.eip4.platform.flowgateway.client.FlowableClient; +import java.util.Optional; + /** * 待办聚合拼接单测:验证流程任务 + 业务摘要连接,以及摘要拼不到时优雅降级为仅流程字段。 */ @@ -32,6 +35,8 @@ class TodoAggregatorTest { FlowableClient flowableClient; @Mock BusinessSummaryService businessSummaryService; + @Mock + PageTemplateService pageTemplateService; @InjectMocks TodoAggregator aggregator;