package main

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

//保存至mysql
func saveCompanyHistoryMysql()  {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(o_company_history_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_history",tmp,tmpid)
		tmp = make(map[string]interface{})
	}
	log.Debug("is company history over ",total)
}


func exportCompanyAliasNameMysql ()  {

	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total,isok:=map[string]interface{}{},0,0
	it := sess.DB(save_mgo.DbName).C(o_company_coll).Find(&q).Select(map[string]interface{}{
		"company_id":1,
		"company_name":1,
		"business_type":1,
	}).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%10000==0 {
			log.Debug("curent index ",total,isok)
		}
		company_id := qu.ObjToString(tmp["company_id"])
		company_name := qu.ObjToString(tmp["company_name"])
		business_type := qu.ObjToString(tmp["business_type"])
		arr,_:=spi_mgo.Find(s_company_history_coll, map[string]interface{}{
			"company_id":company_id,
		},nil,map[string]interface{}{
			"history_name":1,
		})

		for _,v := range arr{
			history_name := qu.ObjToString(v["history_name"])
			if history_name=="" || history_name==company_name{
				continue
			}
			isok++
			save_mgo.Save(o_company_history_coll, map[string]interface{}{
				"name":history_name,
				"company_id":company_id,
				"company_name":company_name,
				"business_type":business_type,
			})
		}

		tmp = make(map[string]interface{})
	}
	log.Debug("is company history over ",total,isok)
}
