package task

import (
	qu "qfw/util"
	"strings"
	"time"

	. "../util"

	"github.com/go-xweb/xweb"
	"gopkg.in/mgo.v2/bson"
)

type Task struct {
	*xweb.Action
	taskList  xweb.Mapper `xweb:"/task/list"`  //查询所有任务
	taskSave  xweb.Mapper `xweb:"/task/save"`  //保存任务
	taskDel   xweb.Mapper `xweb:"/task/del"`   //删除任务
	taskStope xweb.Mapper `xweb:"/task/start"` //启动任务
	taskStop  xweb.Mapper `xweb:"/task/stop"`  //停止任务
	//逻辑配置
	taskPreProcessLogic xweb.Mapper `xweb:"/task/logic/preProcess"` //预处理逻辑
	taskPreProcessSave  xweb.Mapper `xweb:"/task/preprocess/save"`  //预处理逻辑保存
	askPreProcessDel    xweb.Mapper `xweb:"/task/preprocess/del"`   //预处理逻辑删除

	taskMatchLogic xweb.Mapper `xweb:"/task/logic/match"` //匹配逻辑
	taskfieldLogic xweb.Mapper `xweb:"/task/logic/field"` //字段逻辑
}

func (t *Task) TaskList() {
	defer qu.Catch()
	if t.Method() == "POST" {
		data, _ := Mgo.Find("task", `{"delete":false}`, nil, nil, false, -1, -1)
		t.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		t.Render("com/task_list.html")
	}
}

func (t *Task) TaskSave() {
	//判断存储配置是否完整
	s_mgosavecoll := t.GetString("s_mgosavecoll")
	strs := strings.Split(s_mgosavecoll, "/")
	if len(strs) < 3 {
		t.ServeJson(map[string]interface{}{
			"rep": false,
			"msg": "保存表输入地址不正确！",
		})
		return
	}
	_id := t.GetString("_id")
	data := GetPostForm(t.Request)
	b := false
	if _id == "" {
		data["delete"] = false
		data["l_comeintime"] = time.Now().Unix()
		session := t.GetSession("user").(map[string]interface{})
		data["s_username"] = session["name"].(string)
		data["s_userid"] = session["id"].(string)
		data["i_runstate"] = 0
		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)
	}
	t.ServeJson(map[string]interface{}{
		"rep": b,
	})
}

func (t *Task) TaskDel() {
	_id := t.GetString("_id")
	query := bson.M{
		"_id": qu.StringTOBsonId(_id),
	}
	set := bson.M{
		"$set": bson.M{
			"delete": true,
		},
	}
	b := Mgo.Update("task", query, set, false, false)
	t.ServeJson(map[string]interface{}{
		"rep": b,
	})

}

func (t *Task) TaskPreProcessLogic() {
	defer qu.Catch()
	taskid := t.GetString("taskid")
	if t.Method() == "POST" {
		data, _ := Mgo.Find("task", `{"delete":false}`, nil, nil, false, -1, -1)
		t.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		t.T["taskid"] = taskid
		t.Render("com/task_preprocess_list.html", &t.T)
	}
}

func (t *Task) TaskMatchLogic() {
	defer qu.Catch()
	taskid := t.GetString("taskid")
	if t.Method() == "POST" {
		data, _ := Mgo.Find("task", `{"delete":false}`, nil, nil, false, -1, -1)
		t.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		t.T["taskid"] = taskid
		t.Render("com/task_match_list.html", &t.T)
	}
}

func (t *Task) TaskfieldLogic() {
	defer qu.Catch()
	taskid := t.GetString("taskid")
	if t.Method() == "POST" {
		data, _ := Mgo.Find("task", `{"delete":false}`, nil, nil, false, -1, -1)
		t.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		t.T["taskid"] = taskid
		t.Render("com/task_field_list.html", &t.T)
	}
}
