package main

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

var (
	SourceMgo, ExtMgo *MongodbSim
	QyxyMgo, SpiMgo   *MongodbSim
	IsLocal           bool
	TimeLayout        = "2006-01-02 15:04:05"
	numLock           sync.Mutex
	Url               = "https://www.jianyu360.com/article/content/%s.html"
)

func init() {
	//IsLocal = true
	initMgo()
	InitUdpMsg()
}

func main() {

	lock := make(chan bool)
	<-lock
}

//处理海南数据
func dealWithHaiNaiData(gteid string, ltid string) {
	log.Debug("开始海南数据~", gteid, "~", ltid)
	sess := SourceMgo.GetMgoConn()
	defer SourceMgo.DestoryMongoConn(sess)
	q := map[string]interface{}{
		"_id": map[string]interface{}{
			"$gte": StringTOBsonId(gteid),
			"$lt":  StringTOBsonId(ltid),
		},
	}
	log.Debug("查询语句 ~ ", q)
	it := sess.DB(SourceMgo.DbName).C("bidding").Find(&q).Sort("_id").Iter()
	pool := make(chan bool, 2)
	wg := &sync.WaitGroup{}
	total, isok := 0, 0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		if total%10000 == 0 {
			log.Debug("cur index ", total, "~", isok)
		}
		pool <- true
		wg.Add(1)
		go func(tmp map[string]interface{}) {
			defer func() {
				<-pool
				wg.Done()
			}()
			publishtime := qu.Int64All(tmp["publishtime"])
			if publishtime < 1546272000 {
				return
			}
			if qu.IntAll(tmp["extracttype"]) != 1 {
				return
			}
			subtype := qu.ObjToString(tmp["subtype"])
			if subtype == "中标" || subtype == "成交" || subtype == "合同" {
				b := getHaiNanInfo(tmp)
				if b {
					numLock.Lock()
					isok++
					numLock.Unlock()
				}
			}
		}(tmp)
		tmp = make(map[string]interface{})
	}
	wg.Wait()
	log.Debug("is over ~ ", total, "~", isok)
}

func getHaiNanInfo(tmp map[string]interface{}) bool {
	isExists := false
	//省份
	area := qu.ObjToString(tmp["area"])
	if area == "海南" {
		isExists = true
	} else {
		s_winner := qu.ObjToString(tmp["s_winner"])
		if s_winner != "" {
			arr := strings.Split(s_winner, ",")
			for _, v := range arr {
				if v != "" {
					data := QyxyMgo.FindOne("qyxy_std", map[string]interface{}{
						"company_name": v,
					})
					if data != nil {
						if qu.ObjToString(data["company_area"]) == "海南" {
							isExists = true
							break
						}
					}
				}
			}
		}
	}

	if isExists {
		info := map[string]interface{}{}
		tmpid := BsonTOStringId(tmp["_id"])
		info["id"] = tmpid
		if tmp["area"] != nil {
			info["area"] = tmp["area"]
		}
		if tmp["city"] != nil {
			info["city"] = tmp["city"]
		}
		if tmp["district"] != nil {
			info["district"] = tmp["district"]
		}
		info["title"] = qu.ObjToString(tmp["title"])
		info["subtype"] = qu.ObjToString(tmp["subtype"])
		info["detail"] = qu.ObjToString(tmp["detail"])
		info["publishtime"] = qu.Int64All(tmp["publishtime"])
		info["href"] = qu.ObjToString(tmp["href"])
		jyhref := fmt.Sprintf(Url, qu.CommonEncodeArticle("content", tmpid))
		info["jybxhref"] = jyhref
		info["projectname"] = qu.ObjToString(tmp["projectname"])

		if tmp["projectcode"] != nil {
			info["projectcode"] = tmp["projectcode"]
		}
		if tmp["projectscope"] != nil {
			info["projectscope"] = tmp["projectscope"]
		}
		if tmp["budget"] != nil {
			info["budget"] = tmp["budget"]
		}
		if tmp["bidamount"] != nil {
			info["bidamount"] = tmp["bidamount"]
		}
		if tmp["bidopentime"] != nil {
			info["bidopentime"] = tmp["bidopentime"]
		}
		if tmp["buyer"] != nil {
			info["buyer"] = tmp["buyer"]
		}
		if tmp["buyerperson"] != nil {
			info["buyerperson"] = tmp["buyerperson"]
		}
		if tmp["buyertel"] != nil {
			info["buyertel"] = tmp["buyertel"]
		}
		if tmp["agency"] != nil {
			info["agency"] = tmp["agency"]
		}
		if tmp["s_winner"] != nil {
			info["s_winner"] = tmp["s_winner"]
		}
		if tmp["winnerperson"] != nil {
			info["winnerperson"] = tmp["winnerperson"]
		}
		if tmp["winnertel"] != nil {
			info["winnertel"] = tmp["winnertel"]
		}

		info["appid"] = "jyGQ5XQQECBwVeSkVPBAZT"
		info["createtime"] = time.Now().Unix()

		//项目信息id
		pro_data := ExtMgo.FindOne("projectset_20220721", map[string]interface{}{
			"ids": tmpid,
		})
		if pro_data != nil {
			if pro_data["_id"] != nil {
				info["projectId"] = BsonTOStringId(pro_data["_id"])
			}
		}

		ext_data := ExtMgo.FindById("result_20220218", tmpid)
		if ext_data == nil {
			ext_data = ExtMgo.FindById("result_20220219", tmpid)
		}
		if ext_data != nil {
			if ext_data["winnerorder"] != nil {
				info["winnerorder"] = ext_data["winnerorder"]
			}
		}

		//保存服务
		SpiMgo.Save("usermail", info)
	}

	return isExists
}
