package main

import (
	"fmt"
	"github.com/spf13/viper"
	"go.uber.org/zap"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/mysqldb"
	"os"
	"time"
)

var GF GlobalConf

func InitConfig() (err error) {
	viper.SetConfigFile("config.toml") // 指定配置文件路径
	viper.SetConfigName("config")      // 配置文件名称(无扩展名)
	viper.SetConfigType("toml")        // 如果配置文件的名称中没有扩展名，则需要配置此项

	viper.AddConfigPath("./")
	viper.AddConfigPath("./conf/")  // 还可以在工作目录中查找配置
	viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
	err = viper.ReadInConfig()      // 查找并读取配置文件
	if err != nil {                 // 处理读取配置文件的错误
		return
	}

	err = viper.Unmarshal(&GF)

	return err

}

func init() {
	err := InitConfig()
	if err != nil {
		fmt.Println("InitConfig err", err)
		os.Exit(1)
	}

	InitLog()
	InitMgo()
	//InitEs()
	InitMysql()
	initSpider()
	//

}

func InitLog() {
	now := time.Now()
	err := log.InitLog(
		log.Path(GF.Log.LogPath),
		log.Level(GF.Log.LogLevel),
		log.Compress(GF.Log.Compress),
		log.MaxSize(GF.Log.MaxSize),
		log.MaxBackups(GF.Log.MaxBackups),
		log.MaxAge(GF.Log.MaxAge),
		log.Format(GF.Log.Format),
	)
	if err != nil {
		fmt.Printf("InitLog failed: %v\n", err)
		os.Exit(1)
	}

	log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
}

func InitMysql() {
	now := time.Now()
	Mysql = &mysqldb.Mysql{
		Address:  GF.Mysql.Address,
		DBName:   GF.Mysql.Dbname,
		UserName: GF.Mysql.Username,
		PassWord: GF.Mysql.Password,
	}
	Mysql.Init()
	log.Info("InitMysql", zap.Any("duration", time.Since(now).Seconds()))
}

//func InitEs() {
//	now := time.Now()
//	//Es = &elastic.Elastic{
//	//	//S_esurl: "http://127.0.0.1:19805",
//	//	S_esurl:  GF.EsP.URL,
//	//	I_size:   5,
//	//	Username: GF.EsP.Username,
//	//	Password: GF.EsP.Password,
//	//}
//	//Es.InitElasticSize()
//
//	EsClient, _ = es.NewClient(
//		es.SetURL(GF.EsP.URL),
//		es.SetBasicAuth(GF.EsP.Username, GF.EsP.Password),
//		es.SetSniff(false),
//	)
//
//	log.Info("InitEs", zap.Any("duration", time.Since(now).Seconds()))
//}

func InitMgo() {
	now := time.Now()
	Mgo = &mongodb.MongodbSim{
		MongodbAddr: GF.Mongob.Host,
		DbName:      GF.Mongob.DB,
		Size:        10,
		UserName:    GF.Mongob.Username,
		Password:    GF.Mongob.Password,
	}
	Mgo.InitPool()

	MgoP = &mongodb.MongodbSim{
		MongodbAddr: GF.Mongop.Host,
		DbName:      GF.Mongop.DB,
		Size:        10,
		UserName:    GF.Mongop.Username,
		Password:    GF.Mongop.Password,
	}
	MgoP.InitPool()

	MgoSpider = &mongodb.MongodbSim{
		MongodbAddr: GF.Mongospider.Host,
		DbName:      GF.Mongospider.DB,
		Size:        10,
		UserName:    GF.Mongospider.Username,
		Password:    GF.Mongospider.Password,
	}
	MgoSpider.InitPool()

	log.Info("InitMgo", zap.Any("duration", time.Since(now).Seconds()))
}

//initSpider 初始化 竞品站点
func initSpider() {
	fmt.Println("开始初始化 -- 竞品站点")
	sess := MgoSpider.GetMgoConn()
	defer MgoSpider.DestoryMongoConn(sess)
	query := sess.DB(GF.Mongospider.DB).C(GF.Mongospider.Coll).Find(nil).Iter()
	count := 0

	for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
		if tmp["site"] != nil {
			site := tmp["site"].(string)
			SpiderSites = append(SpiderSites, site)
		}
	}
	log.Info("initSpider", zap.Int("初始化竞品站点完毕,竞品网站数量为：", len(SpiderSites)))

}
