approveflow/app/provider/flow_definition/model_approval_dynamic.go

82 lines
2.8 KiB
Go
Raw Normal View History

2024-11-14 17:02:41 +08:00
package flow_definition
import (
"approveflow/app/base"
userProvider "approveflow/app/provider/user"
2024-11-19 17:03:12 +08:00
"encoding/json"
2024-11-14 17:02:41 +08:00
"github.com/Superdanda/hade/framework/gin"
)
const (
ActionDirectSupervisorApproval = "DirectSupervisorApproval" // 直属领导审批
)
// DynamicApprovalStepConfig 动态审批步骤配置表
type DynamicApprovalStepConfig struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"` // 主键ID
Name string `gorm:"type:varchar(100);not null" json:"name"` // 规则名称
Action string `gorm:"type:varchar(50)" json:"action"` // 执行动作
Priority int `gorm:"type:int;default:0" json:"priority"` // 优先级
StepID int64 `gorm:"index;not null" json:"step_id"`
ConditionExpression string `gorm:"type:text" json:"condition_expression"` // 条件表达式
ApprovalConditionCommon `gorm:"-"`
base.Model // 通用字段,包括创建时间、更新时间等
}
2024-11-19 17:03:12 +08:00
func (config *DynamicApprovalStepConfig) MarshalBinary() ([]byte, error) {
return json.Marshal(config)
}
func (config *DynamicApprovalStepConfig) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, config)
}
2024-11-14 17:02:41 +08:00
// NewDirectSupervisorRule 创建审批人为直属领导审批规则
func NewDirectSupervisorRule() *DynamicApprovalStepConfig {
directSupervisorApproval := &DynamicApprovalStepConfig{
Name: "审批人为直属领导审批规则",
Action: ActionDirectSupervisorApproval,
Priority: 0,
}
directSupervisorApproval.ConditionExpression = ""
return directSupervisorApproval
}
// IsConditionMet 判断动态步骤的条件是否满足
func (config *DynamicApprovalStepConfig) IsConditionMet(data map[string]interface{}) (bool, error) {
result, err := config.EvaluateCondition(data)
if err != nil {
return false, err
}
return result, nil
}
// GetApprover 获取动态审批人
func (config *DynamicApprovalStepConfig) GetApprover(ctx *gin.Context, data map[string]interface{}) (*userProvider.User, error) {
userService := ctx.MustMake(userProvider.Key).(userProvider.Service)
// 检查是否需要直属领导审批
if config.Action == ActionDirectSupervisorApproval {
// 获取直属领导作为审批人
directSupervisor, err := userService.GetDirectSupervisorByUserKey(data[base.ApplicantKey].(string))
if err != nil {
return nil, err
}
return directSupervisor, nil
}
// 如果没有特定的直属领导规则,默认使用 ApproverRule 模板
//approver, err := parseApproverRule(config.ApproverRule, data)
//if err != nil {
// return nil, err
//}
// 使用解析后的结果找到实际的用户(假设 approver 是用户 ID 或角色标识)
//user, err := userService.GetUserByApproverIdentifier(approver)
//if err != nil {
// return nil, err
//}
return &userProvider.User{}, nil
}