添加请求追踪中间件以记录请求和响应内容 在用户订阅查询中新增includeExpired参数支持查询历史记录 完善配置系统以支持float64类型默认值解析
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -75,18 +77,36 @@ func (m *defaultUserModel) FindUsersSubscribeBySubscribeId(ctx context.Context,
|
||||
func (m *defaultUserModel) QueryUserSubscribe(ctx context.Context, userId int64, status ...int64) ([]*SubscribeDetails, error) {
|
||||
var list []*SubscribeDetails
|
||||
key := fmt.Sprintf("%s%d", cacheUserSubscribeUserPrefix, userId)
|
||||
|
||||
// 1. Get includeExpired from Context
|
||||
includeExpired := ""
|
||||
if v := ctx.Value(constant.CtxKeyIncludeExpired); v != nil {
|
||||
includeExpired, _ = v.(string)
|
||||
}
|
||||
|
||||
// 2. If query mode is different, must modify Cache Key
|
||||
if includeExpired == "all" {
|
||||
key += ":all"
|
||||
}
|
||||
|
||||
err := m.QueryCtx(ctx, &list, key, func(conn *gorm.DB, v interface{}) error {
|
||||
// 获取当前时间
|
||||
now := time.Now()
|
||||
// 获取当前时间向前推 7 天
|
||||
sevenDaysAgo := time.Now().Add(-7 * 24 * time.Hour)
|
||||
// 基础条件查询
|
||||
conn = conn.Model(&Subscribe{}).Where("`user_id` = ?", userId)
|
||||
// Base condition
|
||||
db := conn.Model(&Subscribe{}).Where("`user_id` = ?", userId)
|
||||
if len(status) > 0 {
|
||||
conn = conn.Where("`status` IN ?", status)
|
||||
db = db.Where("`status` IN ?", status)
|
||||
}
|
||||
// 订阅过期时间大于当前时间或者订阅结束时间大于当前时间
|
||||
return conn.Where("`expire_time` > ? OR `finished_at` >= ? OR `expire_time` = ?", now, sevenDaysAgo, time.UnixMilli(0)).
|
||||
|
||||
// 3. Adjust SQL based on param
|
||||
if includeExpired == "all" {
|
||||
// Mode A: Query all history
|
||||
return db.Order("created_at DESC").Preload("Subscribe").Find(&list).Error
|
||||
}
|
||||
|
||||
// Mode B: Default only query valid subscriptions
|
||||
// Logic: ExpireTime > Now OR FinishedAt >= 7 days ago OR ExpireTime = 0 (Never expire)
|
||||
now := time.Now()
|
||||
sevenDaysAgo := now.Add(-7 * 24 * time.Hour)
|
||||
return db.Where("`expire_time` > ? OR `finished_at` >= ? OR `expire_time` = ?", now, sevenDaysAgo, time.UnixMilli(0)).
|
||||
Preload("Subscribe").
|
||||
Find(&list).Error
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user