package main

import (
	qu "qfw/util"
	"strings"
	"unicode/utf8"
)

var element_reason map[string]interface{}

func dealWithElementRate(tmp map[string]interface{}) (int,int,int,map[string]interface{}) {

	//score_standard 打分标准  要素打分 - 需慎重
	element_reason = map[string]interface{}{}
	m,n,z :=0,0,0
	core_value,other_value,deduct_value :="","",""

	//第一次-配置验证
	coreArr,otherArr:=filterConfiguration(tmp)
	//细节过滤-验证
	new_coreArr,new_otherArr:=[]string{},[]string{}
	for _,v:=range coreArr{
		if v=="projectname"||v=="buyer"||v=="winner" {
			if isChinese(qu.ObjToString(tmp[v])) {
				new_coreArr = append(new_coreArr,v)
			}
		}else {
			new_coreArr = append(new_coreArr,v)
		}
	}

	for _,v:=range otherArr{
		if v=="agencyperson"||v=="buyerperson"||v=="winnerperson" {
			if isChinese(qu.ObjToString(tmp[v])) {
				new_otherArr = append(new_otherArr,v)
			}
		}else if v=="agencytel"||v=="buyertel"||v=="winnertel" {
			if !isChinese(qu.ObjToString(tmp[v])) {
				if isTelephone(qu.ObjToString(tmp[v])) {
					new_otherArr = append(new_otherArr,v)
				}
			}
		}else if v=="bidopentime"||v=="signaturedate" {
			if isTimestamp(qu.Int64All(tmp[v])) {
				new_otherArr = append(new_otherArr,v)
			}
		}else if v=="bidopenaddress"||v=="winneraddr"||v=="agencyaddr"||
			v=="buyeraddr" {
			if isPlaceAddr(qu.ObjToString(tmp[v])) {
				new_otherArr = append(new_otherArr,v)
			}
		}else if v=="projectaddr" {
			if isChinese(qu.ObjToString(tmp[v])) {
				new_otherArr = append(new_otherArr,v)
			}
		}else {
			new_otherArr = append(new_otherArr,v)
		}
	}

	core_value = strings.Join(new_coreArr, ",")
	m = len(new_coreArr)

	other_value = strings.Join(new_otherArr, ",")
	n = len(new_otherArr)



	//扣分项
	for _,v:=range deduct_element{
		if qu.ObjToString(tmp[v])=="" {
			z--
			if deduct_value == "" {
				deduct_value = v
			}else {
				deduct_value = deduct_value+","+v
			}
		}
	}

	total_s,core_s,other_s:=calculateScore(m,n,z)
	return total_s,core_s,other_s,map[string]interface{}{
		"coreElement":map[string]interface{}{
			"key":core_value,
		},
		"otherElement":map[string]interface{}{
			"key":other_value,
		},
		"deductElement":map[string]interface{}{
			"key":deduct_value,
		},
	}
}

func filterConfiguration(tmp map[string]interface{})([]string,[]string)  {
	coreArr ,otherArr:= []string{},[]string{}
	//核心要素   int - 时间-   float-金额区间  string-字符串长度
	for _,v:=range core_element{
		for k1,v1:=range v{
			if tmp[k1]==nil {
				continue
			}
			dict :=*qu.ObjToMap(v1)
			element_type := qu.ObjToString(dict["type"])

			if element_type=="int" {
				min:=qu.IntAll(dict["min"])
				if qu.IntAll(tmp[k1])>min {
					coreArr = append(coreArr,k1)
				}
			}else if element_type=="float" {
				min:=qu.Float64All(dict["min"])
				max:=qu.Float64All(dict["max"])
				if qu.Float64All(tmp[k1])>min && qu.Float64All(tmp[k1])<max{
					coreArr = append(coreArr,k1)
				}
			}else if element_type=="string" {
				min:=qu.IntAll(dict["min"])
				max:=qu.IntAll(dict["max"])

				if utf8.RuneCountInString(qu.ObjToString(tmp[k1]))>min &&
					utf8.RuneCountInString(qu.ObjToString(tmp[k1]))<max{
					coreArr = append(coreArr,k1)
				}
			}else {

			}
		}
	}


	for _,v:=range other_element{
		for k1,v1:=range v{
			if tmp[k1]==nil {
				continue
			}
			dict :=*qu.ObjToMap(v1)
			element_type := qu.ObjToString(dict["type"])

			if element_type=="int" {
				min:=qu.IntAll(dict["min"])
				if qu.IntAll(tmp[k1])>min {
					otherArr = append(otherArr,k1)
				}
			}else if element_type=="float" {
				min:=qu.Float64All(dict["min"])
				max:=qu.Float64All(dict["max"])
				if qu.Float64All(tmp[k1])>min && qu.Float64All(tmp[k1])<max{
					otherArr = append(otherArr,k1)
				}
			}else if element_type=="string" {
				min:=qu.IntAll(dict["min"])
				max:=qu.IntAll(dict["max"])

				if utf8.RuneCountInString(qu.ObjToString(tmp[k1]))>min &&
					utf8.RuneCountInString(qu.ObjToString(tmp[k1]))<max{
					otherArr = append(otherArr,k1)
				}
			}else {

			}
		}
	}

	return coreArr,otherArr
}




func calculateScore(core_num int,other_num int,deduct_num int) (int,int,int)  {

	m ,core_s:=core_each*core_num,core_each*core_num
	if m>core_max {
		m = core_max
	}

	n ,other_s:=other_each*other_num,other_each*other_num

	if n > other_max {
		n = other_max
	}

	z := deduct_each*deduct_num
	t :=m+n+z
	if t > total_score {
		t=total_score
	}

	return t,core_s,other_s
}