// Package models 定义数据库模型 // 包含用户、角色、权限、文档、工作簿、工作表、单元格等核心数据模型 // 所有模型结构必须与根目录 init.sql 保持一致 package models import ( "time" "gorm.io/gorm" ) // ============================================================ // 用户相关模型 // ============================================================ // User 用户模型 // 存储系统用户信息,前期不强制登录 type User struct { ID uint `json:"id" gorm:"primaryKey"` // 用户ID,主键自增 Username string `json:"username" gorm:"size:64;uniqueIndex"` // 用户名(登录名) Password string `json:"-" gorm:"size:255"` // 密码哈希(JSON不输出) Nickname string `json:"nickname" gorm:"size:64"` // 昵称/显示名称 Email string `json:"email" gorm:"size:128"` // 邮箱 Phone string `json:"phone" gorm:"size:20"` // 手机号 Avatar string `json:"avatar" gorm:"size:512"` // 头像URL Status int `json:"status" gorm:"default:1"` // 状态: 0=禁用 1=正常 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 Roles []Role `json:"roles,omitempty" gorm:"many2many:user_roles"` // 关联角色列表 } // Role 角色模型 // 系统角色定义,区分前台角色和后台角色 type Role struct { ID uint `json:"id" gorm:"primaryKey"` // 角色ID,主键自增 Name string `json:"name" gorm:"size:64;not null"` // 角色名称 Code string `json:"code" gorm:"size:64;uniqueIndex"` // 角色编码(唯一标识) Type string `json:"type" gorm:"size:16;default:'frontend'"` // 角色类型: frontend=前台 backend=后台 Description string `json:"description" gorm:"size:255"` // 角色描述 SortOrder int `json:"sortOrder" gorm:"default:0"` // 排序序号 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 Permissions []Permission `json:"permissions,omitempty" gorm:"many2many:role_permissions"` // 关联权限列表 } // Permission 权限模型 // 系统权限定义,区分前台权限和后台权限 type Permission struct { ID uint `json:"id" gorm:"primaryKey"` // 权限ID,主键自增 Name string `json:"name" gorm:"size:64;not null"` // 权限名称 Code string `json:"code" gorm:"size:128;uniqueIndex"` // 权限编码(如 workbook:create) Type string `json:"type" gorm:"size:16;default:'frontend'"` // 权限类型: frontend=前台 backend=后台 Path string `json:"path" gorm:"size:255"` // 关联的API路径 Method string `json:"method" gorm:"size:10"` // HTTP方法: GET/POST/PUT/DELETE Description string `json:"description" gorm:"size:255"` // 权限描述 SortOrder int `json:"sortOrder" gorm:"default:0"` // 排序序号 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // RolePermission 角色-权限关联模型 type RolePermission struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID RoleID uint `json:"roleId" gorm:"uniqueIndex:uk_role_permission"` // 角色ID PermissionID uint `json:"permissionId" gorm:"uniqueIndex:uk_role_permission"` // 权限ID CreatedAt time.Time `json:"createdAt"` // 创建时间 } // UserRole 用户-角色关联模型 type UserRole struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID UserID uint `json:"userId" gorm:"uniqueIndex:uk_user_role"` // 用户ID RoleID uint `json:"roleId" gorm:"uniqueIndex:uk_user_role"` // 角色ID CreatedAt time.Time `json:"createdAt"` // 创建时间 } // ============================================================ // 文档相关模型 // ============================================================ // Document 文档模型 // 顶层文档表,支持多种文档类型(excel/word/pdf) type Document struct { ID uint `json:"id" gorm:"primaryKey"` // 文档ID,主键自增 Type string `json:"type" gorm:"size:16;not null;index"` // 文档类型: excel/word/pdf Title string `json:"title" gorm:"size:255;not null"` // 文档标题 Description string `json:"description" gorm:"size:500"` // 文档描述 CreatorID uint `json:"creatorId" gorm:"index;not null"` // 创建者用户ID Cover string `json:"cover" gorm:"size:512"` // 封面图URL FileSize int64 `json:"fileSize" gorm:"default:0"` // 文件大小(字节) SortOrder int `json:"sortOrder" gorm:"default:0"` // 排序序号 Status int `json:"status" gorm:"default:1"` // 状态: 0=草稿 1=正常 2=归档 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // Workbook 工作簿模型 // Excel文档的工作簿数据,通过 document_id 关联文档表 type Workbook struct { ID uint `json:"id" gorm:"primaryKey"` // 工作簿ID,主键自增 DocumentID uint `json:"documentId" gorm:"uniqueIndex;not null"` // 关联文档ID CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 Document Document `json:"document,omitempty" gorm:"foreignKey:DocumentID"` // 关联的文档 Sheets []Sheet `json:"sheets,omitempty" gorm:"foreignKey:WorkbookID"` // 关联的工作表列表 } // Sheet 工作表/数据表模型 // 每个工作表属于一个工作簿,拥有独立的列配置和布局 type Sheet struct { ID uint `json:"id" gorm:"primaryKey"` // 工作表ID,主键自增 WorkbookID uint `json:"workbookId" gorm:"index;not null"` // 所属工作簿ID Name string `json:"name" gorm:"size:255;not null"` // 工作表名称 Index int `json:"index" gorm:"default:0"` // 工作表排序索引 ColCount int `json:"colCount" gorm:"default:26"` // 列数量 RowCount int `json:"rowCount" gorm:"default:100"` // 行数量 ColWidths string `json:"colWidths" gorm:"type:text"` // 列宽配置(JSON字符串) RowHeights string `json:"rowHeights" gorm:"type:text"` // 行高配置(JSON字符串) CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // Cell 单元格模型 // 每个单元格属于一个工作表,通过行号和列号定位 type Cell struct { ID uint `json:"id" gorm:"primaryKey"` // 单元格ID,主键自增 SheetID uint `json:"sheetId" gorm:"index;not null"` // 所属工作表ID Row int `json:"row" gorm:"index;not null"` // 行号(0-based) Col int `json:"col" gorm:"index;not null"` // 列号(0-based) CellType string `json:"cellType" gorm:"size:20;default:'text'"` // 单元格类型 Value string `json:"value" gorm:"type:text"` // 文本值或计算结果 Formula string `json:"formula" gorm:"type:text"` // 公式表达式(仅formula类型) FileURL string `json:"fileUrl" gorm:"type:text"` // 文件URL FileName string `json:"fileName" gorm:"size:255"` // 文件名 FileSize int64 `json:"fileSize" gorm:"default:0"` // 文件大小(字节) MimeType string `json:"mimeType" gorm:"size:128"` // MIME类型 Style string `json:"style" gorm:"type:text"` // 单元格样式(JSON字符串) CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // ============================================================ // 权限相关模型 // ============================================================ // DocumentRole 文档角色模型 // 定义用户对特定文档的角色(创建者/编辑者/访问者) type DocumentRole struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID DocumentID uint `json:"documentId" gorm:"uniqueIndex:uk_document_user"` // 文档ID UserID uint `json:"userId" gorm:"uniqueIndex:uk_document_user"` // 用户ID RoleType string `json:"roleType" gorm:"size:16;not null"` // 角色类型: creator/editor/viewer CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 } // UserDocument 用户-文档关联模型 // 记录用户访问/收藏/最近打开的文档关系 type UserDocument struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID UserID uint `json:"userId" gorm:"uniqueIndex:uk_user_document"` // 用户ID DocumentID uint `json:"documentId" gorm:"uniqueIndex:uk_user_document"` // 文档ID IsFavorite int `json:"isFavorite" gorm:"default:0"` // 是否收藏: 0=否 1=是 LastOpenAt *time.Time `json:"lastOpenAt"` // 最近打开时间 CreatedAt time.Time `json:"createdAt"` // 创建时间 } // WorkbookPermission 工作簿权限模型 // 工作簿级别的细粒度权限控制 type WorkbookPermission struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID WorkbookID uint `json:"workbookId" gorm:"uniqueIndex:uk_workbook_user"` // 工作簿ID UserID uint `json:"userId" gorm:"uniqueIndex:uk_workbook_user"` // 用户ID CanCreate int `json:"canCreate" gorm:"default:0"` // 是否可创建工作表 CanEdit int `json:"canEdit" gorm:"default:0"` // 是否可编辑单元格 CanDelete int `json:"canDelete" gorm:"default:0"` // 是否可删除 CanShare int `json:"canShare" gorm:"default:0"` // 是否可分享 CanExport int `json:"canExport" gorm:"default:0"` // 是否可导出 CanPrint int `json:"canPrint" gorm:"default:0"` // 是否可打印 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 } // ============================================================ // 操作历史模型 // ============================================================ // OperationLog 编辑历史模型 // 记录用户在工作表上的所有操作,用于撤销/重做和操作审计 type OperationLog struct { ID uint `json:"id" gorm:"primaryKey"` // 操作ID,主键自增 SheetID uint `json:"sheetId" gorm:"index;not null"` // 工作表ID UserID uint `json:"userId" gorm:"default:0"` // 操作用户ID(0=系统操作) Operation string `json:"operation" gorm:"size:32;not null"` // 操作类型 TargetType string `json:"targetType" gorm:"size:32;default:'cell'"` // 操作目标类型: cell/row/col/sheet TargetRange string `json:"targetRange" gorm:"size:128"` // 操作目标范围(如 A1:B5) BeforeData string `json:"beforeData" gorm:"type:json"` // 操作前的数据快照(JSON) AfterData string `json:"afterData" gorm:"type:json"` // 操作后的数据快照(JSON) Description string `json:"description" gorm:"size:512"` // 操作描述(人类可读) IPAddress string `json:"ipAddress" gorm:"size:64"` // 操作者IP地址 CreatedAt time.Time `json:"createdAt"` // 操作时间 } // ============================================================ // 分享链接模型 // ============================================================ // ShareLink 分享链接模型 // 支持生成公开分享链接,可设置密码、有效期、访问次数 type ShareLink struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID DocumentID uint `json:"documentId" gorm:"index;not null"` // 文档ID UserID uint `json:"userId" gorm:"index;not null"` // 创建者用户ID Token string `json:"token" gorm:"size:64;uniqueIndex"` // 分享令牌(唯一标识) Title string `json:"title" gorm:"size:255"` // 分享标题(可自定义) Password string `json:"password" gorm:"size:255"` // 访问密码(加密存储) Permission string `json:"permission" gorm:"size:16;default:'view'"` // 访问权限: view/edit ExpireAt *time.Time `json:"expireAt"` // 过期时间(NULL=永不过期) MaxAccess int `json:"maxAccess" gorm:"default:0"` // 最大访问次数(0=不限) AccessCount int `json:"accessCount" gorm:"default:0"` // 已访问次数 IsActive int `json:"isActive" gorm:"default:1"` // 是否启用 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // ============================================================ // 评论模型 // ============================================================ // Comment 评论模型 // 支持单元格/文档级评论,支持回复和@提及 type Comment struct { ID uint `json:"id" gorm:"primaryKey"` // 评论ID DocumentID uint `json:"documentId" gorm:"index;not null"` // 文档ID SheetID *uint `json:"sheetId" gorm:"index"` // 工作表ID(NULL=文档级评论) ParentID *uint `json:"parentId" gorm:"index"` // 父评论ID(NULL=顶级评论) UserID uint `json:"userId" gorm:"index;not null"` // 评论者用户ID CellRange string `json:"cellRange" gorm:"size:128"` // 关联的单元格范围 Content string `json:"content" gorm:"type:text;not null"` // 评论内容 Mentions string `json:"mentions" gorm:"type:json"` // @提及的用户ID列表(JSON) Status int `json:"status" gorm:"default:1"` // 状态: 0=已隐藏 1=正常 2=已解决 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // ============================================================ // 通知模型 // ============================================================ // Notification 通知模型 // 系统通知:文档分享、评论、协作邀请等 type Notification struct { ID uint `json:"id" gorm:"primaryKey"` // 通知ID UserID uint `json:"userId" gorm:"index;not null"` // 接收者用户ID SenderID *uint `json:"senderId"` // 发送者用户ID(NULL=系统通知) Type string `json:"type" gorm:"size:32;not null"` // 通知类型 Title string `json:"title" gorm:"size:255;not null"` // 通知标题 Content string `json:"content" gorm:"type:text"` // 通知内容 TargetType string `json:"targetType" gorm:"size:32"` // 关联目标类型 TargetID *uint `json:"targetId"` // 关联目标ID IsRead int `json:"isRead" gorm:"default:0"` // 是否已读 CreatedAt time.Time `json:"createdAt"` // 创建时间 } // ============================================================ // 标签模型 // ============================================================ // Tag 标签模型 type Tag struct { ID uint `json:"id" gorm:"primaryKey"` // 标签ID UserID uint `json:"userId" gorm:"not null"` // 创建者用户ID Name string `json:"name" gorm:"size:64;not null"` // 标签名称 Color string `json:"color" gorm:"size:16;default:'#1890ff'"` // 标签颜色 SortOrder int `json:"sortOrder" gorm:"default:0"` // 排序序号 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // DocumentTag 文档-标签关联模型 type DocumentTag struct { ID uint `json:"id" gorm:"primaryKey"` // 主键ID DocumentID uint `json:"documentId" gorm:"uniqueIndex:uk_document_tag"` // 文档ID TagID uint `json:"tagId" gorm:"uniqueIndex:uk_document_tag"` // 标签ID CreatedAt time.Time `json:"createdAt"` // 创建时间 } // ============================================================ // 文件夹模型 // ============================================================ // Folder 文件夹模型 // 支持多层级文件夹组织结构 type Folder struct { ID uint `json:"id" gorm:"primaryKey"` // 文件夹ID ParentID *uint `json:"parentId"` // 父文件夹ID(NULL=根目录) UserID uint `json:"userId" gorm:"index;not null"` // 创建者用户ID Name string `json:"name" gorm:"size:255;not null"` // 文件夹名称 Icon string `json:"icon" gorm:"size:64;default:'folder'"` // 图标名称 SortOrder int `json:"sortOrder" gorm:"default:0"` // 排序序号 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // ============================================================ // 文件上传模型 // ============================================================ // FileUpload 文件上传记录模型 // 集中管理所有上传的文件,支持文件版本控制 type FileUpload struct { ID uint `json:"id" gorm:"primaryKey"` // 文件ID UserID uint `json:"userId" gorm:"index;not null"` // 上传者用户ID DocumentID *uint `json:"documentId"` // 关联文档ID(可选) OriginalName string `json:"originalName" gorm:"size:512;not null"` // 原始文件名 StorageName string `json:"storageName" gorm:"size:512;not null"` // 存储文件名 FilePath string `json:"filePath" gorm:"size:1024;not null"` // 文件存储路径 FileSize int64 `json:"fileSize" gorm:"default:0"` // 文件大小(字节) MimeType string `json:"mimeType" gorm:"size:128"` // MIME类型 FileType string `json:"fileType" gorm:"size:32;default:'file'"` // 文件类型 MD5Hash string `json:"md5Hash" gorm:"size:64"` // 文件MD5哈希(用于去重) Version int `json:"version" gorm:"default:1"` // 版本号 ParentID *uint `json:"parentId"` // 父版本ID Status int `json:"status" gorm:"default:1"` // 状态: 0=已删除 1=正常 CreatedAt time.Time `json:"createdAt"` // 上传时间 } // ============================================================ // 版本历史模型 // ============================================================ // Version 文档版本历史模型 // 支持版本对比和回滚 type Version struct { ID uint `json:"id" gorm:"primaryKey"` // 版本ID DocumentID uint `json:"documentId" gorm:"uniqueIndex:uk_doc_version;not null"` // 文档ID UserID uint `json:"userId" gorm:"index;not null"` // 操作者用户ID VersionNumber int `json:"versionNumber" gorm:"uniqueIndex:uk_doc_version;not null"` // 版本号 Title string `json:"title" gorm:"size:255"` // 版本标题 Description string `json:"description" gorm:"size:500"` // 版本描述 Snapshot string `json:"snapshot" gorm:"type:json;not null"` // 文档数据快照(JSON) Diff string `json:"diff" gorm:"type:json"` // 与上一版本的差异(JSON) IsAuto int `json:"isAuto" gorm:"default:0"` // 是否自动保存 CreatedAt time.Time `json:"createdAt"` // 创建时间 } // ============================================================ // 协作会话模型 // ============================================================ // CollaborationSession 协作会话模型 // 记录实时协作的会话信息,跟踪在线用户状态 type CollaborationSession struct { ID uint `json:"id" gorm:"primaryKey"` // 会话ID DocumentID uint `json:"documentId" gorm:"index;not null"` // 文档ID UserID uint `json:"userId" gorm:"index;not null"` // 用户ID SessionID string `json:"sessionId" gorm:"size:64;index"` // WebSocket会话ID CursorRow *int `json:"cursorRow"` // 当前光标行位置 CursorCol *int `json:"cursorCol"` // 当前光标列位置 Selection string `json:"selection" gorm:"size:128"` // 当前选区范围 IsActive int `json:"isActive" gorm:"default:1"` // 是否在线 JoinedAt time.Time `json:"joinedAt"` // 加入时间 LastActiveAt time.Time `json:"lastActiveAt"` // 最后活跃时间 } // ============================================================ // 导出历史模型 // ============================================================ // ExportHistory 导出历史模型 type ExportHistory struct { ID uint `json:"id" gorm:"primaryKey"` // 导出ID DocumentID uint `json:"documentId" gorm:"index;not null"` // 文档ID UserID uint `json:"userId" gorm:"index;not null"` // 导出者用户ID Format string `json:"format" gorm:"size:16;not null"` // 导出格式: xlsx/csv/pdf FilePath string `json:"filePath" gorm:"size:1024"` // 导出文件路径 FileSize int64 `json:"fileSize" gorm:"default:0"` // 导出文件大小 Status int `json:"status" gorm:"default:0"` // 状态: 0=处理中 1=完成 2=失败 ErrorMsg string `json:"errorMsg" gorm:"size:512"` // 错误信息 CreatedAt time.Time `json:"createdAt"` // 创建时间 } // ============================================================ // 设置模型 // ============================================================ // Setting 设置模型 // 用户个人设置和系统全局配置 type Setting struct { ID uint `json:"id" gorm:"primaryKey"` // 设置ID UserID *uint `json:"userId"` // 用户ID(NULL=系统设置) Category string `json:"category" gorm:"size:32;not null"` // 设置分类 Key string `json:"key" gorm:"size:128;not null"` // 设置键名 Value string `json:"value" gorm:"type:text"` // 设置值 Description string `json:"description" gorm:"size:255"` // 设置描述 CreatedAt time.Time `json:"createdAt"` // 创建时间 UpdatedAt time.Time `json:"updatedAt"` // 更新时间 } // ============================================================ // API密钥模型 // ============================================================ // ApiKey API密钥模型 type ApiKey struct { ID uint `json:"id" gorm:"primaryKey"` // 密钥ID UserID uint `json:"userId" gorm:"index;not null"` // 用户ID Name string `json:"name" gorm:"size:128;not null"` // 密钥名称 KeyHash string `json:"keyHash" gorm:"size:255;index"` // 密钥哈希 KeyPrefix string `json:"keyPrefix" gorm:"size:16"` // 密钥前缀 Permissions string `json:"permissions" gorm:"type:json"` // 权限范围(JSON) RateLimit int `json:"rateLimit" gorm:"default:1000"` // 速率限制 LastUsedAt *time.Time `json:"lastUsedAt"` // 最后使用时间 ExpireAt *time.Time `json:"expireAt"` // 过期时间 IsActive int `json:"isActive" gorm:"default:1"` // 是否启用 CreatedAt time.Time `json:"createdAt"` // 创建时间 DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` // 软删除时间戳 } // ============================================================ // 审计日志模型 // ============================================================ // AuditLog 审计日志模型 type AuditLog struct { ID uint `json:"id" gorm:"primaryKey"` // 日志ID UserID *uint `json:"userId"` // 操作者用户ID Action string `json:"action" gorm:"size:64;not null"` // 操作类型 ResourceType string `json:"resourceType" gorm:"size:32;not null"` // 资源类型 ResourceID *uint `json:"resourceId"` // 资源ID OldValue string `json:"oldValue" gorm:"type:json"` // 操作前的值(JSON) NewValue string `json:"newValue" gorm:"type:json"` // 操作后的值(JSON) IPAddress string `json:"ipAddress" gorm:"size:64"` // 操作者IP地址 UserAgent string `json:"userAgent" gorm:"size:512"` // 浏览器User-Agent Status int `json:"status" gorm:"default:1"` // 状态: 0=失败 1=成功 ErrorMsg string `json:"errorMsg" gorm:"size:512"` // 错误信息 CreatedAt time.Time `json:"createdAt"` // 操作时间 }