package fileManager

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

type ApiFileQiniu struct {
}

// QueryVersionByType 查询某task_id下某一类型文件最新一个渠道号
func (r *ApiFileQiniu) QueryVersionByType(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)

	if fileVersion, err := ServiceFileQiniu.QueryVersionByType(paramsInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(map[string]int64{"fileVersion": fileVersion}, "获取成功", c)
	}
}

// AddQiniuFile 新增文件
func (r *ApiFileQiniu) AddQiniuFile(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)

	if err := ServiceFileQiniu.AddQiniuFile(paramsInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithMessage("文件上传成功", c)
	}
}

// GetQiniuToken 获取七牛上传token
func (v *ApiFileQiniu) GetQiniuToken(c *gin.Context) {

	data := ServiceFileQiniu.GetQiniuToken()
	response.OkWithDetailed(data, "获取七牛上传TOKEN成功", c)
}

// GetMirrorDownloadByTaskId 根据任务id获取最新镜像下载链接
func (v *ApiFileQiniu) GetMirrorDownloadByTaskId(c *gin.Context) {
	//var paramsInfo request.FileQiniuRequest
	//_ = c.ShouldBindJSON(&paramsInfo)
	//global.GVA_LOG.Warn(strconv.Itoa(paramsInfo.TaskId))
	taskId := c.Query("task_id")
	//global.GVA_LOG.Warn(taskId)
	//if taskId == "" {
	//	response.FailWithMessage("获取失败,参数不正确", c)
	//}
	if data, err := ServiceFileQiniu.GetMirrorDownloadByTaskId(taskId); err != nil {
		//global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败,"+err.Error(), c)
	} else {
		response.OkWithDetailed(data, "获取成功", c)
	}
}

// DeleteQiniuFile 删除单个文件
func (v *ApiFileQiniu) DeleteQiniuFile(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := ServiceFileQiniu.DeleteQiniuFile(paramsInfo); err != nil {
		global.GVA_LOG.Error("文件删除失败!", zap.Error(err))
		response.FailWithMessage("文件删除失败,"+err.Error(), c)
	} else {
		response.OkWithMessage("文件删除成功", c)
	}
}

// DeleteQiniuFileAndRecord 从七牛云删除文件并且清除数据库记录
func (v *ApiFileQiniu) DeleteQiniuFileAndRecord(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := ServiceFileQiniu.DeleteQiniuFileAndRecord(paramsInfo); err != nil {
		global.GVA_LOG.Error("文件删除失败!", zap.Error(err))
		response.FailWithMessage("文件删除失败, "+err.Error(), c)
	} else {
		response.OkWithMessage("文件删除成功", c)
	}
}

// RenameQiniuFileAndRecord 重命名七牛云文件和数据库记录
func (v *ApiFileQiniu) RenameQiniuFileAndRecord(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := ServiceFileQiniu.RenameQiniuFileAndRecord(paramsInfo); err != nil {
		global.GVA_LOG.Error("文件重命名失败!", zap.Error(err))
		response.FailWithMessage("文件重命名失败, "+err.Error(), c)
	} else {
		response.OkWithMessage("文件重命名成功", c)
	}
}

// UpdateMirrorPushPolicy 修改镜像更新策略
func (v *ApiFileQiniu) UpdateMirrorPushPolicy(c *gin.Context) {
	var paramsInfo request.FileQiniuRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := ServiceFileQiniu.UpdateMirrorPushPolicy(paramsInfo); err != nil {
		global.GVA_LOG.Error("修改镜像更新策略失败!", zap.Error(err))
		response.FailWithMessage("修改镜像更新策略失败, "+err.Error(), c)
	} else {
		response.OkWithMessage("修改镜像更新策略成功", c)
	}
}
