package com.yunhai.flowcenter.service; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.yunhai.flowcenter.engine.FlowDefModel; import com.yunhai.platform.apistd.BizException; import com.yunhai.platform.utils.exception.ErrorCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Map; import java.util.Set; /** 发起前按冻结的定义契约校验;历史无声明版本保留原有的引用变量存在性规则。 */ @Slf4j @Component @RequiredArgsConstructor public class ApplyVariableValidator { private final ObjectMapper objectMapper; /** def 的全部 EXPRESSION 出边引用的业务变量键(去重保序) */ public Set referencedBusinessKeys(FlowDefModel model) { return com.yunhai.flowcenter.engine.VariableContract.referencedKeys(model); } public void requireVariablesPresent(FlowDefModel model, String formDataJson) { com.yunhai.flowcenter.engine.VariableContract.validateInput(model, parse(formDataJson, model.variables != null)); } /** 与 ConditionRouteService.parseVariables 同源:formData 顶层即变量;非对象 JSON=空(后续逐键报缺失) */ private Map parse(String formDataJson, boolean strict) { if (formDataJson == null || formDataJson.trim().isEmpty()) { return new java.util.HashMap<>(); } try { Map values = objectMapper.readValue(formDataJson, new TypeReference>() { }); if (values == null) { if (strict) throw new IllegalArgumentException("null object"); return new java.util.HashMap<>(); } return values; } catch (Exception e) { if (strict) throw new BizException(ErrorCode.PARAM_ERROR, "formData 必须为 JSON 对象"); return new java.util.HashMap<>(); } } }