package svc

import (
	"sundynix-micro-go/app/file/rpc/fileservice"
	"sundynix-micro-go/app/plant/api/internal/config"
	"sundynix-micro-go/app/plant/rpc/plantservice"
	"sundynix-micro-go/app/system/rpc/systemservice"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/zrpc"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type ServiceContext struct {
	Config   config.Config
	PlantRpc plantservice.PlantService
	UserRpc  systemservice.SystemService
	FileRpc  fileservice.FileService
	DB       *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
	if err != nil {
		logx.Errorf("连接数据库失败: %v", err)
	}

	return &ServiceContext{
		Config:   c,
		PlantRpc: plantservice.NewPlantService(zrpc.MustNewClient(c.PlantRpc)),
		UserRpc:  systemservice.NewSystemService(zrpc.MustNewClient(c.UserRpc)),
		FileRpc:  fileservice.NewFileService(zrpc.MustNewClient(c.FileRpc)),
		DB:       db,
	}
}
