98 lines
2.8 KiB
Go
Executable File
98 lines
2.8 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"bindbox-game/configs"
|
|
"bindbox-game/internal/pkg/logger"
|
|
"bindbox-game/internal/repository/mysql"
|
|
tcmodel "bindbox-game/internal/repository/mysql/task_center"
|
|
taskcenter "bindbox-game/internal/service/task_center"
|
|
)
|
|
|
|
func main() {
|
|
// 1. 初始化
|
|
configs.Init()
|
|
dbRepo, err := mysql.New()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
l, _ := logger.NewCustomLogger(logger.WithOutputInConsole())
|
|
|
|
// 这里简化 service 初始化,只传必要的 db
|
|
svc := taskcenter.New(l, dbRepo, nil, nil, nil)
|
|
ctx := context.Background()
|
|
|
|
fmt.Println("=== 验证 1: 时区解析一致性 ===")
|
|
// 模拟管理后台输入 (本地时间)
|
|
startTimeStr := "2026-02-20 12:00:00"
|
|
loc, _ := time.LoadLocation("Local")
|
|
expectedT, _ := time.ParseInLocation("2006-01-02 15:04:05", startTimeStr, loc)
|
|
|
|
// 直接通过 GORM 创建任务(模拟 Admin API 后的 Service 调用)
|
|
st := &expectedT
|
|
taskID, err := svc.CreateTask(ctx, taskcenter.CreateTaskInput{
|
|
Name: "时区测试任务",
|
|
Status: 1,
|
|
Visibility: 1,
|
|
StartTime: st,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Printf("任务创建成功, ID: %d\n", taskID)
|
|
|
|
// 从数据库读回
|
|
var task tcmodel.Task
|
|
dbRepo.GetDbR().First(&task, taskID)
|
|
|
|
fmt.Printf("输入本地时间: %s\n", startTimeStr)
|
|
fmt.Printf("数据库存储时间(Unix): %d\n", task.StartTime.Unix())
|
|
fmt.Printf("预期时间戳(Unix): %d\n", expectedT.Unix())
|
|
|
|
if task.StartTime.Unix() == expectedT.Unix() {
|
|
fmt.Println("✅ [SUCCESS] 时区解析一致性验证通过")
|
|
} else {
|
|
fmt.Printf("❌ [FAILED] 时区解析不一致! 偏差: %d 秒\n", task.StartTime.Unix()-expectedT.Unix())
|
|
}
|
|
|
|
fmt.Println("\n=== 验证 2: 有效期过滤 (ListTasks) ===")
|
|
// 创建一个已经结束的任务
|
|
pastTime := time.Now().Add(-24 * time.Hour)
|
|
svc.CreateTask(ctx, taskcenter.CreateTaskInput{
|
|
Name: "过期任务",
|
|
Status: 1,
|
|
Visibility: 1,
|
|
EndTime: &pastTime,
|
|
})
|
|
|
|
// App 端查询 (OnlyActive=true)
|
|
list, total, _ := svc.ListTasks(ctx, taskcenter.ListTasksInput{Page: 1, PageSize: 10, OnlyActive: true})
|
|
fmt.Printf("App 端展示任务数: %d / 总数: %d\n", len(list), total)
|
|
|
|
foundPast := false
|
|
for _, item := range list {
|
|
if item.Name == "过期任务" {
|
|
foundPast = true
|
|
}
|
|
}
|
|
if !foundPast {
|
|
fmt.Println("✅ [SUCCESS] App 端成功过滤已过期任务")
|
|
} else {
|
|
fmt.Println("❌ [FAILED] App 端未能过滤已过期任务")
|
|
}
|
|
|
|
fmt.Println("\n=== 验证 3: 领取拦截 (ClaimTier) ===")
|
|
err = svc.ClaimTier(ctx, 1, taskID, 1) // 尝试领取那个还没开始的任务
|
|
if err != nil && err.Error() == "任务尚未开始" {
|
|
fmt.Printf("✅ [SUCCESS] 成功拦截未开始任务的领取行为: %v\n", err)
|
|
} else {
|
|
fmt.Printf("❌ [FAILED] 未能按预期拦截领取: %v\n", err)
|
|
}
|
|
|
|
// 清理数据 (可选)
|
|
// dbRepo.GetDbW().Delete(&tcmodel.Task{}, taskID)
|
|
}
|