package main

import (
	"encoding/json"
	log "github.com/donnie4w/go-logger/logger"
	"io/ioutil"
	"net/http"
	qu "qfw/util"
	"regexp"
	"strings"
)

var med_lev_Reg1 = regexp.MustCompile("^([一二三])([甲乙丙特])$")
var med_lev_Reg2 = regexp.MustCompile("^([一二三])(级)(医院)?$")

//类型 等级 性质  ~ 综合清洗分析验证
func inquirHospitalLevelType(med_level string, med_type string, med_bus_type string) (string, string, string) {
	new_level, new_type, new_bus_type := "", "", ""
	//医疗性质相关
	if med_bus_type != "" {
		new_bus_type = relevanceBusType(med_bus_type)
		//针对~类型划分
		arr := strings.Split(med_bus_type, "/")
		if len(arr) == 2 {
			new_type = relevanceType(arr[1])
		}
	}
	//医疗等级相关
	if med_level != "" {
		new_level = relevanceLevel(med_level)
		//针对~性质划分
		new_bus_type = relevanceBusType(med_bus_type)
	}

	//医疗类型相关~采集异常~需要清洗
	if med_type != "" && new_type == "" {

		//医疗类型
		new_type = relevanceType(med_type)

		if new_level == "" {
			new_level = relevanceLevel(new_type)
		}
		if new_bus_type == "" {
			new_bus_type = relevanceBusType(new_type)
		}
	}

	//最终核对
	if new_level == "" {
		new_level = medical_level["其它"]
	}
	if new_type == "" {
		new_type = medical_type["其它"]
		log.Debug(new_type)
	}
	if new_bus_type == "" {
		new_bus_type = "2"
	}
	return new_level, new_type, new_bus_type
}

// level 医疗等级对比
func relevanceLevel(med_level string) string {
	new_level := ""
	if medical_level[med_level] != "" {
		new_level = medical_level[med_level]
	} else { //特殊描述映射关系
		if med_lev_Reg1.MatchString(med_level) {
			med_level = med_lev_Reg1.ReplaceAllString(med_level, "${1}级${2}等")
			new_level = medical_level[med_level]
		}
		if med_lev_Reg2.MatchString(med_level) {
			med_level = med_lev_Reg2.ReplaceAllString(med_level, "${1}${2}其它")
			new_level = medical_level[med_level]
		}
	}
	return new_level
}

//type 类型划分
func relevanceType(med_type string) string {
	new_type := ""
	//扩展~包含关系~等等规则
	new_type = medical_type[med_type]
	return new_type
}

//bus_type 性质划分
func relevanceBusType(med_bus_type string) string {
	new_bus_type := ""
	//0公立、1民营、2其它
	if strings.Contains(med_bus_type, "公立") {
		new_bus_type = "0"
	}
	if strings.Contains(med_bus_type, "民营") {
		new_bus_type = "1"
	}
	return new_bus_type
}

//查询企业id
func inquirBaseInfoid(name string) (company_id string) {
	company_id = ""
	query := map[string]interface{}{
		"company_name": name,
	}
	data_base := spi_mgo.FindOne("company_base", query)
	if data_base != nil && len(data_base) > 2 {
		company_id = qu.ObjToString(data_base["company_id"])
	} else {
		data_spec := spi_mgo.FindOne("special_enterprise", query)
		if data_spec != nil && len(data_spec) > 2 {
			company_id = qu.ObjToString(data_spec["company_id"])
		}
	}
	return
}

//补充~地域区划
func supplementRegionally(area *string, city *string, district *string, name string, address string) {
	//查询其他表~
	if *area == "" || *area == "全国" {
		*area = "全国"
		*city = ""
		*district = ""
	}
	data := save_mgo.FindOne("data_info", map[string]interface{}{
		"name": name,
	})
	if data != nil && len(data) > 2 {
		data_area := qu.ObjToString(data["area"])
		data_city := qu.ObjToString(data["city"])
		data_district := qu.ObjToString(data["district"])
		if data_district != "" {
			*area = data_area
			*city = data_city
			*district = data_district
		} else {
			if data_city != "" {
				*area = data_area
				*city = data_city
			} else {
				if data_area != "" && data_area != "全国" {
					*area = data_area
				}
			}
		}
	}
	if *city != "" {
		return
	}

	//地址字段抽取~请求接口
	resp, err := http.Get("http://extcity.spdata.jianyu360.com/getcity?detail=" + address)
	if err == nil {
		defer resp.Body.Close()
		bs, _ := ioutil.ReadAll(resp.Body)
		var rep map[string]interface{}
		json.Unmarshal(bs, &rep)

		rep_area := qu.ObjToString(rep["area"])
		rep_city := qu.ObjToString(rep["city"])
		rep_district := qu.ObjToString(rep["district"])

		if rep_district != "" {
			*area = rep_area
			*city = rep_city
			*district = rep_district
		} else {
			if rep_city != "" {
				*area = rep_area
				*city = rep_city
			} else {
				if rep_area != "" && rep_area != "全国" {
					*area = rep_area
				}
			}
		}
	}
}

//是否相同地域
func repeatRegion(region map[string]string, data map[string]string) bool {
	if region["district"] != "" && data["district"] != "" &&
		region["district"] != data["district"] {
		return false
	}
	if region["city"] != "" && data["city"] != "" &&
		region["city"] != data["city"] {
		return false
	}
	if region["area"] != "" && data["area"] != "" &&
		region["area"] != data["area"] &&
		region["area"] != "全国" && data["area"] != "全国" {
		return false
	}

	return true
}
