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 得出):
+ *
+ * - {@code tablename} → {@code sys_entity.sys_entity_id}(entityid)
+ * - {@code entityid} → {@code view_templateentity.templateid}(经 {@code view_template.type} 优先级筛选)
+ *
+ *
+ * 多模板优先级规则(由 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;