refactor: update repository interfaces and improve dependency injection
- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
This commit is contained in:
@@ -36,21 +36,36 @@ type activityCache interface {
|
||||
Expire(ctx context.Context, key string, ttl time.Duration) error
|
||||
}
|
||||
|
||||
// UserActivityRepository 用户活跃数据仓储
|
||||
type UserActivityRepository struct {
|
||||
// UserActivityRepository 用户活跃数据仓储接口
|
||||
type UserActivityRepository interface {
|
||||
RecordActivity(ctx context.Context, userID, loginType, ip, userAgent string) error
|
||||
GetDAU(ctx context.Context, date time.Time) (int64, error)
|
||||
GetActiveUserIDs(ctx context.Context, date time.Time) ([]string, error)
|
||||
GetUserActivityCount(ctx context.Context, userID string, startDate, endDate time.Time) (int64, error)
|
||||
SaveStat(ctx context.Context, stat *model.UserActivityStat) error
|
||||
GetStat(ctx context.Context, statType string, date time.Time) (*model.UserActivityStat, error)
|
||||
RecordActivityToRedis(ctx context.Context, userID string) error
|
||||
GetDAUFromRedis(ctx context.Context, date time.Time) (int64, error)
|
||||
GetWAUFromRedis(ctx context.Context, year int, week int) (int64, error)
|
||||
GetMAUFromRedis(ctx context.Context, year int, month int) (int64, error)
|
||||
GetDAUUserIDsFromRedis(ctx context.Context, date time.Time) ([]string, error)
|
||||
}
|
||||
|
||||
// userActivityRepository 用户活跃数据仓储实现
|
||||
type userActivityRepository struct {
|
||||
db *gorm.DB
|
||||
cache activityCache
|
||||
}
|
||||
|
||||
// NewUserActivityRepository 创建用户活跃数据仓储
|
||||
func NewUserActivityRepository(db *gorm.DB, cache activityCache) *UserActivityRepository {
|
||||
return &UserActivityRepository{db: db, cache: cache}
|
||||
func NewUserActivityRepository(db *gorm.DB, cache activityCache) UserActivityRepository {
|
||||
return &userActivityRepository{db: db, cache: cache}
|
||||
}
|
||||
|
||||
// ==================== 数据库操作方法 ====================
|
||||
|
||||
// RecordActivity 记录用户活跃(写入数据库)
|
||||
func (r *UserActivityRepository) RecordActivity(ctx context.Context, userID, loginType, ip, userAgent string) error {
|
||||
func (r *userActivityRepository) RecordActivity(ctx context.Context, userID, loginType, ip, userAgent string) error {
|
||||
log := &model.UserActiveLog{
|
||||
UserID: userID,
|
||||
ActiveDate: time.Now(),
|
||||
@@ -62,7 +77,7 @@ func (r *UserActivityRepository) RecordActivity(ctx context.Context, userID, log
|
||||
}
|
||||
|
||||
// GetDAU 获取指定日期的日活用户数(从数据库)
|
||||
func (r *UserActivityRepository) GetDAU(ctx context.Context, date time.Time) (int64, error) {
|
||||
func (r *userActivityRepository) GetDAU(ctx context.Context, date time.Time) (int64, error) {
|
||||
var count int64
|
||||
startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
|
||||
endOfDay := startOfDay.Add(24 * time.Hour)
|
||||
@@ -77,7 +92,7 @@ func (r *UserActivityRepository) GetDAU(ctx context.Context, date time.Time) (in
|
||||
}
|
||||
|
||||
// GetActiveUserIDs 获取指定日期的活跃用户ID列表
|
||||
func (r *UserActivityRepository) GetActiveUserIDs(ctx context.Context, date time.Time) ([]string, error) {
|
||||
func (r *userActivityRepository) GetActiveUserIDs(ctx context.Context, date time.Time) ([]string, error) {
|
||||
var userIDs []string
|
||||
startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
|
||||
endOfDay := startOfDay.Add(24 * time.Hour)
|
||||
@@ -92,7 +107,7 @@ func (r *UserActivityRepository) GetActiveUserIDs(ctx context.Context, date time
|
||||
}
|
||||
|
||||
// GetUserActivityCount 获取用户在指定日期范围内的活跃天数
|
||||
func (r *UserActivityRepository) GetUserActivityCount(ctx context.Context, userID string, startDate, endDate time.Time) (int64, error) {
|
||||
func (r *userActivityRepository) GetUserActivityCount(ctx context.Context, userID string, startDate, endDate time.Time) (int64, error) {
|
||||
var count int64
|
||||
startOfDay := time.Date(startDate.Year(), startDate.Month(), startDate.Day(), 0, 0, 0, 0, startDate.Location())
|
||||
endOfDay := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 23, 59, 59, 0, endDate.Location())
|
||||
@@ -107,7 +122,7 @@ func (r *UserActivityRepository) GetUserActivityCount(ctx context.Context, userI
|
||||
}
|
||||
|
||||
// SaveStat 保存统计数据快照
|
||||
func (r *UserActivityRepository) SaveStat(ctx context.Context, stat *model.UserActivityStat) error {
|
||||
func (r *userActivityRepository) SaveStat(ctx context.Context, stat *model.UserActivityStat) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "stat_type"}, {Name: "stat_date"}},
|
||||
@@ -117,7 +132,7 @@ func (r *UserActivityRepository) SaveStat(ctx context.Context, stat *model.UserA
|
||||
}
|
||||
|
||||
// GetStat 获取统计数据快照
|
||||
func (r *UserActivityRepository) GetStat(ctx context.Context, statType string, date time.Time) (*model.UserActivityStat, error) {
|
||||
func (r *userActivityRepository) GetStat(ctx context.Context, statType string, date time.Time) (*model.UserActivityStat, error) {
|
||||
var stat model.UserActivityStat
|
||||
startOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
|
||||
|
||||
@@ -134,7 +149,7 @@ func (r *UserActivityRepository) GetStat(ctx context.Context, statType string, d
|
||||
|
||||
// RecordActivityToRedis 记录用户活跃到 Redis Sorted Set
|
||||
// score 为时间戳,member 为用户ID
|
||||
func (r *UserActivityRepository) RecordActivityToRedis(ctx context.Context, userID string) error {
|
||||
func (r *userActivityRepository) RecordActivityToRedis(ctx context.Context, userID string) error {
|
||||
now := time.Now()
|
||||
score := float64(now.Unix())
|
||||
|
||||
@@ -164,7 +179,7 @@ func (r *UserActivityRepository) RecordActivityToRedis(ctx context.Context, user
|
||||
}
|
||||
|
||||
// GetDAUFromRedis 从 Redis 获取日活用户数
|
||||
func (r *UserActivityRepository) GetDAUFromRedis(ctx context.Context, date time.Time) (int64, error) {
|
||||
func (r *userActivityRepository) GetDAUFromRedis(ctx context.Context, date time.Time) (int64, error) {
|
||||
key := formatDAUKey(date)
|
||||
count, err := r.cache.ZCard(ctx, key)
|
||||
if err != nil {
|
||||
@@ -174,7 +189,7 @@ func (r *UserActivityRepository) GetDAUFromRedis(ctx context.Context, date time.
|
||||
}
|
||||
|
||||
// GetWAUFromRedis 从 Redis 获取周活用户数
|
||||
func (r *UserActivityRepository) GetWAUFromRedis(ctx context.Context, year int, week int) (int64, error) {
|
||||
func (r *userActivityRepository) GetWAUFromRedis(ctx context.Context, year int, week int) (int64, error) {
|
||||
key := formatWAUKey(year, week)
|
||||
count, err := r.cache.ZCard(ctx, key)
|
||||
if err != nil {
|
||||
@@ -184,7 +199,7 @@ func (r *UserActivityRepository) GetWAUFromRedis(ctx context.Context, year int,
|
||||
}
|
||||
|
||||
// GetMAUFromRedis 从 Redis 获取月活用户数
|
||||
func (r *UserActivityRepository) GetMAUFromRedis(ctx context.Context, year int, month int) (int64, error) {
|
||||
func (r *userActivityRepository) GetMAUFromRedis(ctx context.Context, year int, month int) (int64, error) {
|
||||
key := formatMAUKey(year, month)
|
||||
count, err := r.cache.ZCard(ctx, key)
|
||||
if err != nil {
|
||||
@@ -194,7 +209,7 @@ func (r *UserActivityRepository) GetMAUFromRedis(ctx context.Context, year int,
|
||||
}
|
||||
|
||||
// GetDAUUserIDsFromRedis 获取日活用户ID列表
|
||||
func (r *UserActivityRepository) GetDAUUserIDsFromRedis(ctx context.Context, date time.Time) ([]string, error) {
|
||||
func (r *userActivityRepository) GetDAUUserIDsFromRedis(ctx context.Context, date time.Time) ([]string, error) {
|
||||
key := formatDAUKey(date)
|
||||
// 获取所有成员(从最小分数到最大分数)
|
||||
members, err := r.cache.ZRangeByScore(ctx, key, "-inf", "+inf", 0, -1)
|
||||
|
||||
Reference in New Issue
Block a user