package log

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

type ApiEnvLog struct {
}

// @Tags logEnv
// @Summary 创建基础环境码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.EnvLogRequest true " coding环境编码, describe环境描述""
// @Success 200 {object} response.Response{msg=string} "创建环境编码"
// @Router /logEnv/create [post]
func (s *ApiEnvLog) CreateEnv(c *gin.Context) {
	var api log.EnvLog
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := ServiceLogEnv.CreateEnv(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags logEnv
// @Summary 更新环境编码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.EnvLogRequest true "coding环境编码, describe环境描述"
// @Success 200 {object} response.Response{msg=string} "更新环境编码"
// @Router /logEnv/update [post]
func (s *ApiEnvLog) UpdateEnv(c *gin.Context) {
	var api request.EnvLogRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if api.Id == 0 {
		response.FailWithMessage("参数id没有", c)
		return
	}
	if err := ServiceLogEnv.UpdateEnv(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags logEnv
// @Summary 根据id获取log env
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取log env"
// @Success 200 {object} response.Response{data=response.EnvLogReply} "根据id获取返回log env详情"
// @Router /logEnv/getCodeById [post]
func (s *ApiEnvLog) GetEnvById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	api, err := ServiceLogEnv.GetEnvById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags logEnv
// @Summary 根据id获取log env
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=[]response.EnvLogReply} "返回log env列表"
// @Router /logEnv/getEnvList [post]
func (s *ApiEnvLog) GetEnvList(c *gin.Context) {
	var paramsInfo request.GetCodeListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	api, _, err := ServiceLogEnv.GetEnvList(paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}
