package main

import (
	"github.com/tealeg/xlsx"
	"log"
	qu "qfw/util"
	"strings"
	"time"
)

var (
	sysconfig    				map[string]interface{} //配置文件
	save_mgo        			*MongodbSim            //mongodb操作对象
	save_coll,export_coll		string
	start_time,end_time			int64
	words_arr					[]map[string]interface{}
	timeLayout = "2006-01"
	timeLayout_1 = "2006-01-02"
	city_map					map[string]map[string]string
	area_map					map[string]map[string]string
)
func initMgo()  {
	saveconf := sysconfig["save_mgodb"].(map[string]interface{})
	save_coll = qu.ObjToString(saveconf["coll"])
	save_mgo = &MongodbSim{
		MongodbAddr: saveconf["addr"].(string),
		DbName:      saveconf["db"].(string),
		Size:        qu.IntAllDef(saveconf["pool"], 5),
	}
	save_mgo.InitPool()
	export_coll = qu.ObjToString(sysconfig["export_coll"])

	start_time = time.Date(time.Now().Year(), 5, 1, 0, 0, 0, 0, time.Local).Unix()
	end_time = time.Date(time.Now().Year()+1, 1, 1, 0, 0, 0, 0, time.Local).Unix()

	words_arr = qu.ObjArrToMapArr(sysconfig["words_arr"].([]interface{}))
	log.Println(sysconfig)
}

func initRJQY() {
	city_map = map[string]map[string]string{}
	area_map = map[string]map[string]string{}
	ff,_ := xlsx.OpenFile("rjqy.xlsx")
	for _, sheet := range ff.Sheets {
		for _, row := range sheet.Rows {
			var strArr []string
			for _, cell := range row.Cells {
				strArr = append(strArr, cell.String())
			}
			//匹配特殊市
			str_0 := strArr[0]
			str_1 := strArr[1]
			str_2 := strArr[2]
			if str_2!="" {
				arr := strings.Split(str_2,"、")
				if strings.Contains(str_2,"市") {
					for _,v := range arr {
						city_map[v] = map[string]string{
							"region":str_0,
							"region_pro":str_1,
						}
					}
					area_map[str_1] = map[string]string{
						"region":str_0,
						"region_pro":str_1,
					}
				}else {
					for _,v := range arr {
						area_map[v] = map[string]string{
							"region":str_0,
							"region_pro":str_1,
						}
					}
				}
			}else {
				area_map[str_1] = map[string]string{
					"region":str_0,
					"region_pro":str_1,
				}
			}
		}
	}
}

func init()  {
	qu.ReadConfig(&sysconfig)
	initMgo()
	initRJQY()
}
func main()  {
	log.Println("测试...")
	//dealWithCgyxData()
	exportExcle()

}