package core

import (
	"fmt"

	"mtp20_assisted/global"

	_ "github.com/mattn/go-oci8"
	"go.uber.org/zap"
	"xorm.io/xorm"
)

// 连接Oracle数据库
func XormOracle() *xorm.Engine {
	driver := global.M2A_CONFIG.Oracle.Driver
	dbsource := fmt.Sprintf("%s/%s@%s:%s/%s",
		global.M2A_CONFIG.Oracle.User,
		global.M2A_CONFIG.Oracle.Pwd,
		global.M2A_CONFIG.Oracle.Address,
		global.M2A_CONFIG.Oracle.Port,
		global.M2A_CONFIG.Oracle.Name)

	if engine, err := xorm.NewEngine(driver, dbsource); err != nil {
		global.M2A_LOG.Error("oracle connect failed, err:", zap.Error(err))
		return nil
	} else {
		engine.SetMaxIdleConns(global.M2A_CONFIG.Oracle.MaxIdleConns)
		engine.SetMaxOpenConns(global.M2A_CONFIG.Oracle.MaxOpenConns)

		// 控制台打印出生成的SQL语句
		engine.ShowSQL(true)
		// 不仅打印到控制台，而是保存为文件
		// engine.SetLogger(log.NewSimpleLogger(global.M2A_LOG))

		global.M2A_LOG.Info("oracle connect successed.")

		return engine
	}
}
