package main

import (
	log "github.com/donnie4w/go-logger/logger"
	"github.com/tealeg/xlsx"
	"go.mongodb.org/mongo-driver/bson"
	qu "qfw/util"
	"regexp"
	"strings"
	"sync"
	"unicode/utf8"
)

var CleanTailReg *regexp.Regexp = regexp.MustCompile("(服务)$")
var numLock sync.Mutex

//源代码表~五级分类
func prparePurClassCode() {
	ff, _ := xlsx.OpenFile("res/source.xlsx")
	isok := 0
	for _, sheet := range ff.Sheets {
		for _, row := range sheet.Rows {
			var str []string
			for _, cell := range row.Cells {
				s := cell.String()
				s = strings.ReplaceAll(s, "　", "")
				s = strings.ReplaceAll(s, " ", "")
				str = append(str, s)
			}
			if len(str) < 3 {
				log.Debug("异常~~~~~~")
				continue
			}
			name, code, level := str[0], str[1], str[2]
			pcode := ""
			switch {
			case qu.IntAll(level) == 1:

			case qu.IntAll(level) == 2:
				pcode = code[:2]
			case qu.IntAll(level) == 3:
				pcode = code[:4]
			case qu.IntAll(level) == 4:
				pcode = code[:6]
			case qu.IntAll(level) == 5:
				pcode = code[:8]
			default:
				log.Debug("异常~~~", str)
			}
			//if code == "47" || code == "48" || code == "49" || code == "50" {
			//	name = CleanTailReg.ReplaceAllString(name, "")
			//}
			data_mgo.Save(InitialColl, map[string]interface{}{
				"name":  name,
				"code":  code,
				"pcode": pcode,
				"level": qu.IntAll(level),
			})
			isok++
		}
	}
	log.Debug("is over ~ ", isok)

}

//补充数据表
func prepareReplenishInfo() {
	temp := map[string]string{}
	ff, _ := xlsx.OpenFile("res/replenish.xlsx")
	for _, sheet := range ff.Sheets {
		isok := 0
		for _, row := range sheet.Rows {
			var arr []string
			for _, cell := range row.Cells {
				s := cell.String()
				s = strings.ReplaceAll(s, "　", "")
				s = strings.ReplaceAll(s, " ", "")
				arr = append(arr, s)
			}
			if len(arr) >= 2 {
				name, code := arr[0], arr[1]
				s_code := ""
				if len(arr) > 2 {
					s_code = arr[2]
				}
				if code != "" && name != "" {
					if PurClassInfo[name].name == "" && temp[name] == "" {
						isok++
						data_mgo.Save(ReplenishColl, map[string]interface{}{
							"name":   name,
							"code":   code,
							"s_code": s_code,
							"type":   sheet.Name,
						})
						temp[name] = name
					}
				}
			}
		}
		log.Debug("cur sheet ", sheet.Name, " is over ~ ", isok)
	}

	//补充近义词
	fs, _ := xlsx.OpenFile("res/similar.xlsx")
	total, isok := 0, 0
	for _, sheet := range fs.Sheets {
		for index, row := range sheet.Rows {
			if index == 0 {
				continue
			}
			var arr []string
			for _, cell := range row.Cells {
				s := cell.String()
				s = strings.ReplaceAll(s, "　", "")
				s = strings.ReplaceAll(s, " ", "")
				arr = append(arr, s)
			}
			if len(arr) > 5 {
				total++
				arr = arr[6:]
				code := ""
				for k, v := range arr {
					if k == 0 {
						code = v
					} else if k == 1 {
						if qu.IntAll(v) != 1 {
							break
						}
					} else {
						if PurClassInfo[v].name == "" && temp[v] == "" && v != "" {
							isok++
							info := map[string]interface{}{
								"name":   v,
								"code":   code,
								"s_code": "",
								"type":   "近义词",
							}
							if len(info) > 0 {
								data_mgo.Save(ReplenishColl, info)
							}
							temp[v] = v
						}
					}
				}
			}
		}
		log.Debug("cur sheet ", sheet.Name, " is over ~ ", isok)
	}
}

//补充三大类对应表
func prepareBasicInfo() {
	temp := map[string]string{}
	ff, _ := xlsx.OpenFile("res/basic.xlsx")
	for _, sheet := range ff.Sheets {
		isok := 0
		for _, row := range sheet.Rows {
			var arr []string
			for _, cell := range row.Cells {
				s := cell.String()
				s = strings.ReplaceAll(s, "　", "")
				s = strings.ReplaceAll(s, " ", "")
				arr = append(arr, s)
			}
			isok++
			//代码  分类
			s1, s2 := arr[0], arr[1]
			if s1 != "" && s2 != "" {
				temp[s1] = s2
			}
		}
		log.Debug("cur sheet is over ~ ", isok)
	}

	log.Debug("开始更新基础代码表~~")
	updateBasicCode(InitialColl, temp)
	updateBasicCode(ReplenishColl, temp)
}

//更新三大类
func updateBasicCode(coll_name string, temp map[string]string) {
	dataArr, _ := data_mgo.Find(coll_name, nil, map[string]interface{}{"_id": 1}, nil)
	for _, v := range dataArr {
		code := qu.ObjToString(v["code"])
		tmpid := BsonTOStringId(v["_id"])
		basic := ""
		l := utf8.RuneCountInString(code)
		if temp[code] != "" { //更新
			basic = temp[code]
		} else {
			if l == 2 {
				log.Debug("找不到~", code)
				continue
			} else if l == 4 {
				n_c := code[:2]
				if temp[n_c] != "" { //更新
					basic = temp[n_c]
				} else {
					log.Debug("异常~", code)
				}
			} else if l >= 6 {
				n_c := code[:2]
				if temp[n_c] != "" { //更新
					basic = temp[n_c]
				} else {
					n_c = code[:4]
					if temp[n_c] != "" { //更新
						basic = temp[n_c]
					} else {
						log.Debug("异常~", code)
					}
				}
			}
		}
		data_mgo.UpdateById(coll_name, tmpid, map[string]interface{}{
			"$set": map[string]interface{}{
				"basic": basic,
			},
		})
	}
	log.Debug("更新完毕~", coll_name)
}

//计算医疗数据
func prepareMedicalNameClass() {
	temp := map[string]string{}
	ff, _ := xlsx.OpenFile("res/medical.xlsx")
	for _, sheet := range ff.Sheets {
		for _, row := range sheet.Rows {
			var str []string
			for _, cell := range row.Cells {
				s := cell.String()
				s = strings.ReplaceAll(s, "　", "")
				s = strings.ReplaceAll(s, " ", "")
				str = append(str, s)
			}
			class_1, class_2, class_3, class_code := str[0], str[1], str[2], str[3]
			if class_1 == "" || class_2 == "" || class_3 == "" || class_code == "" {
				log.Debug("异常~~~")
			}
			query := bson.M{
				"product_class1": class_1,
				"product_class2": class_2,
				"product_class3": class_3,
			}
			dataArr, _ := data_mgo.Find("zktest_mysql_product_info", query, nil, bson.M{"product_name": 1})
			if len(dataArr) > 0 {
				for _, v := range dataArr {
					product_name := qu.ObjToString(v["product_name"])
					if product_name != "" {
						if temp[product_name] == "" {
							temp[product_name] = class_code
						}
					}
				}
			}
		}
	}
	for k, v := range temp {
		data_mgo.Save("111111", map[string]interface{}{
			"name": k,
			"code": v,
		})
	}
	log.Debug("is over ", len(temp))
}
