feat(design): GET /api/design/flows 列出实体默认流预览(建物3-Task10)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 10:05:30 +08:00
parent 05181d75bf
commit 9765872382
3 changed files with 189 additions and 2 deletions

View File

@@ -0,0 +1,98 @@
package com.eip4.platform.metadata.publish;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* 默认服务流「预览/持久态」读取(建物 3 · 发布前人工确认)。
*
* <p>flowName 与 {@code com.dsasp.server.core.service.flow.model.DefaultFlowHelper}
* 的 Flow id 完全一致({@code <TABLE>_ADD} 等),在 SYS_SERVICEFLOW 中即主键 sys_serviceflow_id。
*
* <ul>
* <li>实体尚无流(SYS_SERVICEFLOW 无 entityid 行)→ 返回合成名预览,
* 含 STATUS 列时 +2(SUBMIT / SAVE_AND_SUBMIT)。</li>
* <li>实体已有流 → 从 SYS_SERVICEFLOW 读真实行(sys_serviceflow_id + displayname)。</li>
* </ul>
*
* <p>不引入保真层重型 POInfo:STATUS 列存在性用元数据 SQL 判断,避免预览阶段触发 POInfo 缓存。
*/
@Service
public class DefaultFlowPreviewService {
/** 5 个基础流的有序后缀 → kind / 显名映射。 */
private static final String[][] BASE_FLOWS = {
{"_ADD", "ADD", "新增"},
{"_DELETE", "DELETE", "删除"},
{"_UPDATE", "UPDATE", "更新"},
{"_SEARCH", "SEARCH", "查询"},
{"_LOAD", "LOAD", "加载"},
};
/** 含 STATUS 列时追加的 2 个提交流。 */
private static final String[][] STATUS_FLOWS = {
{"_SUBMIT", "SUBMIT", "提交审批"},
{"_SAVE_AND_SUBMIT", "SAVE_AND_SUBMIT", "保存并提交审批"},
};
private final JdbcTemplate jdbcTemplate;
public DefaultFlowPreviewService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public record FlowItem(String flowName, String displayName, String kind) {}
public record FlowsPreview(String entityId, String tableName, boolean persisted, List<FlowItem> flows) {}
public FlowsPreview preview(String entityId) {
Integer existing = jdbcTemplate.queryForObject(
"SELECT COUNT(*) FROM SYS_SERVICEFLOW WHERE entityid=?", Integer.class, entityId);
String tableName = jdbcTemplate.queryForObject(
"SELECT TRIM(tablename) FROM SYS_ENTITY WHERE TRIM(sys_entity_id)=?", String.class, entityId);
if (existing != null && existing >= 1) {
return new FlowsPreview(entityId, tableName, true, persistedFlows(entityId));
}
return new FlowsPreview(entityId, tableName, false, synthesizedFlows(entityId, tableName));
}
private List<FlowItem> synthesizedFlows(String entityId, String tableName) {
List<FlowItem> out = new ArrayList<>();
for (String[] f : BASE_FLOWS) {
out.add(new FlowItem(tableName + f[0], tableName + " " + f[2], f[1]));
}
Integer hasStatus = jdbcTemplate.queryForObject(
"SELECT COUNT(*) FROM SYS_ENTITYATTRIBUTE WHERE entityid=? AND UPPER(TRIM(columnname))='STATUS'",
Integer.class, entityId);
if (hasStatus != null && hasStatus >= 1) {
for (String[] f : STATUS_FLOWS) {
out.add(new FlowItem(tableName + f[0], tableName + " " + f[2], f[1]));
}
}
return out;
}
private List<FlowItem> persistedFlows(String entityId) {
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
"SELECT TRIM(sys_serviceflow_id) AS fid, displayname FROM SYS_SERVICEFLOW WHERE entityid=?"
+ " ORDER BY sys_serviceflow_id", entityId);
List<FlowItem> out = new ArrayList<>();
for (Map<String, Object> r : rows) {
String fid = String.valueOf(r.get("fid"));
String display = r.get("displayname") == null ? fid : String.valueOf(r.get("displayname"));
out.add(new FlowItem(fid, display, kindOf(fid)));
}
return out;
}
private static String kindOf(String flowName) {
int us = flowName.indexOf('_');
if (us < 0) {
return "UNKNOWN";
}
return flowName.substring(us + 1);
}
}

View File

@@ -56,26 +56,30 @@ public class DesignFeatureController {
private final TypedPageSchemaValidator strictValidator;
private final JdbcTemplate jdbcTemplate; // 仅 requireAdmin 用
private final boolean skipAdminCheck; // 仅单测置 true
private final com.eip4.platform.metadata.publish.DefaultFlowPreviewService flowPreviewService; // Task 10
/** 生产构造(Spring 注入)。 */
public DesignFeatureController(JdbcTemplate jdbcTemplate,
PageSchemaWriter pageSchemaWriter,
EntityAttributeReader attributeReader) {
EntityAttributeReader attributeReader,
com.eip4.platform.metadata.publish.DefaultFlowPreviewService flowPreviewService) {
this.jdbcTemplate = jdbcTemplate;
this.pageSchemaWriter = pageSchemaWriter;
this.attributeReader = attributeReader;
this.flowPreviewService = flowPreviewService;
this.composer = new RecipePageComposer(MAPPER);
this.strictValidator = TypedPageSchemaValidator.defaults();
this.skipAdminCheck = false;
}
/** 包级测试构造:脱库 + 可旁路 admin 门禁。 */
/** 包级测试构造:脱库 + 可旁路 admin 门禁(发布编排依赖在 compile 单测中置 null)。 */
DesignFeatureController(PageSchemaWriter pageSchemaWriter,
EntityAttributeReader attributeReader,
boolean skipAdminCheck) {
this.jdbcTemplate = null;
this.pageSchemaWriter = pageSchemaWriter;
this.attributeReader = attributeReader;
this.flowPreviewService = null;
this.composer = new RecipePageComposer(MAPPER);
this.strictValidator = TypedPageSchemaValidator.defaults();
this.skipAdminCheck = skipAdminCheck;
@@ -160,6 +164,33 @@ public class DesignFeatureController {
return isBlank(req.entityName) ? req.title.trim() : req.entityName.trim();
}
@Operation(summary = "列出实体的 5(+2) 条默认服务流供人工确认(仅管理员)",
description = "未落库则合成预览(含 STATUS 列 +2),已落库则读 SYS_SERVICEFLOW 真实行")
@org.springframework.web.bind.annotation.GetMapping("/flows")
public Map<String, Object> flows(
@org.springframework.web.bind.annotation.RequestParam("entityId") String entityId) {
requireAdmin();
com.eip4.platform.metadata.publish.DefaultFlowPreviewService.FlowsPreview p =
flowPreviewService.preview(entityId);
List<Map<String, Object>> flowList = new ArrayList<>();
for (com.eip4.platform.metadata.publish.DefaultFlowPreviewService.FlowItem f : p.flows()) {
Map<String, Object> m = new LinkedHashMap<>();
m.put("flowName", f.flowName());
m.put("displayName", f.displayName());
m.put("kind", f.kind());
flowList.add(m);
}
Map<String, Object> data = new LinkedHashMap<>();
data.put("entityId", p.entityId());
data.put("tableName", p.tableName());
data.put("persisted", p.persisted());
data.put("flows", flowList);
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("status", 0);
resp.put("data", data);
return resp;
}
// ===== admin 门禁(逐字对齐 PageSchemaController.requireAdmin / currentUserId)=====
private void requireAdmin() {

View File

@@ -0,0 +1,58 @@
package com.eip4.platform.metadata.publish;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
class DefaultFlowPreviewServiceTest {
@Test
void synthesizesFiveFlowsWhenNoServiceflowAndNoStatusColumn() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
// 无持久流
when(jdbc.queryForObject(
eq("SELECT COUNT(*) FROM SYS_SERVICEFLOW WHERE entityid=?"),
eq(Integer.class), eq("ENT-1"))).thenReturn(0);
// 实体表名
when(jdbc.queryForObject(
eq("SELECT TRIM(tablename) FROM SYS_ENTITY WHERE TRIM(sys_entity_id)=?"),
eq(String.class), eq("ENT-1"))).thenReturn("DEMO_TBL");
// 无 STATUS 列
when(jdbc.queryForObject(
eq("SELECT COUNT(*) FROM SYS_ENTITYATTRIBUTE WHERE entityid=? AND UPPER(TRIM(columnname))='STATUS'"),
eq(Integer.class), eq("ENT-1"))).thenReturn(0);
DefaultFlowPreviewService svc = new DefaultFlowPreviewService(jdbc);
DefaultFlowPreviewService.FlowsPreview preview = svc.preview("ENT-1");
assertThat(preview.persisted()).isFalse();
assertThat(preview.tableName()).isEqualTo("DEMO_TBL");
assertThat(preview.flows()).extracting(DefaultFlowPreviewService.FlowItem::flowName)
.containsExactly("DEMO_TBL_ADD", "DEMO_TBL_DELETE", "DEMO_TBL_UPDATE",
"DEMO_TBL_SEARCH", "DEMO_TBL_LOAD");
assertThat(preview.flows().get(0).kind()).isEqualTo("ADD");
}
@Test
void synthesizesSevenFlowsWhenStatusColumnPresent() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
when(jdbc.queryForObject(
eq("SELECT COUNT(*) FROM SYS_SERVICEFLOW WHERE entityid=?"),
eq(Integer.class), eq("ENT-2"))).thenReturn(0);
when(jdbc.queryForObject(
eq("SELECT TRIM(tablename) FROM SYS_ENTITY WHERE TRIM(sys_entity_id)=?"),
eq(String.class), eq("ENT-2"))).thenReturn("OA_DOC");
when(jdbc.queryForObject(
eq("SELECT COUNT(*) FROM SYS_ENTITYATTRIBUTE WHERE entityid=? AND UPPER(TRIM(columnname))='STATUS'"),
eq(Integer.class), eq("ENT-2"))).thenReturn(1);
DefaultFlowPreviewService.FlowsPreview p = new DefaultFlowPreviewService(jdbc).preview("ENT-2");
assertThat(p.flows()).extracting(DefaultFlowPreviewService.FlowItem::flowName)
.contains("OA_DOC_SUBMIT", "OA_DOC_SAVE_AND_SUBMIT");
assertThat(p.flows()).hasSize(7);
}
}