package main

import (
	log "github.com/donnie4w/go-logger/logger"
	"go.mongodb.org/mongo-driver/bson"
	qu "qfw/util"
)

//法人基础信息表
func exportLegalBaseInfo() {
	sess := Save_Mgo.GetMgoConn()
	defer Save_Mgo.DestoryMongoConn(sess)
	q, total := map[string]interface{}{}, 0
	it := sess.DB(Save_Mgo.DbName).C("nmpa_company_dealer").Find(&q).Sort("_id").Select(map[string]interface{}{
		"company_id":  1,
		"dealer_name": 1,
	}).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); {
		if total%10000 == 0 {
			log.Debug("curent index ", total)
		}
		company_id := qu.ObjToString(tmp["company_id"])
		dealer_name := qu.ObjToString(tmp["dealer_name"])
		createLegalBaseInfo(dealer_name, company_id)

		tmp = make(map[string]interface{})
	}
	log.Debug("经销商信息~~~", total)
}

func createLegalBaseInfo(company_name string, company_id string) {
	info := map[string]interface{}{}
	info["company_name"] = company_name
	info["company_id"] = company_id
	infoArr, _ := Qyxy_Mgo.Find("qyxy_std", map[string]interface{}{"company_name": company_name}, nil, nil)
	if len(infoArr) > 0 {
		ind := getOneBaseCompany(infoArr)
		tmp := infoArr[ind]
		info["company_code"] = qu.ObjToString(tmp["company_code"])
		info["credit_no"] = qu.ObjToString(tmp["credit_no"])
		info["org_code"] = qu.ObjToString(tmp["org_code"])
		info["tax_code"] = qu.ObjToString(tmp["tax_code"])
		info["establish_date"] = qu.ObjToString(tmp["establish_date"])
		info["legal_person"] = qu.ObjToString(tmp["legal_person"])
		info["legal_person_caption"] = qu.ObjToString(tmp["legal_person_caption"])
		info["company_status"] = qu.ObjToString(tmp["company_status"])
		info["company_type"] = qu.ObjToString(tmp["company_type"])
		info["authority"] = qu.ObjToString(tmp["authority"])
		info["issue_date"] = qu.ObjToString(tmp["issue_date"])
		info["operation_startdate"] = qu.ObjToString(tmp["operation_startdate"])
		info["operation_enddate"] = qu.ObjToString(tmp["operation_enddate"])

		data := Spi_Mgo.FindOne("company_base", bson.M{"company_id": company_id})
		new_capital := qu.ObjToString(data["capital"])
		info["capital"] = new_capital

		info["company_address"] = qu.ObjToString(tmp["company_address"])
		info["business_scope"] = qu.ObjToString(tmp["business_scope"])
		info["cancel_date"] = qu.ObjToString(tmp["cancel_date"])
		info["cancel_reason"] = qu.ObjToString(tmp["cancel_reason"])
		info["revoke_date"] = qu.ObjToString(tmp["revoke_date"])
		info["revoke_reason"] = qu.ObjToString(tmp["revoke_reason"])
		info["legal_person_type"] = qu.IntAll(tmp["legal_person_type"])
		info["real_capital"] = qu.ObjToString(tmp["real_capital"])
		info["list_code"] = qu.ObjToString(tmp["list_code"])

		//区域代码
		area_code, _, _ := dealWithRegionCode(qu.ObjToString(tmp["company_area"]), qu.ObjToString(tmp["company_city"]), qu.ObjToString(tmp["company_district"]))
		info["area_code"] = area_code

		info["employee_no"] = qu.IntAll(tmp["employee_num"])
		info["company_phone"] = qu.ObjToString(tmp["company_phone"])
		info["insurance_amount"] = qu.ObjToString(tmp["employee_num"])
		info["company_email"] = qu.ObjToString(tmp["company_email"])
		info["website"] = qu.ObjToString(tmp["website_url"])
		info["sourcetype"] = 1

		Save_Mgo.Save("nmpa_legal_baseinfo", info)
	}
}
