2026-04-22 16:01:59 +08:00
|
|
|
|
package repository
|
2026-03-15 02:25:10 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-04-22 16:01:59 +08:00
|
|
|
|
"with_you/internal/dto"
|
|
|
|
|
|
"with_you/internal/model"
|
2026-03-15 02:25:10 +08:00
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-26 18:14:16 +08:00
|
|
|
|
// LoginLogRepository 登录日志仓储接口
|
|
|
|
|
|
type LoginLogRepository interface {
|
|
|
|
|
|
CreateLoginLog(ctx context.Context, log *model.LoginLog) error
|
|
|
|
|
|
BatchCreateLoginLogs(ctx context.Context, logs []*model.LoginLog) error
|
|
|
|
|
|
GetLoginLogs(ctx context.Context, filters dto.LoginFilter, page, pageSize int) ([]*model.LoginLog, int64, error)
|
|
|
|
|
|
GetLoginLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.LoginLog, int64, error)
|
|
|
|
|
|
GetFailedLoginsByIP(ctx context.Context, ip string, timeWindow time.Duration) (int64, error)
|
|
|
|
|
|
GetRecentSuccessLogins(ctx context.Context, userID string, limit int) ([]*model.LoginLog, error)
|
|
|
|
|
|
DeleteOldLogs(ctx context.Context, beforeDate string) error
|
|
|
|
|
|
GetStatistics(ctx context.Context, startTime, endTime string) (*LoginLogStatistics, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// loginLogRepository 登录日志仓储实现
|
|
|
|
|
|
type loginLogRepository struct {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewLoginLogRepository 创建登录日志仓储
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func NewLoginLogRepository(db *gorm.DB) LoginLogRepository {
|
|
|
|
|
|
return &loginLogRepository{db: db}
|
2026-03-15 02:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreateLoginLog 创建单条登录日志
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) CreateLoginLog(ctx context.Context, log *model.LoginLog) error {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
return r.db.WithContext(ctx).Create(log).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BatchCreateLoginLogs 批量创建登录日志
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) BatchCreateLoginLogs(ctx context.Context, logs []*model.LoginLog) error {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
if len(logs) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return r.db.WithContext(ctx).Create(logs).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetLoginLogs 获取登录日志列表(分页)
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) GetLoginLogs(ctx context.Context, filters dto.LoginFilter, page, pageSize int) ([]*model.LoginLog, int64, error) {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
query := r.db.WithContext(ctx).Model(&model.LoginLog{})
|
|
|
|
|
|
|
|
|
|
|
|
if filters.UserID != "" {
|
|
|
|
|
|
query = query.Where("user_id = ?", filters.UserID)
|
|
|
|
|
|
}
|
|
|
|
|
|
if filters.Event != "" {
|
|
|
|
|
|
query = query.Where("event = ?", filters.Event)
|
|
|
|
|
|
}
|
|
|
|
|
|
if filters.Result != "" {
|
|
|
|
|
|
query = query.Where("result = ?", filters.Result)
|
|
|
|
|
|
}
|
|
|
|
|
|
if filters.FailReason != "" {
|
|
|
|
|
|
query = query.Where("fail_reason = ?", filters.FailReason)
|
|
|
|
|
|
}
|
|
|
|
|
|
if filters.LoginType != "" {
|
|
|
|
|
|
query = query.Where("login_type = ?", filters.LoginType)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !filters.StartTime.IsZero() {
|
|
|
|
|
|
query = query.Where("created_at >= ?", filters.StartTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !filters.EndTime.IsZero() {
|
|
|
|
|
|
query = query.Where("created_at <= ?", filters.EndTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
if filters.IP != "" {
|
|
|
|
|
|
query = query.Where("ip = ?", filters.IP)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var total int64
|
|
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var logs []*model.LoginLog
|
|
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
|
if err := query.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&logs).Error; err != nil {
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return logs, total, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetLoginLogsByUser 获取指定用户的登录日志
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) GetLoginLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.LoginLog, int64, error) {
|
|
|
|
|
|
return r.GetLoginLogs(ctx, dto.LoginFilter{UserID: userID}, page, pageSize)
|
2026-03-15 02:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetFailedLoginsByIP 获取指定IP的失败登录记录(用于风控)
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) GetFailedLoginsByIP(ctx context.Context, ip string, timeWindow time.Duration) (int64, error) {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
startTime := time.Now().Add(-timeWindow)
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
err := r.db.WithContext(ctx).
|
|
|
|
|
|
Model(&model.LoginLog{}).
|
|
|
|
|
|
Where("ip = ? AND result = ? AND created_at >= ?", ip, string(model.LoginResultFail), startTime).
|
|
|
|
|
|
Count(&count).Error
|
|
|
|
|
|
return count, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetRecentSuccessLogins 获取最近成功的登录记录
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) GetRecentSuccessLogins(ctx context.Context, userID string, limit int) ([]*model.LoginLog, error) {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
var logs []*model.LoginLog
|
|
|
|
|
|
err := r.db.WithContext(ctx).
|
|
|
|
|
|
Where("user_id = ? AND result = ? AND event = ?", userID, string(model.LoginResultSuccess), string(model.LoginEventLogin)).
|
|
|
|
|
|
Order("created_at DESC").
|
|
|
|
|
|
Limit(limit).
|
|
|
|
|
|
Find(&logs).Error
|
|
|
|
|
|
return logs, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteOldLogs 删除旧的登录日志(用于定时清理)
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) DeleteOldLogs(ctx context.Context, beforeDate string) error {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
return r.db.WithContext(ctx).Where("created_at < ?", beforeDate).Delete(&model.LoginLog{}).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-26 01:50:54 +08:00
|
|
|
|
// GetStatistics 获取登录日志统计(使用单次 GROUP BY 查询,避免多次 COUNT)
|
2026-03-26 18:14:16 +08:00
|
|
|
|
func (r *loginLogRepository) GetStatistics(ctx context.Context, startTime, endTime string) (*LoginLogStatistics, error) {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
stats := &LoginLogStatistics{}
|
|
|
|
|
|
|
|
|
|
|
|
query := r.db.WithContext(ctx).Model(&model.LoginLog{})
|
|
|
|
|
|
if startTime != "" {
|
|
|
|
|
|
query = query.Where("created_at >= ?", startTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
if endTime != "" {
|
|
|
|
|
|
query = query.Where("created_at <= ?", endTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-26 01:50:54 +08:00
|
|
|
|
// 使用单次 GROUP BY 查询获取 result 计数
|
|
|
|
|
|
type resultCount struct {
|
|
|
|
|
|
Result string
|
|
|
|
|
|
Count int64
|
2026-03-15 02:25:10 +08:00
|
|
|
|
}
|
2026-03-26 01:50:54 +08:00
|
|
|
|
var results []resultCount
|
|
|
|
|
|
err := query.Select("result, count(*) as count").Group("result").Scan(&results).Error
|
|
|
|
|
|
if err != nil {
|
2026-03-15 02:25:10 +08:00
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-26 01:50:54 +08:00
|
|
|
|
// 汇总结果
|
|
|
|
|
|
for _, r := range results {
|
|
|
|
|
|
stats.TotalCount += r.Count
|
|
|
|
|
|
if r.Result == string(model.LoginResultSuccess) {
|
|
|
|
|
|
stats.SuccessCount = r.Count
|
|
|
|
|
|
} else if r.Result == string(model.LoginResultFail) {
|
|
|
|
|
|
stats.FailCount = r.Count
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 单独查询登录次数
|
|
|
|
|
|
if startTime != "" || endTime != "" {
|
|
|
|
|
|
loginQuery := r.db.WithContext(ctx).Model(&model.LoginLog{})
|
|
|
|
|
|
if startTime != "" {
|
|
|
|
|
|
loginQuery = loginQuery.Where("created_at >= ?", startTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
if endTime != "" {
|
|
|
|
|
|
loginQuery = loginQuery.Where("created_at <= ?", endTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
loginQuery.Where("event = ?", string(model.LoginEventLogin)).Count(&stats.LoginCount)
|
2026-03-15 02:25:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LoginLogStatistics 登录日志统计
|
|
|
|
|
|
type LoginLogStatistics struct {
|
|
|
|
|
|
TotalCount int64
|
|
|
|
|
|
SuccessCount int64
|
|
|
|
|
|
FailCount int64
|
|
|
|
|
|
LoginCount int64
|
|
|
|
|
|
}
|