package com.bonus.file.controller;


import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.BASE64DecodedMultipartFile;
import com.bonus.common.entity.file.FileExportVo;
import com.bonus.common.security.annotation.InnerAuth;
import com.bonus.file.mongodb.service.FileUploadService;
import com.bonus.file.mongodb.util.MongodbFileUtil;
import com.bonus.system.api.domain.SysFile;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author：cwchen
 * @date：2024-03-21-14:58
 * @version：1.0
 * @description：文件上传
 */
@RestController
@RequestMapping("/mongoFile/")
@Slf4j
public class MongoFileUploadController {

    @Resource(name = "fileMongoServiceImpl")
    private FileUploadService mongoService;

    @ApiOperation(value = "获取图片base64地址")
    @PostMapping("getImgBase64")
    @InnerAuth
    public R<SysFile> getImgBase64(@RequestBody String fileId){
        try {
            SysFile sysFile = new SysFile();
            FileExportVo fileExportVo = mongoService.downloadFile(fileId);
            String base64 = MongodbFileUtil.getBase64(fileExportVo);
            sysFile.setName(fileExportVo.getFileName());
            sysFile.setUrl(base64);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("获取图片的base64地址",e);
            return R.fail();
        }
    }

    @ApiOperation(value = "获取图片base64地址")
    @PostMapping("getFileBast64")
    @InnerAuth
    public R<SysFile> getFileBast64(@RequestBody String fileId){
        try {
            SysFile sysFile = new SysFile();
            FileExportVo fileExportVo = mongoService.downloadFile(fileId);
            String base64 = MongodbFileUtil.getBase64File(fileExportVo);
            sysFile.setName(fileExportVo.getFileName());
            sysFile.setUrl(base64);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("获取图片的base64地址",e);
            return R.fail();
        }
    }

    @ApiOperation(value = "获取图片base64地址")
    @PostMapping("getFileByte")
    @InnerAuth
    public R<byte[]> getFileByte(@RequestBody String fileId){
        try {
            SysFile sysFile = new SysFile();
            FileExportVo fileExportVo = mongoService.downloadFile(fileId);
            return R.ok(fileExportVo.getData());
        } catch (Exception e) {
            log.error("获取图片的base64地址",e);
            return R.fail();
        }
    }


    @ApiOperation(value = "单文件上传")
    @PostMapping("singleUploadFile")
    @InnerAuth
    public R singleUploadFile(@RequestParam(value = "file", required = false) MultipartFile file){
        try {
            FileExportVo fileExportVo = mongoService.uploadFile(file);
            return R.ok(fileExportVo);
        } catch (Exception e) {
            log.error("单文件上传",e);
            return R.fail();
        }
    }

    @ApiOperation(value = "多文件上传")
    @PostMapping("mostUploadFile")
    @InnerAuth
    public R mostUploadFile(@RequestParam(value = "files", required = false) MultipartFile[] files){
        try {
            List<MultipartFile> multipartFiles = Arrays.asList(files);
            List<FileExportVo> list = mongoService.uploadFiles(multipartFiles);
            return R.ok(list);
        } catch (Exception e) {
            log.error("多文件上传",e);
            return R.fail();
        }
    }

    @ApiOperation(value = "文件删除")
    @PostMapping("delFile")
    @InnerAuth
    public R delFile(@RequestBody String fileId){
        try {
            mongoService.removeFile(fileId);
            return R.ok();
        } catch (Exception e) {
            log.error("文件删除",e);
            return R.fail();
        }
    }

    @ApiOperation(value = "base64上传")
    @PostMapping("uploadFileByBase64")
    @InnerAuth
    public R uploadFileByBase64(@RequestParam(value = "base64", required = false) String base64){
        try {
            MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64);
            FileExportVo fileExportVo = mongoService.uploadFile(multipartFile);
            return R.ok(fileExportVo);
        } catch (Exception e) {
            log.error("base64上传",e);
            return R.fail();
        }
    }
}
