package config import ( "flag" "log" "github.com/topfans/backend/pkg/database" "github.com/topfans/backend/pkg/logger" "github.com/topfans/backend/pkg/models" "go.uber.org/zap" ) // GalleryRulesConfig 展馆规则配置(硬编码) type GalleryRulesConfig struct { // 初始展位数(默认:3) InitialSlotCount int // 抢展位时长(秒,默认:14400,即4小时) GrabSlotDuration int64 // 按等级解锁成本(slot_index -> level) // 例如:第4个展位需要等级5,第5个展位需要等级6 UnlockLevelBySlot map[int]int // 按水晶解锁成本(slot_index -> crystal_cost) // 例如:第4个展位需要水晶100,第5个展位需要水晶200 UnlockCrystalBySlot map[int]int // 最大展位数(默认:10) MaxSlotCount int } // DatabaseConfig 数据库配置 type DatabaseConfig struct { Host string Port int User string Password string DBName string SSLMode string } // ServiceURLs RPC服务地址配置 type ServiceURLs struct { AssetService string UserService string TaskService string } var ( // GalleryRules 展馆规则配置(硬编码) GalleryRules = &GalleryRulesConfig{ InitialSlotCount: 2, // 2 个展位 GrabSlotDuration: 14400, // 4小时 // 按等级解锁:清空(不再解锁额外展位) UnlockLevelBySlot: map[int]int{}, // 按水晶解锁:清空 UnlockCrystalBySlot: map[int]int{}, MaxSlotCount: 2, // 最多 2 个展位 } // DBConfig 数据库配置(由 main.go 的 flag 或环境变量注入) DBConfig = &DatabaseConfig{} // ServiceURLsConfig RPC服务地址配置(由 main.go 的 flag 或环境变量注入) ServiceURLsConfig = &ServiceURLs{ AssetService: "tri://localhost:20003", UserService: "tri://localhost:20000", TaskService: "tri://localhost:20004", } ) // InitConfig 从命令行参数初始化配置 func InitConfig() { // 数据库配置 flag.StringVar(&DBConfig.Host, "db-host", "localhost", "数据库主机地址") flag.IntVar(&DBConfig.Port, "db-port", 5432, "数据库端口") flag.StringVar(&DBConfig.User, "db-user", "postgres", "数据库用户名") flag.StringVar(&DBConfig.Password, "db-password", "", "数据库密码") flag.StringVar(&DBConfig.DBName, "db-name", "topfans", "数据库名称") flag.StringVar(&DBConfig.SSLMode, "db-sslmode", "disable", "数据库SSL模式") // RPC服务地址配置 flag.StringVar(&ServiceURLsConfig.AssetService, "asset-service-url", "tri://localhost:20003", "Asset Service RPC地址") flag.StringVar(&ServiceURLsConfig.UserService, "user-service-url", "tri://localhost:20000", "User Service RPC地址") flag.StringVar(&ServiceURLsConfig.TaskService, "task-service-url", "tri://localhost:20004", "Task Service RPC地址") flag.Parse() log.Println("配置初始化完成:") log.Printf(" 数据库: %s:%d/%s", DBConfig.Host, DBConfig.Port, DBConfig.DBName) log.Printf(" Asset Service: %s", ServiceURLsConfig.AssetService) log.Printf(" User Service: %s", ServiceURLsConfig.UserService) log.Printf(" Task Service: %s", ServiceURLsConfig.TaskService) } // GetExhibitionDuration 获取展览时长(秒),从数据库配置读取 func GetExhibitionDuration() int64 { defer func() { if r := recover(); r != nil { logger.Logger.Error("Panic in GetExhibitionDuration, using default=14400", zap.Any("error", r)) } }() db := database.GetDB() if db == nil { logger.Logger.Warn("Database not initialized, using default GrabSlotDuration=14400") return 14400 } var config models.SystemConfig err := db.Where("config_key = ? AND is_active = ?", "exhibition_duration", true).First(&config).Error if err != nil { logger.Logger.Warn("Failed to get exhibition_duration config, using default=14400", zap.Error(err)) return 14400 } // 数据库存储的是小时,转换为秒(先乘后转换,避免 int64(0.10)=0 的截断问题) return int64(config.ConfigValue * 3600) }