//任务管理
//编辑、启动、停止、测试
package task

import (
	. "jy/admin"
	"jy/extract"
	. "jy/mongodbutil"
	"net/http"
	qu "qfw/util"
	"strings"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func init() {
	//首页
	Admin.GET("/task", func(c *gin.Context) {
		c.HTML(200, "task_list.html", nil)
	})
	Admin.GET("/taskclear", func(c *gin.Context) {
		c.HTML(200, "task_clearlist.html", nil)
	})
	//获取任务列表
	Admin.POST("/task/list", func(c *gin.Context) {
		data, _ := Mgo.Find("task", ``, `{"_id":-1}`, nil, false, -1, -1)
		for _, v := range *data {
			_id := qu.BsonIdToSId(v["_id"])
			tmp := extract.TaskList[_id]
			if tmp != nil {
				v["runstatus"] = tmp.IsRun
			} else {
				v["runstatus"] = false
			}
		}
		c.JSON(200, gin.H{"data": data})
	})

	//新增任务、编辑任务
	Admin.POST("/task/save", func(c *gin.Context) {
		//判断存储配置是否完整
		s_mgosavecoll, _ := c.GetPostForm("s_mgosavecoll")
		strs := strings.Split(s_mgosavecoll, "/")
		if len(strs) < 3 {
			c.JSON(200, gin.H{"rep": false, "msg": "保存表输入地址不正确！"})
			return
		}
		data := GetPostForm(c)
		_id, _ := c.GetPostForm("_id")
		b := false
		if _id == "" {
			data["l_createtime"] = time.Now().Unix()
			data["s_username"] = sessions.Default(c).Get("username")
			b = Mgo.Save("task", data) != ""
		} else {
			data["l_lasttime"] = time.Now().Unix()
			b = Mgo.Update("task", `{"_id":"`+_id+`"}`, map[string]interface{}{
				"$set": data,
			}, false, false)
		}
		c.JSON(200, gin.H{"rep": b})
	})

	//获取版本列表
	Admin.POST("/task/getversion", func(c *gin.Context) {
		list, b := Mgo.Find("version", `{"delete":false}`, `{"_id":-1}`, `{"version":1}`, false, -1, -1)
		if b && list != nil {
			for _, v := range *list {
				v["_id"] = v["version"]
				v["s_name"] = v["version"]
			}
		}
		c.JSON(200, gin.H{"data": list})
	})
	//获取清理版本列表
	Admin.POST("/task/getclearversions", func(c *gin.Context) {
		list, b := Mgo.Find("clearversion", `{"delete":false}`, `{"_id":-1}`, `{"clearversion":1}`, false, -1, -1)
		if b && list != nil {
			for _, v := range *list {
				v["_id"] = v["clearversion"]
				v["s_name"] = v["clearversion"]
			}
		}
		c.JSON(200, gin.H{"data": list})
	})
	//获取任务名称
	Admin.POST("/task/gettaskname", func(c *gin.Context) {
		list, b := Mgo.Find("task", `{}`, `{"_id":-1}`, `{"s_taskname":1}`, false, -1, -1)
		if b && list != nil {
			for _, v := range *list {
				v["s_name"] = v["s_taskname"]
			}
		}
		c.JSON(200, gin.H{"data": list})
	})
	//任务测试
	Admin.POST("/task/test", func(c *gin.Context) {
		startid, _ := c.GetPostForm("startid")
		num, _ := c.GetPostForm("num")
		taskid, _ := c.GetPostForm("taskid")
		resultcoll, _ := c.GetPostForm("resultcoll")
		trackcoll, _ := c.GetPostForm("trackcoll")
		version, _ := Mgo.FindById("task", taskid, `{"s_version":1}`)
		b := extract.StartExtractTestTask(taskid, startid, num, resultcoll, trackcoll)
		if b { //保存结果表和日志表
			data := make(map[string]interface{})
			data["resultcoll"] = resultcoll
			data["trackcoll"] = trackcoll
			data["version"] = (*version)["s_version"]
			Mgo.Update("tasktestlog", `{"resultcoll":"`+resultcoll+`","trackcoll":"`+trackcoll+`"}`, map[string]interface{}{
				"$set": data,
			}, true, false)
		}
		c.JSON(200, gin.H{"rep": b, "version": (*version)["s_version"]})
	})
	//启动任务
	Admin.POST("/task/start", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := extract.StartExtractTaskId(_id)
		c.JSON(200, gin.H{"rep": b})
	})
	//停止任务
	Admin.POST("/task/stop", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := extract.StopExtractTaskId(_id)
		c.JSON(200, gin.H{"rep": b})
	})
	//查询任务测试的表信息
	Admin.POST("/task/gettasktestlog", func(c *gin.Context) {
		data, _ := Mgo.Find("tasktestlog", nil, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": data})
	})

	//抽取导出相关
	Admin.GET("/task/export", func(c *gin.Context) {
		list, b := Mgo.Find("version", `{"delete":false}`, `{"_id":-1}`, `{"version":1}`, false, -1, -1)
		if b && list != nil {
			for _, v := range *list {
				v["_id"] = qu.BsonIdToSId(v["_id"])
				v["s_name"] = v["version"]
			}
		}
		c.HTML(
			http.StatusOK, "task_export.html",
			gin.H{"vlist": list},
		)
	})
	Admin.POST("/task/export/data", func(c *gin.Context) {
		data, _ := Mgo.Find("task_export", `{}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": data})
	})
	Admin.POST("task/export/save", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		name, _ := c.GetPostForm("name")
		v1id, _ := c.GetPostForm("v1")
		v2id, _ := c.GetPostForm("v2")
		v1name, _ := c.GetPostForm("v1name")
		v2name, _ := c.GetPostForm("v2name")
		dbaddr, _ := c.GetPostForm("dbaddr")
		dbname, _ := c.GetPostForm("dbname")
		query, _ := c.GetPostForm("query")
		table, _ := c.GetPostForm("table")
		limit, _ := c.GetPostForm("limit")
		tmpfields, _ := c.GetPostFormMap("fields")
		fields := []string{}
		for _, v := range tmpfields {
			fields = append(fields, v)
		}
		data := map[string]interface{}{
			"name": name,
			"v1id": v1id, "v1name": v1name, "v2id": v2id, "v2name": v2name,
			"dbaddr": dbaddr, "dbname": dbname, "table": table,
			"query": query, "limit": limit, "fields": fields,
		}
		if _id != "" {
			Mgo.UpdateById("task_export", _id, map[string]interface{}{"$set": data})
		} else {
			data["state"] = 0 //0待执行，1已完成
			data["intime"] = time.Now().Unix()
			Mgo.Save("task_export", data)
		}
		c.JSON(200, gin.H{"rep": true})
	})
	Admin.POST("/task/export/del", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := Mgo.Del("task_export", `{"_id":"`+_id+`"}`)
		c.JSON(200, gin.H{"rep": b})
	})

	//清理任务相关
	//获取任务列表
	Admin.POST("/cleartask/list", func(c *gin.Context) {
		data, _ := Mgo.Find("cleartask", ``, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": data})
	})
	//新增任务、编辑任务
	Admin.POST("/cleartask/save", func(c *gin.Context) {
		//判断存储配置是否完整
		data := GetPostForm(c)
		_id, _ := c.GetPostForm("_id")
		b := false
		if _id == "" {
			data["l_createtime"] = time.Now().Unix()
			data["s_username"] = sessions.Default(c).Get("username")
			b = Mgo.Save("cleartask", data) != ""
		} else {
			data["l_lasttime"] = time.Now().Unix()
			b = Mgo.Update("cleartask", `{"_id":"`+_id+`"}`, map[string]interface{}{
				"$set": data,
			}, false, false)
		}
		c.JSON(200, gin.H{"rep": b})
	})
}
