package main

import (
	log "github.com/donnie4w/go-logger/logger"
	qu "qfw/util"
	"unicode/utf8"
)


func initMySqlMgoData() {
	//招标分类
	init_bidclass()
	init_buyerclass()
	init_industryclass()
	init_area()
	init_proclass()

	log.Debug("init mysql 完毕",len(bidclass_dict),len(buyerclass_dict),len(industry_dict))
	log.Debug("init mgo 完毕",len(area_dict),len(proclass_name_dict),len(proclass_code_dict))

}



func init_bidclass() {
	arr:= *MysqlTool.Find("v_bidclass",nil,"","",-1,-1)
	pre_str :=""
	for _,v := range arr {
		level := qu.ObjToString(v["level"])
		name := qu.ObjToString(v["name"])
		code := qu.ObjToString(v["code"])
		if level=="1" {
			pre_str = name
		}else {
			key := pre_str+"_"+name
			bidclass_dict[key] = code
		}
	}
}
func init_buyerclass() {
	//采购单位分类 v_buyerclass
	arr:= *MysqlTool.Find("v_buyerclass",nil,"","",-1,-1)
	for _,v := range arr {
		code :=qu.ObjToString(v["code"])
		name:= qu.ObjToString(v["name"])
		buyerclass_dict[name]  = code
	}
}
func init_industryclass() {
	//行业代码表 v_industryclass
	arr := *MysqlTool.Find("v_industryclass",nil,"","",-1,-1)
	pre_str :=""
	for _,v := range arr {
		level := qu.ObjToString(v["level"])
		name := qu.ObjToString(v["name"])
		code := qu.ObjToString(v["code"])
		if level=="1" {
			pre_str = name
		}else {
			key := pre_str+"_"+name
			industry_dict[key] = code
		}
	}
}
func init_area() {

	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)

	data ,_:= save_mgo.Find(o_area_coll,nil,nil,nil)
	code_map := make(map[string]string,0)
	for _,v := range data{
		name := qu.ObjToString(v["name"])
		code := qu.ObjToString(v["code"])
		code_map[code]=name
	}
	log.Debug(len(code_map))
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(o_area_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		name := qu.ObjToString(tmp["name"])
		code := qu.ObjToString(tmp["code"])
		pcode :=qu.ObjToString(tmp["pcode"])
		level := qu.ObjToString(tmp["level"])
		key := ""
		if level=="1" {
			key = name+"_"+"_"
		}else if level=="2" {
			key = code_map[pcode]+"_"+name+"_"
		}else if level=="3" {
			pc := code[:2]+"0000"
			key = code_map[pc]+"_"+code_map[pcode]+"_"+name
		}else {

		}
		if key!="" {
			area_dict[key] = code
		}
		tmp = make(map[string]interface{})
	}
}

//7780 -- 7746  采用最后一个 34个重复
func init_proclass() {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(o_class_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		name := qu.ObjToString(tmp["name"])
		code := qu.ObjToString(tmp["code"])
		proclass_code_dict[code] = name
		tmp = make(map[string]interface{})
	}
	//最多六位
	for k,v := range proclass_code_dict {
		key := ""
		if utf8.RuneCountInString(k)==2 {
			key = v+"_"+"_"
		} else if utf8.RuneCountInString(k)==4 {
			code_1 := k[:2]
			name_1 := proclass_code_dict[code_1]
			key = name_1+"_"+v+"_"
		}else if utf8.RuneCountInString(k)== 8{
			code_1 := k[:2]
			name_1 := proclass_code_dict[code_1]
			code_2 := k[:4]
			name_2 := proclass_code_dict[code_2]
			key = name_1+"_"+name_2+"_"+v
		}else if utf8.RuneCountInString(k)==14 {
			key = "四级"+"_"+v
		}
		if key!="" {
			proclass_name_dict[key] = k
		}
	}
}
