package util

import (
	"mongodb"
	qu "qfw/util"
	"qfw/util/elastic"
	"sync"
)

//const JYDATAFROMDB = "jyqyfw"
//const JYDATAFROMCOLL = "usermail_history"
//const JYDATATODB = "jyqyfw"

var (
	Sysconfig           map[string]interface{} //配置文件
	Quaconfig           map[string]interface{} //质量配置文件
	Mgo                 *mongodb.MongodbSim
	AllToColl           string              //所有标注数据汇总表
	Password            string              //默认登陆密码
	MgoB                *mongodb.MongodbSim //bidding
	BidColl1            string
	BidColl2            string
	MgoE                *mongodb.MongodbSim //extract
	ExtColl1            string
	ExtColl2            string
	ProjectColl         string
	MgoJy               *mongodb.MongodbSim //jy
	JyHistory           string
	JyUser              string
	Es                  *elastic.Elastic //es
	Index               string
	Itype               string
	CustomerFieldMap_EH map[string]string
	CustomerFieldMap_HE map[string]string
)


var (
	Qy_Es                  					*elastic.Elastic 		//企业es
	Mgo_QY									*mongodb.MongodbSim		//企业mgo
	Qy_Lock 								sync.Mutex         	   	//锁-多线程备用
	Ext_Type,Ext_From						map[string]interface{}	//抽取来源，方式分
	Buyer_Score,S_Winner_Score				map[string]interface{}	//字段规则分
	Budget_Score,Bidamount_Score			map[string]interface{}
	Projectname_Score,Projectcode_Score		map[string]interface{}
)



var SE = qu.SimpleEncrypt{Key: "topJYBX2019"}

const BIDDINGSTARTID = "5a862f0640d2d9bbe88e3cec"
const PROJECTCOLLNAME = "f_project"
const TASKCOLLNAME = "f_task"

func InitConfig() {
	Mgo = &mongodb.MongodbSim{
		MongodbAddr: qu.ObjToString(Sysconfig["mgodb"]),
		Size:        qu.IntAll(Sysconfig["dbsize"]),
		DbName:      qu.ObjToString(Sysconfig["dbname"]),
	}
	Mgo.InitPool()
	AllToColl = qu.ObjToString(Sysconfig["alltocoll"])
	Password = "123"

	//bid
	bid := Sysconfig["bidding"].(map[string]interface{})
	BidColl1 = qu.ObjToString(bid["coll1"])
	BidColl2 = qu.ObjToString(bid["coll2"])
	MgoB = &mongodb.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 := Sysconfig["extract"].(map[string]interface{})
	ExtColl1 = qu.ObjToString(ext["coll1"])
	ExtColl2 = qu.ObjToString(ext["coll2"])
	ProjectColl = qu.ObjToString(ext["projectcoll"])
	MgoE = &mongodb.MongodbSim{
		MongodbAddr: qu.ObjToString(ext["addr"]),
		DbName:      qu.ObjToString(ext["db"]),
		Size:        qu.IntAll(ext["size"]),
	}
	MgoE.InitPool()

	//jy
	jy := Sysconfig["jy"].(map[string]interface{})
	JyHistory = qu.ObjToString(jy["history"])
	JyUser = qu.ObjToString(jy["user"])
	MgoJy = &mongodb.MongodbSim{
		MongodbAddr: qu.ObjToString(jy["addr"]),
		DbName:      qu.ObjToString(jy["db"]),
		Size:        qu.IntAll(jy["size"]),
	}
	MgoJy.InitPool()

	//es
	//es := Sysconfig["es"].(map[string]interface{})
	//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()

	//other
	CustomerFieldMap_EH = map[string]string{}
	CustomerFieldMap_HE = map[string]string{}
	for k, v := range Sysconfig["customerfield"].(map[string]interface{}) {
		val := v.(string)
		CustomerFieldMap_HE[k] = val
		CustomerFieldMap_EH[val] = k
	}



	initQuaConfig()
}




//质量相关初始化配置
func initQuaConfig()  {
	//线上-需替换-172.17.145.163:27083,172.17.4.187:27082，
	qyxyconf := Quaconfig["qy_mongodb"].(map[string]interface{})
	Mgo_QY = &mongodb.MongodbSim{
		MongodbAddr: qyxyconf["qy_addrName"].(string),
		DbName:      qyxyconf["qy_dbName"].(string),
		Size:        qu.IntAllDef(qyxyconf["qy_pool"], 5),
		UserName:    qyxyconf["qy_username"].(string),
		Password:    qyxyconf["qy_password"].(string),
	}
	Mgo_QY.InitPool()

	//查询企业库-可使用es - 也可以mongo  http://172.17.145.170:9800
	//Qy_Es = &elastic.Elastic{
	//	S_esurl: qu.ObjToString(Quaconfig["qyxy_es"]),
	//	I_size:  10,
	//}
	//Es.InitElasticSize()

	Ext_Type = Quaconfig["ext_type"].(map[string]interface{})
	Ext_From = Quaconfig["ext_from"].(map[string]interface{})

	Buyer_Score = Quaconfig["buyer_score"].(map[string]interface{})
	S_Winner_Score = Quaconfig["s_winner_score"].(map[string]interface{})

	Budget_Score = Quaconfig["budget_score"].(map[string]interface{})
	Bidamount_Score = Quaconfig["bidamount_score"].(map[string]interface{})

	Projectname_Score = Quaconfig["projectname_score"].(map[string]interface{})
	Projectcode_Score = Quaconfig["projectcode_score"].(map[string]interface{})
}