553 lines
24 KiB
Python
553 lines
24 KiB
Python
"""In-memory view of a published process definition.
|
||
|
||
Equivalent to the ``Processing`` object that O2OA builds from the designer XML
|
||
before handing it to the ``x_processplatform_service_processing`` engine.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass, field
|
||
|
||
from sqlalchemy import select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from .. import models
|
||
|
||
ACTIVITY_BEGIN = "begin"
|
||
ACTIVITY_MANUAL = "manual"
|
||
ACTIVITY_CHOICE = "choice"
|
||
ACTIVITY_PARALLEL = "parallel"
|
||
ACTIVITY_MERGE = "merge"
|
||
ACTIVITY_SPLIT = "split"
|
||
ACTIVITY_SCRIPT = "script"
|
||
ACTIVITY_INVOKE = "invoke"
|
||
ACTIVITY_EMBED = "embed"
|
||
ACTIVITY_DELAY = "delay"
|
||
ACTIVITY_END = "end"
|
||
ACTIVITY_CANCEL = "cancel"
|
||
|
||
MANUAL_MODES = {"any", "all", "join", "serial"}
|
||
_FORM_PERMS = {"hidden", "readonly", "editable", "required"}
|
||
|
||
ACTIVITY_TYPES = {
|
||
ACTIVITY_BEGIN,
|
||
ACTIVITY_MANUAL,
|
||
ACTIVITY_CHOICE,
|
||
ACTIVITY_PARALLEL,
|
||
ACTIVITY_MERGE,
|
||
ACTIVITY_SPLIT,
|
||
ACTIVITY_SCRIPT,
|
||
ACTIVITY_INVOKE,
|
||
ACTIVITY_EMBED,
|
||
ACTIVITY_DELAY,
|
||
ACTIVITY_END,
|
||
ACTIVITY_CANCEL,
|
||
}
|
||
|
||
|
||
@dataclass
|
||
class ActivityNode:
|
||
id: str
|
||
type: str
|
||
name: str
|
||
alias: str = ""
|
||
description: str = ""
|
||
form: str = ""
|
||
config: dict = field(default_factory=dict)
|
||
x: float = 0.0
|
||
y: float = 0.0
|
||
order_number: int = 0
|
||
|
||
@property
|
||
def manual_mode(self) -> str:
|
||
return str(self.config.get("mode") or "any")
|
||
|
||
@property
|
||
def split_mode(self) -> str:
|
||
return str(self.config.get("mode") or "serial")
|
||
|
||
|
||
@dataclass
|
||
class RouteNode:
|
||
id: str
|
||
name: str
|
||
alias: str
|
||
from_activity: str
|
||
to_activity: str
|
||
decision: str = ""
|
||
expression: str = ""
|
||
order_number: int = 0
|
||
|
||
|
||
@dataclass
|
||
class ProcessDefinition:
|
||
process_id: str
|
||
name: str
|
||
alias: str = ""
|
||
category: str = ""
|
||
category_name: str = ""
|
||
latest_form: str = ""
|
||
version: int = 1
|
||
activities: dict[str, ActivityNode] = field(default_factory=dict)
|
||
routes: list[RouteNode] = field(default_factory=list)
|
||
outbound: dict[str, list[RouteNode]] = field(default_factory=dict)
|
||
inbound: dict[str, list[RouteNode]] = field(default_factory=dict)
|
||
begin: ActivityNode | None = None
|
||
#: 流程级审批规则默认值(``skipStarter`` / ``dedup`` / ``dedupAhead``),
|
||
#: 见 models.Process.approval_config。
|
||
approval: dict = field(default_factory=dict)
|
||
#: 流程标题生成规则,见 models.Process.title_config 与 service/titles.py。
|
||
title: dict = field(default_factory=dict)
|
||
|
||
# -- lookup helpers ---------------------------------------------------
|
||
def activity(self, activity_id: str) -> ActivityNode | None:
|
||
return self.activities.get(activity_id)
|
||
|
||
def activity_by_name(self, name: str) -> ActivityNode | None:
|
||
for node in self.activities.values():
|
||
if node.name == name or node.alias == name or node.id == name:
|
||
return node
|
||
return None
|
||
|
||
def routes_from(self, activity_id: str) -> list[RouteNode]:
|
||
return sorted(self.outbound.get(activity_id, []), key=lambda r: (r.order_number, r.name))
|
||
|
||
def routes_to(self, activity_id: str) -> list[RouteNode]:
|
||
return self.inbound.get(activity_id, [])
|
||
|
||
def route(self, route_id: str) -> RouteNode | None:
|
||
for node in self.routes:
|
||
if node.id == route_id or node.name == route_id or node.alias == route_id:
|
||
return node
|
||
return None
|
||
|
||
def find_routes(self, activity_id: str, route_name: str) -> list[RouteNode]:
|
||
"""本环节上叫得出这个名字的全部出口,顺序同 ``routes_from``。
|
||
|
||
同名路由是允许的:它们在待办界面上就是同一个按钮,到底走哪一条由路由
|
||
自己写的条件决定(挑法见 ``engine._route_by_condition``)。所以按名字找
|
||
路由要回全部候选让调用方去挑,而不是悄悄取第一条。
|
||
"""
|
||
if not route_name:
|
||
return []
|
||
return [
|
||
item
|
||
for item in self.routes_from(activity_id)
|
||
if route_name in (item.id, item.name, item.alias, item.decision)
|
||
]
|
||
|
||
def find_route(self, activity_id: str, route_name: str) -> RouteNode | None:
|
||
rows = self.find_routes(activity_id, route_name)
|
||
return rows[0] if rows else None
|
||
|
||
@property
|
||
def manual_activities(self) -> list[ActivityNode]:
|
||
return [a for a in self.activities.values() if a.type == ACTIVITY_MANUAL]
|
||
|
||
def form_ids(self) -> list[str]:
|
||
ids = [self.latest_form]
|
||
ids.extend(a.form for a in self.activities.values() if a.form)
|
||
return [i for i in ids if i]
|
||
|
||
|
||
def load(db: Session, process_id: str) -> ProcessDefinition:
|
||
process = db.get(models.Process, process_id)
|
||
if process is None:
|
||
raise ValueError(f"流程不存在: {process_id}")
|
||
definition = ProcessDefinition(
|
||
process_id=process.id,
|
||
name=process.name,
|
||
alias=process.alias or process.name,
|
||
category=process.category,
|
||
category_name=process.category_name,
|
||
latest_form=process.latest_form,
|
||
version=process.version,
|
||
approval=dict(getattr(process, "approval_config", None) or {}),
|
||
title=dict(getattr(process, "title_config", None) or {}),
|
||
)
|
||
activities = db.execute(
|
||
select(models.Activity).where(models.Activity.process == process.id)
|
||
).scalars().all()
|
||
for row in activities:
|
||
definition.activities[row.id] = ActivityNode(
|
||
id=row.id,
|
||
type=(row.type or "manual").lower(),
|
||
name=row.name,
|
||
alias=row.alias or row.name,
|
||
description=row.description or "",
|
||
form=row.form or "",
|
||
config=dict(row.config or {}),
|
||
x=row.x,
|
||
y=row.y,
|
||
order_number=row.order_number,
|
||
)
|
||
routes = db.execute(
|
||
select(models.Route)
|
||
.where(models.Route.process == process.id)
|
||
.order_by(models.Route.order_number, models.Route.name)
|
||
).scalars().all()
|
||
for row in routes:
|
||
node = RouteNode(
|
||
id=row.id,
|
||
name=row.name,
|
||
alias=row.alias or row.name,
|
||
from_activity=row.from_activity,
|
||
to_activity=row.to_activity,
|
||
decision=row.decision or "",
|
||
expression=row.expression or "",
|
||
order_number=row.order_number,
|
||
)
|
||
definition.routes.append(node)
|
||
definition.outbound.setdefault(node.from_activity, []).append(node)
|
||
definition.inbound.setdefault(node.to_activity, []).append(node)
|
||
for node in definition.activities.values():
|
||
if node.type == ACTIVITY_BEGIN:
|
||
definition.begin = node
|
||
break
|
||
if definition.begin is None and definition.activities:
|
||
definition.begin = next(iter(definition.activities.values()))
|
||
return definition
|
||
|
||
|
||
def validate(definition: ProcessDefinition) -> list[str]:
|
||
"""Designer side validation, returns a list of human readable problems."""
|
||
problems: list[str] = []
|
||
begins = [a for a in definition.activities.values() if a.type == ACTIVITY_BEGIN]
|
||
ends = [a for a in definition.activities.values() if a.type == ACTIVITY_END]
|
||
if not begins:
|
||
problems.append("缺少开始环节")
|
||
if len(begins) > 1:
|
||
problems.append("只能有一个开始环节")
|
||
if not ends:
|
||
problems.append("缺少结束环节")
|
||
for node in definition.activities.values():
|
||
if node.type not in ACTIVITY_TYPES:
|
||
problems.append(f"环节 {node.name} 类型未知: {node.type}")
|
||
if node.type == ACTIVITY_MANUAL and not definition.routes_from(node.id):
|
||
problems.append(f"人工环节 {node.name} 没有出口路由")
|
||
if node.type in {ACTIVITY_END, ACTIVITY_CANCEL} and definition.routes_from(node.id):
|
||
problems.append(f"{node.name} 是结束类环节,不能有出口路由")
|
||
same_name: dict[tuple[str, str], list[RouteNode]] = {}
|
||
for route in definition.routes:
|
||
if route.from_activity not in definition.activities:
|
||
problems.append(f"路由 {route.name} 的起点不存在")
|
||
continue
|
||
if route.to_activity not in definition.activities:
|
||
problems.append(f"路由 {route.name} 的终点不存在")
|
||
continue
|
||
same_name.setdefault((route.from_activity, route.name), []).append(route)
|
||
problems.extend(_check_route_names(definition, same_name))
|
||
for node in definition.activities.values():
|
||
if node.type == ACTIVITY_CHOICE:
|
||
routes = definition.routes_from(node.id)
|
||
if len(routes) < 2:
|
||
problems.append(f"分支环节 {node.name} 至少需要两条路由")
|
||
if not any(not (r.expression or "").strip() for r in routes):
|
||
problems.append(f"分支环节 {node.name} 需要一条无条件路由作为默认分支")
|
||
if node.type == ACTIVITY_PARALLEL and len(definition.routes_from(node.id)) < 2:
|
||
problems.append(f"平行环节 {node.name} 至少需要两条路由")
|
||
if node.type == ACTIVITY_MERGE and len(definition.routes_to(node.id)) < 2:
|
||
problems.append(f"汇总环节 {node.name} 至少需要两条入口路由")
|
||
problems.extend(_check_approver_policy(definition))
|
||
problems.extend(_check_approval_defaults(definition))
|
||
problems.extend(_check_approver_targets(definition))
|
||
problems.extend(_check_skip_rules(definition))
|
||
problems.extend(_check_must_rules(definition))
|
||
problems.extend(_check_cc_rules(definition))
|
||
problems.extend(_check_reachable(definition))
|
||
return problems
|
||
|
||
|
||
def _check_route_names(
|
||
definition: ProcessDefinition, same_name: dict[tuple[str, str], list[RouteNode]]
|
||
) -> list[str]:
|
||
"""同名出口是允许的(界面上就是同一个按钮),但有两件事不能含糊。
|
||
|
||
过去一律禁止同名,逼得设计者把每条分支上的按钮都改成不同名字,审批人看到的
|
||
按钮就成了「同意-大额」「同意-常规」这种一看就不像同一个动作的东西。放开之后
|
||
靠两条守住底线:
|
||
|
||
1. 语义要一致——一个按钮不能既是「同意」又是「退回」,否则这张票算哪一票说不清;
|
||
2. 走哪条要分得开——同名里最多留一条不写条件的当默认,其余必须写条件,
|
||
否则运行时面对两条完全一样的候选只能瞎挑。
|
||
"""
|
||
problems: list[str] = []
|
||
for (source_id, name), siblings in same_name.items():
|
||
if len(siblings) < 2:
|
||
continue
|
||
source = definition.activities[source_id]
|
||
decisions = {(route.decision or "").strip() for route in siblings}
|
||
if len(decisions) > 1:
|
||
shown = "、".join(sorted(decision or "未设置" for decision in decisions))
|
||
problems.append(
|
||
f"环节 {source.name} 的同名路由「{name}」语义不一致({shown}):"
|
||
"同一个按钮只能是一个意思,请改名字或者改成同一个决定"
|
||
)
|
||
defaults = [route for route in siblings if not (route.expression or "").strip()]
|
||
if len(defaults) > 1:
|
||
problems.append(
|
||
f"环节 {source.name} 有 {len(defaults)} 条同名的路由「{name}」都没写条件,"
|
||
"流转时分不出该走哪条:只留一条不写条件当默认,其余的写上条件"
|
||
)
|
||
return problems
|
||
|
||
|
||
def _check_approver_targets(definition: ProcessDefinition) -> list[str]:
|
||
"""审批人来源:组织层级那几种必须带参数,否则运行时一定解析不出人。"""
|
||
problems: list[str] = []
|
||
for node in definition.activities.values():
|
||
rows = (node.config or {}).get("taskValues")
|
||
if rows in (None, ""):
|
||
continue
|
||
if not isinstance(rows, list):
|
||
problems.append(f"环节 {node.name} 的审批人设置格式不对")
|
||
continue
|
||
problems.extend(approver_row_problems(node.name, rows))
|
||
return problems
|
||
|
||
|
||
def approver_row_problems(node_name: str, rows: object, subject: str = "审批人") -> list[str]:
|
||
"""校验一批「找人」配置行(环节审批人、跳过对象、抄送对象都是这个形状)。
|
||
|
||
``subject`` 只影响报错里那句主语:抄送对象出问题时说成「审批人设置」会把人带偏。
|
||
"""
|
||
from ..service import org # 延迟导入,避免 service 与 engine 相互引用成环
|
||
|
||
problems: list[str] = []
|
||
for item in rows if isinstance(rows, list) else []:
|
||
if not isinstance(item, dict):
|
||
problems.append(f"环节 {node_name} 有{subject}设置格式不对")
|
||
continue
|
||
kind = org.normalize_kind(item.get("type"))
|
||
data = [str(v) for v in (item.get("data") or []) if str(v or "").strip()]
|
||
if kind == "unitManager" and not data:
|
||
problems.append(f"环节 {node_name} 选了「指定部门负责人」但还没选部门")
|
||
elif kind == "creatorLeaderChain":
|
||
depth = next((v for v in data if v.isdigit()), "")
|
||
if depth and not 1 <= int(depth) <= org.MAX_LEADER_CHAIN:
|
||
problems.append(
|
||
f"环节 {node_name} 的连续上级层数要在 1-{org.MAX_LEADER_CHAIN} 级之间"
|
||
)
|
||
elif kind == "form" and not data:
|
||
# 设计器现在从字段清单里挑(见 utils/approverTargets.ts 的 formFieldPathOptions),
|
||
# 空着等于这个环节谁也找不到,别让它悄悄跑到运行时才发现。
|
||
problems.append(f"环节 {node_name} 选了「表单字段」找人但还没挑字段")
|
||
return problems
|
||
|
||
|
||
def _check_skip_rules(definition: ProcessDefinition) -> list[str]:
|
||
"""条件跳过审批人:条件得能编译,「只免其中几个人」还得真挑得出要免谁。
|
||
|
||
没设条件的规则在运行时按不命中处理(引擎那边宁可多批不敢乱免),但那是
|
||
一条永远不生效的配置,设计期就该报出来,而不是等上线后才发现免审没生效。
|
||
"""
|
||
from . import expressions # 与 expressions 同包,放函数里只为和其它检查保持一致
|
||
|
||
problems: list[str] = []
|
||
for node in definition.activities.values():
|
||
if node.type != ACTIVITY_MANUAL:
|
||
continue
|
||
rules = (node.config or {}).get("skipRules")
|
||
if rules in (None, ""):
|
||
continue
|
||
if not isinstance(rules, list):
|
||
problems.append(f"环节 {node.name} 的跳过设置格式不对")
|
||
continue
|
||
for index, rule in enumerate(rules, start=1):
|
||
if not isinstance(rule, dict):
|
||
problems.append(f"环节 {node.name} 有跳过设置格式不对")
|
||
continue
|
||
# 一个环节可以配好几条规则,报错得说清是哪一条,否则只有「规则名」能当门牌
|
||
label = rule_label(node.name, "跳过", index, rule)
|
||
scope = str(rule.get("scope") or "matched")
|
||
if scope not in {"matched", "all"}:
|
||
problems.append(f"{label}的跳过范围不支持:{scope}")
|
||
expression = str(rule.get("expression") or "").strip()
|
||
if not expression:
|
||
problems.append(f"{label}还没设条件")
|
||
continue
|
||
error = expressions.validate_script(expression)
|
||
if error:
|
||
problems.append(f"{label}的条件写错了:{error}")
|
||
if scope == "matched" and not (rule.get("taskValues") or []):
|
||
problems.append(f"{label}选了「只免其中几个人」但还没选免谁")
|
||
problems.extend(approver_row_problems(node.name, rule.get("taskValues")))
|
||
return problems
|
||
|
||
|
||
def rule_label(node_name: str, kind: str, index: int, rule: dict) -> str:
|
||
"""规则报错的门牌:有名字用名字,没名字用第几条(两处规则共用一套说法)。"""
|
||
name = str(rule.get("name") or "").strip()
|
||
head = f"环节 {node_name} 的{kind}规则「{name}」" if name else f"环节 {node_name} 的第 {index} 条{kind}规则"
|
||
return head
|
||
|
||
|
||
def _check_must_rules(definition: ProcessDefinition) -> list[str]:
|
||
"""条件必批:和免审规则一样,条件要能编译,而且必须点得出"谁来批"。"""
|
||
from . import expressions
|
||
|
||
problems: list[str] = []
|
||
for node in definition.activities.values():
|
||
if node.type != ACTIVITY_MANUAL:
|
||
continue
|
||
rules = (node.config or {}).get("mustRules")
|
||
if rules in (None, ""):
|
||
continue
|
||
if not isinstance(rules, list):
|
||
problems.append(f"环节 {node.name} 的必批设置格式不对")
|
||
continue
|
||
for index, rule in enumerate(rules, start=1):
|
||
if not isinstance(rule, dict):
|
||
problems.append(f"环节 {node.name} 有必批设置格式不对")
|
||
continue
|
||
label = rule_label(node.name, "必批", index, rule)
|
||
expression = str(rule.get("expression") or "").strip()
|
||
if not expression:
|
||
problems.append(f"{label}还没设条件")
|
||
continue
|
||
error = expressions.validate_script(expression)
|
||
if error:
|
||
problems.append(f"{label}的条件写错了:{error}")
|
||
if not (rule.get("taskValues") or []):
|
||
problems.append(f"{label}还没选必须审批的人")
|
||
problems.extend(approver_row_problems(node.name, rule.get("taskValues")))
|
||
return problems
|
||
|
||
|
||
def _check_cc_rules(definition: ProcessDefinition) -> list[str]:
|
||
"""抄送规则:时机合法、必须选到对象;设了抄送条件的话条件得能编译,否则等于没配。
|
||
|
||
抄送条件是可选项(留空=无条件照抄),所以只在"写了"的时候校验写法,不要求必填。
|
||
"""
|
||
from . import expressions
|
||
|
||
problems: list[str] = []
|
||
for node in definition.activities.values():
|
||
rules = (node.config or {}).get("ccRules")
|
||
if rules in (None, ""):
|
||
continue
|
||
if not isinstance(rules, list):
|
||
problems.append(f"环节 {node.name} 的抄送设置格式不对")
|
||
continue
|
||
for index, rule in enumerate(rules, start=1):
|
||
if not isinstance(rule, dict):
|
||
problems.append(f"环节 {node.name} 有抄送设置格式不对")
|
||
continue
|
||
# 一个环节可以配好几条抄送,报错得说清是哪一条,否则只有一样的文案能当门牌
|
||
label = rule_label(node.name, "抄送", index, rule)
|
||
when = str(rule.get("when") or "arrive")
|
||
if when not in {"arrive", "complete"}:
|
||
problems.append(f"{label}的抄送时机只能是到达时或办结后")
|
||
targets = rule.get("taskValues") or []
|
||
if not targets and not str(rule.get("taskScript") or "").strip():
|
||
problems.append(f"{label}还没选抄送对象")
|
||
expression = str(rule.get("expression") or "").strip()
|
||
if expression:
|
||
error = expressions.validate_script(expression)
|
||
if error:
|
||
problems.append(f"{label}的抄送条件写错了:{error}")
|
||
problems.extend(approver_row_problems(node.name, rule.get("taskValues"), "抄送对象"))
|
||
return problems
|
||
|
||
|
||
def _check_reachable(definition: ProcessDefinition) -> list[str]:
|
||
if definition.begin is None:
|
||
return []
|
||
seen: set[str] = {definition.begin.id}
|
||
stack = [definition.begin.id]
|
||
while stack:
|
||
current = stack.pop()
|
||
for route in definition.routes_from(current):
|
||
if route.to_activity not in seen:
|
||
seen.add(route.to_activity)
|
||
stack.append(route.to_activity)
|
||
return [
|
||
f"环节 {node.name} 从开始环节不可达"
|
||
for node in definition.activities.values()
|
||
if node.id not in seen
|
||
]
|
||
|
||
|
||
#: 审批人去重的取值:``dedup`` 往回看(前面批过),``dedupAhead`` 往前看(后面还要批)。
|
||
DEDUP_MODES = {"none", "all"}
|
||
AHEAD_MODES = {"none", "all", "any"}
|
||
|
||
|
||
def _check_approval_defaults(definition: ProcessDefinition) -> list[str]:
|
||
"""流程级审批规则与环节上的前瞻口径:只在取值不认识时报错,配置本身是可选的。"""
|
||
problems: list[str] = []
|
||
if str(definition.approval.get("dedup") or "none") not in DEDUP_MODES:
|
||
problems.append(f"审批人去重口径不支持:{definition.approval.get('dedup')}")
|
||
if str(definition.approval.get("dedupAhead") or "none") not in AHEAD_MODES:
|
||
problems.append(f"「后面还要他批才跳过」的口径不支持:{definition.approval.get('dedupAhead')}")
|
||
for node in definition.activities.values():
|
||
scope = (node.config or {}).get("skipAheadScope")
|
||
if scope not in (None, "") and str(scope) not in {"all", "any"}:
|
||
problems.append(f"环节 {node.name} 的前瞻去重口径不支持:{scope}")
|
||
return problems
|
||
|
||
|
||
def _check_approver_policy(definition: ProcessDefinition) -> list[str]:
|
||
"""审批方式的补充开关:依次审批要能解析出人,空审批人兜底要么选对策略要么配好人。"""
|
||
problems: list[str] = []
|
||
for node in definition.activities.values():
|
||
if node.type != ACTIVITY_MANUAL:
|
||
continue
|
||
config = node.config or {}
|
||
mode = str(config.get("mode") or "any")
|
||
if mode not in {"any", "all", "join", "serial", "ratio"}:
|
||
problems.append(f"环节 {node.name} 的审批方式不支持")
|
||
if mode == "ratio":
|
||
problems.extend(_quota_problems(node, config, "按比例会签"))
|
||
policy = str(config.get("emptyApprover") or "").strip()
|
||
if policy and policy not in {"toCreator", "autoPass", "toAdmin", "fixed"}:
|
||
problems.append(f"环节 {node.name} 的空审批人兜底策略不支持:{policy}")
|
||
if policy == "fixed" and not (config.get("emptyApproverValues") or []):
|
||
problems.append(f"环节 {node.name} 的空审批人兜底选了「指定人员」但还没选人")
|
||
action = str(config.get("timeoutAction") or "notify")
|
||
if action not in {"notify", "autoPass", "autoReject"}:
|
||
problems.append(f"环节 {node.name} 的超时处理方式不支持:{action}")
|
||
elif action != "notify" and not (config.get("expireValue") or 0):
|
||
problems.append(f"环节 {node.name} 配了超时自动处理但没设处理时限,定时器不会动它")
|
||
elif action == "autoReject" and not any(r.decision == "goBack" for r in definition.routes_from(node.id)):
|
||
problems.append(f"环节 {node.name} 要超时自动退回,但没有任何退回路由")
|
||
perms = config.get("formPerms")
|
||
if perms not in (None, "") and not isinstance(perms, dict):
|
||
problems.append(f"环节 {node.name} 的表单字段权限格式不对")
|
||
elif isinstance(perms, dict):
|
||
bad = [key for key, value in perms.items() if str(value) not in _FORM_PERMS]
|
||
if bad:
|
||
problems.append(f"环节 {node.name} 有字段的权限取值不合法:{'、'.join(sorted(map(str, bad)))}")
|
||
for node in definition.activities.values():
|
||
if node.type != ACTIVITY_MERGE:
|
||
continue
|
||
config = node.config or {}
|
||
if config.get("passCount") or config.get("passRatio"):
|
||
problems.extend(
|
||
_quota_problems(node, config, "汇聚放行", len(definition.routes_to(node.id)))
|
||
)
|
||
return problems
|
||
|
||
|
||
def _quota_problems(node, config: dict, label: str, incoming: int = 0) -> list[str]:
|
||
""""几张票 / 多少比例"这类放行配额的校验:要么没配,要么配得对。
|
||
|
||
配错的比例在运行时会被夹回合法区间(不会卡死流程),但设计期还是得说清楚,
|
||
否则「200% 通过」这种配置上线后行为与设计者预期完全对不上。
|
||
"""
|
||
problems: list[str] = []
|
||
try:
|
||
count = int(config.get("passCount") or 0)
|
||
ratio = float(config.get("passRatio") or 0)
|
||
except (TypeError, ValueError):
|
||
return [f"环节 {node.name} 的{label}票数/比例要填数字"]
|
||
if count < 0 or ratio < 0:
|
||
problems.append(f"环节 {node.name} 的{label}票数/比例不能是负数")
|
||
if not count and not ratio:
|
||
problems.append(f"环节 {node.name} 选了{label},但还没设放行票数或比例")
|
||
if ratio and not 0 < ratio <= 100:
|
||
problems.append(f"环节 {node.name} 的{label}比例要在 0 到 100 之间")
|
||
if incoming and count > incoming:
|
||
problems.append(f"环节 {node.name} 只有 {incoming} 条入口支线,要求 {count} 票永远达不成")
|
||
return problems
|