package main

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

var data_hospitals = map[string][]map[string]interface{}{}

//整合医院数据
func confirmHospitalData(save_coll string) {
	//处理~医院信息
	log.Debug("整合医院信息~", len(data_hospitals))
	for _, v := range data_hospitals {
		data := mergeHospitalInfo(v)
		save_mgo.Save(save_coll, data)
	}
	log.Debug("待判重数据存储完毕~")
	time.Sleep(10 * time.Second)
	repeatHospital(save_coll)
}

//判重~医院数据
func repeatHospital(save_coll string) {
	log.Debug("开始判重医院数据...")
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	data_hospitals_alias := make(map[string][]string, 0)
	data_hospitals_id := make(map[string]string, 0)
	data_hospitals_region := make(map[string]map[string]string, 0)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C(save_coll).Find(&q).Sort("_id").Select(map[string]interface{}{
		"name":  1,
		"alias": 1,
	}).Iter()
	total, isok := 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%1000 == 0 {
			log.Debug("cur index ", total, "~", isok)
		}
		region := map[string]string{
			"area":     qu.ObjToString(tmp["area"]),
			"city":     qu.ObjToString(tmp["city"]),
			"district": qu.ObjToString(tmp["district"]),
		}
		alias := qu.ObjToString(tmp["alias"])
		name := qu.ObjToString(tmp["name"])
		alias_arr := strings.Split(alias, "，")
		alias_arr = append(alias_arr, name)

		//减少重复...待

		tmpid := BsonTOStringId(tmp["_id"])
		if data_hospitals_alias[name] == nil {
			//对比名称~
			is_r := false
			is_r_n := ""
			for _, name_new := range alias_arr {
				if name_new == "" {
					continue
				}
				for k, v := range data_hospitals_alias {
					//地域不一致~过滤
					if !repeatRegion(data_hospitals_region[k], region) {
						continue
					}
					for _, v1 := range v {
						if v1 == "" {
							continue
						}
						if strings.Contains(v1, name_new) || strings.Contains(name_new, v1) {
							is_r = true
							is_r_n = k
							break
						}
					}
					if is_r {
						break
					}
				}
				if is_r {
					break
				}
			}
			if is_r {
				isok++
				if is_r_n != "" {

				}
				save_mgo.UpdateById(save_coll, tmpid, map[string]interface{}{
					"$set": map[string]interface{}{
						"repeat":    1,
						"repeat_id": data_hospitals_id[is_r_n],
					},
				})
			}
			data_hospitals_id[name] = tmpid
			data_hospitals_region[name] = region
			data_hospitals_alias[name] = alias_arr
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ", total, "~", isok)

	//合并判重后的数据
	mergeRepeatHospital(save_coll)
}

//最终合并判重后的医院数据
func mergeRepeatHospital(save_coll string) {
	log.Debug("开始合并重复后的数据~待")
	return
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C(save_coll).Find(&q).Sort("_id").Iter()
	total, isok := 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%1000 == 0 {
			log.Debug("cur index ", total, "~", isok)
		}
		//最终保存

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

//处理医院基本信息~涉及多张表
func dealWithHospitalBaseInfo(hos_coll string, depart_coll string) {
	log.Debug("处理医疗~医院基本信息")
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(save_mgo.DbName).C(hos_coll).Find(&q).Sort("_id").Iter()
	pool := make(chan bool, 6)
	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)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()
			name := qu.ObjToString(tmp["hospitalname"])
			if name != "" && utf8.RuneCountInString(name) > 3 {
				//医院基本信息
				data_info := treatHospitalInfo(tmp)
				name = qu.ObjToString(data_info["name"])
				//科室相关信息
				hos_id := qu.ObjToString(tmp["_id"])
				data_depart := treatHospitalDepartment(hos_id, depart_coll)
				data_info["departs"] = data_depart
				//别名信息
				data_info["alias"] = qu.ObjToString(tmp["alias"])

				datalock.Lock()
				if data_hospitals[name] == nil {
					arr := []map[string]interface{}{data_info}
					data_hospitals[name] = arr
					isok++
				} else {
					arr := data_hospitals[name]
					arr = append(arr, data_info)
					data_hospitals[name] = arr
				}
				datalock.Unlock()
			}
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()
	log.Debug("is cur over ", len(data_hospitals), "~", total, "~", isok)
}

//数据构建~医院基本信息
func treatHospitalInfo(tmp map[string]interface{}) map[string]interface{} {
	data := map[string]interface{}{}
	hospitalname := qu.ObjToString(tmp["hospitalname"])
	hospitalname = strings.ReplaceAll(hospitalname, " ", "")
	//医院名称~
	data["name"] = hospitalname
	//医院简介
	data["introduce"] = qu.ObjToString(tmp["introduce"])

	//医院地址
	data["address"] = qu.ObjToString(tmp["address"])
	//医院设备
	data["equipment"] = qu.ObjToString(tmp["equipment"])
	//床位数~
	beds := qu.IntAll(tmp["beds"])
	if beds > 0 {
		data["beds"] = beds
	}
	//门诊量/日
	visit_perday := qu.IntAll(tmp["visit_perday"])
	if visit_perday > 0 {
		data["visit_perday"] = visit_perday
	}
	//员工人数
	doctorsnum := qu.IntAll(tmp["doctorsnum"])
	if doctorsnum > 0 {
		data["doctorsnum"] = doctorsnum
	}
	//联系电话~
	data["tel"] = qu.ObjToString(tmp["tel"])
	//传真号码
	data["fax_number"] = qu.ObjToString(tmp["fax_number"])
	//医院网站
	data["website"] = qu.ObjToString(tmp["website"])

	//等级~性质~代码
	//n_level, n_type, n_bus_type := inquirHospitalLevelType(qu.ObjToString(tmp["level"]), qu.ObjToString(tmp["type"]), qu.ObjToString(tmp["business_type"]))
	//data["level_code"] = n_level
	//data["mi_type_code"] = n_type
	//data["business_type"] = qu.IntAll(n_bus_type)
	data["level"] = qu.ObjToString(tmp["level"])
	data["type"] = qu.ObjToString(tmp["type"])
	data["business_type"] = qu.ObjToString(tmp["business_type"])

	//省~市~区 ~可完善
	area := qu.ObjToString(tmp["area"])
	city := qu.ObjToString(tmp["city"])
	district := qu.ObjToString(tmp["district"])
	if city == "" { //补充~省份城市信息
		//supplementRegionally(&area, &city, &district, hospitalname, qu.ObjToString(tmp["address"]))
	}
	data["area"] = area
	data["city"] = city
	data["district"] = district

	company_id := ""
	//关联企业信息~查询顺序~企业~特殊~自生
	//company_id = inquirBaseInfoid(hospitalname)
	data["company_id"] = company_id

	//入库时间
	//data["comeintime"] = time.Unix(qu.Int64All(tmp["comeintime"]), 0).Format(timeLayout)
	//更新时间
	//data["updatetime"] = time.Unix(qu.Int64All(tmp["updatetime"]), 0).Format(timeLayout)

	return data
}

//数据构建~医院科室信息
func treatHospitalDepartment(hos_id string, depart_coll string) []map[string]interface{} {
	new_arr := []map[string]interface{}{}
	if depart_coll == "" {
		return new_arr
	}
	dataArr, _ := save_mgo.Find(depart_coll, map[string]interface{}{"hospital_id": hos_id}, nil, nil)
	//考虑去重~相同科室
	keys := map[string]string{}
	for _, v := range dataArr {
		main_departclass1 := qu.ObjToString(v["main_departclass1"])
		main_departclass2 := qu.ObjToString(v["main_departclass2"])
		key := main_departclass1 + "~" + main_departclass2
		if keys[key] == "" {
			new_arr = append(new_arr, map[string]interface{}{
				"departclass1": main_departclass1,
				"departclass2": main_departclass2,
			})
			keys[key] = key
		} else {
			//log.Debug("重复科室~",main_departclass1,main_departclass2)
		}
	}
	return new_arr
}

//合并医院信息~
func mergeHospitalInfo(dataArr []map[string]interface{}) map[string]interface{} {
	data := map[string]interface{}{}
	if len(dataArr) == 1 {
		return dataArr[0]
	} else if len(dataArr) > 1 {
		//合并重复信息
		temp := dataArr[0]
		for k, v := range temp {
			isMerge := false
			//合并别名~

			//合并科室信息

			//选取最优值
			if new_v, ok := v.(string); ok {
				if new_v == "" { //查询其他数据~补充
					for ks, vs := range dataArr {
						if ks == 0 {
							continue
						}
						if qu.ObjToString(vs[k]) != "" {
							data[k] = vs[k]
							isMerge = true
							break
						}
					}
				}
			}
			if new_v, ok := v.(int); ok {
				if new_v == 0 { //查询其他数据~补充
					for ks, vs := range dataArr {
						if ks == 0 {
							continue
						}
						if qu.IntAll(vs[k]) > 0 {
							data[k] = vs[k]
							isMerge = true
							break
						}
					}
				}
			}
			if !isMerge {
				data[k] = v
			}
		}
	} else {

	}
	return data
}
