package main

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"github.com/robfig/cron/v3"
	"github.com/spf13/viper"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
	"log"
	"time"
)

var (
	GF   GlobalConf
	Rest = make(map[string]interface{}, 0) //保存最终各个字段识别率
	MgoB *mongodb.MongodbSim
)

func main() {
	InitConfig()
	InitMgo()
	//getStatistic()

	local, _ := time.LoadLocation("Asia/Shanghai")
	c := cron.New(cron.WithLocation(local), cron.WithSeconds())
	_, err := c.AddFunc(GF.Env.Spec, getStatistic)
	if err != nil {
		log.Println(err)
	}

	c.Start()
	defer c.Stop()

	select {}
}

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 InitMgo() {
	MgoB = &mongodb.MongodbSim{
		MongodbAddr: GF.MongoB.Host,
		Size:        10,
		DbName:      GF.MongoB.DB,
		UserName:    GF.MongoB.Username,
		Password:    GF.MongoB.Password,
		Direct:      GF.MongoB.Direct,
	}

	MgoB.InitPool()

}

//getStatistic 获取统计
func getStatistic() {
	log.Println("开始统计")
	// 创建 Elasticsearch 客户端
	client, err := elastic.NewClient(
		elastic.SetURL(GF.ES.URL),
		elastic.SetBasicAuth(GF.ES.Username, GF.ES.Password),
		elastic.SetSniff(false),
	)
	if err != nil {
		log.Fatalf("创建 Elasticsearch 客户端失败：%s", err)
	}
	// 获取昨天零点和今天零点的时间戳
	now := time.Now()
	yesterday := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
	today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)

	totalQuery := elastic.NewBoolQuery().Must(
		elastic.NewRangeQuery("comeintime").
			Gte(yesterday.Unix()). // 大于昨天零点
			Lt(today.Unix()),      // 小于今天零点
	)
	totalNum, _ := client.Count().
		Index("bidding").
		Query(totalQuery).
		Do(context.Background())

	Rest["total"] = totalNum
	Rest["statistic_date"] = yesterday.Format("2006-01-02")

	for _, v := range GF.Fields {
		//log.Println(v.DataType)
		// 构建查询
		topQuery := elastic.NewBoolQuery().Must(
			elastic.NewTermQuery("toptype", v.Toptype),
			elastic.NewRangeQuery("comeintime").
				Gte(yesterday.Unix()). // 大于昨天零点
				Lt(today.Unix()),      // 小于今天零点
		)
		topNum, _ := client.Count().
			Index("bidding").
			Query(topQuery).
			Do(context.Background())

		ra := make(map[string]interface{}, 0)
		for _, field := range v.Field {
			//fmt.Println(field)
			query := elastic.NewBoolQuery().
				Must(
					elastic.NewTermQuery("toptype", v.Toptype),
					elastic.NewExistsQuery(field),
					elastic.NewRangeQuery("comeintime").
						Gte(yesterday.Unix()). // 大于昨天零点
						Lt(today.Unix()),      // 小于今天零点
				)

			// 执行查询
			count, _ := client.Count().
				Index("bidding").
				Query(query).
				Do(context.Background())
			// 将 int64 转换为 float64，并计算百分比
			percentage := (float64(count) / float64(topNum)) * 100.0
			ra[field] = fmt.Sprintf("%.2f%%", percentage)
		}

		Rest[v.DataType] = ra
	}

	//fmt.Println(Rest)

	MgoB.Save("bidding_statistic", Rest)
	log.Println("结束统计")
}
