Files
pig-farm-controller/internal/infra/repository/user_action_log_repository.go

96 lines
2.8 KiB
Go
Raw Normal View History

2025-09-28 00:13:47 +08:00
package repository
import (
2025-11-05 23:00:07 +08:00
"context"
2025-10-18 15:47:13 +08:00
"time"
2025-11-05 23:00:07 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-09-28 00:13:47 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-11-05 23:00:07 +08:00
2025-09-28 00:13:47 +08:00
"gorm.io/gorm"
)
2025-10-18 15:47:13 +08:00
// 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"
2025-09-28 01:48:15 +08:00
}
2025-09-28 00:13:47 +08:00
// UserActionLogRepository 定义了与用户操作日志相关的数据库操作接口
type UserActionLogRepository interface {
2025-11-05 23:00:07 +08:00
Create(ctx context.Context, log *models.UserActionLog) error
List(ctx context.Context, opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
2025-09-28 00:13:47 +08:00
}
// gormUserActionLogRepository 是 UserActionLogRepository 的 GORM 实现
type gormUserActionLogRepository struct {
2025-11-05 23:00:07 +08:00
ctx context.Context
db *gorm.DB
2025-09-28 00:13:47 +08:00
}
// NewGormUserActionLogRepository 创建一个新的 UserActionLogRepository GORM 实现实例
2025-11-05 23:00:07 +08:00
func NewGormUserActionLogRepository(ctx context.Context, db *gorm.DB) UserActionLogRepository {
return &gormUserActionLogRepository{ctx: ctx, db: db}
2025-09-28 00:13:47 +08:00
}
// Create 创建一条新的用户操作日志记录
2025-11-05 23:00:07 +08:00
func (r *gormUserActionLogRepository) Create(ctx context.Context, log *models.UserActionLog) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
return r.db.WithContext(repoCtx).Create(log).Error
2025-09-28 00:13:47 +08:00
}
2025-09-28 01:48:15 +08:00
// List 根据选项查询用户操作日志,并返回总数
2025-11-05 23:00:07 +08:00
func (r *gormUserActionLogRepository) List(ctx context.Context, opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
2025-10-18 15:47:13 +08:00
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
var logs []models.UserActionLog
2025-09-28 01:48:15 +08:00
var total int64
2025-11-05 23:00:07 +08:00
query := r.db.WithContext(repoCtx).Model(&models.UserActionLog{})
2025-09-28 01:48:15 +08:00
2025-10-18 15:47:13 +08:00
if opts.UserID != nil {
query = query.Where("user_id = ?", *opts.UserID)
}
if opts.Username != nil {
query = query.Where("username LIKE ?", "%"+*opts.Username+"%")
2025-09-28 01:48:15 +08:00
}
2025-10-18 15:47:13 +08:00
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)
2025-09-28 01:48:15 +08:00
}
// 统计总数
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
2025-10-18 15:47:13 +08:00
orderBy := "time DESC"
if opts.OrderBy != "" {
orderBy = opts.OrderBy
2025-09-28 01:48:15 +08:00
}
2025-10-18 15:47:13 +08:00
query = query.Order(orderBy)
2025-09-28 01:48:15 +08:00
2025-10-18 15:47:13 +08:00
offset := (page - 1) * pageSize
if err := query.Limit(pageSize).Offset(offset).Find(&logs).Error; err != nil {
2025-09-28 01:48:15 +08:00
return nil, 0, err
}
return logs, total, nil
}