package main

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

//导出产品信息相关
func exportProductBaseInfo() {
	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).Select(map[string]interface{}{
		"dealer_name": 1,
		"company_id":  1,
		"name_id":     1,
	}).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); {
		if total%10000 == 0 {
			log.Debug("curent index ", total)
		}
		num := createProductBaseInfo(tmp)
		total += num
		tmp = make(map[string]interface{})
	}
	log.Debug("产品信息~~~", total)
}

func createProductBaseInfo(dealerInfo map[string]interface{}) int {
	num := 0
	dealer_name := qu.ObjToString(dealerInfo["dealer_name"])
	infoArr, _ := Save_Mgo.Find("nmpa_product_new", bson.M{"reg_company": dealer_name}, nil, nil)
	for _, v := range infoArr {
		num++
		info := dealWithProductInfo(v, dealerInfo)
		Save_Mgo.Save("nmpa_product_baseinfo", info)
	}
	return num
}

//信息处理
func dealWithProductInfo(tmp map[string]interface{}, dealerInfo map[string]interface{}) map[string]interface{} {
	info := map[string]interface{}{}
	info["dealer_id"] = qu.ObjToString(dealerInfo["name_id"])
	info["company_id"] = qu.ObjToString(dealerInfo["company_id"])
	info["company_name"] = qu.ObjToString(dealerInfo["dealer_name"])
	classify, reg_no := qu.ObjToString(tmp["classify"]), qu.ObjToString(tmp["reg_no"])
	info["regno"] = reg_no
	info["company_address"] = qu.ObjToString(tmp["reg_address"])
	info["make_adress"] = qu.ObjToString(tmp["product_address"])

	if strings.Contains(classify, "进口") || strings.Contains(reg_no, "国") {
		info["make_country"] = 2
	} else {
		info["make_country"] = 1
	}
	info["agent_name"] = qu.ObjToString(tmp["agent_name"])
	info["agent_address"] = qu.ObjToString(tmp["agent_address"])
	info["brand"] = ""
	info["model"] = qu.ObjToString(tmp["model"])
	if strings.Contains(classify, "注册") || strings.Contains(reg_no, "注") {
		info["main_part"] = qu.ObjToString(tmp["product_depict"])
		info["main_part"] = ""
		info["regist_type"] = 1
	} else {
		info["main_part"] = ""
		info["product_descrip"] = qu.ObjToString(tmp["product_depict"])
		info["regist_type"] = 2
	}
	info["use_scope "] = qu.ObjToString(tmp["application"])
	info["storage_conditions"] = qu.ObjToString(tmp["conditions_use"])
	info["remark "] = qu.ObjToString(tmp["remark"])
	info["approve_depart"] = pickfirst(qu.ObjToString(tmp["spbm"]), qu.ObjToString(tmp["badw"]))
	info["approve_date"] = pickfirst(qu.ObjToString(tmp["pzrq"]), qu.ObjToString(tmp["barq"]))
	info["validity_date"] = qu.ObjToString(tmp["yxqz"])
	info["changes"] = qu.ObjToString(tmp["changes"])
	info["source_channel"] = classify
	info["changes"] = qu.ObjToString(tmp["changes"])
	if qu.ObjToString(tmp["class"]) == "第一类" {
		info["manage_type"] = "Ⅰ"
	} else if qu.ObjToString(tmp["class"]) == "第二类" {
		info["manage_type"] = "Ⅱ"
	} else {
		info["manage_type"] = "Ⅲ"
	}

	//产品相关信息
	info["product_name"] = qu.ObjToString(tmp["product_name"])
	info["medical_equipment_class1"] = qu.ObjToString(tmp["product_class_1"])
	info["medical_equipment_class2"] = qu.ObjToString(tmp["product_class_2"])
	info["medical_equipment_class3"] = qu.ObjToString(tmp["product_class_3"])
	info["sdproduct_name"] = qu.ObjToString(tmp["product_class_4"])
	info["sdequipment_code"] = qu.ObjToString(tmp["product_class_code"])

	return info
}
