approveflow/app/provider/flow_definition/model_approval_path.go

127 lines
3.2 KiB
Go

package flow_definition
import (
"approveflow/app/base"
"bytes"
"encoding/json"
"fmt"
"text/template"
)
// ApprovalPathConfig 审批路径配置表
type ApprovalPathConfig struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"` // 主键ID
NodeID int64 `gorm:"type:bigint;index;not null" json:"node_id"`
FromNodeKey string `gorm:"type:varchar(50);index;not null" json:"from_node_key"`
ToNodeKey string ` gorm:"type:varchar(50);index;not null" json:"to_node_key"`
IsParallel bool `gorm:"type:bool;default:false" json:"is_parallel"` // 是否并行
ConditionExpression string `gorm:"type:text" json:"condition_expression"` // 条件表达式
base.Model
ApprovalConditionCommon `gorm:"-"`
}
func (a *ApprovalPathConfig) GetKey() string {
return a.Key
}
func (a *ApprovalPathConfig) GetNodeID() int64 {
return a.NodeID
}
func (a *ApprovalPathConfig) GetFromNodeKey() string {
return a.FromNodeKey
}
func (a *ApprovalPathConfig) GetToNodeKey() string {
return a.ToNodeKey
}
func (a *ApprovalPathConfig) MarshalBinary() ([]byte, error) {
return json.Marshal(a)
}
func (a *ApprovalPathConfig) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, a)
}
func (a *ApprovalPathConfig) NewPathWithStep(step *ApprovalStep) (*ApprovalPathConfig, *ApprovalPathConfig) {
toPath := &ApprovalPathConfig{
IsParallel: false,
}
a.FromNodeKey = step.Key
a.NodeID = step.ID
a.ToNodeKey = step.Key
return a, toPath
}
func NewPathBetween(fromStep, toStep *ApprovalStep) *ApprovalPathConfig {
path := &ApprovalPathConfig{
IsParallel: false,
}
path.FromNodeKey = fromStep.Key
path.NodeID = fromStep.ID
path.ToNodeKey = toStep.Key
return path
}
func NewPathConfig(fromStepKey, toStepKey string) *ApprovalPathConfig {
path := &ApprovalPathConfig{
IsParallel: false,
}
path.FromNodeKey = fromStepKey
path.ToNodeKey = toStepKey
return path
}
func NewPathWithTarget(target *ApprovalStep) *ApprovalPathConfig {
path := &ApprovalPathConfig{
//ToStepID: target.ID,
IsParallel: false,
}
return path
}
func (a *ApprovalPathConfig) condition(condition string) *ApprovalPathConfig {
a.ConditionExpression = condition
return a
}
// IsConditionMet 判断路径条件是否满足
func (a *ApprovalPathConfig) IsConditionMet(data map[string]interface{}) (bool, error) {
if a.ConditionExpression == "" {
return true, nil // 无条件,默认满足
}
result, err := a.EvaluateCondition(data)
if err != nil {
return false, err
}
return result, nil
}
// 解析审批人规则
func parseApproverRule(rule string, data map[string]interface{}) (string, error) {
if rule == "" {
return "", fmt.Errorf("approver rule is empty")
}
// 创建模板并解析
tmpl, err := template.New("approverRule").Parse(rule)
if err != nil {
return "", fmt.Errorf("failed to parse approver rule template: %v", err)
}
// 执行模板
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
return "", fmt.Errorf("failed to execute approver rule template: %v", err)
}
approver := buf.String()
if approver == "" {
return "", fmt.Errorf("approver rule evaluated to empty string")
}
return approver, nil
}