package front

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

// GroupTaskListByGroup 用户组权限用户组任务列表
func (f *Front) GroupTaskListByGroup() {
	defer qu.Catch()
	if f.Method() == "POST" {
		start, _ := f.GetInteger("start")
		limit, _ := f.GetInteger("length")
		draw, _ := f.GetInteger("draw")
		status := f.GetString("status")
		searchStr := f.GetString("search[value]")
		search := strings.TrimSpace(searchStr)
		query := map[string]interface{}{
			"s_stype": "group", //检索用户组任务
		}
		if status != "-1" { //任务状态
			query["s_status"] = status
		}
		if search != "" {
			query["$or"] = []interface{}{
				map[string]interface{}{"s_projectname": map[string]interface{}{"$regex": search}},
			}
		}
		list, _ := util.Mgo.Find(util.TASKCOLLNAME, query, nil, nil, false, start, limit)
		count := util.Mgo.Count(util.TASKCOLLNAME, query)
		f.ServeJson(map[string]interface{}{"draw": draw, "data": *list, "recordsFiltered": count, "recordsTotal": count})
	} else {
		_ = f.Render("project/task_group_list.html")
	}
}

// GroupTaskListByAdmin 系统管理员权限用户组任务列表
func (f *Front) GroupTaskListByAdmin() {
	defer qu.Catch()
	if f.Method() == "POST" {
		start, _ := f.GetInteger("start")
		limit, _ := f.GetInteger("length")
		draw, _ := f.GetInteger("draw")
		status := f.GetString("status")
		searchStr := f.GetString("search[value]")
		search := strings.TrimSpace(searchStr)
		query := map[string]interface{}{
			"s_stype": "group", //检索用户组任务
		}
		if status != "-1" { //任务状态
			query["s_status"] = status
		}
		if search != "" {
			query["$or"] = []interface{}{
				map[string]interface{}{"s_projectname": map[string]interface{}{"$regex": search}},
			}
		}
		list, _ := util.Mgo.Find(util.TASKCOLLNAME, query, nil, nil, false, start, limit)
		count := util.Mgo.Count(util.TASKCOLLNAME, query)
		f.ServeJson(map[string]interface{}{"draw": draw, "data": *list, "recordsFiltered": count, "recordsTotal": count})
	} else {
		_ = f.Render("project/task_group_list.html")
	}
}

// GroupTaskDeliver 用户组任务交付
func (f *Front) GroupTaskDeliver() {
	defer qu.Catch()
	user := f.GetSession("user").(map[string]interface{})
	username := qu.ObjToString(user["s_name"]) //当前登录用户
	success := true
	msg := ""
	taskId := f.GetString("taskid") //用户组任务id
	qu.Debug("Group Task ID：", taskId)
	groupTask, _ := util.Mgo.FindById(util.TASKCOLLNAME, taskId, map[string]interface{}{"v_sonids": 1})
	if groupTask != nil && len(*groupTask) > 0 {
		sonUserIds := (*groupTask)["v_sonids"].([]interface{})
		if len(sonUserIds) > 0 {
			for _, id := range sonUserIds {
				userTaskId := qu.ObjToString(id)
				userTask, _ := util.Mgo.FindById(util.TASKCOLLNAME, userTaskId, map[string]interface{}{"s_status": 1})
				status := qu.ObjToString((*userTask)["s_status"])
				if status != "已完成" || status != "已关闭" {
					success = false
					msg = "任务暂未完成"
				}
			}
			if success { //用户组下用户任务全部完成，更新该用户组任务状态
				success = util.Mgo.UpdateById(util.TASKCOLLNAME, taskId, map[string]interface{}{
					"$set": map[string]interface{}{
						"i_completetime": time.Now().Unix(),
						"s_updateperson": username,
						"i_updatetime":   time.Now().Unix(),
						"s_status":       "已完成",
					},
				})
				if !success {
					msg = "更新任务信息失败"
				}
			}
		} else {
			success = false
			msg = "任务暂未分配"
		}
	} else {
		success = false
		msg = "查询任务失败"
	}
	f.ServeJson(map[string]interface{}{"success": success, "msg": msg})
}
