mirror of
https://github.com/rulego/gflow-engine.git
synced 2026-12-05 16:02:03 +00:00
565 lines
21 KiB
Go
565 lines
21 KiB
Go
/*
|
||
* Copyright 2025 The RuleGo Authors.
|
||
*
|
||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
* you may not use this file except in compliance with the License.
|
||
* You may obtain a copy of the License at
|
||
*
|
||
* http://www.apache.org/licenses/LICENSE-2.0
|
||
*
|
||
* Unless required by applicable law or agreed to in writing, software
|
||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
* See the License for the specific language governing permissions and
|
||
* limitations under the License.
|
||
*/
|
||
|
||
package components
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"strings"
|
||
|
||
"github.com/rulego/gflow-engine/types/constants"
|
||
"github.com/rulego/gflow-engine/types/dto"
|
||
"github.com/rulego/rulego/components/base"
|
||
"github.com/rulego/rulego/utils/el"
|
||
"github.com/rulego/rulego/utils/maps"
|
||
"github.com/rulego/rulego/utils/str"
|
||
"github.com/sirupsen/logrus"
|
||
|
||
"github.com/rulego/gflow-engine/model"
|
||
"github.com/rulego/gflow-engine/service"
|
||
"github.com/rulego/gflow-engine/types/enums"
|
||
"github.com/rulego/rulego/api/types"
|
||
)
|
||
|
||
// UserTaskNodeType 用户任务节点类型(与 DSL 节点类型/任务类型同值,统一取自 types/constants)
|
||
const UserTaskNodeType = constants.NodeTypeUserTask
|
||
|
||
// UserTaskNodeConfiguration 用户任务节点配置结构(节点 configuration 字段)
|
||
// additionalInfo 中的 formPermissions/ActionPermissions 仅前端显示控制。
|
||
type UserTaskNodeConfiguration struct {
|
||
// 任务显示名(可选):覆盖节点 name 作为任务名称,
|
||
// 未配置时回退节点 name(additionalInfo.description 同理回退)。
|
||
TaskName string `json:"taskName"`
|
||
// 任务描述(可选):覆盖 additionalInfo.description。
|
||
TaskDescription string `json:"taskDescription"`
|
||
// 表单标识(可选):透传到 wf_task.form_key,前端按此渲染任务表单。
|
||
FormKey string `json:"formKey"`
|
||
|
||
// Approver 审批人配置
|
||
Approver ApproverConfig `json:"approver"`
|
||
|
||
// ApproveMode 审批方式:single(单人,缺省)/any(或签)/all(会签)/
|
||
// sequential(顺序审批)/vote(票签,阈值见 VoteRule)
|
||
ApproveMode string `json:"approveMode"`
|
||
// VoteRule 票签通过阈值,仅 approveMode=vote 消费;未配置时按过半处理
|
||
VoteRule *VoteRule `json:"voteRule,omitempty"`
|
||
|
||
// SelfApproval 自审处理:审批人与提交人为同一人时的处理方式
|
||
// none(缺省,不做自审过滤)/skip(移除发起人)/autoApprove(保留发起人)/
|
||
// delegateToManager(转交直接上级)/delegateToDeptManager(转交部门负责人)
|
||
SelfApproval string `json:"selfApproval"`
|
||
|
||
// EmptyApproverPolicy 审批人解析为空时的兜底策略(与驳回策略同级,节点级生效):
|
||
// tenant_admin(缺省,转交租户管理员)/auto_approve(自动通过,以系统身份记录并推进)/
|
||
// park(挂起待指派,任务照建等待管理员直接指派或补候选池)
|
||
EmptyApproverPolicy string `json:"emptyApproverPolicy"`
|
||
|
||
// Timeout 超时处理策略:到期时间相对每个任务创建时刻的时长,到期后由宿主
|
||
// 逾期巡检(overdue sweeper)按 Action 处理。
|
||
Timeout *TimeoutPolicy `json:"timeout,omitempty"`
|
||
|
||
// Reject 驳回配置
|
||
Reject RejectConfig `json:"reject"`
|
||
}
|
||
|
||
// TimeoutPolicy userTask 超时处理策略。
|
||
// DueInMinutes 相对任务创建时刻的时长;Action 由宿主逾期巡检执行:
|
||
// - remind 到期提醒办理人(默认,缺省/未知动作同此,仅提醒不代办)
|
||
// - autoApprove 到期自动通过(以 system 身份记录审批意见并推进流程)
|
||
// - autoReject 到期自动拒绝
|
||
type TimeoutPolicy struct {
|
||
DueInMinutes int `json:"dueInMinutes"`
|
||
Action string `json:"action"`
|
||
}
|
||
|
||
// resolveDueDate 计算任务到期时间:相对任务创建时刻的超时时长。
|
||
func (n *UserTaskNode) resolveDueDate() *time.Time {
|
||
if p := n.Config.Timeout; p != nil && p.DueInMinutes > 0 {
|
||
t := time.Now().Add(time.Duration(p.DueInMinutes) * time.Minute)
|
||
return &t
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// UserTaskNode 用户任务节点
|
||
type UserTaskNode struct {
|
||
Config UserTaskNodeConfiguration
|
||
TaskService service.TaskServiceInternal
|
||
IdentityService service.IdentityService // 由上层应用注入,按角色/部门/主管解析审批人
|
||
RuntimeService service.RuntimeServiceInternal // reject 时主动 terminate / jump 节点
|
||
CurrentNodeDef types.RuleNode
|
||
|
||
TaskEventListener service.TaskEventListener // 任务事件监听器
|
||
|
||
TaskName string // 任务名称
|
||
TaskDescription string // 任务描述
|
||
// 预编译的模板
|
||
initiatorSelectedTemplate el.Template // 发起人自选审批人模板
|
||
// 会签阈值规则串(Config.approvalRuleJSON 的缓存),创建任务与阈值判定共用
|
||
approvalRule string
|
||
}
|
||
|
||
// Type 返回节点类型
|
||
func (n *UserTaskNode) Type() string {
|
||
return UserTaskNodeType
|
||
}
|
||
|
||
// New 创建新的用户任务节点实例
|
||
func (n *UserTaskNode) New() types.Node {
|
||
return &UserTaskNode{
|
||
Config: UserTaskNodeConfiguration{
|
||
ApproveMode: string(enums.ApprovalTypeSingle),
|
||
},
|
||
TaskService: n.TaskService,
|
||
IdentityService: n.IdentityService,
|
||
RuntimeService: n.RuntimeService,
|
||
TaskEventListener: n.TaskEventListener,
|
||
}
|
||
}
|
||
|
||
// Init 初始化节点
|
||
func (n *UserTaskNode) Init(ruleConfig types.Config, configuration types.Configuration) error {
|
||
err := maps.Map2Struct(configuration, &n.Config)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
n.Config.Normalize()
|
||
// 保存当前节点信息,用于流程实例节点保存 CurrentActivity Name
|
||
n.CurrentNodeDef = base.NodeUtils.GetSelfDefinition(configuration)
|
||
// 任务基础配置,从节点名称和扩展字段获取
|
||
// configuration.taskName / taskDescription 优先,未配置时回退节点 name / additionalInfo.description
|
||
n.TaskName = strings.TrimSpace(n.Config.TaskName)
|
||
if n.TaskName == "" {
|
||
n.TaskName = n.CurrentNodeDef.Name
|
||
}
|
||
n.TaskDescription = strings.TrimSpace(n.Config.TaskDescription)
|
||
if n.TaskDescription == "" {
|
||
if description, ok := n.CurrentNodeDef.GetAdditionalInfo("description"); ok {
|
||
// additionalInfo 的值类型由 DSL 决定(可能是 map/number/[]interface{}),
|
||
// 直接 .(string) 在非字符串时会 panic;用 str.ToString 安全转换。
|
||
n.TaskDescription = str.ToString(description)
|
||
}
|
||
}
|
||
// 配置合法性在部署期由 ValidateNodeConfiguration 拦截(链加载期 Init 错误只
|
||
// warn,拦不住部署);这里仅对未知的枚举取值落定缺省,保证运行期行为确定。
|
||
if !enums.IsValidUserTaskApprovalType(enums.ApprovalType(n.Config.ApproveMode)) {
|
||
logrus.Warnf("userTask node %s has unknown approveMode %q; fallback to single", n.GetSelfId(), n.Config.ApproveMode)
|
||
n.Config.ApproveMode = string(enums.ApprovalTypeSingle)
|
||
}
|
||
if !enums.IsValidSelfApprovalType(enums.SelfApprovalType(n.Config.SelfApproval)) {
|
||
logrus.Warnf("userTask node %s has unknown selfApproval %q; fallback to none", n.GetSelfId(), n.Config.SelfApproval)
|
||
n.Config.SelfApproval = string(enums.SelfApprovalTypeNone)
|
||
}
|
||
if !isValidUserTaskRejectStrategy(n.Config.Reject.Strategy) {
|
||
logrus.Warnf("userTask node %s has unknown reject.strategy %q; will terminate as fallback at runtime", n.GetSelfId(), n.Config.Reject.Strategy)
|
||
n.Config.Reject.Strategy = RejectStrategyTerminate
|
||
}
|
||
if !isValidEmptyApproverPolicy(n.Config.EmptyApproverPolicy) {
|
||
logrus.Warnf("userTask node %s has unknown emptyApproverPolicy %q; fallback to tenant_admin", n.GetSelfId(), n.Config.EmptyApproverPolicy)
|
||
n.Config.EmptyApproverPolicy = EmptyApproverPolicyTenantAdmin
|
||
}
|
||
// 会签阈值规则串:落库到 wf_task.approval_rule 供 service 层阈值判定复用
|
||
n.approvalRule = n.Config.approvalRuleJSON()
|
||
// 预编译表达式模板,提高运行时性能
|
||
if err := n.compileTemplates(); err != nil {
|
||
return fmt.Errorf("failed to compile templates: %w", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// OnMsg 处理消息
|
||
// 核心逻辑:
|
||
// 1. 如果该节点没创建任务,则创建任务
|
||
// 2. 每个用户审批后,也会进来OnMsg,只有全部都审批完,才能调用 ctx.TellSuccess 否则调用ctx.DoOnEnd 结束
|
||
func (n *UserTaskNode) OnMsg(ctx types.RuleContext, msg types.RuleMsg) {
|
||
defer recoverNodePanic(ctx, msg, UserTaskNodeType, n.GetSelfId())
|
||
// 从消息中获取流程实例信息
|
||
processInstanceID := n.getProcessInstanceID(msg)
|
||
if processInstanceID == "" {
|
||
ctx.TellFailure(msg, fmt.Errorf("process instance ID not found"))
|
||
return
|
||
}
|
||
|
||
// 查询与创建在同一把锁内完成:同一实例同一节点的并发重入
|
||
// (多个审批人同时操作、引擎重试)会在"查无任务"判定上竞争,导致重复建任务。
|
||
// 锁在闭包内 defer 释放:DB 调用 panic 时不会把该实例+节点的 key 永久锁死
|
||
created := false
|
||
existingTasks, err := func() ([]*model.WfTask, error) {
|
||
unlock := taskOpMutex.Lock(processInstanceID + "/" + n.GetSelfId())
|
||
defer unlock()
|
||
tasks, err := n.getExistingTasks(ctx.GetContext(), processInstanceID)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("failed to check existing tasks: %w", err)
|
||
}
|
||
if len(tasks) == 0 {
|
||
if err := n.createUserTasks(ctx, processInstanceID, msg); err != nil {
|
||
return nil, fmt.Errorf("failed to create user tasks: %w", err)
|
||
}
|
||
created = true
|
||
return nil, nil
|
||
}
|
||
return tasks, nil
|
||
}()
|
||
if err != nil {
|
||
ctx.TellFailure(msg, err)
|
||
return
|
||
}
|
||
|
||
// 如果没有任务,说明刚创建了新任务
|
||
if created {
|
||
// 发起人自动通过:命中发起人的任务立即按通过完成。必须在创建锁释放后
|
||
// 调用——完成动作经 AfterCommit→ExecuteNext 重入本节点重新拿锁,
|
||
// taskOpMutex 非重入,锁内调用会死锁。
|
||
n.autoApproveOwnerTasks(ctx, msg, processInstanceID)
|
||
// 审批人为空自动通过(emptyApproverPolicy=auto_approve):同样锁外执行
|
||
n.autoApproveEmptyApproverTasks(ctx, msg, processInstanceID)
|
||
// 任务创建后,流程暂停等待用户操作,调用DoOnEnd结束当前节点执行
|
||
logrus.Debugf("User tasks created for node %s, waiting for completion", n.GetSelfId())
|
||
ctx.DoOnEnd(msg, nil, "")
|
||
return
|
||
}
|
||
|
||
// 检查所有任务是否都已完成
|
||
allCompleted, err := n.checkTasksCompletion(ctx, msg, existingTasks)
|
||
if err != nil {
|
||
ctx.TellFailure(msg, fmt.Errorf("failed to check tasks completion: %w", err))
|
||
return
|
||
}
|
||
|
||
// 顺序审批:如果尚未完成且当前没有激活任务,则按顺序创建下一个任务
|
||
if enums.ApprovalType(n.Config.ApproveMode) == enums.ApprovalTypeSequential && !allCompleted {
|
||
// 拒绝即停:在创建下一个顺序任务之前,先扫描已完成的任务里是否有被拒绝的。
|
||
// 顺序审批任一拒绝即触发驳回策略;会签的拒绝判定在下方统一处理
|
||
//(顺序审批的子任务按需创建,必须在此早退)。
|
||
for _, t := range existingTasks {
|
||
if t.Status == string(enums.TaskStatusCompleted) && t.EndReason != nil &&
|
||
*t.EndReason == string(enums.ApprovalResultRejected) {
|
||
newMsg := msg.Copy()
|
||
if newMsg.Metadata == nil {
|
||
newMsg.Metadata = types.NewMetadata()
|
||
}
|
||
logrus.Debugf("Sequential rejected for node %s, invoking reject strategy", n.GetSelfId())
|
||
n.handleRejection(ctx, newMsg, processInstanceID)
|
||
return
|
||
}
|
||
}
|
||
|
||
// 优先从已完成任务的 variables 读取 _sequentialAssignees 缓存
|
||
assignees := sequentialAssigneesFrom(existingTasks)
|
||
// 缓存缺失时重新解析
|
||
if len(assignees) == 0 {
|
||
resolved, rerr := n.resolveAssignees(ctx.GetContext(), metaValue(msg, constants.KeyTenantID), metaValue(msg, constants.KeyOwner), extractVariables(ctx, msg))
|
||
if rerr == nil && len(resolved) > 0 {
|
||
assignees = resolved
|
||
}
|
||
}
|
||
if len(assignees) > 0 {
|
||
completed := 0
|
||
active := 0
|
||
for _, t := range existingTasks {
|
||
if t.Status == string(enums.TaskStatusCompleted) {
|
||
completed++
|
||
} else if t.Status == string(enums.TaskStatusActive) {
|
||
active++
|
||
}
|
||
}
|
||
if active == 0 && completed < len(assignees) {
|
||
processID := n.getProcessID(msg)
|
||
tenantID := metaValue(msg, constants.KeyTenantID)
|
||
// 锁内重新确认状态后创建,防并发完成同时推进产生重复任务;
|
||
// defer 释放防 panic 锁死(与 OnMsg 同一写法)
|
||
var createErr error
|
||
advanced := false
|
||
func() {
|
||
unlock := taskOpMutex.Lock(processInstanceID + "/" + n.GetSelfId())
|
||
defer unlock()
|
||
tasks, terr := n.getExistingTasks(ctx.GetContext(), processInstanceID)
|
||
if terr != nil {
|
||
createErr = terr
|
||
return
|
||
}
|
||
c, a := 0, 0
|
||
for _, t := range tasks {
|
||
if t.Status == string(enums.TaskStatusCompleted) {
|
||
c++
|
||
} else if t.Status == string(enums.TaskStatusActive) {
|
||
a++
|
||
}
|
||
}
|
||
if a == 0 && c < len(assignees) {
|
||
taskVars := extractVariables(ctx, msg)
|
||
taskVars[constants.KeySequentialAssignees] = assignees
|
||
// 每个后续子任务独立求到期时间:timeout 相对各自创建时刻,
|
||
// 与首个任务(createUserTasks)口径一致
|
||
createErr = n.createSingleTask(ctx, processInstanceID, processID, tenantID, assignees[c], taskVars, n.resolveDueDate(), "")
|
||
advanced = createErr == nil
|
||
}
|
||
}()
|
||
if createErr != nil {
|
||
ctx.TellFailure(msg, fmt.Errorf("failed to create next sequential task: %w", createErr))
|
||
return
|
||
}
|
||
if advanced {
|
||
logrus.Debugf("Sequential next task created for node %s", n.GetSelfId())
|
||
// 发起人自动通过:顺序推进创建的后续任务同样命中发起人时立即完成
|
||
//(锁段已退出,完成后经 AfterCommit→ExecuteNext 重入推进下一环)
|
||
n.autoApproveOwnerTasks(ctx, msg, processInstanceID)
|
||
n.autoApproveEmptyApproverTasks(ctx, msg, processInstanceID)
|
||
ctx.DoOnEnd(msg, nil, "")
|
||
return
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查审批结果:统计approved和rejected的任务数
|
||
approvedCount := 0
|
||
rejectedCount := 0
|
||
for _, task := range existingTasks {
|
||
if task.Status == string(enums.TaskStatusCompleted) && task.EndReason != nil {
|
||
if *task.EndReason == string(enums.ApprovalResultApproved) {
|
||
approvedCount++
|
||
} else if *task.EndReason == string(enums.ApprovalResultRejected) {
|
||
rejectedCount++
|
||
}
|
||
}
|
||
}
|
||
|
||
// 会签(全员通过)一票否决:任一拒绝立即触发驳回策略。
|
||
if n.Config.ApproveMode == string(enums.ApprovalTypeCountersign) && rejectedCount > 0 {
|
||
newMsg := msg.Copy()
|
||
if newMsg.Metadata == nil {
|
||
newMsg.Metadata = types.NewMetadata()
|
||
}
|
||
logrus.Debugf("Countersign rejected for node %s, invoking reject strategy", n.GetSelfId())
|
||
n.handleRejection(ctx, newMsg, processInstanceID)
|
||
return
|
||
}
|
||
// 顺序审批的拒绝处理已在 sequential-advance 分支顶部完成(早退路径)。
|
||
|
||
if allCompleted {
|
||
// 所有任务都已完成,根据审批结果决定流程走向
|
||
newMsg := msg.Copy()
|
||
if newMsg.Metadata == nil {
|
||
newMsg.Metadata = types.NewMetadata()
|
||
}
|
||
|
||
// 判断整体审批结果:按类型计算
|
||
approved := n.evaluateApproval(existingTasks, approvedCount, rejectedCount)
|
||
|
||
if approved {
|
||
logrus.Debugf("All tasks completed and approved for node %s", n.GetSelfId())
|
||
ctx.TellSuccess(newMsg)
|
||
} else {
|
||
logrus.Debugf("All tasks completed but rejected for node %s, invoking reject strategy", n.GetSelfId())
|
||
n.handleRejection(ctx, newMsg, processInstanceID)
|
||
}
|
||
} else {
|
||
// 还有任务未完成,继续等待
|
||
logrus.Debugf("Tasks still pending for node %s, waiting for completion", n.GetSelfId())
|
||
ctx.DoOnEnd(msg, nil, "")
|
||
}
|
||
}
|
||
|
||
// getExistingTasks 获取当前节点的现有任务(含已完成终态任务)。
|
||
// 顺序审批的推进逻辑依赖终态任务计算进度、读取 _sequentialAssignees 缓存,
|
||
// 因此不能过滤 Completed 任务。驳回回跳场景的终态任务清理由 jumpToNode
|
||
// 里的 SupersedeNodeTasks 完成。
|
||
// 必须取全量:顺序审批每步留一行终态任务,默认分页 10 条会把推进计数
|
||
// 永久钉在 10,>10 人即重复建任务死循环。
|
||
func (n *UserTaskNode) getExistingTasks(ctx context.Context, processInstanceID string) ([]*model.WfTask, error) {
|
||
query := &dto.TaskQuery{
|
||
InstanceID: &processInstanceID,
|
||
TaskDefKey: n.GetSelfId(),
|
||
ParentIDIsNull: true,
|
||
}
|
||
|
||
return fetchTasksPageAll(ctx, n.TaskService, service.ActorFromCtx(ctx), query)
|
||
}
|
||
|
||
// checkTasksCompletion 检查任务完成状态
|
||
func (n *UserTaskNode) checkTasksCompletion(ctx types.RuleContext, msg types.RuleMsg, tasks []*model.WfTask) (bool, error) {
|
||
completedTasks := 0
|
||
for _, task := range tasks {
|
||
if task.Status == string(enums.TaskStatusCompleted) {
|
||
completedTasks++
|
||
}
|
||
}
|
||
|
||
switch enums.ApprovalType(n.Config.ApproveMode) {
|
||
case enums.ApprovalTypeSingle:
|
||
// 单人审批:只要完成就按审批结果
|
||
allCompleted := completedTasks == len(tasks)
|
||
return allCompleted, nil
|
||
|
||
case enums.ApprovalTypeAny:
|
||
// 或签:任何一个完成就完成
|
||
anyCompleted := completedTasks > 0
|
||
return anyCompleted, nil
|
||
|
||
case enums.ApprovalTypeSequential:
|
||
// 顺序审批每次只有一个 active 子任务,期望总数取 _sequentialAssignees 缓存
|
||
// 长度;缓存缺失时退化为 len(tasks)。
|
||
expectedTotal := len(sequentialAssigneesFrom(tasks))
|
||
if expectedTotal == 0 {
|
||
return completedTasks == len(tasks), nil
|
||
}
|
||
return completedTasks >= expectedTotal, nil
|
||
|
||
case enums.ApprovalTypeCountersign, enums.ApprovalTypeVote:
|
||
// 会签/票签:委托 service 按 ApprovalRule 规则(all/any/majority/percent/count)判定。
|
||
// vote 复用会签父+子结构,达到阈值或注定拒绝即完成(剩余子任务终止)。
|
||
parentID := ""
|
||
for _, t := range tasks {
|
||
if t.ParentID == nil || *t.ParentID == "" {
|
||
parentID = t.ID
|
||
break
|
||
}
|
||
}
|
||
if parentID == "" {
|
||
return false, nil
|
||
}
|
||
isCompleted, _, err := n.TaskService.CheckCountersignSubTaskCompletion(ctx.GetContext(), parentID, n.approvalRule)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return isCompleted, nil
|
||
|
||
default:
|
||
allCompleted := completedTasks == len(tasks)
|
||
return allCompleted, nil
|
||
}
|
||
}
|
||
|
||
// sequentialAssigneesFrom 从任务 variables 提取 KeySequentialAssignees 缓存。
|
||
// createUserTasks 在创建第一个顺序审批任务时写入审批人列表,
|
||
// 后续按需创建的子任务沿用同一份缓存;多份缓存取最长的一份。
|
||
// 找不到缓存返回 nil。
|
||
func sequentialAssigneesFrom(tasks []*model.WfTask) []string {
|
||
var best []string
|
||
for _, t := range tasks {
|
||
if t == nil || t.Variables == nil || *t.Variables == "" {
|
||
continue
|
||
}
|
||
vars, err := service.ParseVariablesJSON(t.Variables)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
cached, ok := vars[constants.KeySequentialAssignees]
|
||
if !ok {
|
||
continue
|
||
}
|
||
arr, ok := cached.([]interface{})
|
||
if !ok {
|
||
continue
|
||
}
|
||
if len(arr) > len(best) {
|
||
best = make([]string, 0, len(arr))
|
||
for _, v := range arr {
|
||
if s, ok := v.(string); ok {
|
||
best = append(best, s)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return best
|
||
}
|
||
|
||
// evaluateApproval 计算整体审批是否通过
|
||
// 根据不同审批类型与规则,判断是否通过
|
||
func (n *UserTaskNode) evaluateApproval(tasks []*model.WfTask, approvedCount, rejectedCount int) bool {
|
||
switch enums.ApprovalType(n.Config.ApproveMode) {
|
||
case enums.ApprovalTypeSingle:
|
||
return rejectedCount == 0 && approvedCount > 0
|
||
case enums.ApprovalTypeAny:
|
||
return rejectedCount == 0 && approvedCount > 0
|
||
case enums.ApprovalTypeSequential:
|
||
// 顺序审批:若任何一步拒绝则失败;全部通过则成功
|
||
for _, t := range tasks {
|
||
if t.Status == string(enums.TaskStatusCompleted) && t.EndReason != nil {
|
||
if *t.EndReason == string(enums.ApprovalResultRejected) {
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
return approvedCount > 0 && rejectedCount == 0
|
||
case enums.ApprovalTypeCountersign:
|
||
// 会签在 OnMsg 上面已有一票否决处理(遇拒绝立即失败)
|
||
return rejectedCount == 0 && approvedCount > 0
|
||
case enums.ApprovalTypeVote:
|
||
// 票签阈值判定在 complete 时由 service 层 checkCountersign 完成(父任务 complete + ExecuteNext)。
|
||
// 此为 OnMsg 兜底粗判(与会签一致),精确阈值不在此重复。
|
||
return rejectedCount == 0 && approvedCount > 0
|
||
default:
|
||
return rejectedCount == 0 && approvedCount > 0
|
||
}
|
||
}
|
||
|
||
// getProcessInstanceID 从消息 metadata 获取流程实例 ID
|
||
func (n *UserTaskNode) getProcessInstanceID(msg types.RuleMsg) string {
|
||
if msg.Metadata != nil {
|
||
if pid := metaValue(msg, constants.KeyInstanceID); pid != "" {
|
||
return pid
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// getProcessID 从消息 metadata 获取流程定义 ID
|
||
func (n *UserTaskNode) getProcessID(msg types.RuleMsg) string {
|
||
if msg.Metadata != nil {
|
||
if pid := metaValue(msg, constants.KeyProcessID); pid != "" {
|
||
return pid
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// GetSelfId 获取当前节点ID
|
||
func (n *UserTaskNode) GetSelfId() string {
|
||
return selfID(n.CurrentNodeDef, UserTaskNodeType)
|
||
}
|
||
|
||
// GetSelfName 获取当前节点Name
|
||
func (n *UserTaskNode) GetSelfName() string {
|
||
return selfName(n.CurrentNodeDef)
|
||
}
|
||
|
||
// Destroy 无需清理的资源,空实现满足 types.Node 接口
|
||
func (n *UserTaskNode) Destroy() {}
|
||
|
||
// compileTemplates 预编译所有表达式模板,提高运行时性能
|
||
func (n *UserTaskNode) compileTemplates() error {
|
||
// 发起人自选:提前编译 expression 表达式
|
||
if enums.CandidateType(n.Config.Approver.Type) == enums.CandidateTypeInitiatorSelect {
|
||
s := strings.TrimSpace(n.Config.Approver.Expression)
|
||
if s != "" {
|
||
tpl, terr := el.NewTemplate(s)
|
||
if terr != nil {
|
||
// 坏模板必须让 Init 失败:静默跳过会把错误推迟到运行期,
|
||
// 表现为误导性的 "no assignees found for task"
|
||
return fmt.Errorf("failed to compile approver expression %q: %w", s, terr)
|
||
}
|
||
n.initiatorSelectedTemplate = tpl
|
||
}
|
||
}
|
||
return nil
|
||
}
|