107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package models
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// BaseModel 基础模型,包含通用字段
|
||
|
|
type BaseModel struct {
|
||
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
||
|
|
CreatedAt time.Time `json:"created_at"`
|
||
|
|
UpdatedAt time.Time `json:"updated_at"`
|
||
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// User 用户模型
|
||
|
|
type User struct {
|
||
|
|
BaseModel
|
||
|
|
Username string `gorm:"uniqueIndex;size:50;not null" json:"username"`
|
||
|
|
Password string `gorm:"size:255;not null" json:"-"`
|
||
|
|
Email string `gorm:"uniqueIndex;size:100;not null" json:"email"`
|
||
|
|
IsAdmin bool `gorm:"default:false" json:"is_admin"`
|
||
|
|
IsActive bool `gorm:"default:true" json:"is_active"`
|
||
|
|
LastLoginAt time.Time `json:"last_login_at"`
|
||
|
|
Quota Quota `gorm:"foreignKey:UserID" json:"quota"`
|
||
|
|
Conversations []Conversation `gorm:"foreignKey:UserID" json:"conversations,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// Quota 配额模型
|
||
|
|
type Quota struct {
|
||
|
|
BaseModel
|
||
|
|
UserID uint `gorm:"uniqueIndex;not null" json:"user_id"`
|
||
|
|
TotalTokens int64 `gorm:"default:100000" json:"total_tokens"`
|
||
|
|
UsedTokens int64 `gorm:"default:0" json:"used_tokens"`
|
||
|
|
ResetAt time.Time `json:"reset_at"`
|
||
|
|
TokenLimit int64 `gorm:"default:1000" json:"token_limit"` // 单次请求限制
|
||
|
|
}
|
||
|
|
|
||
|
|
// Conversation 对话模型
|
||
|
|
type Conversation struct {
|
||
|
|
BaseModel
|
||
|
|
UserID uint `gorm:"index;not null" json:"user_id"`
|
||
|
|
Title string `gorm:"size:200;default:'新对话'" json:"title"`
|
||
|
|
Messages []Message `gorm:"foreignKey:ConversationID" json:"messages,omitempty"`
|
||
|
|
TotalTokens int64 `gorm:"default:0" json:"total_tokens"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// Message 消息模型
|
||
|
|
type Message struct {
|
||
|
|
BaseModel
|
||
|
|
ConversationID uint `gorm:"index;not null" json:"conversation_id"`
|
||
|
|
Role string `gorm:"size:20;not null" json:"role"` // user, assistant
|
||
|
|
Content string `gorm:"type:text;not null" json:"content"`
|
||
|
|
Tokens int64 `gorm:"default:0" json:"tokens"`
|
||
|
|
Model string `gorm:"size:50;default:'default'" json:"model"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// ChatRequest 聊天请求模型
|
||
|
|
type ChatRequest struct {
|
||
|
|
Message string `json:"message" binding:"required"`
|
||
|
|
ConversationID uint `json:"conversation_id,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// ChatResponse 聊天响应模型
|
||
|
|
type ChatResponse struct {
|
||
|
|
Response string `json:"response"`
|
||
|
|
ConversationID uint `json:"conversation_id"`
|
||
|
|
TokensUsed int64 `json:"tokens_used"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// UserResponse 用户响应模型(隐藏敏感信息)
|
||
|
|
type UserResponse struct {
|
||
|
|
ID uint `json:"id"`
|
||
|
|
Username string `json:"username"`
|
||
|
|
Email string `json:"email"`
|
||
|
|
IsAdmin bool `json:"is_admin"`
|
||
|
|
IsActive bool `json:"is_active"`
|
||
|
|
CreatedAt time.Time `json:"created_at"`
|
||
|
|
Quota Quota `json:"quota"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoginRequest 登录请求模型
|
||
|
|
type LoginRequest struct {
|
||
|
|
Username string `json:"username" binding:"required"`
|
||
|
|
Password string `json:"password" binding:"required"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// LoginResponse 登录响应模型
|
||
|
|
type LoginResponse struct {
|
||
|
|
Token string `json:"token"`
|
||
|
|
User UserResponse `json:"user"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// RegisterRequest 注册请求模型
|
||
|
|
type RegisterRequest struct {
|
||
|
|
Username string `json:"username" binding:"required,min=3,max=50"`
|
||
|
|
Email string `json:"email" binding:"required,email"`
|
||
|
|
Password string `json:"password" binding:"required,min=6"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// QuotaUpdateRequest 配额更新请求模型
|
||
|
|
type QuotaUpdateRequest struct {
|
||
|
|
TotalTokens int64 `json:"total_tokens" binding:"required,min=0"`
|
||
|
|
TokenLimit int64 `json:"token_limit" binding:"required,min=1"`
|
||
|
|
}
|