package main

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

//导出标的物词 2022年之后的标的物词
func exportPurchasingWords() {
	sess := Bid_Mgo.GetMgoConn()
	defer Bid_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{
		"_id": map[string]interface{}{
			"$gte": StringTOBsonId("61cf29000000000000000000"),
			"$lt":  StringTOBsonId("635ff1000000000000000000"),
		},
	}
	it := sess.DB(Bid_Mgo.DbName).C("bidding").Find(&q).Sort("_id").Select(map[string]interface{}{
		"extracttype":    1,
		"purchasinglist": 1,
	}).Iter()
	total, isok := 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total, "~", isok, "~", tmp["_id"])
		}
		if qu.IntAll(tmp["extracttype"]) != 1 {
			continue
		}
		p_list := IsMarkInterfaceMap(tmp["purchasinglist"])
		listArr := []string{}
		for _, v := range p_list {
			itemname := qu.ObjToString(v["itemname"])
			if utf8.RuneCountInString(itemname) > 0 && utf8.RuneCountInString(itemname) < 30 {
				listArr = append(listArr, itemname)
			}
		}
		if len(listArr) > 0 {
			isok++
			Bid_Mgo.Save("zktest_purchasing_word", map[string]interface{}{
				"p_list": strings.Join(listArr, ","),
			})
		}
		tmp = make(map[string]interface{})
	}
	log.Debug("is over ~", total, "~", isok)
}

//计算标的物词汇
func calculatePurchasingWords() {
	sess := Bid_Mgo.GetMgoConn()
	defer Bid_Mgo.DestoryMongoConn(sess)
	q := map[string]interface{}{}
	it := sess.DB(Bid_Mgo.DbName).C("zktest_purchasing_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["p_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))
}
