package main

import (
	"fmt"
	"github.com/cron"
	"github.com/tealeg/xlsx"
	"log"
	"os"
	qu "qfw/util"
	"time"
)


var (
	sysconfig    							map[string]interface{} //配置文件
	save_mgo,qy_mgo,st_mgo        			*MongodbSim            //mongodb操作对象
	qy_c_name,st_c_name						string
	es_index_st,es_type_st					string
	save_other_name,save_site_name			string
	xlsx_name,unique_site_name				string
)

func initMgo()  {

	saveconf := sysconfig["save_mgodb"].(map[string]interface{})
	qy_c_name = qu.ObjToString(saveconf["coll"])
	save_mgo = &MongodbSim{
		MongodbAddr: saveconf["addr"].(string),
		DbName:      saveconf["db"].(string),
		Size:        qu.IntAllDef(saveconf["pool"], 5),
	}
	save_mgo.InitPool()

	qconf := sysconfig["qy_mgodb"].(map[string]interface{})
	qy_c_name = qu.ObjToString(qconf["coll"])
	qy_mgo = &MongodbSim{
		MongodbAddr: qconf["addr"].(string),
		DbName:      qconf["db"].(string),
		Size:        qu.IntAllDef(qconf["pool"], 5),
	}
	qy_mgo.InitPool()

	sconf := sysconfig["st_mgodb"].(map[string]interface{})
	st_c_name = qu.ObjToString(sconf["coll"])
	st_mgo = &MongodbSim{
		MongodbAddr: sconf["addr"].(string),
		DbName:      sconf["db"].(string),
		Size:        qu.IntAllDef(sconf["pool"], 5),
	}
	st_mgo.InitPool()


	//属性赋值
	es_index_st = qu.ObjToString(sysconfig["es_index_st"])
	es_type_st = qu.ObjToString(sysconfig["es_type_st"])
	save_other_name = qu.ObjToString(sysconfig["save_other_name"])
	save_site_name = qu.ObjToString(sysconfig["save_site_name"])
	unique_site_name = qu.ObjToString(sysconfig["unique_site_name"])
	xlsx_name = qu.ObjToString(sysconfig["xlsx_name"])

}

func init() {
	//加载配置文件
	qu.ReadConfig(&sysconfig)
	initMgo()
}

func main() {
	log.Println("等待执行")
	go taskTime()
	time.Sleep(99999 * time.Hour)
}

func taskTime()  {

	log.Println("部署定时任务")
	c := cron.New()
	//企业变更-mongo-site-es
	c.AddFunc("0 50 8 ? * *", func() { dealWithUniqueData() })
	c.Start()
}

func dealWithUniqueData()  {

	log.Println("开始统计相关数据...")

	qy_data := dealWithQYData()
	st_data :=dealWithSTData()

	log.Println("处理数据...发送邮件...")

	os.Remove(xlsx_name)
	f :=xlsx.NewFile()
	sheet, _ := f.AddSheet("统计")
	//企业数据
	row := sheet.AddRow()
	row.AddCell().Value = "企业变更/日期"
	row.AddCell().Value = "数量"
	for k,v :=range qy_data {
		row = sheet.AddRow()
		row.AddCell().SetString(k)
		row.AddCell().SetString(fmt.Sprintf("%d",qu.IntAll(v)))
	}
	row = sheet.AddRow()
	row = sheet.AddRow()


	if len(st_data)>0 { //有数据
		row = sheet.AddRow()
		row.AddCell().Value = "主要站点/日期"
		row.AddCell().Value = "名称"
		row.AddCell().Value = "数量"
		row.AddCell().Value = "总计"


		mgodata := *qu.ObjToMap(st_data["mgodata"])
		date := qu.ObjToString(st_data["date"])
		total := qu.IntAll(st_data["mgonum"])
		isT := false
		for key,_ := range uniqueData {
			row = sheet.AddRow()
			if !isT {
				row.AddCell().SetString(date)
				row.AddCell().SetString(key)
				row.AddCell().SetString(fmt.Sprintf("%d",qu.IntAll(mgodata[key])))
				row.AddCell().SetString(fmt.Sprintf("%d",qu.IntAll(total)))
				isT = true
			}else {
				row.AddCell().Value = ""
				row.AddCell().SetString(key)
				row.AddCell().SetString(fmt.Sprintf("%d",qu.IntAll(mgodata[key])))
				row.AddCell().Value = ""
			}
		}
	}
	err := f.Save(xlsx_name)
	if err != nil {
		log.Println("保存xlsx失败:", err)
	}else {
		log.Println("保存xlsx成功:", err)
	}

	sendErrMailSmtp("主要站点以及企业相关统计","附件")
}