package main

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



//企业许可备案信息
func saveReferenceMysql()  {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	ok1,ok2:=0,0
	it := sess.DB(save_mgo.DbName).C(s_company_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%10000==0 {
			log.Debug("curent index ",total,ok1,ok2)
		}
		class := qu.ObjToString(tmp["class"])
		company := qu.ObjToString(tmp["company"])
		tmpid := BsonTOStringId(tmp["_id"])
		if company!="" {
			if class=="生产型"{
				ok1++
				data := dealWithSCX(tmp)
				insertMysqlData("f_production_register",data,tmpid)
			}else if class=="经营型" {
				ok2++
				data := dealWithJYX(tmp)
				insertMysqlData("f_business_register",data,tmpid)
			}else {

			}
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is reference over ",total,ok1,ok2)
}

//处理生产型
func dealWithSCX (tmp map[string]interface{}) map[string]interface{}{
	data := map[string]interface{}{}
	yxqx := qu.ObjToString(tmp["yxqx"])
	barq := qu.ObjToString(tmp["barq"])
	if yxqx!="" && timeReg.MatchString(yxqx) {
		data["validity_date"] = yxqx
	}
	if barq!="" && timeReg.MatchString(barq){
		data["approve_date"] = barq
	}

	data["regnum"] = qu.ObjToString(tmp["reg_no"])
	type_str:=strings.ReplaceAll(qu.ObjToString(tmp["type"]),"企业","")
	data["type"] = type_str
	data["address"] = qu.ObjToString(tmp["product_address"])
	data["scope"] = qu.ObjToString(tmp["product_range"])
	data["approve_depart"] = qu.ObjToString(tmp["badw"])
	data["company_name"] = qu.ObjToString(tmp["company"])
	data["responsible_person"] = qu.ObjToString(tmp["responsible_persons"])
	data["legal_person"] = qu.ObjToString(tmp["legal_person"])
	data["residence"] = qu.ObjToString(tmp["address"])

	return data
}
//处理经营型
func dealWithJYX (tmp map[string]interface{}) map[string]interface{}{
	data := map[string]interface{}{}
	yxqx := qu.ObjToString(tmp["yxqx"])
	barq := qu.ObjToString(tmp["barq"])
	if yxqx!="" && timeReg.MatchString(yxqx) {
		data["validity_date"] = yxqx
	}
	if barq!="" && timeReg.MatchString(barq){
		data["approve_date"] = barq
	}

	data["regnum"] = qu.ObjToString(tmp["reg_no"])
	type_str:=strings.ReplaceAll(qu.ObjToString(tmp["type"]),"企业","")
	data["type"] = type_str
	data["address"] = qu.ObjToString(tmp["business_address"])
	data["scope"] = qu.ObjToString(tmp["business_range"])

	business_mode := qu.ObjToString(tmp["business_mode"])
	if strings.Contains(business_mode,"批零") ||
		(strings.Contains(business_mode,"批发") && strings.Contains(business_mode,"零售") ){
		data["business_type"] = "3"
	}else if strings.Contains(business_mode,"批发") {
		data["business_type"] = "1"
	}else if strings.Contains(business_mode,"零售") {
		data["business_type"] = "2"
	}else {
		data["business_type"] = "4"
	}

	data["approve_depart"] = qu.ObjToString(tmp["badw"])
	data["website_name"] = ""
	data["website"] = ""
	data["platform_name"] = ""
	data["client_name"] = ""
	data["storage_room"] = qu.ObjToString(tmp["storehouse_address"])
	data["company_name"] = qu.ObjToString(tmp["company"])
	data["responsible_person"] = qu.ObjToString(tmp["legal_person"])
	data["legal_person"] = qu.ObjToString(tmp["responsible_persons"])
	data["residence"] = qu.ObjToString(tmp["address"])

	return data
}

