package svc import ( "git.noahlan.cn/northlan/ntools-go/kafka" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "live-service/app/user_center/model" "live-service/app/user_center/rpc/internal/common/integral_manager" "live-service/app/user_center/rpc/internal/common/reward_pool" "live-service/app/user_center/rpc/internal/config" kfk "live-service/common/kafka" "log" "os" "time" ) type ServiceContext struct { Config config.Config Db *gorm.DB UserModel model.UserModel UserPlatformModel model.UserPlatformModel UserGiftModel model.UserGiftModel UserIntegralModel model.UserIntegralModel UserNobilityModel model.UserNobilityModel UserCheckInModel model.UserCheckInModel StatisticsPvpModel model.StatisticsPvpModel RankPvpModel model.RankPvpModel GiftModel model.GiftModel RewardPoolManager *reward_pool.PoolManager IntegralManager *integral_manager.Manager } func NewServiceContext(c config.Config) *ServiceContext { var logLevel logger.LogLevel if c.Log.Mode == "console" { logLevel = logger.Info } else { logLevel = logger.Warn } gormDb, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{ Logger: logger.New( log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{ SlowThreshold: 5 * time.Second, LogLevel: logLevel, IgnoreRecordNotFoundError: true, Colorful: true, }, ), }) if err != nil { log.Fatal(err) } userIntegralModel := model.NewUserIntegralModel(gormDb) rewardPoolManager := reward_pool.NewRewardPoolManager( c.Integral.RewardPool.InitReward, reward_pool.Ratio{ Ratio: c.Integral.RewardPool.Ratio, BattleRatio: c.Integral.RewardPool.BattleRatio, GiftRatio: c.Integral.RewardPool.GiftRatio, ReturnRatio: c.Integral.RewardPool.ReturnRatio, WelfareRatio: c.Integral.RewardPool.WelfareRatio, }, kafka.NewKafkaProducer(kfk.DefaultProducerConfig, c.Kafka.RewardPool.Addr, c.Kafka.RewardPool.Topic)) return &ServiceContext{ Config: c, Db: gormDb, UserModel: model.NewUserModel(gormDb), UserPlatformModel: model.NewUserPlatformModel(gormDb), UserGiftModel: model.NewUserGiftModel(gormDb), UserIntegralModel: userIntegralModel, UserNobilityModel: model.NewUserNobilityModel(gormDb), UserCheckInModel: model.NewUserCheckInModel(gormDb), StatisticsPvpModel: model.NewStatisticsPvpModel(gormDb), RankPvpModel: model.NewRankPvpModel(gormDb), GiftModel: model.NewGiftModel(gormDb), RewardPoolManager: rewardPoolManager, IntegralManager: integral_manager.NewIntegralManager(userIntegralModel, rewardPoolManager), } }