package extract

import (
	"encoding/json"
	db "jy/mongodbutil"
	ju "jy/util"
	"log"
	mu "mfw/util"
	"net"
	qu "qfw/util"

	"gopkg.in/mgo.v2/bson"
)

var ClearUdpclient mu.UdpClient //清理udp对象

//udp通知清理
func ClearUdp() {
	//nextNodes = qu.ObjArrToMapArr(ju.Config["nextNode"].([]interface{}))
	ClearUdpclient = mu.UdpClient{Local: ":" + qu.ObjToString(ju.Config["clearudpport"]), BufSize: 1024}
	ClearUdpclient.Listen(clearProcessUdpMsg)
}

func clearProcessUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
	switch act {
	case mu.OP_TYPE_DATA:
		var rep map[string]interface{}
		err := json.Unmarshal(data, &rep)
		if err != nil {
			log.Println(err)
			Udpclient.WriteUdp([]byte("false"), mu.OP_NOOP, ra) //回应上一个节点
		} else {
			sid, _ := rep["gtid"].(string)
			eid, _ := rep["lteid"].(string)
			//stype, _ := rep["stype"].(string)
			log.Println("======", sid, eid)
			Udpclient.WriteUdp([]byte("ok"), mu.OP_NOOP, ra) //回应上一个节点
			ClearByUdp(sid, eid)
			log.Println("udp通知清理完成,eid=", eid)
		}
	case mu.OP_NOOP: //下个节点回应
		var rep map[string]interface{}
		err := json.Unmarshal(data, &rep)
		if err != nil {
			log.Println(err)
		} else {
			log.Println(rep)
		}
	}
}

//根据id区间清理
func ClearByUdp(sid, eid string) {
	clt := &ClearTask{}
	clt.Id = qu.ObjToString(ju.Config["cleartaskid"])
	//加载任务信息
	clt.InitClearTaskInfo()
	clt.ClearTaskInfo.FDB = db.MgoFactory(2, 3, 120, clt.ClearTaskInfo.FromDbAddr, clt.ClearTaskInfo.FromDB)
	//初始化脚本信息
	clt.InitClearLuas()
	//更新结果表清理后的字段值
	go clt.UpdateResultVal()

	query := bson.M{"_id": bson.M{"$gte": bson.ObjectIdHex(sid), "$lte": bson.ObjectIdHex(eid)}}
	log.Println("query---", query, clt.ClearTaskInfo.FromColl)
	count := clt.ClearTaskInfo.FDB.Count(clt.ClearTaskInfo.FromColl, query)
	log.Println("count---", count)
	pageNum := (count + PageSize - 1) / PageSize
	limit := PageSize
	if count < PageSize {
		limit = count
	}
	//一次查询5000条数据进行清理
	for i := 0; i < pageNum; i++ {
		query = bson.M{"_id": bson.M{"$gte": bson.ObjectIdHex(sid)}}
		log.Printf("page=%d,query=%v", i+1, query)
		list, _ := clt.ClearTaskInfo.FDB.Find(clt.ClearTaskInfo.FromColl, query, nil, Fields2, false, 0, limit)
		for _, v := range *list {
			clt.ClearTaskInfo.ProcessPool <- true
			go func(val map[string]interface{}) {
				clt.ClearProcess(&val)
			}(v)
			sid = qu.BsonIdToSId(v["_id"])
		}
	}
}
