package task

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

type CentralControlApi struct {

}

//创建中控配置
func (a *CentralControlApi) CreateCentralControlConf (c *gin.Context)  {
	var conf task.CentralControlConf
	_ = c.ShouldBindJSON(&conf)
	if conf.Mirror == "" {
		response.FailWithMessage("请传入镜像名称", c)
		return
	}
	if conf.Script == "" {
		response.FailWithMessage("请传入脚本名称", c)
		return
	}
	if err := utils.Verify(conf, utils.CentralControlConfVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centralControlService.CreateCentralControlConf(conf);err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

//删除配置列表
func (a *CentralControlApi) DeleteCentralControlConf(c *gin.Context) {
	var conf task.CentralControlConf
	 _ = c.ShouldBindJSON(&conf)

	 //数据校验
	 if err := utils.Verify(conf, utils.CentralControlConfVerify); err != nil {
		 response.FailWithMessage(err.Error(), c)
		 return
	 }
	if err := centralControlService.DeleteCentralControlConf(conf); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除配置列表
func (a *CentralControlApi) DeleteCentralControlConfByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := centralControlService.DeleteCentralControlConfByIds(ids.Ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//更新中控配置
func (a *CentralControlApi) UpdateCentralControlConf (c *gin.Context)  {
	var conf task.CentralControlConf
	_ = c.ShouldBindJSON(&conf)
	if err := utils.Verify(conf, utils.CentralControlConfVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centralControlService.UpdateCentralControlConf(conf); err != nil{
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (a *CentralControlApi) GetCentralControlConfById (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 conf, err := centralControlService.GetCentralControlConfById(id.ID); err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(conf, "获取成功", c)
	}
}

//获取中控配置列表
func (a *CentralControlApi) GetCentralControlConfList (c *gin.Context)  {
	//获取前端传值并校验
	var pageInfo taskRequest.SearchCentralControlConfParams
	_ = c.ShouldBindJSON(&pageInfo)
	//页面信息校验
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := centralControlService.GetCentralControlConfList(pageInfo.CentralControlConf, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil{
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}