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"
	"strconv"
)

type ApiFileFolder struct {
}

func (v *ApiFileFolder) AddFileFolder(c *gin.Context) {
	var api request.FileFolderRequest
	_ = c.ShouldBindJSON(&api)
	//if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
	//	response.FailWithMessage(err.Error(), c)
	//	return
	//}
	//fmt.Printf("%#v\n", &api)
	if parentId, parentName, err := ServiceFileFolder.AddFileFolder(api); err != nil {
		global.GVA_LOG.Error("文件夹创建失败!", zap.Error(err))
		response.FailWithMessage("文件夹创建失败,"+err.Error(), c)
	} else {
		global.GVA_LOG.Info(parentName)
		response.OkWithDetailed(map[string]string{"id": strconv.Itoa(parentId), "name": parentName}, "文件夹创建成功", c)
	}
}

// GetAllTaskSimple 查询所有任务
func (api *ApiFileFolder) GetAllTaskSimple(c *gin.Context) {

	if list, err := ServiceFileFolder.GetAllTaskSimple(); err != nil {
		//global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		//响应结果
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}

// SelectFolderMsg 查询目录信息
func (api *ApiFileFolder) SelectFolderMsg(c *gin.Context) {
	var info request.FileFolderRequest
	_ = c.ShouldBindJSON(&info)
	if data, err := ServiceFileFolder.SelectFolderMsg(info); err != nil {
		response.OkWithDetailed(map[string]interface{}{"data": data}, "获取失败", c)
	} else {
		response.OkWithDetailed(map[string]interface{}{"data": data}, "获取成功", c)
	}
}

// GetFolderFileList 获取当前父文件夹下所有的子文件夹和文件
func (v *ApiFileFolder) GetFolderFileList(c *gin.Context) {
	var paramsInfo request.FileFolderPageRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	//开始查询当前父文件夹下的所有文件夹
	folderList, err := ServiceFileFolder.FileFolderList(c, paramsInfo.FileFolder)
	if err != nil {
		global.GVA_LOG.Error("FileFolderList获取失败!", zap.Error(err))
		response.FailWithMessage("FileFolderList获取失败", c)
	}
	//===============================
	//开始查询当前父文件夹下的所有文件
	fileList, err := ServiceFileQiniu.QueryFilesByParentId(paramsInfo.FileFolder.ParentId)
	if err != nil {
		global.GVA_LOG.Error("QueryFilesByParentId获取失败!", zap.Error(err))
		response.FailWithMessage("QueryFilesByParentId获取失败", c)
	}
	//===============================
	response.OkWithDetailed(map[string]interface{}{
		"folderList": folderList,
		"fileList":   fileList,
	}, "获取成功", c)
}

// QueryFolder 条件查询文件夹
func (v *ApiFileFolder) QueryFolder(c *gin.Context) {
	var paramsInfo request.FileFolderRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	//开始查询当前父文件夹下的所有文件夹
	folderList, err := ServiceFileFolder.QueryFolder(c, paramsInfo)
	if err != nil {
		global.GVA_LOG.Error("QueryFolder获取失败!", zap.Error(err))
		response.FailWithMessage("QueryFolder获取失败", c)
	}
	response.OkWithDetailed(map[string]interface{}{
		"folderList": folderList,
	}, "获取成功", c)
}
