package database

import (
	"github.com/suyuan32/simple-admin-common/config"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

func InitWechatDB(databaseConf config.DatabaseConf, mode string) (*gorm.DB, error) {
	dsn := databaseConf.GetDSN()
	wechatDb, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: NewGormLogger(mode),
	})
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	if wechatDb != nil {
		sqlDB, err := wechatDb.DB()
		if err != nil {
			logx.Error(err)
			return nil, err
		}
		// 设置连接池
		sqlDB.SetMaxIdleConns(10)

		sqlDB.SetMaxOpenConns(100)

		sqlDB.SetConnMaxLifetime(time.Hour)
	}
	//初始化DAO查询接口
	//wechatQ := query.Use(wechatDb)

	return wechatDb, nil
}
