package main

import (
	"flag"
	"fmt"
	"github.com/cron"
	"github.com/tealeg/xlsx"
	"github.com/xuri/excelize"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"log"
	"os"
	qu "qfw/util"
	"time"
)

var (
	sysconfig                                                            map[string]interface{} //配置文件
	save_mgo, site_mgo, python_mgo, qy_mgo                               *MongodbSim            //mongodb操作对象
	save_c_name, site_c_name, python_c_name, xlsx_name, site_unique_name string
	prepareData                                                          []map[string]interface{}
	startRun, startSend                                                  string
)

func initMgo() {
	saveconf := sysconfig["save_mgodb"].(map[string]interface{})
	save_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()

	siteconf := sysconfig["site_mgodb"].(map[string]interface{})
	site_c_name = qu.ObjToString(siteconf["coll"])
	site_mgo = &MongodbSim{
		MongodbAddr: siteconf["addr"].(string),
		DbName:      siteconf["db"].(string),
		Size:        qu.IntAllDef(siteconf["pool"], 5),
	}
	site_mgo.InitPool()

	pconf := sysconfig["python_mgodb"].(map[string]interface{})
	python_c_name = qu.ObjToString(pconf["coll"])
	python_mgo = &MongodbSim{
		MongodbAddr: pconf["addr"].(string),
		DbName:      pconf["db"].(string),
		Size:        qu.IntAllDef(pconf["pool"], 5),
		//Password:	 "zk@123123",
		//UserName:	 "zhengkun",
		Password: "datazy@read",
		UserName: "dataZY",
	}
	python_mgo.InitPool()
	site_unique_name = qu.ObjToString(sysconfig["site_unique_name"])
	xlsx_name = qu.ObjToString(sysconfig["xlsx_name"])
	log.Println(site_unique_name, xlsx_name)

}

func init() {
	return
	qu.ReadConfig(&sysconfig)
	//加载配置文件
	flag.StringVar(&startRun, "sr", "", "是否启动")  //增量
	flag.StringVar(&startSend, "ss", "", "是否发送") //增量
	flag.Parse()
	initMgo()
}

func main() {

	//qy_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.145.163:27083,172.17.4.187:27082",
	//	DbName:      "mixdata",
	//	Size:        10,
	//	UserName:    "zhengkun",
	//	Password:    "zk@123123",
	//}
	//qy_mgo.InitPool()

	//save_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.4.87:27080",
	//	DbName:      "editor",
	//	Size:        10,
	//	UserName:    "",
	//	Password:    "",
	//}
	//save_mgo.InitPool()

	//python_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.4.85:27080",
	//	DbName:      "qfw",
	//	Size:        10,
	//	UserName:    "",
	//	Password:    "",
	//}
	//python_mgo.InitPool()

	//save_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.4.87:27080",
	//	DbName:      "zk_regions",
	//	Size:        10,
	//	UserName:    "",
	//	Password:    "",
	//}
	//save_mgo.InitPool()

	//save_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.4.85:27080",
	//	DbName:      "qfw",
	//	Size:        10,
	//	UserName:    "",
	//	Password:    "",
	//}
	//save_mgo.InitPool()

	//save_mgo = &MongodbSim{
	//	MongodbAddr: "172.17.145.163:27083,172.17.4.187:27082",
	//	DbName:      "qfw",
	//	Size:        10,
	//	UserName:    "zhengkun",
	//	Password:    "zk@123123",
	//}
	//save_mgo.InitPool()

	save_mgo = &MongodbSim{
		MongodbAddr: "127.0.0.1:27017",
		DbName:      "zhengkun",
		Size:        10,
		UserName:    "",
		Password:    "",
	}
	save_mgo.InitPool()

	//decodeJyUrl("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
	//decodeSEUrl("QltDeTokbwUCBA0WV0V6anBgCwIGW0VZ")
	//decodeFeedBackInfo()
	//export0427wininfo()

	//decodeSEUrl("QltFK2gnbAIIUFwVV0dybyZpVAkEWkNb")
	//decodeSEUrl("QltFK2h2OQMIUFwVV0dybyZpVAkFWEdY")
	exportErrInfo0515()
	return

	/*
		repairYiXieChaData() 		//医械查
		dealWithClassData()			//AB 匹配
		repairEqRegNoClassData()	//注册号-截取匹配
		repairEqClassData()			//同分类-补充-0123
	*/

	c := cron.New()
	c.AddFunc("0 50 8 ? * *", func() { dealWithSiteData() })
	c.Start()

	if startRun != "" {
		log.Println("立即执行一次...")
		dealWithSiteData()
	} else {
		log.Println("正常监听...")
	}

	time.Sleep(99999 * time.Hour)
}

func dealWithSiteData() {

	cur_time := time.Now().Unix()
	today := GetOneWeekDay(TimeStampToString(cur_time))
	if today == 7 || today == 6 {
		log.Println("今个周末...休息休息")
		return
	}

	prepareXlsxSiteData() //准备数据

	if prepareData == nil || len(prepareData) == 0 {
		log.Println("异常表site_unique_new...未加载数据...")
		return
	}

	now := time.Now()
	durdays := 7 //周期7天 假设今天5月15日   周六 now.Day()
	start := time.Date(now.Year(), now.Month(), now.Day()-durdays, 0, 0, 0, 0, time.Local).Unix()
	end := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()

	log.Println(start, end)
	q := map[string]interface{}{
		"comeintime": map[string]interface{}{
			"$gte": start,
			"$lt":  end,
		},
	}
	//构建时间模型模型数据
	sitedata, pythondata := make(map[string]map[string]int, 0), make(map[string]map[string]map[string]interface{}, 0)
	siteTimeArr := []string{}
	for i := 0; i < durdays; i++ {
		t := int64(i*86400) + start
		time_key := TimeStampToString(t)
		time_day := GetOneWeekDay(time_key)
		if time_day == 7 || time_day == 6 {
		} else {
			sitedata[time_key] = map[string]int{}
			pythondata[time_key] = map[string]map[string]interface{}{}
			siteTimeArr = append(siteTimeArr, time_key)
		}
	}
	sess_site := site_mgo.GetMgoConn()
	defer site_mgo.DestoryMongoConn(sess_site)
	log.Println("data_bak 查询条件：", q)
	it_site := sess_site.DB(site_mgo.DbName).C(site_c_name).Find(&q).Sort("comeintime").Select(map[string]interface{}{
		"comeintime": 1,
		"site":       1,
	}).Iter()
	total := 0
	for tmp := make(map[string]interface{}); it_site.Next(&tmp); total++ {
		site := qu.ObjToString(tmp["site"])
		comeintime := qu.Int64All(tmp["comeintime"])
		time_key := TimeStampToString(comeintime)
		time_day := GetOneWeekDay(time_key)
		if time_day == 6 || time_day == 7 {
		} else {
			timedata := sitedata[time_key]
			if qu.Int64All(timedata[site]) == 0 {
				timedata[site] = 1
			} else {
				num := timedata[site]
				timedata[site] = num + 1
			}
		}
		tmp = make(map[string]interface{})
	}

	log.Println("is site over :", total)

	sess_python := python_mgo.GetMgoConn()
	defer python_mgo.DestoryMongoConn(sess_python)
	log.Println("bidding 查询条件：", q)
	it_python := sess_python.DB(python_mgo.DbName).C(python_c_name).Find(&q).Sort("comeintime").Select(map[string]interface{}{
		"comeintime": 1,
		"site":       1,
		"spidercode": 1,
	}).Iter()
	total = 0
	for tmp := make(map[string]interface{}); it_python.Next(&tmp); total++ {
		//if total%100000 == 0 {
		//	log.Println("current:", total)
		//}
		site := qu.ObjToString(tmp["site"])
		spidercode := qu.ObjToString(tmp["spidercode"])
		comeintime := qu.Int64All(tmp["comeintime"])

		time_key := TimeStampToString(comeintime)
		time_day := GetOneWeekDay(time_key)
		if time_day == 6 || time_day == 7 {
		} else {
			timedata := pythondata[time_key]
			if timedata[site] == nil {
				timedata[site] = map[string]interface{}{
					spidercode: 1,
				}
			} else {
				dict := timedata[site]
				num := qu.IntAll(dict[spidercode])
				timedata[site][spidercode] = num + 1
			}
		}

		tmp = make(map[string]interface{})
	}

	log.Println("is python over :", total)

	os.Remove(xlsx_name)
	f := xlsx.NewFile()

	for _, tmp := range prepareData {
		type_name := qu.ObjToString(tmp["type"])
		data, _ := tmp["data"].(primitive.A)
		dataArr := qu.ObjArrToMapArr(data)
		sheet, _ := f.AddSheet(type_name)
		row := sheet.AddRow()
		row.AddCell().Value = "国家/省份"
		row.AddCell().Value = "站点名称"
		if type_name == "python" || type_name == "chrome插件" {
			row.AddCell().Value = "爬虫名称"
		}
		row.AddCell().Value = "负责人"
		for _, timekey := range siteTimeArr {
			row.AddCell().SetString(fmt.Sprintf("%s入库量", timekey))
		}
		row.AddCell().Value = "前五天总量"

		for _, dict := range dataArr {
			row = sheet.AddRow()
			row.AddCell().SetString(qu.ObjToString(dict["area"]))
			row.AddCell().SetString(qu.ObjToString(dict["site"]))
			if type_name == "python" || type_name == "chrome插件" {
				row.AddCell().SetString(qu.ObjToString(dict["spidercode"]))
			}
			row.AddCell().SetString(qu.ObjToString(dict["person"]))
			total_num := 0
			if type_name == "python" || type_name == "chrome插件" {
				for _, timekey := range siteTimeArr {
					site_key := qu.ObjToString(dict["site"])
					spider_key := qu.ObjToString(dict["spidercode"])
					num := qu.IntAll(pythondata[timekey][site_key][spider_key])
					total_num = total_num + num
					row.AddCell().SetString(fmt.Sprintf("%d", num))
				}
				row.AddCell().SetString(fmt.Sprintf("%d", total_num))
			} else {
				for _, timekey := range siteTimeArr {
					key := qu.ObjToString(dict["site"])
					num := qu.IntAll(sitedata[timekey][key])
					total_num = total_num + num
					row.AddCell().SetString(fmt.Sprintf("%d", num))
				}
				row.AddCell().SetString(fmt.Sprintf("%d", total_num))
			}

		}

	}

	err := f.Save(xlsx_name)

	if err != nil {
		log.Println("保存xlsx失败:", err)
		return
	} else {
		log.Println("保存xlsx成功:", err)
	}

	//是否存日志- 待定
	time.Sleep(5 * time.Second)

	//发送邮件
	if startRun != "" && startSend == "" {
		log.Println("调试过程...不发邮件")
	} else {
		sendErrMailSmtp("主要站点最近五个工作日相关统计", "附件")
	}
	log.Println("结束......")
}

//准备模板站点数据
func prepareXlsxSiteData() {

	prepareData = make([]map[string]interface{}, 0)
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C(site_unique_name).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); {
		dict := tmp
		delete(dict, "_id")
		prepareData = append(prepareData, dict)
		tmp = make(map[string]interface{})
	}
	log.Println("准备完毕... ...")
}

//准备站点模型数据
func prepareTableData() {

	log.Println("准备数据...")

	f, err := excelize.OpenFile("站点跟踪维护新需求.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	sheetArr := []string{"Sheet1", "Sheet2", "Sheet3", "Sheet4"}
	sheetNumArr := []int{38, 36, 87, 16}
	dataArr := make([][]map[string]interface{}, 0)
	for index, sheet := range sheetArr {
		log.Println("*************")
		log.Println("*************")
		rows := f.GetRows(sheet)
		arr := make([]map[string]interface{}, 0)
		max := sheetNumArr[index]
		for k, row := range rows {
			if k > 0 && k < max {
				dict := map[string]interface{}{}
				for k1, value := range row {
					if index == 3 {
						if k1 == 0 {
							dict["area"] = qu.ObjToString(value)
						} else if k1 == 1 {
							dict["site"] = qu.ObjToString(value)
						} else if k1 == 2 {
							dict["spidercode"] = qu.ObjToString(value)
						} else if k1 == 3 {
							dict["person"] = qu.ObjToString(value)
						} else {
							break
						}
					} else {
						if k1 == 0 {
							dict["area"] = qu.ObjToString(value)
						} else if k1 == 1 {
							dict["site"] = qu.ObjToString(value)
						} else if k1 == 2 {
							dict["person"] = qu.ObjToString(value)
						} else {

							break
						}
					}
				}
				arr = append(arr, dict)
			}
			if k > max {
				break
			}
		}

		dataArr = append(dataArr, arr)
	}

	typeArr := []string{"政府采购", "公共资源", "其他站点", "python"}
	for index, arr := range dataArr {
		type_name := typeArr[index]
		save_mgo.Save(save_c_name, map[string]interface{}{
			"type": type_name,
			"data": arr,
		})
	}

	log.Println("is save over")

}
