package main

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

//保存订阅词
func SaveMysqlSubscribeInfo(mgoName string, sqlName string) {
	sess := Source_Mgo.GetMgoConn()
	defer Source_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Source_Mgo.DbName).C(mgoName).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)
		}
		info := map[string]interface{}{}
		info["name"] = qu.ObjToString(tmp["name"])
		info["letter"] = qu.ObjToString(tmp["letter"])
		InsertMysqlDevData(sqlName, info, qu.ObjToString(tmp["name"]))

		tmp = make(map[string]interface{})
	}
	log.Debug("is over ~", total)
}

//保存行业词
func SaveMysqlIndustryInfo(mgoName string, sqlName string) {
	sess := Source_Mgo.GetMgoConn()
	defer Source_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Source_Mgo.DbName).C(mgoName).Find(&q).Sort("_id").Iter()
	total := 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%1000 == 0 {
			log.Debug("cur index ", total)
		}
		info := map[string]interface{}{}
		info["name"] = qu.ObjToString(tmp["name"])
		info["letter"] = qu.ObjToString(tmp["letter"])
		info["class_1"] = qu.ObjToString(tmp["class_1"])
		info["class_2"] = qu.ObjToString(tmp["class_2"])
		InsertMysqlDevData(sqlName, info, qu.ObjToString(tmp["name"]))
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ~", total)
}

func SaveMysqlPurchasingInfo(mgoName string, sqlName string) {
	sess := Source_Mgo.GetMgoConn()
	defer Source_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Source_Mgo.DbName).C(mgoName).Find(&q).Sort("_id").Iter()
	total := 0
	pool := make(chan bool, 6)
	wg := &sync.WaitGroup{}
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()
			name := qu.ObjToString(tmp["name"])
			l := utf8.RuneCountInString(name)
			if l > 1 && l <= 6 {
				info := map[string]interface{}{}
				info["name"] = qu.ObjToString(tmp["name"])
				info["letter"] = qu.ObjToString(tmp["letter"])
				InsertMysqlDevData(sqlName, info, qu.ObjToString(tmp["name"]))
			}
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()
	log.Debug("is over ~", total)
}

//整合资源~标的物、订阅词
func ResourceIntegrationInfo() {
	unique_info := map[string]string{}
	pur_datas := *MysqlDevTool.Find("seo_purchasing", nil,
		"", "id", -1, -1)
	for _, v := range pur_datas {
		name := qu.ObjToString(v["name"])
		unique_info[name] = name
	}
	sub_datas := *MysqlDevTool.Find("seo_subscribe", nil,
		"", "id", -1, -1)
	log.Debug("整合前~词库~", len(pur_datas), "~", len(sub_datas))

	isok := 0
	for k, v := range sub_datas {
		if k%5000 == 0 {
			log.Debug("cur index ", k, "~", isok)
		}
		name := qu.ObjToString(v["name"])
		letter := qu.ObjToString(v["letter"])
		if unique_info[name] == "" {
			isok++
			unique_info[name] = name
			new_info := map[string]interface{}{}
			new_info["name"] = name
			new_info["letter"] = letter
			InsertMysqlDevData("seo_purchasing", new_info, name)
		}
	}
	log.Debug("补充后~词库~", len(unique_info), "~", isok)
}

func descriptivePurInfo() {
	sess := Source_Mgo.GetMgoConn()
	defer Source_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Source_Mgo.DbName).C("zktest_mysql_purchasing_info").Find(&q).Sort("_id").Iter()
	total := 0
	isok1, isok2, isok3, isok4 := 0, 0, 0, 0
	pool := make(chan bool, 6)
	wg := &sync.WaitGroup{}
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total, "~", isok1, "~", isok2, "~", isok3, "~", isok4)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()
			name := qu.ObjToString(tmp["name"])
			length := utf8.RuneCountInString(name)
			if length > 1 && length <= 10 {
				numLock.Lock()
				if length <= 4 {
					isok1++
				}
				if length <= 6 {
					isok2++
				}
				if length <= 8 {
					isok3++
				}
				if length <= 10 {
					isok4++
				}
				numLock.Unlock()
			}
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()
	log.Debug("is over ~", total, "~", isok1, "~", isok2, "~", isok3, "~", isok4)
}
