package main

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

//处理产品信息~
func dealWithProductInfo() {

	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C("zktest_mysql_product_info").Find(&q).Sort("_id").Iter()
	pool := make(chan bool, 5)
	wg := &sync.WaitGroup{}
	total, isok := 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total, isok)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()

			data := treatProductInfo(tmp)
			if data != nil {
				datalock.Lock()
				isok++
				datalock.Unlock()
			}
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()

	log.Debug("is over ", total, isok)

}

//构建产品信息数据~
func treatProductInfo(tmp map[string]interface{}) map[string]interface{} {
	data := map[string]interface{}{}
	data["regno"] = qu.ObjToString(tmp["regno"])
	data["company_name"] = qu.ObjToString(tmp["company_name"])
	data["company_address"] = qu.ObjToString(tmp["company_address"])
	data["make_adress"] = qu.ObjToString(tmp["make_adress"])
	data["make_country"] = qu.ObjToString(tmp["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["main_part"] = qu.ObjToString(tmp["main_part"])
	data["product_descrip"] = qu.ObjToString(tmp["product_descrip"])
	data["use_scope"] = qu.ObjToString(tmp["use_scope"])
	data["storage_conditions"] = qu.ObjToString(tmp["storage_conditions"])
	data["remark"] = qu.ObjToString(tmp["remark"])
	data["approve_depart"] = qu.ObjToString(tmp["approve_depart"])

	//日期清洗
	approve_date := qu.ObjToString(tmp["approve_date"])
	validity_date := qu.ObjToString(tmp["validity_date"])
	if !cleanErrDateTime(approve_date) {
		data["approve_date"] = approve_date
	}
	if !cleanErrDateTime(validity_date) {
		data["validity_date"] = validity_date
	}

	data["changes"] = qu.ObjToString(tmp["changes"])
	data["regist_type"] = qu.ObjToString(tmp["regist_type"])
	data["source_channel"] = qu.ObjToString(tmp["source_channel"])
	data["manage_type"] = qu.ObjToString(tmp["manage_type"])
	data["product_name"] = qu.ObjToString(tmp["product_name"])

	data["medical_equipment_class1"] = qu.ObjToString(tmp["product_class1"])
	data["medical_equipment_class2"] = qu.ObjToString(tmp["product_class2"])
	data["medical_equipment_class3"] = qu.ObjToString(tmp["product_class3"])
	data["sdproduct_name"] = qu.ObjToString(tmp["product_class4"])

	//需要清洗
	data["medical_equipment_code"] = qu.ObjToString(tmp["product_code"])
	data["Isvalid"] = true

	return data
}

//清洗日期~
func cleanErrDateTime(datetime string) bool {
	/*
		1、有效年1900 ~  2200年
	*/
	arr := strings.Split(datetime, "-")
	if len(arr) > 0 {
		year := qu.IntAll(arr[0])
		if year < 1900 || year > 2200 {
			return true
		}
	}
	return false
}
