C1第一刀:flw_def.form_uuid启用+发布期form-center schema快照嵌入formContract+表达式键查①红牌(audience按数组解析)
build / build-push (push) Failing after 2m6s
build / build-push (push) Failing after 2m6s
This commit is contained in:
@@ -103,7 +103,7 @@ public class FlowAdminController {
|
||||
return defAdminService.get(adminGuard.requireAdmin(user, null), id);
|
||||
}
|
||||
|
||||
/** 保存(新建草稿/编辑;已发布定义保存=升版本,flowGuid 不变;baseGuid=在指定流程线上开新版本草稿;changeLog=升版本改动说明,仅新版本行落库,>500 截断) */
|
||||
/** 保存(新建草稿/编辑;已发布定义保存=升版本,flowGuid 不变;baseGuid=在指定流程线上开新版本草稿;changeLog=升版本改动说明,仅新版本行落库,>500 截断;formUuid=关联表单定义 uuid(C1,2026-10-20):null=保持、空串=取消关联、发布后不可改改=升版) */
|
||||
@PostMapping("/def/save")
|
||||
public FlwDef defSave(BoaUser user, @RequestBody Map<String, Object> body) {
|
||||
Long schId = adminGuard.requireAdmin(user, null);
|
||||
@@ -111,7 +111,8 @@ public class FlowAdminController {
|
||||
return defAdminService.save(schId, id,
|
||||
(String) body.get("code"), (String) body.get("name"),
|
||||
(String) body.get("contentJson"), (String) body.get("baseGuid"),
|
||||
(String) body.get("appKey"), (String) body.get("changeLog"));
|
||||
(String) body.get("appKey"), (String) body.get("changeLog"),
|
||||
(String) body.get("formUuid"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -282,8 +282,12 @@ public final class DefValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/** 表达式内置变量白名单(ConditionRouteService.builtinEnv 注入的同名集) */
|
||||
private static final Set<String> EXPRESSION_BUILTINS = new HashSet<>(java.util.Arrays.asList(
|
||||
/**
|
||||
* 表达式内置变量白名单(ConditionRouteService.builtinEnv 注入的同名集)。<br>
|
||||
* 2026-10-20 C1 起 {@link com.yunhai.flowcenter.service.FlowDefAdminService} 的键查①红牌
|
||||
* 复用本集合(表达式变量 ⊆ 快照字段 ∪ 内置变量)——两处必须是同一份,内置集扩了这边自动跟上。
|
||||
*/
|
||||
public static final Set<String> EXPRESSION_BUILTINS = new HashSet<>(java.util.Arrays.asList(
|
||||
"schId", "instanceId", "instanceVersion", "bizType", "bizId",
|
||||
"nodeId", "nodeName", "submitterId"));
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ public class FlowDefAdminService {
|
||||
private final FlowDeptAdminService deptAdminService;
|
||||
private final FlowEngine flowEngine;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final FormCenterSchemaClient formCenterSchemaClient;
|
||||
|
||||
/** 定义列表:status/appKey 可选过滤(各流程线各版本都展示;appKey 精确匹配,2026-10-10 按应用筛选) */
|
||||
public List<FlwDef> list(Long schId, Integer status, String appKey) {
|
||||
@@ -169,7 +170,32 @@ public class FlowDefAdminService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 发布:静态校验(START/END 唯一、无孤儿无环、DEPT_KEY 有启用映射)后置为已发布 */
|
||||
/**
|
||||
* 保存重载(2026-10-20 C1 第一刀):formUuid=关联表单定义 uuid(form-center 对外身份,32 位)。<br>
|
||||
* <b>发布后不可改,改=升版本</b>由既有升版分派天然保证(非 DRAFT 直改必走升版新行);
|
||||
* 参数 null=保持不动(缺省语义,兼容旧调用面);空串=显式取消关联(归一 null 落库);
|
||||
* 非空=设置(不做格式强校验——寻址失败在发布期由快照拉取阻断,错误不进库但也不卡保存)。
|
||||
*/
|
||||
@Transactional
|
||||
public FlwDef save(Long schId, Long id, String code, String name, String contentJson,
|
||||
String baseGuid, String appKey, String changeLog, String formUuid) {
|
||||
FlwDef saved = save(schId, id, code, name, contentJson, baseGuid, appKey, changeLog);
|
||||
if (formUuid != null) {
|
||||
String clean = formUuid.trim();
|
||||
saved.setFormUuid(clean.isEmpty() ? null : clean);
|
||||
}
|
||||
try {
|
||||
return defRepository.save(saved);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
throw new BizException(ErrorCode.PARAM_ERROR, "保存冲突:该流程可能正被他人同时升版本,请返回列表刷新后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/** 发布:静态校验(START/END 唯一、无孤儿无环、DEPT_KEY 有启用映射)后置为已发布。<br>
|
||||
* 2026-10-20 C1 第一刀:def 关联了表单(form_uuid 非空)时,发布期拉 form-center schema 快照
|
||||
* 嵌入版本行 content_json.formContract(与 defId 快照纪律同构:在途办件按发起时快照校验),
|
||||
* 并做表达式<b>键查①</b>红牌(变量 ⊆ 快照字段 ∪ 内置变量)。拉取失败=发布阻断(可重试);
|
||||
* 运行期对 form-center 零依赖。form_uuid 为空(存量线/Mode C)行为逐字不变。 */
|
||||
@Transactional
|
||||
public FlwDef publish(Long schId, Long id) {
|
||||
FlwDef def = requireDef(schId, id);
|
||||
@@ -178,11 +204,88 @@ public class FlowDefAdminService {
|
||||
}
|
||||
FlowDefModel model = FlowDefModel.parse(objectMapper, def.getContentJson());
|
||||
DefValidator.validate(model, enabledDeptKeys(schId), enabledRouterKeys());
|
||||
if (def.getFormUuid() != null && !def.getFormUuid().trim().isEmpty()) {
|
||||
Map<String, Object> snapshot;
|
||||
try {
|
||||
snapshot = formCenterSchemaClient.fetchSchema(schId, def.getFormUuid().trim());
|
||||
} catch (FormCenterSchemaClient.SchemaUnavailableException e) {
|
||||
throw new BizException(ErrorCode.PARAM_ERROR,
|
||||
"关联表单快照拉取失败,发布已阻断(可重试):" + e.getMessage());
|
||||
}
|
||||
List<String> problems = new ArrayList<>();
|
||||
validateExpressionKeysAgainstSnapshot(model, snapshot, problems);
|
||||
if (!problems.isEmpty()) {
|
||||
throw new BizException(ErrorCode.PARAM_ERROR,
|
||||
"发布校验失败(表达式键查①):\n" + String.join("\n", problems));
|
||||
}
|
||||
def.setContentJson(embedFormContract(def.getContentJson(), snapshot));
|
||||
}
|
||||
def.setStatus(FlwEnums.DefStatus.PUBLISHED.getCode());
|
||||
def.setPublishTime(LocalDateTime.now());
|
||||
return defRepository.save(def);
|
||||
}
|
||||
|
||||
/**
|
||||
* 表达式键查①(C1 三查之一,红牌):对每条 EXPRESSION 判断的出边表达式,
|
||||
* 变量集 ⊆ 快照字段 keys ∪ 内置变量({@link DefValidator#EXPRESSION_BUILTINS})。
|
||||
* 不执行表达式、不改沙箱(口径与 v2 方案 §4.7 一致)。
|
||||
*/
|
||||
private void validateExpressionKeysAgainstSnapshot(FlowDefModel model, Map<String, Object> snapshot,
|
||||
List<String> problems) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> fields = (List<Map<String, Object>>) snapshot.get("fields");
|
||||
java.util.Set<String> fieldKeys = new java.util.HashSet<>();
|
||||
if (fields != null) {
|
||||
for (Map<String, Object> f : fields) {
|
||||
Object k = f.get("key");
|
||||
if (k != null) {
|
||||
fieldKeys.add(String.valueOf(k));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fieldKeys.isEmpty()) {
|
||||
problems.add("关联表单快照无字段(formContract.fields 为空):请检查表单是否已发布");
|
||||
return;
|
||||
}
|
||||
for (FlowDefModel.Node n : model.nodes) {
|
||||
if (n == null || !n.expressionJudge()) {
|
||||
continue;
|
||||
}
|
||||
for (FlowDefModel.Edge e : model.outEdges(n.id)) {
|
||||
if (e.condition == null || e.condition.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
List<String> vars;
|
||||
try {
|
||||
vars = com.yunhai.flowcenter.engine.ExpressionEvaluator.variableNames(e.condition.trim());
|
||||
} catch (Exception ex) {
|
||||
// 语法错已由 DefValidator 拦下,这里理论上到不了;防御口径跳过
|
||||
continue;
|
||||
}
|
||||
for (String v : vars) {
|
||||
if (!fieldKeys.contains(v) && !DefValidator.EXPRESSION_BUILTINS.contains(v)) {
|
||||
problems.add("节点「" + n.name + "」出口「" + e.name + "」表达式变量 " + v
|
||||
+ " 不在关联表单字段集内(快照 " + fieldKeys.size() + " 个字段)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** content_json 顶层嵌入 formContract 节(ObjectNode 保字段顺序;原值存在则整体替换=本版快照) */
|
||||
private String embedFormContract(String contentJson, Map<String, Object> snapshot) {
|
||||
try {
|
||||
com.fasterxml.jackson.databind.node.ObjectNode root = (com.fasterxml.jackson.databind.node.ObjectNode)
|
||||
objectMapper.readTree(contentJson);
|
||||
root.set("formContract", objectMapper.valueToTree(snapshot));
|
||||
return objectMapper.writeValueAsString(root);
|
||||
} catch (BizException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BizException(ErrorCode.PARAM_ERROR, "快照嵌入失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 停用:不可新发起,存量跑完 */
|
||||
@Transactional
|
||||
public void offline(Long schId, Long id) {
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
package com.yunhai.flowcenter.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yunhai.platform.openapi.core.YschOpenCall;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* form-center schema 快照客户端(2026-10-20 C1 第一刀:发布期契约快照)。<br>
|
||||
* GET {目录解析 form-center}/oapi/form/schema?schId=&uuid= ——按 form_uuid 拉当前已发布版 schema
|
||||
* (T11 建立的 by-uuid 形态;version 缺省=当前发布版)。调用形态对齐 {@link RouteJudgeClient}:
|
||||
* 身份=本应用 appKey(签名拦截器由 {@link YschOpenCall} 装配链注入)。<br>
|
||||
* <b>只在发布期调用</b>(拉取失败=发布阻断,可重试);运行期对 form-center 零依赖(快照嵌 def 版本行)。<br>
|
||||
* audience 解析:<b>契约 v2 C5/G3 起是字符串数组</b>(breaking,T13 切换)——本客户端按数组读取,
|
||||
* 快照不消费人群语义、只透传形态,兼容旧单值串(读到串自动包一层数组,迁移窗口容错)。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FormCenterSchemaClient {
|
||||
|
||||
/** 服务目录 key(yh.open.services.form-center / targets 覆盖 / 枚举兜底三级解析) */
|
||||
static final String TARGET = "form-center";
|
||||
static final String SCHEMA_PATH = "/oapi/form/schema";
|
||||
/** 快照是发布期一次性动作,超时给足(对照 RouteJudgeClient 的 3s 运行期预算) */
|
||||
static final int TIMEOUT_MS = 10_000;
|
||||
static final int MAX_ATTEMPTS = 2;
|
||||
|
||||
private final YschOpenCall openCall;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public FormCenterSchemaClient(YschOpenCall openCall, ObjectMapper objectMapper) {
|
||||
this.openCall = openCall;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/** 快照不可用(网络/信封/形态异常);发布期转 BizException 阻断,可重试 */
|
||||
public static class SchemaUnavailableException extends RuntimeException {
|
||||
public SchemaUnavailableException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布期快照:{formUuid, formVersion, fields:[{key,type,required,enum?}], audience(数组)}。
|
||||
* fields 由 groups 扁平化(分组只是展示结构,表达式键域与分组无关);
|
||||
* select/radio 类字段的 options 值域收进 enum(供域查③,本轮仅透传)。
|
||||
*/
|
||||
public Map<String, Object> fetchSchema(Long schId, String formUuid) {
|
||||
RuntimeException last = null;
|
||||
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
Map<String, String> query = new HashMap<>();
|
||||
query.put("schId", String.valueOf(schId));
|
||||
query.put("uuid", formUuid);
|
||||
Map<String, Object> data = openCall.get(TARGET, SCHEMA_PATH, query, Map.class);
|
||||
if (data == null) {
|
||||
throw new IllegalStateException("schema 返回空 data");
|
||||
}
|
||||
return toSnapshot(data);
|
||||
} catch (RuntimeException e) {
|
||||
last = e;
|
||||
log.warn("[flow-center] form-center schema 快照拉取失败(第 {}/{} 次):uuid={} {}",
|
||||
attempt, MAX_ATTEMPTS, formUuid, e.toString());
|
||||
} catch (Exception e) {
|
||||
last = new IllegalStateException(e.toString(), e);
|
||||
log.warn("[flow-center] form-center schema 快照拉取异常(第 {}/{} 次):uuid={} {}",
|
||||
attempt, MAX_ATTEMPTS, formUuid, e.toString());
|
||||
}
|
||||
}
|
||||
throw new SchemaUnavailableException(
|
||||
"关联表单 schema 快照拉取失败(已重试 " + MAX_ATTEMPTS + " 次,发布阻断,可重试)", last);
|
||||
}
|
||||
|
||||
/** SchemaView(data) → 快照节(fields 扁平化 + audience 数组归一) */
|
||||
private Map<String, Object> toSnapshot(Map<String, Object> data) {
|
||||
Map<String, Object> snap = new LinkedHashMap<>();
|
||||
snap.put("formUuid", data.get("formUuid"));
|
||||
snap.put("formVersion", data.get("version"));
|
||||
snap.put("audience", normalizeAudience(data.get("audience")));
|
||||
List<Map<String, Object>> fields = new ArrayList<>();
|
||||
JsonNode groups = data.get("groups") == null ? null
|
||||
: objectMapper.valueToTree(data.get("groups"));
|
||||
if (groups != null && groups.isArray()) {
|
||||
for (JsonNode group : groups) {
|
||||
JsonNode groupFields = group.get("fields");
|
||||
if (groupFields == null || !groupFields.isArray()) {
|
||||
continue;
|
||||
}
|
||||
for (JsonNode f : groupFields) {
|
||||
Map<String, Object> field = new LinkedHashMap<>();
|
||||
field.put("key", f.path("key").asText(null));
|
||||
field.put("type", f.path("type").asText(null));
|
||||
JsonNode required = f.get("required");
|
||||
field.put("required", required == null ? Boolean.FALSE : required.asBoolean(false));
|
||||
JsonNode options = f.get("options");
|
||||
if (options != null && options.isArray() && options.size() > 0) {
|
||||
List<String> enums = new ArrayList<>();
|
||||
for (JsonNode opt : options) {
|
||||
JsonNode v = opt.has("value") ? opt.get("value") : opt.get("label");
|
||||
if (v != null) {
|
||||
enums.add(v.asText());
|
||||
}
|
||||
}
|
||||
field.put("enum", enums);
|
||||
}
|
||||
fields.add(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
snap.put("fields", fields);
|
||||
return snap;
|
||||
}
|
||||
|
||||
/** audience 数组归一:数组照读;旧单值串包一层(迁移窗口容错);null → 空数组 */
|
||||
private List<String> normalizeAudience(Object audience) {
|
||||
List<String> out = new ArrayList<>();
|
||||
if (audience instanceof Iterable) {
|
||||
for (Object a : (Iterable<?>) audience) {
|
||||
if (a != null) {
|
||||
out.add(String.valueOf(a));
|
||||
}
|
||||
}
|
||||
} else if (audience != null) {
|
||||
out.add(String.valueOf(audience));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user