/**
*读取基本配置到内存
**/
package util

import (
	mgo "mongodb"
	qu "qfw/util"
	elastic "qfw/util/elastic"
	"sort"
)

//系统配置
type config struct {
	Port          string                 `json:"port"`
	Dbaddress     string                 `json:"dbaddress"`
	Dbname        string                 `json:"dbname"`
	Size          int                    `json:"size"`
	Fromtable     string                 `json:"fromtable"`
	Totablel      string                 `json:"totablel"`
	Biaozhu       map[string]interface{} `json:"biaozhu"`
	InfluxdbUrl   string                 `json:"influxdbUrl"`
	Redis         string                 `json:"redis"`
	Bidding       map[string]interface{} `json:"bidding"`
	Extract       map[string]interface{} `json:"extract"`
	Elas          map[string]interface{} `json:"es"`
	TopSubType    map[string]interface{} `json:"topsubtype"`
	CustomerField map[string]string      `json:"customerfield"`
}

const SPECIALTYPE = "二级不存在"
const BIDDINGSTARTID = "5a862f0640d2d9bbe88e3cec"

var (
	Config   config
	MgoB     *mgo.MongodbSim //bidding
	BidColl1 string
	BidColl2 string
	MgoE     *mgo.MongodbSim //extract
	ExtColl1 string
	ExtColl2 string
	MgoM     *mgo.MongodbSim  //标注
	Es       *elastic.Elastic //es
	Index    string
	Itype    string
	Coll     string
	//
	AllFieldArr         []map[string]string
	PurchasinglistField []map[string]string
	PackageField        []map[string]string
	WinnerorderField    []map[string]string
	TopSubStypeArr      []string
)

func InitConfig() {
	//标注
	MgoM = &mgo.MongodbSim{
		MongodbAddr: Config.Dbaddress,
		DbName:      Config.Dbname,
		Size:        Config.Size,
	}
	MgoM.InitPool()

	//bid
	bid := Config.Bidding
	BidColl1 = qu.ObjToString(bid["coll1"])
	BidColl2 = qu.ObjToString(bid["coll2"])
	MgoB = &mgo.MongodbSim{
		MongodbAddr: qu.ObjToString(bid["addr"]),
		DbName:      qu.ObjToString(bid["db"]),
		Size:        qu.IntAll(bid["size"]),
		UserName:    qu.ObjToString(bid["username"]),
		Password:    qu.ObjToString(bid["password"]),
	}
	MgoB.InitPool()

	//ext
	ext := Config.Extract
	ExtColl1 = qu.ObjToString(ext["coll1"])
	ExtColl2 = qu.ObjToString(ext["coll2"])
	MgoE = &mgo.MongodbSim{
		MongodbAddr: qu.ObjToString(ext["addr"]),
		DbName:      qu.ObjToString(ext["db"]),
		Size:        qu.IntAll(ext["size"]),
	}
	MgoE.InitPool()

	//es
	es := Config.Elas
	Index = qu.ObjToString(es["index"])
	Itype = qu.ObjToString(es["itype"])
	Es = &elastic.Elastic{
		S_esurl: qu.ObjToString(es["addr"]),
		I_size:  qu.IntAll(es["pool"]),
	}
	Es.InitElasticSize()

	//
	tmpAllField := Arr{}
	AllFieldMap := map[string]string{}
	AllFieldArr = []map[string]string{}
	common, _ := Config.Biaozhu["common"].([]interface{})
	timeplace, _ := Config.Biaozhu["timeplace"].([]interface{})
	other, _ := Config.Biaozhu["other"].([]interface{})
	for _, com := range common {
		tmp := com.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		if key == "" {
			continue
		}
		descript := qu.ObjToString(tmp["descript"])
		tmpAllField = append(tmpAllField, descript)
		AllFieldMap[descript] = key
	}
	for _, tp := range timeplace {
		tmp := tp.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		descript := qu.ObjToString(tmp["descript"])
		tmpAllField = append(tmpAllField, descript)
		AllFieldMap[descript] = key
	}
	for _, o := range other {
		tmp := o.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		descript := qu.ObjToString(tmp["descript"])
		tmpAllField = append(tmpAllField, descript)
		AllFieldMap[descript] = key
	}
	tmpAllField = append(tmpAllField, "标的物")
	tmpAllField = append(tmpAllField, "多包")
	tmpAllField = append(tmpAllField, "中标候选人")
	AllFieldMap["标的物"] = "purchasinglist"
	AllFieldMap["多包"] = "package"
	AllFieldMap["中标候选人"] = "winnerorder"
	sort.Sort(tmpAllField)
	for _, f := range tmpAllField {
		AllFieldArr = append(AllFieldArr, map[string]string{AllFieldMap[f]: f})
	}
	//
	PurchasinglistField = []map[string]string{}
	pcls, _ := Config.Biaozhu["purchasinglist"].([]interface{})
	for _, pcl := range pcls {
		tmp := pcl.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		descript := qu.ObjToString(tmp["descript"])
		PurchasinglistField = append(PurchasinglistField, map[string]string{key: descript})
	}
	PackageField = []map[string]string{}
	pkgs, _ := Config.Biaozhu["package"].([]interface{})
	for _, pkg := range pkgs {
		tmp := pkg.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		descript := qu.ObjToString(tmp["descript"])
		PackageField = append(PackageField, map[string]string{key: descript})
	}
	WinnerorderField = []map[string]string{}
	wods, _ := Config.Biaozhu["winnerorder"].([]interface{})
	for _, wod := range wods {
		tmp := wod.(map[string]interface{})
		key := qu.ObjToString(tmp["key"])
		descript := qu.ObjToString(tmp["descript"])
		WinnerorderField = append(WinnerorderField, map[string]string{key: descript})
	}
	//qu.Debug(PurchasinglistField, PackageField, WinnerorderField)

	//公告类型
	for top, v := range Config.TopSubType {
		tmp := v.([]interface{})
		for _, sub := range tmp {
			TopSubStypeArr = append(TopSubStypeArr, top+"-"+qu.ObjToString(sub))
		}
	}
	TopSubStypeArr = append(TopSubStypeArr, SPECIALTYPE)
	//coll
	Coll = Config.Fromtable
}
