package main

import (
	"github.com/strsim"
	"go.mongodb.org/mongo-driver/bson/primitive"
	qu "qfw/util"
	"strings"
	"unicode/utf8"
)

//前置判断
func prejudgmentListInfo(update *map[string]interface{}, tmp map[string]interface{}) {
	p_list := IsMarkInterfaceMap(tmp["purchasinglist"])
	logArr := []map[string]interface{}{}
	if len(p_list) == 1 {
		itemname_old := qu.ObjToString(p_list[0]["itemname"])
		itemname_new := CleanItemReg3.ReplaceAllString(itemname_old, "")
		itemname_new = CleanItemReg4.ReplaceAllString(itemname_new, "")
		logArr = append(logArr, map[string]interface{}{
			"itemname_old": itemname_old,
			"itemname_new": itemname_new,
		})
		if RulePreReg1.MatchString(itemname_new) {
			(*update)["class_code"] = "83"
			(*update)["class_name"] = PurCodeInfo["83"]
			(*update)["class_level"] = 0
			if IsSaveLog {
				(*update)["class_link"] = "前置筛选"
				(*update)["class_log"] = logArr
			}
		}
	}
}

//处理标讯的标的物
func acquirePlistClassInfo(update *map[string]interface{}, tmp map[string]interface{}) {
	p_list := IsMarkInterfaceMap(tmp["purchasinglist"])
	basic := qu.ObjToString(tmp["basicClass"])
	codeArr := []string{}
	logArr := []map[string]interface{}{}
	for _, v := range p_list {
		itemname := qu.ObjToString(v["itemname"])
		logInfo := map[string]interface{}{}
		logInfo["itemname"] = itemname
		if itemname != "" && utf8.RuneCountInString(itemname) <= PurLength {
			info := processPurNameClass(itemname, basic) //标的打分类
			if len(info) > 0 {
				class_code := qu.ObjToString(info["class_code"])
				if !strings.Contains(class_code, "~") {
					codeArr = append(codeArr, class_code)
				}
				logInfo["matching_name"] = qu.ObjToString(info["matching_name"])
				logInfo["class_code"] = class_code
			}
		}
		logArr = append(logArr, logInfo)
	}
	if IsSaveLog {
		(*update)["class_log"] = logArr
	}
	class_code, class_name := standardCodeArr(codeArr)
	(*update)["class_code"] = class_code
	(*update)["class_name"] = class_name
	if class_code != "" {
		(*update)["class_level"] = 5
	}
}

//补充逻辑规则
func replenishRuleClassInfo(update *map[string]interface{}, tmp map[string]interface{}) {
	//项目名称补充
	r_code, r_name, r_match := checkDetailClass(qu.ObjToString(tmp["projectname"]))
	if r_code != "" {
		(*update)["class_code"] = r_code
		(*update)["class_name"] = r_name
		(*update)["class_level"] = 4
		if IsSaveLog {
			(*update)["class_link"] = "项目名称补充"
			(*update)["class_link_matching"] = r_match
		}
	}

	//标题名称补充
	if r_code == "" {
		r_code, r_name, r_match = checkDetailClass(qu.ObjToString(tmp["title"]))
		if r_code != "" {
			(*update)["class_code"] = r_code
			(*update)["class_name"] = r_name
			(*update)["class_level"] = 3
			if IsSaveLog {
				(*update)["class_link"] = "标题名称补充"
				(*update)["class_link_matching"] = r_match
			}
		}
	}

	//行业类别补充
	if r_code == "" {
		r_code, r_name = checkTopScopeClass(qu.ObjToString(tmp["s_subscopeclass"]), 2)
		if r_code != "" {
			(*update)["class_code"] = r_code
			(*update)["class_name"] = r_name
			(*update)["class_level"] = 2
			if IsSaveLog {
				(*update)["class_link"] = "行业二级补充"
			}
		} else {
			r_code, r_name = checkTopScopeClass(qu.ObjToString(tmp["s_topscopeclass"]), 1)
			if r_code != "" {
				(*update)["class_code"] = r_code
				(*update)["class_name"] = r_name
				(*update)["class_level"] = 2
				if IsSaveLog {
					(*update)["class_link"] = "行业一级补充"
				}
			}
		}
	}
}

//标准化后code
func standardCodeArr(codeArr []string) (string, string) {
	new_code := ""
	new_name := ""
	new_codeArr := []string{}
	new_nameArr := []string{}
	temp := map[string]string{}
	for _, v := range codeArr {
		if temp[v] == "" {
			new_codeArr = append(new_codeArr, v)
			new_nameArr = append(new_nameArr, PurCodeInfo[v])
			temp[v] = v
		}
	}
	new_code = strings.Join(new_codeArr, "~")
	new_name = strings.Join(new_nameArr, "~")
	return new_code, new_name
}

//行业补充词
func checkTopScopeClass(text string, level int) (string, string) {
	t_code := TopScopeClass[text]
	if t_code != "" {
		return t_code, PurCodeInfo[t_code]
	}
	if text == "" {
		return "", ""
	}
	if level == 2 { //计算比例
		temp := map[string]int{}
		for _, v := range strings.Split(text, ",") {
			key := strings.Split(v, "_")[0]
			num := qu.IntAll(temp[key]) + 1
			temp[key] = num
		}
		//找到最高次数
		key_name := ""
		count := 0
		for k, v := range temp {
			if v > count {
				key_name = k
				count = v
			} else if v == count {
				key_name = ""
			} else {

			}
		}
		if key_name == "信息技术" {
			if strings.Contains(key_name, "软件开发") {
				return "61~62", PurCodeInfo["61"] + "~" + PurCodeInfo["62"]
			} else {
				return "61", PurCodeInfo["61"]
			}
		} else if key_name == "交通工程" || key_name == "弱电安防" || key_name == "市政设施" ||
			key_name == "水利水电" || key_name == "机械设备" {
			r_code := TopScopeClass[key_name]
			if r_code != "" {
				return r_code, PurCodeInfo[r_code]
			}
		}
	}
	return "", ""
}

//文本补充词
func checkDetailClass(text string) (string, string, string) {
	new_code, new_name, new_match := "", "", ""
	dataArr := SensitiveInfo.FindAll(text)
	codeArr, matchArr := []string{}, []string{}
	nameArr := []string{}
	tempInfo := map[string]string{}
	for _, v := range dataArr {
		info := PurClassInfo[v]
		code := info.code
		if tempInfo[code] == "" {
			tempInfo[code] = code
			codeArr = append(codeArr, code)
			matchArr = append(matchArr, v)
			nameArr = append(nameArr, PurCodeInfo[code])
		}
	}
	new_code = strings.Join(codeArr, "~")
	new_name = strings.Join(nameArr, "~")
	new_match = strings.Join(matchArr, "~")

	return new_code, new_name, new_match
}

//相似度计算
func similarPurClass(update *map[string]interface{}, tmp map[string]interface{}) {
	basicClass := qu.ObjToString(tmp["basicClass"])
	if basicClass == "" {
		return
	}
	basicArr := BasicClassNameInfo[basicClass]
	if len(basicArr) > 0 {
		p_list := IsMarkInterfaceMap(tmp["purchasinglist"])
		codeArr := []string{}
		logArr := []map[string]interface{}{}
		for _, v := range p_list {
			itemname := qu.ObjToString(v["itemname"])
			logInfo := map[string]interface{}{}
			logInfo["itemname"] = itemname
			if itemname != "" && utf8.RuneCountInString(itemname) <= PurLength {
				textArr := cleanItemName(itemname)
				if len(textArr) == 0 {
					continue
				}
				for _, text := range textArr {
					req := strsim.FindBestMatchOne(text, basicArr, strsim.DiceCoefficient())
					if req.Score >= 0.5 { //认为有效
						req_code := PurClassInfo[req.S].code
						if req_code != "" {
							logInfo["matching_name"] = req.S
							codeArr = append(codeArr, req_code)
							logInfo["class_code"] = req_code
							break
						}
					}
				}
			}
			logArr = append(logArr, logInfo)
		}
		if len(codeArr) > 0 {
			class_code, class_name := standardCodeArr(codeArr)
			(*update)["class_code"] = class_code
			(*update)["class_name"] = class_name
			(*update)["class_level"] = 1
			if IsSaveLog {
				(*update)["class_similar"] = logArr
			}
		}
	}
}

//标的物分类计算
func dealWithPurInfo(info map[string]interface{}, classArr []string) map[string]interface{} {
	new_info := map[string]interface{}{}
	name := qu.ObjToString(info["name"])
	total := qu.IntAll(info["total"])
	class := qu.ObjToString(info["class"])
	newArr := []string{}
	if class != "" {
		newArr = strings.Split(class, ",")
	}
	if len(classArr) > 0 {
		for _, v := range classArr {
			isExists := false
			for _, v1 := range newArr {
				if v1 == v {
					isExists = true
					break
				}
			}
			if !isExists {
				newArr = append(newArr, v)
			}
		}
	}
	new_class := ""
	if len(newArr) > 0 {
		new_class = strings.Join(newArr, ",")
	}
	new_info["name"] = name
	new_info["total"] = total + 1
	new_info["class"] = new_class
	return new_info
}

//服务类校验
func verifyServiceCode(code string) bool {
	dataArr := BasicClassCodeInfo["服务"]
	for _, v := range dataArr {
		if v == code[:2] {
			return true
		}
	}
	return false
}

//通用转换
func IsMarkInterfaceMap(t interface{}) []map[string]interface{} {
	p_list := []map[string]interface{}{}
	if yl_list_1, ok_1 := t.(primitive.A); ok_1 {
		p_list = qu.ObjArrToMapArr(yl_list_1)
	} else {
		if yl_list_2, ok_2 := t.([]interface{}); ok_2 {
			p_list = qu.ObjArrToMapArr(yl_list_2)
		}
	}
	return p_list
}
