package main

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

var Url = "https://www.jianyu360.com/article/content/%s.html"

func CreateBaseUpdateInfo(tmp map[string]interface{}) map[string]interface{} {
	return map[string]interface{}{
		"_id":             tmp["_id"],
		"buyer":           qu.ObjToString(tmp["buyer"]),
		"s_topscopeclass": qu.ObjToString(tmp["s_topscopeclass"]),
		"s_subscopeclass": qu.ObjToString(tmp["s_subscopeclass"]),
		"subtype":         qu.ObjToString(tmp["subtype"]),
		"site":            qu.ObjToString(tmp["site"]),
		"basicClass":      qu.ObjToString(tmp["basicClass"]),
		"buyerclass":      qu.ObjToString(tmp["buyerclass"]),
		"title":           qu.ObjToString(tmp["title"]),
		"detail":          qu.ObjToString(tmp["detail"]),
		"projectname":     qu.ObjToString(tmp["projectname"]),
		"href":            qu.ObjToString(tmp["href"]),
		"jyhref":          fmt.Sprintf(Url, qu.CommonEncodeArticle("content", qu.ObjToString(tmp["_id"]))),
	}
}

//标讯信息分类
func BeaconInfoPurchasingClass() {
	sess := data_mgo.GetMgoConn()
	defer data_mgo.DestoryMongoConn(sess)
	q, total := map[string]interface{}{}, 0
	pool_mgo := make(chan bool, 5)
	wg_mgo := &sync.WaitGroup{}
	it := sess.DB(data_mgo.DbName).C(SourceColl).Find(&q).Sort("_id").Select(SelectFields).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%1000 == 0 {
			log.Debug("cur index ", total)
		}
		pool_mgo <- true
		wg_mgo.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool_mgo
				wg_mgo.Done()
			}()
			dealWithBidInfoClass(tmp)
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg_mgo.Wait()
	log.Debug("彻底结束~~~", total)
}

func dealWithBidInfoClass(tmp map[string]interface{}) {
	update := CreateBaseUpdateInfo(tmp)
	//前置特殊~单标的
	if qu.ObjToString(update["class_code"]) == "" {
		prejudgmentListInfo(&update, tmp)
	}
	//标准分类
	if qu.ObjToString(update["class_code"]) == "" {
		acquirePlistClassInfo(&update, tmp)
	}
	//补充分类
	if qu.ObjToString(update["class_code"]) == "" {
		replenishRuleClassInfo(&update, tmp)
	}
	//相似度分类
	if qu.ObjToString(update["class_code"]) == "" {
		similarPurClass(&update, tmp)
	}
	//特别需求
	relevanceFiveClassCode(qu.ObjToString(update["class_code"]), &update)
	//class_1, class_2, class_3, class_4, class_5 := relevanceEntireClassCode(ab_code)
	//update["class_1"] = class_1
	//update["class_2"] = class_2
	//update["class_3"] = class_3
	//update["class_4"] = class_4
	//update["class_5"] = class_5
	data_mgo.Save(SourceColl+"_new", update)
}

//测试数据
func TestBeaconInfoPurchasingClass(tmpid string) {
	tmp := data_mgo.FindOne(SourceColl, map[string]interface{}{"_id": tmpid})
	update := CreateBaseUpdateInfo(tmp)
	//前置特殊~单标的
	if qu.ObjToString(update["class_code"]) == "" {
		prejudgmentListInfo(&update, tmp)
	}
	//标准分类
	if qu.ObjToString(update["class_code"]) == "" {
		acquirePlistClassInfo(&update, tmp)
	}
	//补充分类
	if qu.ObjToString(update["class_code"]) == "" {
		replenishRuleClassInfo(&update, tmp)
	}
	//相似度分类
	if qu.ObjToString(update["class_code"]) == "" {
		similarPurClass(&update, tmp)
	}
	//特别需求
	relevanceFiveClassCode(qu.ObjToString(update["class_code"]), &update)
	log.Debug(update)
}
