You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB
Go

package svc
import (
3 years ago
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"live-service/app/user_center/model"
"live-service/app/user_center/rpc/internal/config"
3 years ago
"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
UserGiftPackModel model.UserGiftPackModel
StatisticsPvpModel model.StatisticsPvpModel
RankPvpModel model.RankPvpModel
GiftModel model.GiftModel
}
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,
},
),
})
3 years ago
if err != nil {
log.Fatal(err)
}
return &ServiceContext{
Config: c,
Db: gormDb,
UserModel: model.NewUserModel(gormDb),
UserPlatformModel: model.NewUserPlatformModel(gormDb),
UserGiftModel: model.NewUserGiftModel(gormDb),
UserIntegralModel: model.NewUserIntegralModel(gormDb),
UserNobilityModel: model.NewUserNobilityModel(gormDb),
UserCheckInModel: model.NewUserCheckInModel(gormDb),
UserGiftPackModel: model.NewUserGiftPackModel(gormDb),
StatisticsPvpModel: model.NewStatisticsPvpModel(gormDb),
RankPvpModel: model.NewRankPvpModel(gormDb),
GiftModel: model.NewGiftModel(gormDb),
}
}