package task

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/response"
	"log-server/model/task"
	"log-server/model/task/request"
	"log-server/utils"
)

type UrgentTaskApi struct {
}

//查询空闲租机
func (a *UrgentTaskApi) GetUnusedPc (c *gin.Context)  {
	if pcList,err := urgentTaskService.GetUnusedPc(); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithDetailed(pcList, "获取成功", c)
	}
}

//创建紧急任务
func (a *UrgentTaskApi) CreateUrgentTask (c *gin.Context)  {
	var task1 task.CreateAndUpdateUrgentTaskConf
	_ = c.ShouldBindJSON(&task1)
	var pcCodeString string
	if task1.TaskId == 0 {
		response.FailWithMessage("请传入任务id", c)
		return
	}
	if len(task1.PcCode) == 0 {
		response.FailWithMessage("请传入租机列表", c)
		return
	}
	//处理pc_code数组
	for k,pcCode := range task1.PcCode{
		if k != len(task1.PcCode) - 1 {
			pcCodeString = pcCodeString + pcCode + ","
		} else {
			pcCodeString = pcCodeString + pcCode
		}
	}
	//创建实体
	entity := task.UrgentTaskConf {
		Id: task1.Id,
		TaskId: task1.TaskId,
		PcCode: pcCodeString,
	}
	if err := urgentTaskService.CreateUrgentTask(entity); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

//删除单个紧急任务
func (a *UrgentTaskApi) DeleteUrgentTask(c *gin.Context) {
	var task1 task.UrgentTaskConf
	_ = c.ShouldBindJSON(&task1)
	if err := utils.Verify(task1, utils.UrgentTaskVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := urgentTaskService.DeleteUrgentTask(task1); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除紧急任务
func (a *UrgentTaskApi) DeleteUrgentTaskByIds (c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := urgentTaskService.DeleteUrgentTaskByIds(ids.Ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//更新紧急任务
func (a *UrgentTaskApi) UpdateUrgentTask (c *gin.Context)  {
	var task1 task.CreateAndUpdateUrgentTaskConf
	_ = c.ShouldBindJSON(&task1)
	var pcCodeString string
	if task1.TaskId == 0 {
		response.FailWithMessage("请传入任务id", c)
		return
	}
	if len(task1.PcCode) == 0 {
		response.FailWithMessage("请传入租机列表", c)
		return
	}
	//处理pc_code数组
	for k,pcCode := range task1.PcCode{
		if k != len(task1.PcCode) - 1 {
			pcCodeString = pcCodeString + pcCode + ","
		} else {
			pcCodeString = pcCodeString + pcCode
		}
	}
	//创建实体
	entity := task.UrgentTaskConf {
		Id: task1.Id,
		TaskId: task1.TaskId,
		PcCode: pcCodeString,
		CreateDate: task1.CreateDate[:10],
	}

	if err := urgentTaskService.UpdateUrgentTask(entity); err != nil{
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

//id获取紧急任务
func (a *UrgentTaskApi) GetUrgentTaskById(c *gin.Context){
	var id request.GetById
	_ = c.ShouldBindJSON(&id)
	if err := utils.Verify(id, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if task1, err := urgentTaskService.GetUrgentTaskById(id.ID); err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(task1, "获取成功", c)
	}
}

//获取紧急任务列表
func (a *UrgentTaskApi) GetUrgentTaskList(c *gin.Context) {
	var paramsInfo request.SearchUrgentTaskParams
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := urgentTaskService.GetUrgentTaskList(paramsInfo.UrgentTaskConfRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     paramsInfo.Page,
			PageSize: paramsInfo.PageSize,
		}, "获取成功", c)
	}
}
