feat: 实现RBAC系统 - 完成Tenant/Role/Resource/User模型、路由配置及MySQL连接配置

This commit is contained in:
OpenClaw
2026-03-01 11:55:04 +08:00
parent 1de4524b5e
commit 3af5009ba8
6 changed files with 870 additions and 4 deletions

View File

@@ -0,0 +1,103 @@
package models
import (
"gorm.io/gorm"
"time"
)
// KnowledgeBase 知识库模型
type KnowledgeBase struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
TenantID uint `gorm:"not null;index;comment:租户 ID" json:"tenant_id"`
Name string `gorm:"size:200;not null;comment:知识库名称" json:"name"`
Slug string `gorm:"unique;size:100;not null;comment:知识库唯一标识" json:"slug"`
Description *string `gorm:"type:text;comment:知识库描述" json:"description"`
Icon *string `gorm:"size:100;comment:图标" json:"icon"`
SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"`
Status string `gorm:"default:'draft';comment:状态 (draft/published/archived)" json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
// 关联
Tenant *Tenant `gorm:"foreignKey:TenantID" json:"tenant,omitempty"`
KnowledgeItems []KnowledgeItem `gorm:"foreignKey:KnowledgeBaseID" json:"items,omitempty"`
}
// TableName 指定表名
func (KnowledgeBase) TableName() string {
return "knowledge_bases"
}
// KnowledgeItem 知识库条目模型
type KnowledgeItem struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
TenantID uint `gorm:"not null;index;comment:租户 ID" json:"tenant_id"`
KnowledgeBaseID uint `gorm:"not null;index;comment:知识库 ID" json:"knowledge_base_id"`
Title string `gorm:"size:500;not null;comment:标题" json:"title"`
Slug string `gorm:"size:200;not null;comment:唯一标识" json:"slug"`
Content string `gorm:"type:longtext;not null;comment:内容 (Markdown)" json:"content"`
Summary *string `gorm:"type:text;comment:摘要" json:"summary"`
Tags *string `gorm:"type:json;comment:标签 (JSON)" json:"tags"`
Metadata *string `gorm:"type:text;comment:元数据 (JSON)" json:"metadata"`
AuthorID *uint `gorm:"comment:作者 ID" json:"author_id"`
Status string `gorm:"default:'draft';index;comment:状态 (draft/review/published/archived)" json:"status"`
Views int `gorm:"default:0;comment:浏览次数" json:"views"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
// 关联
Tenant *Tenant `gorm:"foreignKey:TenantID" json:"tenant,omitempty"`
Author *User `gorm:"foreignKey:AuthorID" json:"author,omitempty"`
KnowledgeBase *KnowledgeBase `gorm:"foreignKey:KnowledgeBaseID" json:"knowledge_base,omitempty"`
}
// TableName 指定表名
func (KnowledgeItem) TableName() string {
return "knowledge_items"
}
// KnowledgeCategory 知识库分类
type KnowledgeCategory struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
KnowledgeBaseID uint `gorm:"not null;index" json:"knowledge_base_id"`
ParentID *uint `gorm:"index" json:"parent_id"`
Name string `gorm:"size:200;not null" json:"name"`
Slug string `gorm:"size:100;not null" json:"slug"`
Description *string `gorm:"type:text" json:"description"`
Icon *string `gorm:"size:100" json:"icon"`
SortOrder int `gorm:"default:0" json:"sort_order"`
Status string `gorm:"default:'active'" json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
}
// TableName 指定表名
func (KnowledgeCategory) TableName() string {
return "knowledge_categories"
}
// 知识库状态枚举
const (
KBStatusDraft = "draft"
KBStatusPublished = "published"
KBStatusArchived = "archived"
)
// 知识库条目状态枚举
const (
KnowledgeStatusDraft = "draft"
KnowledgeStatusReview = "review"
KnowledgeStatusPublished = "published"
KnowledgeStatusArchived = "archived"
)
// KnowledgeSearchResult 知识库搜索结果
type KnowledgeSearchResult struct {
Item KnowledgeItem `json:"item"`
Score float64 `json:"score"` // 相关度得分
Matched string `json:"matched"` // 匹配的文本片段
Category string `json:"category"` // 分类
}

View File

@@ -0,0 +1,108 @@
package models
import (
"time"
"gorm.io/gorm"
)
// Ticket 工单模型
type Ticket struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
TicketNumber string `gorm:"unique;size:50;not null;comment:工单编号" json:"ticket_number"`
TenantID uint `gorm:"not null;index;comment:租户 ID" json:"tenant_id"`
UserID *uint `gorm:"index;comment:用户 ID" json:"user_id"`
Title string `gorm:"size:500;not null;comment:工单标题" json:"title"`
Description string `gorm:"type:text;not null;comment:工单描述" json:"description"`
Category *string `gorm:"size:100;comment:工单分类" json:"category"`
Priority string `gorm:"default:'medium';index;comment:优先级 (low/medium/high/urgent)" json:"priority"`
Status string `gorm:"default:'open';index;comment:状态 (open/pending/in_progress/resolved/closed)" json:"status"`
AssignedTo *uint `gorm:"index;comment:指配给" json:"assigned_to"`
DueDate *time.Time `gorm:"comment:截止日期" json:"due_date"`
Resolution *string `gorm:"type:text;comment:解决方案" json:"resolution"`
Metadata *string `gorm:"type:text;comment:元数据 (JSON)" json:"metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
// 关联
User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
Assignee *User `gorm:"foreignKey:AssignedTo" json:"assignee,omitempty"`
Tenat *Tenant `gorm:"foreignKey:TenatID" json:"tenant,omitempty"`
Messages []TicketMessage `json:"messages,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
// TableName 指定表名
func (Ticket) TableName() string {
return "tickets"
}
// TicketMessage 工单消息
type TicketMessage struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
TicketID uint `gorm:"not null;index;comment:工单 ID" json:"ticket_id"`
UserID uint `gorm:"not null;comment:发送者 ID" json:"user_id"`
Content string `gorm:"type:text;not null;comment:消息内容" json:"content"`
ContentType string `gorm:"default:'text';comment:类型 (text/image/file)" json:"content_type"`
ContentURL *string `gorm:"size:1000;comment:内容 URL" json:"content_url"`
IsInternal bool `gorm:"default:false;comment:是否内部备注" json:"is_internal"`
Metadata *string `gorm:"type:text;comment:元数据" json:"metadata"`
CreatedAt time.Time `json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Ticket *Ticket `gorm:"foreignKey:TicketID" json:"ticket,omitempty"`
}
// TableName 指定表名
func (TicketMessage) TableName() string {
return "ticket_messages"
}
// Attachment 附件
type Attachment struct {
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
TenantID uint `gorm:"not null;index;comment:租户 ID" json:"tenant_id"`
TicketID uint `gorm:"not null;index;comment:工单 ID" json:"ticket_id"`
FileName string `gorm:"size:500;not null;comment:文件名" json:"file_name"`
FileURL string `gorm:"not null;comment:文件 URL" json:"file_url"`
FileSize uint64 `gorm:"comment:文件大小 (字节)" json:"file_size"`
MIMEType string `gorm:"size:100;comment:MIME 类型" json:"mime_type"`
Description *string `gorm:"comment:描述" json:"description"`
CreatedAt time.Time `json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Ticket *Ticket `gorm:"foreignKey:TicketID" json:"ticket,omitempty"`
}
// TableName 指定表名
func (Attachment) TableName() string {
return "attachments"
}
// 工单状态枚举
const (
TicketStatusOpen = "open"
TicketStatusPending = "pending"
TicketStatusInProgress = "in_progress"
TicketStatusResolved = "resolved"
TicketStatusClosed = "closed"
)
// 工单优先级枚举
const (
TicketPriorityLow = "low"
TicketPriorityMedium = "medium"
TicketPriorityHigh = "high"
TicketPriorityUrgent = "urgent"
)
// 工单分类枚举
const (
TicketCategoryTechnical = "technical" // 技术问题
TicketCategoryBug = "bug" // Bug 报告
TicketCategoryFeature = "feature" // 功能请求
TicketCategorySupport = "support" // 技术支持
TicketCategoryBilling = "billing" // 计费问题
TicketCategoryOther = "other" // 其他
)