package main

import (
	"fmt"
	"github.com/robfig/cron"
	"github.com/spf13/cobra"
	"go.uber.org/zap"
	util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
	"time"
	"timedtasks/config"
)

func init() {
	config.Init("./common.toml")
	InitLog()

	log.Info("init success")
}

func main() {
	rootCmd := &cobra.Command{Use: "my cmd"}
	rootCmd.AddCommand(includedInfo()) // 剑鱼网站统计数据
	if err := rootCmd.Execute(); err != nil {
		fmt.Println("rootCmd.Execute failed", err.Error())
	}
}

func includedInfo() *cobra.Command {
	cmdClient := &cobra.Command{
		Use:   "included_info",
		Short: "Start processing included_info data",
		Run: func(cmd *cobra.Command, args []string) {
			initEs()
			InitMysql()

			c := cron.New()
			//cronstr := "0 */3 * * * *"
			// 每月第一天的 0 点
			err := c.AddFunc("@monthly", func() {
				taskInfo()
			})
			if err != nil {
				panic(err)
			}
			c.Start()

			t := make(chan bool, 1)
			<-t
		},
	}
	//cmdClient.Flags().StringVarP(&cfg, "conf", "c", "", "server config [toml]")
	return cmdClient
}

func taskInfo() {
	bidCount, proCount, entCount, buyerCount := int64(0), int64(0), int64(0), int64(0)
	esquery := `{"query":{"bool":{"must":[{"match_all":{}}],"must_not":[],"should":[]}}}`
	bidCount = Es.Count(config.Conf.Es.Bidding, esquery)
	proCount = Es.Count(config.Conf.Es.Project, esquery)
	entCount = Es.Count(config.Conf.Es.Qyxy, `{"query":{"bool":{"must":[{"range":{"company_type_int":{"lte":"20"}}}]}}}`)
	buyerCount = Es.Count(config.Conf.Es.Buyer, esquery)
	saveM := map[string]interface{}{"bid": bidCount, "project": proCount, "ent": entCount, "buyer": buyerCount,
		"bid_day_update": config.Conf.Preset.Bidday,
		"bid_field":      config.Conf.Preset.Bidfield,
		"field_accuracy": config.Conf.Preset.Accuracy}
	saveM["create_time"] = time.Now().Format(util.Date_Full_Layout)
	log.Info("included_info", zap.Any("m", saveM))
	MysqlTool.Insert("included_info", saveM)
}
