2026-04-05 10:36:22 +08:00
|
|
|
package model
|
|
|
|
|
|
2026-07-26 22:19:41 +08:00
|
|
|
// Rule 描述一个 Timer 到期后要执行的动作
|
2026-04-05 10:36:22 +08:00
|
|
|
type Rule struct {
|
2026-09-12 15:32:27 +08:00
|
|
|
ID uint `json:"id" gorm:"primaryKey"`
|
|
|
|
|
UID uint `gorm:"index" json:"uid"`
|
|
|
|
|
TimerID uint `gorm:"index" json:"timer_id"`
|
|
|
|
|
Name string `json:"name"`
|
2026-07-26 22:19:41 +08:00
|
|
|
// 关联账号,部分规则类型(如钉钉)不需要账号,此时为 0
|
|
|
|
|
AccountID uint `json:"account_id"`
|
|
|
|
|
// 规则类型,见 internal/ruleConfigChecker 注册表
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
// 规则配置,JSON 字符串
|
|
|
|
|
ConfigJson string `json:"config_json"`
|
|
|
|
|
Enabled bool `gorm:"default:true" json:"enabled"`
|
|
|
|
|
CreateAt int64 `json:"create_at"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RuleRequest 创建/编辑规则的请求体
|
|
|
|
|
type RuleRequest struct {
|
2026-04-05 10:36:22 +08:00
|
|
|
Name string `json:"name"`
|
2026-07-26 22:19:41 +08:00
|
|
|
TimerID uint `json:"timer_id"`
|
2026-04-05 10:36:22 +08:00
|
|
|
AccountID uint `json:"account_id"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
ConfigJson string `json:"config_json"`
|
2026-07-26 22:19:41 +08:00
|
|
|
Enabled *bool `json:"enabled"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate 校验规则请求中与类型无关的通用部分
|
|
|
|
|
func (r *RuleRequest) Validate() error {
|
|
|
|
|
if r.Name == "" {
|
|
|
|
|
return ErrValidation("规则名称不能为空")
|
|
|
|
|
}
|
|
|
|
|
if len(r.Name) > 64 {
|
|
|
|
|
return ErrValidation("规则名称过长")
|
|
|
|
|
}
|
|
|
|
|
if r.Type == "" {
|
|
|
|
|
return ErrValidation("规则类型不能为空")
|
|
|
|
|
}
|
2026-09-12 15:32:27 +08:00
|
|
|
if r.TimerID == 0 {
|
|
|
|
|
return ErrValidation("规则必须关联一个定时器")
|
2026-07-26 22:19:41 +08:00
|
|
|
}
|
|
|
|
|
if r.ConfigJson == "" {
|
|
|
|
|
return ErrValidation("规则配置不能为空")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2026-04-05 10:36:22 +08:00
|
|
|
}
|