package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ContentApi struct {
}

var contentService = service.ServiceGroupApp.AutoCodeServiceGroup.ContentService

// CreateContent 创建Content
// @Tags Content
// @Summary 创建Content
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Content true "创建Content"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /content/createContent [post]
func (contentApi *ContentApi) CreateContent(c *gin.Context) {
	var content autocode.Content
	_ = c.ShouldBindJSON(&content)
	if err := contentService.CreateContent(content); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteContent 删除Content
// @Tags Content
// @Summary 删除Content
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Content true "删除Content"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /content/deleteContent [delete]
func (contentApi *ContentApi) DeleteContent(c *gin.Context) {
	var content autocode.Content
	_ = c.ShouldBindJSON(&content)
	if err := contentService.DeleteContent(content); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteContentByIds 批量删除Content
// @Tags Content
// @Summary 批量删除Content
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Content"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /content/deleteContentByIds [delete]
func (contentApi *ContentApi) DeleteContentByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := contentService.DeleteContentByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateContent 更新Content
// @Tags Content
// @Summary 更新Content
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Content true "更新Content"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /content/updateContent [put]
func (contentApi *ContentApi) UpdateContent(c *gin.Context) {
	var content autocode.Content
	_ = c.ShouldBindJSON(&content)
	if err := contentService.UpdateContent(content); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindContent 用id查询Content
// @Tags Content
// @Summary 用id查询Content
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Content true "用id查询Content"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /content/findContent [get]
func (contentApi *ContentApi) FindContent(c *gin.Context) {
	var content autocode.Content
	_ = c.ShouldBindQuery(&content)
	if err, recontent := contentService.GetContent(content.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recontent": recontent}, c)
	}
}

// GetContentList 分页获取Content列表
// @Tags Content
// @Summary 分页获取Content列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.ContentSearch true "分页获取Content列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /content/getContentList [get]
func (contentApi *ContentApi) GetContentList(c *gin.Context) {
	var pageInfo autocodeReq.ContentSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := contentService.GetContentInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
