package vip.xiaonuo.disk.controller;

import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.disk.domain.StorageBean;
import vip.xiaonuo.disk.dto.file.UploadFileDTO;
import vip.xiaonuo.disk.service.IStorageReService;
import vip.xiaonuo.disk.service.ResourceFileService;
import vip.xiaonuo.disk.vo.file.UploadFileVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Tag(name = "filetransfer", description = "该接口为文件传输接口，主要用来做文件的上传、下载和预览，用于在线教育单拆")
@RestController
@RequestMapping("/resourceFile")
public class ResourceFileController {

    @Resource
    private ResourceFileService resourceFileService;



    @Resource
    private IStorageReService storageReService;

    public static final String SUCCESS = "操作成功";

    public static final String CURRENT_MODULE = "文件传输接口";

    /**
     * 上传前校验状况接口
     * 校验是否已上传过无需上传
     * 校验是否已有分片，并将上传好的分片返给前端
     * */
    @Operation(summary = "极速上传", description = "校验文件MD5判断文件是否存在，如果存在直接上传成功并返回skipUpload=true，如果不存在返回skipUpload=false需要再次调用该接口的POST方法", tags = {"filetransfer"})
    @RequestMapping(value = "/uploadfile", method = RequestMethod.GET)
    @MyLog(operation = "极速上传", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UploadFileVo> uploadFileSpeed(HttpServletRequest request, UploadFileDTO uploadFileDto) {
        String userId = StpLoginUserUtil.getLoginUser().getId();

        boolean isCheckSuccess = storageReService.checkStorage(userId, uploadFileDto.getTotalSize());
        if (!isCheckSuccess) {
            return RestResult.fail().message("存储空间不足");
        }
        UploadFileVo uploadFileVo = resourceFileService.uploadFileSpeed(request,userId,uploadFileDto);
        return RestResult.success().data(uploadFileVo);

    }


    @Operation(summary = "上传文件", description = "真正的上传文件接口", tags = {"filetransfer"})
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @MyLog(operation = "上传文件", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UploadFileVo> uploadFile(HttpServletRequest request, UploadFileDTO uploadFileDto) {
        String userId = StpLoginUserUtil.getLoginUser().getId();
        boolean isCheckSuccess = storageReService.checkStorage(userId, uploadFileDto.getTotalSize());
        if (!isCheckSuccess) {
            return RestResult.fail().message("存储空间不足");
        }
        resourceFileService.uploadFile(request, uploadFileDto, userId);

        UploadFileVo uploadFileVo = new UploadFileVo();


        return RestResult.success().data(uploadFileVo);

    }


    @Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})
    @RequestMapping(value = "/getstorage", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<StorageBean> getStorage(HttpServletRequest httpServletRequest) {

//        JwtUser sessionUserBean = SessionUtil.getSession();
        String userId = StpLoginUserUtil.getLoginUser().getId();
        StorageBean storageBean = new StorageBean();

        storageBean.setUserId(userId);


        Long storageSize = resourceFileService.selectStorageSizeByUserId(userId);
        StorageBean storage = new StorageBean();
        storage.setUserId(userId);
        storage.setStorageSize(storageSize);
        Long totalStorageSize = storageReService.getTotalStorageSize(userId);
        storage.setTotalStorageSize(totalStorageSize);
        return RestResult.success().data(storage);

    }

}
