package com.securitycontrol.files.controller;

import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.entity.file.FileExportVo;
import com.securitycontrol.files.mongodb.service.FileUploadService;
import com.securitycontrol.files.mongodb.util.MongodbFileUtil;
import com.securitycontrol.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("/file/")
@Slf4j
public class FileUploadController {

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

    @ApiOperation(value = "获取图片base64地址")
    @PostMapping("getImgBase64")
    public Result<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 Result.ok(sysFile);
        } catch (Exception e) {
            log.error("获取图片的base64地址",e);
            return Result.fail();
        }
    }

    @ApiOperation(value = "获取图片base64地址")
    @PostMapping("getFileBast64")
    public Result<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 Result.ok(sysFile);
        } catch (Exception e) {
            log.error("获取图片的base64地址",e);
            return Result.fail();
        }
    }

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


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

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

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