package main

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

var fieldArr = []string{
	"company_id",
	"province_short",
	"company_name",
	"company_code",
	"credit_no",
	"org_code",
	"tax_code",
	"establish_date",
	"legal_person",
	"legal_person_caption",
	"company_status",
	"company_type",
	"authority",
	"issue_date",
	"operation_startdate",
	"operation_enddate",
	"capital",
	"company_address",
	"business_scope",
	"cancel_date",
	"cancel_reason",
	"revoke_date",
	"revoke_reason",
	"use_flag",
	"create_time",
	"update_time",
	"legal_person_type",
	"real_capital",
	"en_name",
	"list_code",
}

//保存至mysql
func saveCompanyMysql()  {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(o_company_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%10000==0 {
			log.Debug("curent index ",total)
		}
		tmpid := BsonTOStringId(tmp["_id"])
		delete(tmp,"_id")
		insertMysqlData("f_company",tmp,tmpid)
		tmp = make(map[string]interface{})
	}
	log.Debug("is company over ",total)
}

//关联企业信息-升数据
func exportCompanyMysql()  {
	dataArr := repeatCompanyNamedata()
	for index,tmp := range dataArr{
		if index%10000==0 {
			log.Debug("curent index ",index)
		}
		for company,type_str := range tmp{
			infoArr,_ :=spi_mgo.Find(s_qybase_coll, map[string]interface{}{
				"company_name":company,
			},nil,nil)
			if len(infoArr)>0 {//选取最优-企业信息
				index := getOneBaseCompany(infoArr)
				info := infoArr[index]
				update := buildUpdateData(info,type_str)
				save_mgo.Save(o_company_coll,update)
			}else {//企业未匹配-记录
				save_mgo.Save(o_company_err_coll, map[string]interface{}{
					"company_name":company,
					"business_type":type_str,
				})
			}
		}
	}
}

//构建更新数据
func buildUpdateData(tmp map[string]interface{},type_str string) map[string]interface{} {
	update := map[string]interface{}{}
	for _,v := range fieldArr{
		if tmp[v]!=nil {
			update[v] = tmp[v]
		}
	}
	company_id := qu.ObjToString(update["company_id"])
	if company_id=="" {
		return map[string]interface{}{}
	}
	qy_data := qy_mgo.FindByStrId("qyxy_std",company_id)
	update["area"] = ""
	update["city"] = ""
	update["district"] = ""
	if qy_data!=nil && len(qy_data)>2 {
		update["area"] = qu.ObjToString(qy_data["company_area"])
		update["city"] = qu.ObjToString(qy_data["company_city"])
		update["district"] = qu.ObjToString(qy_data["company_district"])
	}
	update["business_type"] = type_str

	return update
}
//获取最优企业
func getOneBaseCompany(arr []map[string]interface{}) int {
	index :=0
	flag := 100
	if len(arr)>1 {
		for k,tmp := range arr{
			use_flag := qu.IntAll(tmp["use_flag"])
			if use_flag==0 {
				index=k
				break
			}else {
				if use_flag<flag {
					flag = use_flag
					index = k
				}
			}
		}
	}
	return index
}
//重复企业-过滤
func repeatCompanyNamedata() []map[string]string {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total,isok:=map[string]interface{}{},0,0
	dict := map[string]string{}
	company_arr := []string{}
	it := sess.DB(save_mgo.DbName).C(s_company_coll).Find(&q).Select(map[string]interface{}{
		"company":1,
		"class":1,
	}).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%100000==0 {
			log.Debug("curent index ",total,isok)
		}
		company:= qu.ObjToString(tmp["company"])
		class := qu.ObjToString(tmp["class"])
		if company!="" {
			if dict[company]=="" {
				isok++
				dict[company] = class
				company_arr = append(company_arr,company)
			}else {
				pre_class := dict[company]
				if pre_class!=class && !strings.Contains(pre_class,"&") {
					dict[company] = pre_class+"&"+class
				}
			}
		}
		tmp = make(map[string]interface{})
	}
	new_arr := []map[string]string{}
	for _,v := range company_arr{
		type_str :=""
		str := dict[v]
		if strings.Contains(str,"&") {
			type_str = "3"
		}else {
			if strings.Contains(str,"生产") {
				type_str= "1"
			}else if strings.Contains(str,"经营") {
				type_str = "2"
			}else {
				type_str = "4"
			}
		}
		new_arr = append(new_arr, map[string]string{
			v:type_str,
		})
	}
	log.Debug("is repeat company over ",total,isok,len(new_arr))
	return new_arr
}



