package utils

import (
	"fmt"
	"gin-vue-admin/global"
	"github.com/ClickHouse/clickhouse-go/v2"
	"time"
)

func GetCh() error {
	if global.GVA_CH == nil {
		chConf := global.GVA_CONFIG.Clickhouse
		conn, err := clickhouse.Open(&clickhouse.Options{
			Addr: []string{chConf.Host},
			Auth: clickhouse.Auth{
				Database: chConf.Database,
				Username: chConf.Username,
				Password: chConf.Password,
			},
			Debug:           chConf.Debug,
			DialTimeout:     2 * time.Second,
			MaxOpenConns:    10,
			MaxIdleConns:    5,
			ConnMaxLifetime: time.Hour,
			Compression:     &clickhouse.Compression{Method: clickhouse.CompressionLZ4},
		})
		if err != nil {
			fmt.Println(err)
			return err
		}
		global.GVA_CH = conn
	}
	return nil
}
