//结果追踪
package admin

import (
	"jy/admin/track"
	"jy/extract"
	. "jy/mongodbutil"
	qutil "qfw/util"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
)

func init() {
	//首页
	Admin.GET("/resulttrack", func(c *gin.Context) {
		c.HTML(200, "result_list.html", nil)
	})
	//查找追踪数据
	Admin.GET("/resulttrack/trackData", func(c *gin.Context) {
		_id := c.Query("trackId")
		field := c.Query("field")
		trackcoll := c.Query("trackcoll")
		resultcoll := c.Query("resultcoll")
		//tracks := track.GetTrackPath("5b8dce7fa5cb26b9b7f7bd51", "projectname", "track_v3")
		tracks := track.GetTrackPath(_id, field, trackcoll, resultcoll)
		c.HTML(200, "track.html", map[string]interface{}{"tracks": tracks})
	})
	//查询所有的抽取结果
	Admin.POST("/resulttrack/list", ResultData)
	Admin.POST("/resulttrack/clearcoll", ClearColl)
	Admin.POST("/resulttrack/getversioninfo", GetVersionInfo)

}

//查询所有的抽取结果
func ResultData(c *gin.Context) {
	search, _ := c.GetPostForm("search[value]")
	startstr, _ := c.GetPostForm("start")
	limitstr, _ := c.GetPostForm("length")
	resultcoll, _ := c.GetPostForm("resultcoll")
	start, _ := strconv.Atoi(startstr)
	limit, _ := strconv.Atoi(limitstr)
	id := strings.TrimSpace(search)
	query := bson.M{}
	if id != "" {
		_id := extract.IdTrans(search)
		if _id.Valid() {
			query["$or"] = []interface{}{
				bson.M{"_id": _id},
			}
		}
	}
	if resultcoll != "" {
		data, _ := Mgo.Find(resultcoll, query, `{"_id":-1}`, nil, false, start, limit)
		count := Mgo.Count(resultcoll, query)
		for _, d := range *data {
			d["url"] = qutil.EncodeArticleId2ByCheck(d["_id"].(bson.ObjectId).Hex())
		}
		c.JSON(200, gin.H{"data": data, "recordsFiltered": count, "recordsTotal": count})
	} else {
		c.JSON(200, gin.H{"data": map[string]interface{}{}, "recordsFiltered": 0, "recordsTotal": 0})
	}

}

//清理测试表
func ClearColl(c *gin.Context) {
	id, _ := c.GetPostForm("id")
	resultcoll, _ := c.GetPostForm("resultcoll")
	trackcoll, _ := c.GetPostForm("trackcoll")
	b := false
	b4 := true
	b1 := Mgo.DelColl(resultcoll) //删除结果表
	b2 := Mgo.DelColl(trackcoll)  //删除日志表
	query := make(map[string]interface{})
	query["$or"] = []interface{}{
		bson.M{"resultcoll": resultcoll},
		bson.M{"trackcoll": trackcoll},
		bson.M{"_id": id},
	}
	data, _ := Mgo.Find("tasktestlog", query, nil, nil, false, -1, -1)
	for _, d := range *data {
		_id := qutil.BsonIdToSId(d["_id"])
		b3 := Mgo.Del("tasktestlog", `{"_id":"`+_id+`"}`) //删除测试任务的信息
		if !b3 {
			b4 = false
		}
	}
	if b1 && b2 && b4 {
		b = true
	}
	c.JSON(200, gin.H{"rep": b})
}

//根据版本查询启用的字段
func GetVersionInfo(c *gin.Context) {
	//version, _ := c.GetPostForm("version")
	//v, _ := Mgo.FindOne("version", `{"version":"`+version+`","delete":false}`)
	versioninfo,_ := Mgo.Find("fields",bson.M{},nil,nil,false,-1,-1)
	//if v != nil && len(*v) > 0 {
		//vid := (*v)["_id"].(bson.ObjectId).Hex()
		//query := map[string]interface{}{
		//	"vid":    vid,
		//	"isuse":  true,
		//	"delete": false,
		//}
		//versioninfo, _ := Mgo.Find("versioninfo", query, nil, `{"s_field":1}`, false, -1, -1)
		//if len(*versioninfo) > 0 {
		//	for _, val := range *v {
		//		s_field := val["s_field"].(string)
		//		f, _ := Mgo.FindOne("fields", `{"s_field":"`+s_field+`"}`)
		//		val["s_name"] = (*f)["s_name"]
		//	}
		//}
		c.JSON(200, gin.H{"versioninfo": versioninfo})
	//} else {
	//	c.JSON(200, gin.H{"versioninfo": map[string]interface{}{}})
	//}

}
