121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"goalfymax-admin/internal/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// LogStorage 日志存储接口
|
|
type LogStorage interface {
|
|
CreateLoginLog(log *models.LoginLog) error
|
|
CreateOperationLog(log *models.OperationLog) error
|
|
GetLoginLogs(req *models.LoginLogListRequest) ([]models.LoginLog, int64, error)
|
|
GetOperationLogs(req *models.OperationLogListRequest) ([]models.OperationLog, int64, error)
|
|
DeleteLoginLogs(beforeDate string) error
|
|
DeleteOperationLogs(beforeDate string) error
|
|
}
|
|
|
|
type logStorage struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewLogStorage 创建日志存储实例
|
|
func NewLogStorage() LogStorage {
|
|
return &logStorage{db: DB}
|
|
}
|
|
|
|
// CreateLoginLog 创建登录日志
|
|
func (s *logStorage) CreateLoginLog(log *models.LoginLog) error {
|
|
return s.db.Create(log).Error
|
|
}
|
|
|
|
// CreateOperationLog 创建操作日志
|
|
func (s *logStorage) CreateOperationLog(log *models.OperationLog) error {
|
|
return s.db.Create(log).Error
|
|
}
|
|
|
|
// GetLoginLogs 获取登录日志列表
|
|
func (s *logStorage) GetLoginLogs(req *models.LoginLogListRequest) ([]models.LoginLog, int64, error) {
|
|
var logs []models.LoginLog
|
|
var total int64
|
|
|
|
query := s.db.Model(&models.LoginLog{})
|
|
|
|
// 构建查询条件
|
|
if req.Username != "" {
|
|
query = query.Where("username LIKE ?", "%"+req.Username+"%")
|
|
}
|
|
if req.IP != "" {
|
|
query = query.Where("ip LIKE ?", "%"+req.IP+"%")
|
|
}
|
|
if req.Status != nil {
|
|
query = query.Where("status = ?", *req.Status)
|
|
}
|
|
if req.StartTime != "" {
|
|
query = query.Where("created_at >= ?", req.StartTime)
|
|
}
|
|
if req.EndTime != "" {
|
|
query = query.Where("created_at <= ?", req.EndTime)
|
|
}
|
|
|
|
// 获取总数
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 分页查询
|
|
offset := (req.Page - 1) * req.Size
|
|
err := query.Order("created_at DESC").Offset(offset).Limit(req.Size).Find(&logs).Error
|
|
|
|
return logs, total, err
|
|
}
|
|
|
|
// GetOperationLogs 获取操作日志列表
|
|
func (s *logStorage) GetOperationLogs(req *models.OperationLogListRequest) ([]models.OperationLog, int64, error) {
|
|
var logs []models.OperationLog
|
|
var total int64
|
|
|
|
query := s.db.Model(&models.OperationLog{})
|
|
|
|
// 构建查询条件
|
|
if req.Username != "" {
|
|
query = query.Where("username LIKE ?", "%"+req.Username+"%")
|
|
}
|
|
if req.Module != "" {
|
|
query = query.Where("module LIKE ?", "%"+req.Module+"%")
|
|
}
|
|
if req.Operation != "" {
|
|
query = query.Where("operation LIKE ?", "%"+req.Operation+"%")
|
|
}
|
|
if req.Status != nil {
|
|
query = query.Where("status = ?", *req.Status)
|
|
}
|
|
if req.StartTime != "" {
|
|
query = query.Where("created_at >= ?", req.StartTime)
|
|
}
|
|
if req.EndTime != "" {
|
|
query = query.Where("created_at <= ?", req.EndTime)
|
|
}
|
|
|
|
// 获取总数
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 分页查询
|
|
offset := (req.Page - 1) * req.Size
|
|
err := query.Order("created_at DESC").Offset(offset).Limit(req.Size).Find(&logs).Error
|
|
|
|
return logs, total, err
|
|
}
|
|
|
|
// DeleteLoginLogs 删除指定日期之前的登录日志
|
|
func (s *logStorage) DeleteLoginLogs(beforeDate string) error {
|
|
return s.db.Where("created_at < ?", beforeDate).Delete(&models.LoginLog{}).Error
|
|
}
|
|
|
|
// DeleteOperationLogs 删除指定日期之前的操作日志
|
|
func (s *logStorage) DeleteOperationLogs(beforeDate string) error {
|
|
return s.db.Where("created_at < ?", beforeDate).Delete(&models.OperationLog{}).Error
|
|
}
|