package system

import (
	"sundynix-go/global"
	"sundynix-go/model/commom/request"
	"sundynix-go/model/commom/response"
	"sundynix-go/model/system"
	sysReq "sundynix-go/model/system/request"
	sysResp "sundynix-go/model/system/response"

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

type OssApi struct {
}

// UploadFile
// @tags 文件相关
// @Summary 文件上传
// @Security ApiKeyAuth
// @accept   multipart/form-data
// @Produce  application/json
// @Param file formData file true "上传文件"
// @Success 200 {object} response.Response{msg=string} "上传文件"
// @router /oss/upload [post]
func (o *OssApi) UploadFile(c *gin.Context) {
	var file system.Oss
	multipartFile, header, err := c.Request.FormFile("file")
	if err != nil {
		global.Logger.Error("接收文件失败!", zap.Error(err))
		response.FailWithMsg("接收文件失败!", c)
		return
	}
	file, err = ossService.Upload(multipartFile, header) //上传完成后拿到文件信息
	if err != nil {
		global.Logger.Error("上传文件失败!", zap.Error(err))
		response.FailWithMsg("上传文件失败!", c)
		return
	}
	response.OkWithData(sysResp.UploadFileResponse{File: file}, c)
}

// DeleteFile
// @tags 文件相关
// @Summary 删除文件
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除文件"
// @Success 200 {object} response.Response{msg=string} "删除文件"
// @router /oss/delete [post]
func (o *OssApi) DeleteFile(c *gin.Context) {
	var ids request.IdsReq
	err := c.ShouldBindJSON(&ids)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	err = ossService.DeleteFileByIds(ids)
	if err != nil {
		global.Logger.Error("删除文件失败!", zap.Error(err))
		response.FailWithMsg("删除文件失败!", c)
		return
	}
	response.OkWithMsg("删除文件成功!", c)
}

// GetFileList
// @tags 文件相关
// @Summary 文件列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body sysReq.GetOssFileList true "文件列表"
// @Success 200 {object} response.Response{data=string} "文件列表"
// @router /oss/getFileList [post]
func (o *OssApi) GetFileList(c *gin.Context) {
	var pageInfo sysReq.GetOssFileList
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	list, total, err := ossService.GetFileList(pageInfo)
	if err != nil {
		global.Logger.Error("获取文件列表失败!", zap.Error(err))
		response.FailWithMsg("获取文件列表失败!", c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Current,
		PageSize: pageInfo.PageSize,
	}, c)
}

// Detail
// @tags 文件相关
// @Summary 文件详情
// @Security ApiKeyAuth
// @Produce application/json
// @Param id query string true "文件id"
// @Success 200 {object} response.Response{data=string} "文件详情"
// @router /oss/detail [get]
func (o *OssApi) Detail(c *gin.Context) {
	id := c.Query("id")
	file, err := ossService.GetById(id)
	if err != nil {
		global.Logger.Error("获取文件详情失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(file, c)
}
