115 lines
4.8 KiB
Go
115 lines
4.8 KiB
Go
package models
|
||
|
||
import (
|
||
"database/sql/driver"
|
||
"encoding/json"
|
||
"time"
|
||
)
|
||
|
||
// 操作类型常量
|
||
const (
|
||
OperationTypeBalanceAdjustment = "balance_adjustment" // 余额调整
|
||
OperationTypeUserLevelChange = "user_level_change" // 用户等级修改
|
||
OperationTypeUserStatusChange = "user_status_change" // 用户状态变更
|
||
OperationTypeModelPriceChange = "model_price_change" // 模型价格修改
|
||
OperationTypePermissionChange = "permission_change" // 权限变更
|
||
OperationTypeConfigChange = "config_change" // 配置修改
|
||
)
|
||
|
||
// 操作状态常量
|
||
const (
|
||
AuditLogStatusSuccess = "success" // 成功
|
||
AuditLogStatusFailed = "failed" // 失败
|
||
)
|
||
|
||
// 操作对象类型常量
|
||
const (
|
||
TargetTypeUser = "user" // 用户
|
||
TargetTypeModel = "model" // 模型
|
||
TargetTypeConfig = "config" // 配置
|
||
)
|
||
|
||
// OperationDetails 操作详情JSON结构
|
||
type OperationDetails map[string]interface{}
|
||
|
||
// Value 实现 driver.Valuer 接口
|
||
func (d OperationDetails) Value() (driver.Value, error) {
|
||
if d == nil {
|
||
return nil, nil
|
||
}
|
||
return json.Marshal(d)
|
||
}
|
||
|
||
// Scan 实现 sql.Scanner 接口
|
||
func (d *OperationDetails) Scan(value interface{}) error {
|
||
if value == nil {
|
||
*d = nil
|
||
return nil
|
||
}
|
||
bytes, ok := value.([]byte)
|
||
if !ok {
|
||
return nil
|
||
}
|
||
return json.Unmarshal(bytes, d)
|
||
}
|
||
|
||
// AuditLog 审计日志模型
|
||
type AuditLog struct {
|
||
ID uint `json:"id" gorm:"primaryKey;autoIncrement;comment:主键ID"`
|
||
OperationType string `json:"operation_type" gorm:"type:varchar(50);not null;index:idx_operation_type_time;comment:操作类型"`
|
||
OperationTime time.Time `json:"operation_time" gorm:"not null;index:idx_operation_type_time;index:idx_operation_time;comment:操作时间"`
|
||
OperatorID int `json:"operator_id" gorm:"not null;index:idx_operator_time;comment:操作人ID"`
|
||
OperatorEmail string `json:"operator_email" gorm:"type:varchar(255);not null;index:idx_operator_email;comment:操作人邮箱"`
|
||
TargetType string `json:"target_type" gorm:"type:varchar(50);comment:操作对象类型"`
|
||
TargetID *int `json:"target_id" gorm:"comment:操作对象ID"`
|
||
TargetEmail string `json:"target_email" gorm:"type:varchar(255);index:idx_target_email;comment:操作对象邮箱"`
|
||
OperationDetails OperationDetails `json:"operation_details" gorm:"type:json;comment:操作详情JSON"`
|
||
IPAddress string `json:"ip_address" gorm:"type:varchar(45);comment:操作来源IP地址"`
|
||
UserAgent string `json:"user_agent" gorm:"type:varchar(500);comment:用户代理"`
|
||
Status string `json:"status" gorm:"type:varchar(20);default:'success';comment:操作状态"`
|
||
ErrorMessage string `json:"error_message" gorm:"type:text;comment:错误信息"`
|
||
CreatedAt time.Time `json:"created_at" gorm:"not null;comment:记录创建时间"`
|
||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;comment:记录更新时间"`
|
||
}
|
||
|
||
// TableName 指定数据库表名
|
||
func (AuditLog) TableName() string {
|
||
return "admin_audit_logs"
|
||
}
|
||
|
||
// AuditLogListRequest 审计日志列表请求
|
||
type AuditLogListRequest struct {
|
||
OperationType string `form:"operation_type"` // 操作类型筛选
|
||
OperatorEmail string `form:"operator_email"` // 操作人筛选
|
||
TargetEmail string `form:"target_email"` // 操作对象搜索(模糊匹配)
|
||
StartTime string `form:"start_time"` // 开始时间
|
||
EndTime string `form:"end_time"` // 结束时间
|
||
Page int `form:"page,default=1"` // 页码
|
||
Size int `form:"size,default=20"` // 每页数量
|
||
SortBy string `form:"sort_by"` // 排序字段,默认:operation_time
|
||
SortOrder string `form:"sort_order"` // 排序方向,desc/asc,默认:desc
|
||
}
|
||
|
||
// AuditLogListResponse 审计日志列表响应
|
||
type AuditLogListResponse struct {
|
||
List []AuditLog `json:"list"`
|
||
Total int64 `json:"total"`
|
||
Page int `json:"page"`
|
||
Size int `json:"size"`
|
||
}
|
||
|
||
// CreateAuditLogRequest 创建审计日志请求(内部使用)
|
||
type CreateAuditLogRequest struct {
|
||
OperationType string `json:"operation_type" binding:"required"`
|
||
OperatorID int `json:"operator_id" binding:"required"`
|
||
OperatorEmail string `json:"operator_email" binding:"required"`
|
||
TargetType string `json:"target_type"`
|
||
TargetID *int `json:"target_id"`
|
||
TargetEmail string `json:"target_email"`
|
||
OperationDetails OperationDetails `json:"operation_details"`
|
||
IPAddress string `json:"ip_address"`
|
||
UserAgent string `json:"user_agent"`
|
||
Status string `json:"status"`
|
||
ErrorMessage string `json:"error_message"`
|
||
}
|