package main

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

func processPurNameClass(name string, basic string) map[string]interface{} {
	update := map[string]interface{}{}
	//1、标的物名称进行清洗
	textArr := cleanItemName(name)
	if len(textArr) == 0 {
		return update
	}
	for _, text := range textArr {
		//2、完全匹配~唯一解
		if utf8.RuneCountInString(text) <= 1 {
			continue
		}
		ab_name, ab_code := absoluteMatchingMethod(text)
		if ab_name != "" && ab_code != "" {
			update["matching"] = 1
			update["matching_name"] = text
			update["class_name"] = ab_name
			update["class_code"] = ab_code
			return update
		}

		//标的物筛选
		if RulePreReg2.MatchString(name) && !RulePreReg3.MatchString(name) {
			update["matching"] = 3
			update["matching_name"] = "维保类"
			update["class_name"] = PurCodeInfo["8301"]
			update["class_code"] = "8301"
			return update
		} else {
			if RulePreReg4.MatchString(name) {
				update["matching"] = 3
				update["matching_name"] = "工程类"
				update["class_name"] = PurCodeInfo["4701"]
				update["class_code"] = "4701"
				return update
			}
		}

		//包含匹配~多个解
		in_name, in_code, in_match := inclusionMatchingMethod(text, basic)
		if in_name != "" && in_code != "" {
			//对包含匹配出来的结果进行服务校验
			if RulePreReg5.MatchString(text) {
				if verifyServiceCode(in_code) {
					update["matching"] = 2
					update["matching_name"] = in_match
					update["class_name"] = in_name
					update["class_code"] = in_code
				} else {
					update["matching"] = 2
					update["matching_name"] = "服务校验"
					update["class_name"] = PurCodeInfo["83"]
					update["class_code"] = "83"
				}
				return update
			}
			update["matching"] = 2
			update["matching_name"] = in_match
			update["class_name"] = in_name
			update["class_code"] = in_code
		} else {
			if RulePreReg5.MatchString(text) {
				update["matching"] = 2
				update["matching_name"] = "服务校验"
				update["class_name"] = PurCodeInfo["83"]
				update["class_code"] = "83"
			}
		}
	}
	return update
}

//清洗名称
func cleanItemName(text string) []string {
	textArr := []string{}
	if utf8.RuneCountInString(text) <= 1 {
		return textArr
	}
	if CleanItemReg1.MatchString(text) {
		return textArr
	}
	if CleanItemReg2.MatchString(text) {
		return textArr
	}
	if text == "电线" || text == "电缆" {
		textArr = append(textArr, "电线电缆")
		return textArr
	}
	//清洗
	if SplitItemReg1.MatchString(text) {
		text1 := SplitItemReg1.ReplaceAllString(text, "")
		text2 := SplitItemReg1.FindString(text)
		text2 = SplitItemReg2.ReplaceAllString(text2, "")
		if text1 != "" {
			textArr = append(textArr, text1)
		}
		if text2 != "" {
			textArr = append(textArr, text2)
		}
	} else {
		textArr = append(textArr, text)
	}
	return textArr
}

//完全匹配~比对
func absoluteMatchingMethod(text string) (string, string) {
	info := PurClassInfo[text]
	class_name, class_code := info.name, info.code
	return class_name, class_code
}

//包含匹配~比对
func inclusionMatchingMethod(text string, basic string) (string, string, string) {
	codeArr := []string{}
	nameArr := []string{}
	matchArr := []string{}
	temp := map[string]string{}
	for k, v := range PurClassInfo {
		if utf8.RuneCountInString(k) == 1 {
			continue
		}
		if strings.Contains(k, text) || strings.Contains(text, k) {
			if temp[v.code] == "" {
				temp[v.code] = v.code
				codeArr = append(codeArr, v.code)
				nameArr = append(nameArr, v.name)
				matchArr = append(matchArr, k)
			}
		}
	}
	class_name, class_code, matching_name := "", "", ""
	if len(codeArr) == 1 {
		class_name = strings.Join(nameArr, "~")
		class_code = strings.Join(codeArr, "~")
		matching_name = strings.Join(matchArr, "~")
	} else if len(codeArr) > 1 {
		//多分类选取
		new_code := multiClassSelectionInfo(text, basic, codeArr, matchArr)
		if new_code != "" {
			class_code = new_code
			class_name = PurCodeInfo[class_code]
			matching_name = "多分类选取~" + strings.Join(codeArr, "~") + "~" + strings.Join(matchArr, "~")
		} else {
			class_name = strings.Join(nameArr, "~")
			class_code = strings.Join(codeArr, "~")
			matching_name = strings.Join(matchArr, "~")
		}
	} else {

	}
	return class_name, class_code, matching_name
}

//多分类选取
func multiClassSelectionInfo(text string, basic string, codeArr []string, matchArr []string) string {
	//根据三大类~去除掉冗余的分类
	if basic != "" && basic != "服务" {
		codeArr = excludePartClassInfo(text, basic, codeArr)
	}
	if len(codeArr) == 0 {
		return ""
	}

	//比例选取大类
	code := proportionSelectionFirst(codeArr)
	if code != "" {
		//根据比例选择-最小级别代码
		code = proportionSelectionSecond(code, codeArr)
		return code
	}
	//后缀词是否为合理的后缀词
	for k, v := range codeArr {
		name := matchArr[k] //匹配词
		if isPostfixText(text, name) {
			return v
		}
	}

	l, new_code := 0, ""
	for k, v := range codeArr {
		name := matchArr[k]
		n := utf8.RuneCountInString(name)
		if n > l {
			l = n
			new_code = v
		}
	}
	return new_code
}

//是否为后缀词匹配
func isPostfixText(str_1 string, str_2 string) bool {
	if str_1 == "" || str_2 == "" {
		return false
	}
	l_1 := utf8.RuneCountInString(str_1)
	l_2 := utf8.RuneCountInString(str_2)
	c_1, c_2 := "", ""
	if l_1 >= l_2 {
		start := l_1 - l_2
		c_1 = string([]rune(str_1)[start:])
		c_2 = str_2
	} else {
		start := l_2 - l_1
		c_1 = string([]rune(str_2)[start:])
		c_2 = str_1
	}
	return c_1 == c_2
}

//比例选取
func proportionSelectionFirst(codeArr []string) string {
	infos := map[string]int{}
	for _, v := range codeArr {
		code_1 := v[:2]
		num := qu.IntAll(infos[code_1])
		infos[code_1] = num + 1
	}
	class_code := ""
	count := 0
	for k, v := range infos {
		if v > count {
			class_code = k
			count = v
		} else if v == count {
			class_code = ""
		} else {

		}
	}
	return class_code
}
func proportionSelectionSecond(code string, codeArr []string) string {
	level_2, level_3, level_4, level_5 := []string{}, []string{}, []string{}, []string{}
	for _, v := range codeArr {
		if v == "" || v == code {
			continue
		}
		if code == v[:2] {
			switch len(v) {
			case 4:
				level_2 = append(level_2, v)
			case 6:
				level_3 = append(level_3, v)
			case 8:
				level_4 = append(level_4, v)
			case 10:
				level_5 = append(level_5, v)
			default:
				log.Debug("异常代码~", v)
			}
		}
	}

	if len(level_5) == 1 {
		return level_5[0]
	}
	if len(level_4) == 1 {
		return level_4[0]
	}
	if len(level_3) == 1 {
		return level_3[0]
	}
	if len(level_2) == 1 {
		return level_2[0]
	}
	return code
}

//根据三大类判处多选~冗余的选项
func excludePartClassInfo(text string, basic string, codeArr []string) []string {
	//分类
	new_codeArr := []string{}
	basicArr := BasicClassCodeInfo[basic]
	for _, code := range codeArr {
		state := false
		for _, base_code := range basicArr {
			l := utf8.RuneCountInString(base_code)
			if l > utf8.RuneCountInString(code) {
				continue
			}
			if code[:l] == base_code {
				state = true
			}
		}
		if state {
			new_codeArr = append(new_codeArr, code)
		}
	}
	return new_codeArr
}

//关联出全部分类
func relevanceEntireClassCode(class_code string) (string, string, string, string, string) {
	class_1_arr := []string{}
	class_2_arr := []string{}
	class_3_arr := []string{}
	class_4_arr := []string{}
	class_5_arr := []string{}
	class_code_arr := strings.Split(class_code, "~")
	for _, code := range class_code_arr {
		if utf8.RuneCountInString(code) == 10 {
			code_1 := code[:2]
			code_2 := code[:4]
			code_3 := code[:6]
			code_4 := code[:8]
			code_5 := code[:10]
			class_1_arr = append(class_1_arr, PurCodeInfo[code_1])
			class_2_arr = append(class_2_arr, PurCodeInfo[code_2])
			class_3_arr = append(class_3_arr, PurCodeInfo[code_3])
			class_4_arr = append(class_4_arr, PurCodeInfo[code_4])
			class_5_arr = append(class_5_arr, PurCodeInfo[code_5])
		} else if utf8.RuneCountInString(code) == 8 {
			code_1 := code[:2]
			code_2 := code[:4]
			code_3 := code[:6]
			code_4 := code[:8]
			class_1_arr = append(class_1_arr, PurCodeInfo[code_1])
			class_2_arr = append(class_2_arr, PurCodeInfo[code_2])
			class_3_arr = append(class_3_arr, PurCodeInfo[code_3])
			class_4_arr = append(class_4_arr, PurCodeInfo[code_4])
		} else if utf8.RuneCountInString(code) == 6 {
			code_1 := code[:2]
			code_2 := code[:4]
			code_3 := code[:6]
			class_1_arr = append(class_1_arr, PurCodeInfo[code_1])
			class_2_arr = append(class_2_arr, PurCodeInfo[code_2])
			class_3_arr = append(class_3_arr, PurCodeInfo[code_3])
		} else if utf8.RuneCountInString(code) == 4 {
			code_1 := code[:2]
			code_2 := code[:4]
			class_1_arr = append(class_1_arr, PurCodeInfo[code_1])
			class_2_arr = append(class_2_arr, PurCodeInfo[code_2])
		} else if utf8.RuneCountInString(code) == 2 {
			code_1 := code[:2]
			class_1_arr = append(class_1_arr, PurCodeInfo[code_1])
		} else {

		}
	}
	class_1 := strings.Join(class_1_arr, "~")
	class_2 := strings.Join(class_2_arr, "~")
	class_3 := strings.Join(class_3_arr, "~")
	class_4 := strings.Join(class_4_arr, "~")
	class_5 := strings.Join(class_5_arr, "~")
	return class_1, class_2, class_3, class_4, class_5
}

//关联五级代码组~特别需求
func relevanceFiveClassCode(code string, update *map[string]interface{}) {
	codeArr := strings.Split(code, "~")
	arr1, arr2, arr3, arr4, arr5 := []string{}, []string{}, []string{}, []string{}, []string{}
	for _, v := range codeArr {
		if v == "" {
			continue
		}
		l := utf8.RuneCountInString(v)
		switch l {
		case 2:
			arr1 = append(arr1, v)
		case 4:
			arr2 = append(arr2, v)
		case 6:
			arr3 = append(arr3, v)
		case 8:
			arr4 = append(arr4, v)
		case 10:
			arr5 = append(arr5, v)
		default:
			log.Debug("异常~", v)
		}
	}
	(*update)["class_1"] = strings.Join(arr1, "~")
	(*update)["class_2"] = strings.Join(arr2, "~")
	(*update)["class_3"] = strings.Join(arr3, "~")
	(*update)["class_4"] = strings.Join(arr4, "~")
	(*update)["class_5"] = strings.Join(arr5, "~")
}
