package main

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

var (
	words_data = map[string]string{}
)

//计算订阅词
func calculateSubscribeWords() {
	sess := Source_Mgo.GetMgoConn()
	defer Source_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Source_Mgo.DbName).C("zktest_subscribe_word").Find(&q).Sort("_id").Iter()
	total := 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total, "~", len(words_data))
		}
		keys := qu.ObjToString(tmp["key_list"])
		if keys != "" {
			key_list := strings.Split(keys, ",")
			if len(key_list) > 0 {
				createWordsInfo(key_list)
			}
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ~", total, len(words_data))
}

func createWordsInfo(key_list []string) {
	for _, v := range key_list {
		if v != "" && words_data[v] == "" {
			words_data[v] = v
			info := map[string]interface{}{}
			first_word := string([]rune(v)[0:1])
			str, _ := pinyin.New(first_word).Split("").Mode(pinyin.InitialsInCapitals).Convert()
			letter := ""
			if len(str) > 0 {
				l := str[:1]
				if LetterReg.MatchString(l) {
					letter = strings.ToUpper(l)
				}
			}
			info["letter"] = letter
			info["name"] = v
			Source_Mgo.Save("zktest_mysql_purchasing_info", info)
		}
	}
}

//return
func interfaceToStingArr(t interface{}) []string {
	sub_list := []string{}
	if list_1, ok_1 := t.(primitive.A); ok_1 {
		sub_list = qu.ObjArrToStringArr(list_1)
	} else {
		if list_2, ok_2 := t.([]interface{}); ok_2 {
			sub_list = qu.ObjArrToStringArr(list_2)
		}
	}
	return sub_list
}
