package main

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

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


//导出-产品信息
func exportProductMysql() {
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(s_product_coll).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		if total%10000==0 {
			log.Debug("curent index ",total)
		}
		reg_company := qu.ObjToString(tmp["reg_company"])
		if reg_company!="" {
			data := buildInsertData(tmp)
			if data!=nil && len(data)>2 {
				save_mgo.Save(o_product_coll,data)
			}
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is product over ",total)
}


//构建数据
func buildInsertData(tmp map[string]interface{}) map[string]interface{}{
	data:=map[string]interface{}{}
	company_name :=qu.ObjToString(tmp["reg_company"])
	c_data := save_mgo.FindOne(o_company_coll, map[string]interface{}{
		"company_name":company_name,
	})
	if c_data!=nil && len(c_data)>2 {
		data["company_id"] = qu.ObjToString(c_data["company_id"])
		data["business_type"] = qu.ObjToString(c_data["business_type"])
	}else {
		//当前产品-对应企业-未关联f_company 需要比对别名表-修改此三个字段
		data["company_id"] = ""
		data["business_type"] = ""

		//查询别名表--待定

	}
	data["regno"] = qu.ObjToString(tmp["reg_no"])
	data["company_name"] = qu.ObjToString(tmp["reg_company"])
	data["company_address"] = qu.ObjToString(tmp["reg_address"])
	data["brand"] = ""
	p_class:= qu.ObjToString(tmp["p_class"])
	if p_class=="国产" {
		data["make_country"] = "1"
	}else if p_class=="进口"{
		data["make_country"] = "2"
	}else {
		data["make_country"] = ""
	}
	data["agent_name"] = qu.ObjToString(tmp["agent_name"])
	data["agent_address"] = qu.ObjToString(tmp["agent_address"])
	data["model"] = qu.ObjToString(tmp["model"])
	data["product_descrip"] = qu.ObjToString(tmp["product_depict"])
	data["use_scope "] = qu.ObjToString(tmp["application"])
	data["storage_conditions"] = qu.ObjToString(tmp["conditions_use"])
	data["approve_depart"] = qu.ObjToString(tmp["badw"])

	main_part := qu.ObjToString(tmp["main_component"])
	changes := qu.ObjToString(tmp["changes"])
	make_adress := qu.ObjToString(tmp["product_address"])
	remark := qu.ObjToString(tmp["remark"])
	if utf8.RuneCountInString(main_part)>2000 {
		main_part = ""
	}
	if utf8.RuneCountInString(changes)>2000{
		changes = ""
	}
	if utf8.RuneCountInString(make_adress)>800{
		make_adress = ""
	}
	if utf8.RuneCountInString(remark)>800{
		remark = ""
	}
	data["main_part"] = main_part
	data["changes"] = changes
	data["make_adress"] = make_adress
	data["remark"] = remark

	
	type_str := qu.ObjToString(tmp["type"])
	approve_date,validity_date := "",""
	validity_date = qu.ObjToString(tmp["yxqz"])
	if type_str == "注册" {
		data["regist_type"] = "1"
		approve_date = qu.ObjToString(tmp["pzrq"])
	}else if type_str == "备案" {
		data["regist_type"] = "2"
		approve_date = qu.ObjToString(tmp["barq"])
	}else {
		data["regist_type"] = ""
	}
	//处理-日期-格式-多变
	if approve_date != "" {
		approve_date_new := timeStandata(approve_date)
		if approve_date_new!="" {
			data["approve_date"] = approve_date_new
		}
	}
	if validity_date != "" {
		validity_date_new := timeStandata(validity_date)
		if validity_date_new!="" {
			data["validity_date"] = validity_date_new
		}
	}
	data["source_channel"] = qu.ObjToString(tmp["jy_class"])
	class := qu.ObjToString(tmp["class"])
	if class == "/" {
		class = ""
	}
	data["manage_type"] = class
	product_name := qu.ObjToString(tmp["product_name"])
	data["product_name"] = product_name
	product_class_1 := qu.ObjToString(tmp["product_class"])
	product_class_2 := qu.ObjToString(tmp["product_class_1"])
	product_class_3 := qu.ObjToString(tmp["product_class_2"])
	key_4 := "四级_"+product_name
	code_4 := proclass_name_dict[key_4]
	if code_4!="" {
		data["product_code"] = code_4
		data["product_class1"] = proclass_code_dict[code_4[:2]]
		data["product_class2"] = proclass_code_dict[code_4[:4]]
		data["product_class3"] = proclass_code_dict[code_4[:8]]
		data["product_class4"] = product_name
	}else {
		pro_key := fmt.Sprintf("%s_%s_%s",product_class_1,product_class_2,product_class_3)
		code := proclass_name_dict[pro_key]
		data["product_code"] = code
		data["product_class1"] = product_class_1
		data["product_class2"] = product_class_2
		data["product_class3"] = product_class_3
		data["product_class4"] = ""
	}
	return data
}



func timeStandata(str string)string{
	new_str := ""
	str = strings.ReplaceAll(str,".","-")
	if !timeReg1.MatchString(str) {
		return ""
	}
	strArr := strings.Split(str,"-")
	if len(strArr)!=3 {
		return ""
	}
	year :=strArr[0]
	month :=strArr[1]
	day :=strArr[2]
	if len(month)==1{
		month = "0"+month
	}
	if len(day)==1 {
		day = "0"+day
	}
	new_str = year+"-"+month+"-"+day
	if !timeReg.MatchString(new_str) {
		return ""
	}
	return new_str
}