package svc import ( "tyc-server/app/main/api/internal/config" "tyc-server/app/main/api/internal/middleware" "tyc-server/app/main/api/internal/service" "tyc-server/app/main/model" "github.com/hibiken/asynq" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/rest" ) type ServiceContext struct { Config config.Config Redis *redis.Redis // 中间件 SourceInterceptor rest.Middleware AuthInterceptor rest.Middleware // 服务 PayService *service.PayService WestDexService *service.WestDexService YushanService *service.YushanService TianjuService *service.TianjuService ApiRequestService *service.ApiRequestService AsynqServer *asynq.Server AsynqService *service.AsynqService VerificationService *service.VerificationService DictService *service.DictService AdminPromotionLinkStatsService *service.AdminPromotionLinkStatsService // 基础模型 UserModel model.UserModel UserAuthModel model.UserAuthModel ProductModel model.ProductModel FeatureModel model.FeatureModel ProductFeatureModel model.ProductFeatureModel ProductRenderModel model.ProductRenderModel OrderModel model.OrderModel OrderRefundModel model.OrderRefundModel QueryModel model.QueryModel GlobalNotificationsModel model.GlobalNotificationsModel ExampleModel model.ExampleModel ExampleParamsModel model.ExampleParamsModel // 管理后台模型 AdminApiModel model.AdminApiModel AdminMenuModel model.AdminMenuModel AdminRoleModel model.AdminRoleModel AdminRoleApiModel model.AdminRoleApiModel AdminRoleMenuModel model.AdminRoleMenuModel AdminUserModel model.AdminUserModel AdminUserRoleModel model.AdminUserRoleModel AdminDictDataModel model.AdminDictDataModel AdminDictTypeModel model.AdminDictTypeModel // 推广相关模型 AdminPromotionLinkModel model.AdminPromotionLinkModel AdminPromotionLinkStatsTotalModel model.AdminPromotionLinkStatsTotalModel AdminPromotionLinkStatsHistoryModel model.AdminPromotionLinkStatsHistoryModel AdminPromotionOrderModel model.AdminPromotionOrderModel } // 初始化基础服务 func initBaseServices(c config.Config) (*redis.Redis, *asynq.Server) { // 初始化Redis redisConf := redis.RedisConf{ Host: c.CacheRedis[0].Host, Pass: c.CacheRedis[0].Pass, Type: c.CacheRedis[0].Type, } r := redis.MustNewRedis(redisConf) // 初始化Asynq服务 asynqServer := asynq.NewServer( asynq.RedisClientOpt{Addr: c.CacheRedis[0].Host, Password: c.CacheRedis[0].Pass}, asynq.Config{ IsFailure: func(err error) bool { logx.Errorf("异步任务失败: %+v \n", err) return true }, Concurrency: 10, }, ) return r, asynqServer } // 初始化基础模型 func initBaseModels(db sqlx.SqlConn, c config.Config) *BaseModels { return &BaseModels{ UserModel: model.NewUserModel(db, c.CacheRedis), UserAuthModel: model.NewUserAuthModel(db, c.CacheRedis), ProductModel: model.NewProductModel(db, c.CacheRedis), FeatureModel: model.NewFeatureModel(db, c.CacheRedis), ProductFeatureModel: model.NewProductFeatureModel(db, c.CacheRedis), ProductRenderModel: model.NewProductRenderModel(db, c.CacheRedis), OrderModel: model.NewOrderModel(db, c.CacheRedis), OrderRefundModel: model.NewOrderRefundModel(db, c.CacheRedis), QueryModel: model.NewQueryModel(db, c.CacheRedis), GlobalNotificationsModel: model.NewGlobalNotificationsModel(db, c.CacheRedis), ExampleModel: model.NewExampleModel(db, c.CacheRedis), ExampleParamsModel: model.NewExampleParamsModel(db, c.CacheRedis), } } // 初始化管理后台模型 func initAdminModels(db sqlx.SqlConn, c config.Config) *AdminModels { return &AdminModels{ AdminApiModel: model.NewAdminApiModel(db, c.CacheRedis), AdminMenuModel: model.NewAdminMenuModel(db, c.CacheRedis), AdminRoleModel: model.NewAdminRoleModel(db, c.CacheRedis), AdminRoleApiModel: model.NewAdminRoleApiModel(db, c.CacheRedis), AdminRoleMenuModel: model.NewAdminRoleMenuModel(db, c.CacheRedis), AdminUserModel: model.NewAdminUserModel(db, c.CacheRedis), AdminUserRoleModel: model.NewAdminUserRoleModel(db, c.CacheRedis), AdminDictDataModel: model.NewAdminDictDataModel(db, c.CacheRedis), AdminDictTypeModel: model.NewAdminDictTypeModel(db, c.CacheRedis), } } // 初始化推广相关模型 func initPromotionModels(db sqlx.SqlConn, c config.Config) *PromotionModels { return &PromotionModels{ AdminPromotionLinkModel: model.NewAdminPromotionLinkModel(db, c.CacheRedis), AdminPromotionLinkStatsTotalModel: model.NewAdminPromotionLinkStatsTotalModel(db, c.CacheRedis), AdminPromotionLinkStatsHistoryModel: model.NewAdminPromotionLinkStatsHistoryModel(db, c.CacheRedis), AdminPromotionOrderModel: model.NewAdminPromotionOrderModel(db, c.CacheRedis), } } // 初始化业务服务 func initBusinessServices(c config.Config, models *BaseModels, adminModels *AdminModels, promotionModels *PromotionModels) *BusinessServices { // 初始化基础服务 westDexService := service.NewWestDexService(c) yushanService := service.NewYushanService(c) tianjuService := service.NewTianjuService(c) // 初始化API请求服务 apiRequestService := service.NewApiRequestService( c, westDexService, yushanService, tianjuService, models.FeatureModel, models.ProductFeatureModel, ) // 初始化推广统计服务 adminPromotionLinkStatsService := service.NewAdminPromotionLinkStatsService(&service.ServiceContext{ AdminPromotionLinkModel: promotionModels.AdminPromotionLinkModel, AdminPromotionLinkStatsTotalModel: promotionModels.AdminPromotionLinkStatsTotalModel, AdminPromotionLinkStatsHistoryModel: promotionModels.AdminPromotionLinkStatsHistoryModel, }) return &BusinessServices{ PayService: service.NewPayService(c, models.OrderModel, models.OrderRefundModel, models.ProductModel, models.UserAuthModel, models.UserModel), WestDexService: westDexService, YushanService: yushanService, TianjuService: tianjuService, ApiRequestService: apiRequestService, AsynqService: service.NewAsynqService(c), VerificationService: service.NewVerificationService(c, westDexService, apiRequestService), DictService: service.NewDictService(adminModels.AdminDictTypeModel, adminModels.AdminDictDataModel), AdminPromotionLinkStatsService: adminPromotionLinkStatsService, } } func NewServiceContext(c config.Config) *ServiceContext { // 初始化数据库连接 db := sqlx.NewMysql(c.DataSource) // 初始化基础服务 redis, asynqServer := initBaseServices(c) // 初始化各种模型 baseModels := initBaseModels(db, c) adminModels := initAdminModels(db, c) promotionModels := initPromotionModels(db, c) // 初始化业务服务 businessServices := initBusinessServices(c, baseModels, adminModels, promotionModels) return &ServiceContext{ Config: c, Redis: redis, AsynqServer: asynqServer, SourceInterceptor: middleware.NewSourceInterceptorMiddleware().Handle, AuthInterceptor: middleware.NewAuthInterceptorMiddleware(c).Handle, // 注入业务服务 PayService: businessServices.PayService, WestDexService: businessServices.WestDexService, YushanService: businessServices.YushanService, TianjuService: businessServices.TianjuService, ApiRequestService: businessServices.ApiRequestService, AsynqService: businessServices.AsynqService, VerificationService: businessServices.VerificationService, DictService: businessServices.DictService, AdminPromotionLinkStatsService: businessServices.AdminPromotionLinkStatsService, // 注入基础模型 UserModel: baseModels.UserModel, UserAuthModel: baseModels.UserAuthModel, ProductModel: baseModels.ProductModel, FeatureModel: baseModels.FeatureModel, ProductFeatureModel: baseModels.ProductFeatureModel, ProductRenderModel: baseModels.ProductRenderModel, OrderModel: baseModels.OrderModel, OrderRefundModel: baseModels.OrderRefundModel, QueryModel: baseModels.QueryModel, GlobalNotificationsModel: baseModels.GlobalNotificationsModel, ExampleModel: baseModels.ExampleModel, ExampleParamsModel: baseModels.ExampleParamsModel, // 注入管理后台模型 AdminApiModel: adminModels.AdminApiModel, AdminMenuModel: adminModels.AdminMenuModel, AdminRoleModel: adminModels.AdminRoleModel, AdminRoleApiModel: adminModels.AdminRoleApiModel, AdminRoleMenuModel: adminModels.AdminRoleMenuModel, AdminUserModel: adminModels.AdminUserModel, AdminUserRoleModel: adminModels.AdminUserRoleModel, AdminDictDataModel: adminModels.AdminDictDataModel, AdminDictTypeModel: adminModels.AdminDictTypeModel, // 注入推广相关模型 AdminPromotionLinkModel: promotionModels.AdminPromotionLinkModel, AdminPromotionLinkStatsTotalModel: promotionModels.AdminPromotionLinkStatsTotalModel, AdminPromotionLinkStatsHistoryModel: promotionModels.AdminPromotionLinkStatsHistoryModel, AdminPromotionOrderModel: promotionModels.AdminPromotionOrderModel, } } // 辅助结构体,用于分组初始化 type BaseModels struct { UserModel model.UserModel UserAuthModel model.UserAuthModel ProductModel model.ProductModel FeatureModel model.FeatureModel ProductFeatureModel model.ProductFeatureModel ProductRenderModel model.ProductRenderModel OrderModel model.OrderModel OrderRefundModel model.OrderRefundModel QueryModel model.QueryModel GlobalNotificationsModel model.GlobalNotificationsModel ExampleModel model.ExampleModel ExampleParamsModel model.ExampleParamsModel } type AdminModels struct { AdminApiModel model.AdminApiModel AdminMenuModel model.AdminMenuModel AdminRoleModel model.AdminRoleModel AdminRoleApiModel model.AdminRoleApiModel AdminRoleMenuModel model.AdminRoleMenuModel AdminUserModel model.AdminUserModel AdminUserRoleModel model.AdminUserRoleModel AdminDictDataModel model.AdminDictDataModel AdminDictTypeModel model.AdminDictTypeModel } type PromotionModels struct { AdminPromotionLinkModel model.AdminPromotionLinkModel AdminPromotionLinkStatsTotalModel model.AdminPromotionLinkStatsTotalModel AdminPromotionLinkStatsHistoryModel model.AdminPromotionLinkStatsHistoryModel AdminPromotionOrderModel model.AdminPromotionOrderModel } type BusinessServices struct { PayService *service.PayService WestDexService *service.WestDexService YushanService *service.YushanService TianjuService *service.TianjuService ApiRequestService *service.ApiRequestService AsynqService *service.AsynqService VerificationService *service.VerificationService DictService *service.DictService AdminPromotionLinkStatsService *service.AdminPromotionLinkStatsService } func (s *ServiceContext) Close() { if s.AsynqService != nil { s.AsynqService.Close() } }