ListUserActionLogs
This commit is contained in:
@@ -1,22 +1,27 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FindAuditLogOptions 定义了查询审计日志的选项
|
||||
type FindAuditLogOptions struct {
|
||||
UserID *uint // 根据用户ID过滤
|
||||
ActionType string // 根据操作类型过滤
|
||||
Page int // 页码
|
||||
PageSize int // 每页大小
|
||||
// UserActionLogListOptions 定义了查询用户操作日志时的可选参数
|
||||
type UserActionLogListOptions struct {
|
||||
UserID *uint
|
||||
Username *string
|
||||
ActionType *string
|
||||
Status *models.AuditStatus
|
||||
StartTime *time.Time // 基于 time 字段
|
||||
EndTime *time.Time // 基于 time 字段
|
||||
OrderBy string // 例如 "time asc"
|
||||
}
|
||||
|
||||
// UserActionLogRepository 定义了与用户操作日志相关的数据库操作接口
|
||||
type UserActionLogRepository interface {
|
||||
Create(log *models.UserActionLog) error
|
||||
List(options FindAuditLogOptions) ([]*models.UserActionLog, int64, error)
|
||||
List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
|
||||
}
|
||||
|
||||
// gormUserActionLogRepository 是 UserActionLogRepository 的 GORM 实现
|
||||
@@ -35,17 +40,33 @@ func (r *gormUserActionLogRepository) Create(log *models.UserActionLog) error {
|
||||
}
|
||||
|
||||
// List 根据选项查询用户操作日志,并返回总数
|
||||
func (r *gormUserActionLogRepository) List(options FindAuditLogOptions) ([]*models.UserActionLog, int64, error) {
|
||||
var logs []*models.UserActionLog
|
||||
func (r *gormUserActionLogRepository) List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var logs []models.UserActionLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.UserActionLog{})
|
||||
|
||||
if options.UserID != nil {
|
||||
query = query.Where("user_id = ?", *options.UserID)
|
||||
if opts.UserID != nil {
|
||||
query = query.Where("user_id = ?", *opts.UserID)
|
||||
}
|
||||
if options.ActionType != "" {
|
||||
query = query.Where("action_type = ?", options.ActionType)
|
||||
if opts.Username != nil {
|
||||
query = query.Where("username LIKE ?", "%"+*opts.Username+"%")
|
||||
}
|
||||
if opts.ActionType != nil {
|
||||
query = query.Where("action_type = ?", *opts.ActionType)
|
||||
}
|
||||
if opts.Status != nil {
|
||||
query = query.Where("status = ?", *opts.Status)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("time >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("time <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
// 统计总数
|
||||
@@ -53,16 +74,14 @@ func (r *gormUserActionLogRepository) List(options FindAuditLogOptions) ([]*mode
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
if options.Page > 0 && options.PageSize > 0 {
|
||||
offset := (options.Page - 1) * options.PageSize
|
||||
query = query.Offset(offset).Limit(options.PageSize)
|
||||
orderBy := "time DESC"
|
||||
if opts.OrderBy != "" {
|
||||
orderBy = opts.OrderBy
|
||||
}
|
||||
query = query.Order(orderBy)
|
||||
|
||||
// 默认按创建时间倒序
|
||||
query = query.Order("created_at DESC")
|
||||
|
||||
if err := query.Find(&logs).Error; err != nil {
|
||||
offset := (page - 1) * pageSize
|
||||
if err := query.Limit(pageSize).Offset(offset).Find(&logs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user