package util

import (
	"go.mongodb.org/mongo-driver/mongo"
	"mongodb"
	qu "qfw/util"
	"sort"
	"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
	CustomerFieldMap_EH map[string]string
	CustomerFieldMap_HE map[string]string
	BiaoZhu             map[string]interface{}
	PurchasinglistField []map[string]string
	PackageField        []map[string]string
	WinnerorderField    []map[string]string
	AllFieldArr         []map[string]string
	TopSubStypeArr      []string
	TopSubStypeArr2     []string
)

var (
	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"}
var SourceInfoIndexArr = []mongo.IndexModel{
	mongo.IndexModel{Keys: map[string]interface{}{"s_grouptaskid": 1}}, //独立索引
	mongo.IndexModel{Keys: map[string]interface{}{"s_usertaskid": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"s_grouptaskid": 1, "b_istag": 1}}, //联合索引
	mongo.IndexModel{Keys: map[string]interface{}{"s_grouptaskid": 1, "s_isgiveuser": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"s_usertaskid": 1, "b_istag": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"s_usertaskid": 1, "b_check": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_istagging": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_istagging": 1, "b_istag": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "b_istagging": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "s_isgiveuser": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "s_isgiveuser": 1, "b_istag": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "s_isgiveuser": 1, "b_istagging": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "s_isgiveuser": 1, "i_ckdata": 1}},
	mongo.IndexModel{Keys: map[string]interface{}{"b_isgivegroup": 1, "s_isgiveuser": 1, "i_ckdata": 1, "b_istagging": 1}},
}

const JYHREFPRE = "https://www.jianyu360.cn/article/content/"
const BIDDINGSTARTID = "5a862f0640d2d9bbe88e3cec"
const PROJECTCOLLNAME = "f_project"
const TASKCOLLNAME = "f_task"
const LOGCOLLNAME = "l_taglog"
const SPECIALTYPE = "二级不存在"

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()

	Mgo_QY = &mongodb.MongodbSim{
		MongodbAddr: qu.ObjToString(bid["addr"]),
		DbName:      "mixdata",
		Size:        qu.IntAll(bid["size"]),
		UserName:    qu.ObjToString(bid["username"]),
		Password:    qu.ObjToString(bid["password"]),
	}
	Mgo_QY.InitPool()

	//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
	}

	BiaoZhu = Sysconfig["biaozhu"].(map[string]interface{})
	tmpAllField := Arr{}
	AllFieldMap := map[string]string{}
	AllFieldArr = []map[string]string{}
	common := BiaoZhu["common"].([]interface{})
	timeplace := BiaoZhu["timeplace"].([]interface{})
	other := 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, _ := 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, _ := 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, _ := 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})
	}
	//公告类型
	types := Sysconfig["topsubtype"].(map[string]interface{})
	for top, v := range types {
		tmp := v.([]interface{})
		for _, sub := range tmp {
			TopSubStypeArr = append(TopSubStypeArr, top+"-"+qu.ObjToString(sub))
		}
	}
	TopSubStypeArr2 = TopSubStypeArr

	initQuaConfig()
}

//质量相关初始化配置
func initQuaConfig() {
	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{})
}
