package main

import (
	log "github.com/donnie4w/go-logger/logger"
	"go.mongodb.org/mongo-driver/bson"
	qu "qfw/util"
	"regexp"
	"strings"
	"time"
)

func confirmYlqxInfoClass() {
	loadYlqxInfo()
	confirmFirst()
	confirmSecond()
	confirmThird()
	confirmFourth()
	confirmFifth()
	confirmSixth()
	confirmSeventh()
	log.Debug("彻底结束~~~")
}

//医疗分类打标记
func confirmFirst() {
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"product_name": 1,
	})
	for _, v := range dataArr {
		product_class_1 := qu.ObjToString(v["product_class_1"])
		if product_class_1 != "" {
			continue
		}
		tmpid := BsonTOStringId(v["_id"])
		product_name := qu.ObjToString(v["product_name"])
		if code := YlqxNameCode[product_name]; code != "" {
			isok++
			_, update := calculationYlqxCode(code)
			update["isFlag"] = flagTagLog("", "1")
			Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
				"$set": update,
			})
		}
	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
func confirmSecond() {
	codeReg := regexp.MustCompile("20\\d{3}(\\d{2})")
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"reg_no":             1,
		"classify":           1,
		"product_class_code": 1,
		"isFlag":             1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		product_class_code := qu.ObjToString(v["product_class_code"])
		if product_class_code != "" {
			continue
		}
		reg_no := qu.ObjToString(v["reg_no"])
		classify := qu.ObjToString(v["classify"])
		if strings.Contains(classify, "注册") {
			code_str := codeReg.FindString(reg_no)
			if code_str != "" {
				code := codeReg.ReplaceAllString(code_str, "${1}")
				name := codeRelation[code]
				if name != "" && code != "" {
					isok++
					Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
						"$set": bson.M{
							"product_class_1":    name,
							"product_class_code": YlqxNameCode[name],
							"isFlag":             flagTagLog(qu.ObjToString(v["isFlag"]), "2"),
						},
					})
				}
			}
		}
	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
func confirmThird() {
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"product_name":    1,
		"product_class_1": 1,
		"isFlag":          1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		product_name := qu.ObjToString(v["product_name"])
		product_class_1 := qu.ObjToString(v["product_class_1"])
		if product_class_1 == "" && strings.Contains(product_name, "试剂") {
			Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
				"$set": bson.M{
					"product_class_1":    "体外诊断试剂",
					"product_class_code": "23",
					"isFlag":             flagTagLog(qu.ObjToString(v["isFlag"]), "3"),
				},
			})
			isok++
		}
	}
	log.Debug("is over ... ", isok)
}
func confirmFourth() {
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"reg_company":        1,
		"product_name":       1,
		"product_class_1":    1,
		"product_class_2":    1,
		"product_class_code": 1,
		"isFlag":             1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		old_name := qu.ObjToString(v["product_name"])
		old_class_code := qu.ObjToString(v["product_class_code"])
		reg_company := qu.ObjToString(v["reg_company"])
		old_class_1 := qu.ObjToString(v["product_class_1"])
		old_class_2 := qu.ObjToString(v["product_class_2"])
		if old_class_2 != "" {
			continue
		}
		resArr, codeArr := calculationYlqxScRange(reg_company, old_class_1, old_name)
		b, res, code := checkYlqxResultRightFourth(resArr, codeArr, old_class_1)
		if b && res != nil && code != "" && tmpid != "" {
			if len(code) > len(old_class_code) {
				isok++
				res["product_class_code"] = code
				res["isFlag"] = flagTagLog(qu.ObjToString(v["isFlag"]), "4")
				Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
					"$set": res,
				})
			}
		}
	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
func confirmFifth() {
	temp := loadFifthInfo()
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"product_name":       1,
		"product_class_1":    1,
		"product_class_2":    1,
		"product_class_code": 1,
		"isFlag":             1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		old_name := qu.ObjToString(v["product_name"])
		old_class_code := qu.ObjToString(v["product_class_code"])
		old_class_1 := qu.ObjToString(v["product_class_1"])
		old_class_2 := qu.ObjToString(v["product_class_2"])
		if old_class_2 != "" || tmpid == "" {
			continue
		}
		if name := temp[old_name]; name != "" {
			if code := YlqxNameCode[name]; code != "" {
				b, res := calculationYlqxCode(code)
				if b && (old_class_1 == "" || (qu.ObjToString(res["product_class_1"]) == old_class_1 && len(code) > len(old_class_code))) {
					isok++
					res["isFlag"] = flagTagLog(qu.ObjToString(v["isFlag"]), "5")
					Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
						"$set": res,
					})
				}
			}
		}

	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
func confirmSixth() {
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"reg_company":        1,
		"product_name":       1,
		"product_class_1":    1,
		"product_class_2":    1,
		"product_class_code": 1,
		"isFlag":             1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		old_name := qu.ObjToString(v["product_name"])
		old_class_code := qu.ObjToString(v["product_class_code"])
		old_class_1 := qu.ObjToString(v["product_class_1"])
		old_class_2 := qu.ObjToString(v["product_class_2"])
		if old_class_2 != "" || tmpid == "" {
			continue
		}
		new_name := cleanProductName(old_name)
		if code := YlqxNameCode[new_name]; code != "" {
			b, res := calculationYlqxCode(code)
			if b && (old_class_1 == "" || (qu.ObjToString(res["product_class_1"]) == old_class_1 && len(code) > len(old_class_code))) {
				isok++
				res["isFlag"] = flagTagLog(qu.ObjToString(v["isFlag"]), "6")
				Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
					"$set": res,
				})
			}
		}
	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
func confirmSeventh() {
	isok := 0
	dataArr, _ := Save_Mgo.Find("nmpa_product_new", nil, nil, bson.M{
		"reg_company":     1,
		"product_name":    1,
		"product_class_1": 1,
		"product_class_2": 1,
		"isFlag":          1,
	})
	for _, v := range dataArr {
		tmpid := BsonTOStringId(v["_id"])
		old_name := qu.ObjToString(v["product_name"])
		old_class_code := qu.ObjToString(v["product_class_code"])
		old_class_1 := qu.ObjToString(v["product_class_1"])
		old_class_2 := qu.ObjToString(v["product_class_2"])
		if old_class_2 != "" || tmpid == "" {
			continue
		}
		new_name := cleanProductName(old_name)
		resArr := []map[string]interface{}{}
		codeArr := []string{}
		matchArr := []string{}
		logArr := []map[string]interface{}{}
		if new_name != "" {
			for k1, v1 := range YlqxNameCode {
				if (new_name == "医用退热凝胶" && k1 == "凝胶") || k1 == "蜡" {
					continue
				}
				if strings.Contains(new_name, k1) || strings.Contains(k1, new_name) {
					b, res := calculationYlqxCode(YlqxNameCode[k1])
					if b && checkYlqxInfoRight(res, old_class_1, old_name) {
						logArr = append(logArr, map[string]interface{}{
							"name":  k1,
							"code":  v1,
							"class": res,
						})
						resArr = append(resArr, res)
						codeArr = append(codeArr, YlqxNameCode[k1])
						matchArr = append(matchArr, k1)
					}
				}
			}
		}
		if len(resArr) > 0 {
			resArr, codeArr, matchArr = resultRepeat(resArr, codeArr, matchArr)
			resArr, codeArr, matchArr = resultMerge(resArr, codeArr, matchArr)
			//根据结果进行-分类选取逻辑
			b, res, code := checkYlqxResultRightSeventh(resArr, codeArr, matchArr, old_class_1)
			if b && res != nil && tmpid != "" && len(code) > len(old_class_code) {
				isok++
				res["isFlag"] = flagTagLog(qu.ObjToString(v["isFlag"]), "7")
				Save_Mgo.UpdateById("nmpa_product_new", tmpid, map[string]interface{}{
					"$set": res,
				})
			}
		}
	}
	log.Debug("is over ... ", isok)
	time.Sleep(time.Second * 2)
}
