You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
885 B
42 lines
885 B
package svc
|
|
|
|
import (
|
|
"healthapi/internal/config"
|
|
"healthapi/internal/database"
|
|
"healthapi/pkg/ai"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
DB *gorm.DB
|
|
AIClient ai.AIClient
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
// 初始化数据库
|
|
db, err := database.NewDB(c.Database)
|
|
if err != nil {
|
|
logx.Errorf("Failed to connect database: %v", err)
|
|
panic(err)
|
|
}
|
|
|
|
// 初始化数据(问卷题库、测试用户)
|
|
if err := database.SeedQuestionBank(db); err != nil {
|
|
logx.Errorf("Failed to seed question bank: %v", err)
|
|
}
|
|
if err := database.SeedTestUser(db); err != nil {
|
|
logx.Errorf("Failed to seed test user: %v", err)
|
|
}
|
|
|
|
// 初始化 AI 客户端
|
|
aiClient := ai.NewAIClient(c.AI)
|
|
|
|
return &ServiceContext{
|
|
Config: c,
|
|
DB: db,
|
|
AIClient: aiClient,
|
|
}
|
|
}
|
|
|