package taskcenter import ( "context" "testing" "time" "bindbox-game/internal/repository/mysql" tcmodel "bindbox-game/internal/repository/mysql/task_center" "gorm.io/gorm" ) func ensureExtraTablesForServiceTest(t *testing.T, db *gorm.DB) { if !db.Migrator().HasTable("orders") { if err := db.Exec(`CREATE TABLE orders ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, status INTEGER NOT NULL DEFAULT 1, source_type INTEGER NOT NULL DEFAULT 0, total_amount INTEGER NOT NULL DEFAULT 0, actual_amount INTEGER NOT NULL DEFAULT 0, remark TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, deleted_at DATETIME );`).Error; err != nil { t.Fatalf("创建 orders 表失败: %v", err) } } if !db.Migrator().HasTable("activity_draw_logs") { if err := db.Exec(`CREATE TABLE activity_draw_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, order_id INTEGER NOT NULL, issue_id INTEGER NOT NULL );`).Error; err != nil { t.Fatalf("创建 activity_draw_logs 表失败: %v", err) } } if !db.Migrator().HasTable("user_invites") { if err := db.Exec(`CREATE TABLE user_invites ( id INTEGER PRIMARY KEY AUTOINCREMENT, inviter_id INTEGER NOT NULL, invitee_id INTEGER NOT NULL, accumulated_amount INTEGER NOT NULL DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, deleted_at DATETIME );`).Error; err != nil { t.Fatalf("创建 user_invites 表失败: %v", err) } } } func TestGetUserProgress_TimeWindow_Integration(t *testing.T) { repo, err := mysql.NewSQLiteRepoForTest() if err != nil { t.Fatalf("创建 repo 失败: %v", err) } db := repo.GetDbW() initTestTables(t, db) ensureExtraTablesForServiceTest(t, db) svc := New(nil, repo, nil, nil, nil) now := time.Now() taskStart := now.Add(-200 * 24 * time.Hour) taskEnd := now.Add(200 * 24 * time.Hour) // 创建一个具有任务有效期的任务 task := &tcmodel.Task{ Name: "时效性测试任务", Description: "测试各档位时效隔离", Status: 1, Visibility: 1, StartTime: &taskStart, EndTime: &taskEnd, } if err := db.Create(task).Error; err != nil { t.Fatalf("创建任务失败: %v", err) } windows := []string{WindowDaily, WindowWeekly, WindowMonthly, WindowActivityPeriod, WindowLifetime} tierIDMap := make(map[string]int64) for _, w := range windows { tier := &tcmodel.TaskTier{ TaskID: task.ID, Metric: MetricOrderCount, Operator: OperatorGTE, Threshold: 1, Window: w, ActivityID: 0, } if err := db.Create(tier).Error; err != nil { t.Fatalf("创建档位失败: %v", err) } tierIDMap[w] = tier.ID } userID := int64(888) // 插入三笔订单与邀请,处于不同时间段 o1Time := now.Format(time.DateTime) db.Exec("INSERT INTO orders (id, user_id, status, source_type, total_amount, created_at) VALUES (101, ?, 2, 0, 100, ?)", userID, o1Time) db.Exec("INSERT INTO activity_draw_logs (order_id, issue_id) VALUES (101, 1)") db.Exec("INSERT INTO user_invites (inviter_id, invitee_id, created_at) VALUES (?, 901, ?)", userID, o1Time) o2Time := now.AddDate(0, -2, 0).Format(time.DateTime) db.Exec("INSERT INTO orders (id, user_id, status, source_type, total_amount, created_at) VALUES (102, ?, 2, 0, 100, ?)", userID, o2Time) db.Exec("INSERT INTO activity_draw_logs (order_id, issue_id) VALUES (102, 1)") db.Exec("INSERT INTO user_invites (inviter_id, invitee_id, created_at) VALUES (?, 902, ?)", userID, o2Time) o3Time := now.AddDate(-1, 0, 0).Format(time.DateTime) db.Exec("INSERT INTO orders (id, user_id, status, source_type, total_amount, created_at) VALUES (103, ?, 2, 0, 100, ?)", userID, o3Time) db.Exec("INSERT INTO activity_draw_logs (order_id, issue_id) VALUES (103, 1)") db.Exec("INSERT INTO user_invites (inviter_id, invitee_id, created_at) VALUES (?, 903, ?)", userID, o3Time) // 调用统计 progress, err := svc.GetUserProgress(context.Background(), userID, task.ID) if err != nil { t.Fatalf("获取进度失败: %v", err) } // 验证各 Tier 的统计数据符合预期 for w, tid := range tierIDMap { tp, ok := progress.TierProgressMap[tid] if !ok { t.Errorf("缺少 %s 的进度", w) continue } var expectedCount int64 switch w { case WindowDaily, WindowWeekly, WindowMonthly: expectedCount = 1 case WindowActivityPeriod: expectedCount = 2 // O1, O2 case WindowLifetime: expectedCount = 3 // O1, O2, O3 } if tp.OrderCount != expectedCount { t.Errorf("[%s] OrderCount 不符: Expected %d, Got %d", w, expectedCount, tp.OrderCount) } else { t.Logf("[%s] OrderCount 验证成功: %d", w, tp.OrderCount) } if tp.InviteCount != expectedCount { t.Errorf("[%s] InviteCount 不符: Expected %d, Got %d", w, expectedCount, tp.InviteCount) } else { t.Logf("[%s] InviteCount 验证成功: %d", w, tp.InviteCount) } } }