package main

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

//处理企业基本信息
func dealWithCompanyInfo() {
	//来源mgo~181
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C("zktest_mysql_company_info").Find(&q).Sort("_id").Iter()
	pool := make(chan bool, 1)
	wg := &sync.WaitGroup{}
	total, isok1, isok2 := 0, 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%100 == 0 {
			log.Debug("cur index ", total, isok1, isok2)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()
			company_name := qu.ObjToString(tmp["company_name"])
			business_type := qu.ObjToString(tmp["business_type"])
			sc_info, jy_info := treatCompanyTypeInfo(company_name, business_type)

			datalock.Lock()
			isok1 += len(sc_info)
			isok2 += len(jy_info)
			datalock.Unlock()
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()

	log.Debug("is over ", total, isok1, isok2)
}

//构建产品信息~
func treatCompanyTypeInfo(company_name string, company_type string) ([]map[string]interface{}, []map[string]interface{}) {
	sc_info := []map[string]interface{}{}
	jy_info := []map[string]interface{}{}
	arr := *MysqlTool.Find("v_bidclass", nil, "", "", -1, -1)
	log.Debug(arr)
	//关联的有product.id 需要生成产品表
	dataArr := *MysqlTool.Find("f_product", map[string]interface{}{
		"company_name": company_name,
	}, "id,product_name,approve_date,company_id,medical_equipment_code", "", -1, -1)
	//dataArr,_ := save_mgo.Find("zktest_mysql_product_info", map[string]interface{}{
	//	"company_name":company_name,
	//},nil,nil)

	//是否过滤~信息~根绝名称去重~选取一个有效的信息
	keys := map[string]map[string]interface{}{}
	new_dataArr := make([]map[string]interface{}, 0)
	for _, data := range dataArr {
		product_name := qu.ObjToString(data["product_name"])
		if keys[product_name] == nil {
			new_dataArr = append(new_dataArr, data)
			keys[product_name] = data
		} else {
			pre_v := keys[product_name]
			pre_approve_date := qu.ObjToString(pre_v["approve_date"])
			cur_approve_date := qu.ObjToString(data["approve_date"])
			pre_approve_time, _ := time.Parse(timeTmeplate, pre_approve_date)
			cur_approve_time, _ := time.Parse(timeTmeplate, cur_approve_date)
			if cur_approve_time.Unix() > pre_approve_time.Unix() {
				keys[product_name] = data
				//替换数据
				index := 0
				isreplace := false
				for k, v := range new_dataArr {
					if qu.ObjToString(v["product_name"]) == product_name {
						index = k
						isreplace = true
						break
					}
				}
				if isreplace {
					new_dataArr[index] = data
				}
			}
		}
	}

	for _, v := range dataArr {
		dict := map[string]interface{}{}
		dict["company_id"] = qu.ObjToString(v["company_id"])
		dict["product_id"] = qu.ObjToString(v["id"])
		dict["medical_equipment_code"] = qu.ObjToString(v["medical_equipment_code"])
		if company_type == "1" { //生产
			sc_info = append(sc_info, dict)
		} else if company_type == "2" { //经营
			jy_info = append(jy_info, dict)
		} else if company_type == "3" { //生产&经营
			sc_info = append(sc_info, dict)
			jy_info = append(jy_info, dict)
		}
	}
	return sc_info, jy_info
}
