package main
//导出源数据等~
import (
	log "github.com/donnie4w/go-logger/logger"
	qu "qfw/util"
)


//秘钥~base64
func encodeSEUrl(uid string) string {
	var SE = qu.SimpleEncrypt{Key: "topJYBX2019"}
	str := SE.EncodeString(uid)
	return str
}

//模板新增标注数据
func increaseTemplateData(from string ,to string){
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(from).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%1000==0 {
			log.Debug("curent index ",total)
		}
		tmpid := BsonTOStringId(tmp["_id"])
		uid := encodeSEUrl(tmpid)

		save_mgo.UpdateById(to,tmpid, map[string]interface{}{
			"$set":map[string]interface{}{
				"uid":uid,
			},
		})
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ",total)
}

//导出数据-bidding相关
func exportFromMongoToBiddingData(from string,to string){
	sess := qy_mgo.GetMgoConn()
	defer qy_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	isok:=0
	it := sess.DB(qy_mgo.DbName).C(from).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%1000==0 {
			log.Debug("curent index ",total,isok)
		}
		tmpid := BsonTOStringId(tmp["_id"])
		data := qy_mgo.FindById("bidding",tmpid)
		if data!=nil && len(data)>2 {
			isok++
			qy_mgo.Save(to,data)
		}else {
			data = qy_mgo.FindById("bidding_back",tmpid)
			if data!=nil && len(data)>2 {
				isok++
				qy_mgo.Save(to,data)
			}
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ",total,isok)
}

//导出数据-extract相关
func exportFromMongoToExtractData(from string,to string){
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total,isok:=map[string]interface{}{},0,0
	it := sess.DB(save_mgo.DbName).C(from).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%1000==0 {
			log.Debug("curent index ",total,isok)
		}
		tmpid := BsonTOStringId(tmp["_id"])
		data := save_mgo.FindById("result_20220218",tmpid)
		if data!=nil && len(data)>2 {
			isok++
			save_mgo.Save(to,data)
		}else {
			data = save_mgo.FindById("result_20220219",tmpid)
			if data!=nil && len(data)>2 {
				isok++
				save_mgo.Save(to,data)
			}
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ",total,isok)
}


//导出
func exportFenLeipecialData(){
	//导出招标非单一数据
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{
		"_id":map[string]interface{}{
			"$lt":StringTOBsonId("62bdc8800000000000000000"),
		},
	},0
	isok :=0
	it := sess.DB(save_mgo.DbName).C("bidding").Find(&q).Sort("-_id").Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%10000==0 {
			log.Debug("curent index ",total,isok)
		}
		//tmpid := BsonTOStringId(tmp["_id"])
		toptype:=qu.ObjToString(tmp["toptype"])
		subtype:=qu.ObjToString(tmp["subtype"])

		if toptype=="招标" && subtype!="单一" {
			isok++
			save_mgo.Save("zk_fenlei_data",tmp)
		}
		if isok>=100000 {
			return
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ",total,isok)


}





