package app

import (
	"ln/nft/global"
	"ln/nft/model/app"
	appReq "ln/nft/model/app/request"
	"ln/nft/model/common/request"
	"ln/nft/model/common/response"
	exa "ln/nft/model/example"
	exampleRes "ln/nft/model/example/response"
	"ln/nft/service"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ArtsApi struct {
}

var artsService = service.ServiceGroupApp.AppServiceGroup.ArtsService

// CreateArts 创建Arts
// @Tags Arts
// @Summary 创建Arts
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.Arts true "创建Arts"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /arts/createArts [post]
func (artsApi *ArtsApi) CreateArts(c *gin.Context) {
	var arts app.Arts
	_ = c.ShouldBindJSON(&arts)
	if err := artsService.CreateArts(arts); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteArts 删除Arts
// @Tags Arts
// @Summary 删除Arts
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.Arts true "删除Arts"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /arts/deleteArts [delete]
func (artsApi *ArtsApi) DeleteArts(c *gin.Context) {
	var arts app.Arts
	_ = c.ShouldBindJSON(&arts)
	if err := artsService.DeleteArts(arts); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

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

// UpdateArts 更新Arts
// @Tags Arts
// @Summary 更新Arts
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.Arts true "更新Arts"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /arts/updateArts [put]
func (artsApi *ArtsApi) UpdateArts(c *gin.Context) {
	var arts app.Arts
	_ = c.ShouldBindJSON(&arts)
	if err := artsService.UpdateArts(arts); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindArts 用id查询Arts
// @Tags Arts
// @Summary 用id查询Arts
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query app.Arts true "用id查询Arts"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /arts/findArts [get]
func (artsApi *ArtsApi) FindArts(c *gin.Context) {
	var arts app.Arts
	_ = c.ShouldBindQuery(&arts)
	if rearts, err := artsService.GetArts(arts.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rearts": rearts}, c)
	}
}

// GetArtsList 分页获取Arts列表
// @Tags Arts
// @Summary 分页获取Arts列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query appReq.ArtsSearch true "分页获取Arts列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /arts/getArtsList [get]
func (artsApi *ArtsApi) GetArtsList(c *gin.Context) {
	var pageInfo appReq.ArtsSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if list, total, err := artsService.GetArtsInfoList(pageInfo); 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)
	}
}

func (artsApi *ArtsApi) UploadFile(c *gin.Context) {
	var file exa.ExaFileUploadAndDownload
	noSave := c.DefaultQuery("noSave", "0")
	_, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
		response.FailWithMessage("接收文件失败", c)
		return
	}
	file, err = fileUploadAndDownloadService.UploadFile(header, noSave) // 文件上传后拿到文件路径
	if err != nil {
		global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
		response.FailWithMessage("修改数据库链接失败", c)
		return
	}

	response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
}
