package log

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

type ApiLogCoding struct {
}

// @Tags logCoding
// @Summary 创建基础日志码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CodingLogRequest true "parent_id父id, coding日志码, describe日志描述"
// @Success 200 {object} response.Response{msg=string} "创建基础日志码"
// @Router /logCoding/create [post]
func (s *ApiLogCoding) CreateCoding(c *gin.Context) {
	var api request.CodingLogRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	codeStr := strconv.Itoa(api.Coding)
	if len(codeStr) != 7 {
		response.FailWithMessage("编号是7位", c)
		return
	}
	if err := ServiceLogCoding.CreateCoding(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags logCoding
// @Summary 更新基础日志码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CodingLogRequest true "coding日志码, describe日志描述"
// @Success 200 {object} response.Response{msg=string} "更新基础日志码"
// @Router /logCoding/update [post]
func (s *ApiLogCoding) UpdateCoding(c *gin.Context) {
	var api request.CodingLogRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	codeStr := strconv.Itoa(api.Coding)
	if len(codeStr) != 7 {
		response.FailWithMessage("编号是7位", c)
		return
	}
	if api.Id == 0 {
		response.FailWithMessage("参数id没有", c)
		return
	}
	if err := ServiceLogCoding.UpdateLogCoding(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags logCoding
// @Summary 根据id获取coding
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取coding"
// @Success 200 {object} response.Response{data=response.GetLogCodingReply} "根据id获取coding,返回包括coding详情"
// @Router /logCoding/getCodeById [post]
func (s *ApiLogCoding) GetCodeById(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 := ServiceLogCoding.GetCodeById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags logCoding
// @Summary 根据id获取coding
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取coding"
// @Success 200 {object} response.Response{data=response.GetLogCodingReply} "根据id获取coding,返回包括coding详情"
// @Router /logCoding/getCodeList [post]
func (s *ApiLogCoding) GetCodeList(c *gin.Context) {
	var paramsInfo request.GetCodeListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	api, _, err := ServiceLogCoding.GetCodeLogList(paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}
