package com.zksy.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.Result;
import com.zksy.park.domain.FileGeneral;
import com.zksy.park.service.FileGeneralService;
import com.zksy.service.MinioFileStorageService;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
@Api(value = "文件信息",tags = "文件信息")
public class FileController {
    @Autowired
    private FileGeneralService service;
    @Autowired
    private MinioFileStorageService minioFileStorageService;

    @Log(title = "文件信息-文件上传")
    @RequestMapping(path = "/filePath",method = RequestMethod.POST)
    @ApiOperation(value = "文件上传",notes = "文件上传")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "file", value = "文件", required = true, paramType = "path", allowMultiple = false, dataType = "file"),
//            @ApiImplicitParam(name = "businessType", value = "文件夹名称", required = true, paramType = "path", allowMultiple = false, dataType = "String")
//    })
    public Result minioUploadResUrl(MultipartFile file,String businessType) throws Exception {
        String result = minioFileStorageService.uploadFile(file,businessType);
        return Result.ok(result);
    }

    @Log(title = "文件信息-文件下载")
    @GetMapping("/downLoadFile")
    @ApiOperation(value = "文件下载")
    @ApiImplicitParam(name = "filePath", value = "文件目录", required = true, paramType = "query", allowMultiple = false, dataType = "String")
    @SneakyThrows(Exception.class)
    public Result downLoadFile(@RequestParam String filePath,HttpServletResponse response){
        try {
            minioFileStorageService.downLoadFile(filePath, response);
        }catch (Exception e){
            return Result.error("文件下载失败");
        }
        return Result.ok("文件下载成功");
    }

    @Log(title = "文件信息-文件删除")
    @GetMapping("/fileDeleteTP")
    @ApiOperation(value = "文件删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "文件名", required = true, paramType = "path", allowMultiple = false, dataType = "list"),
            @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "path", allowMultiple = false, dataType = "Long")
    })
    public Result fileDelete(@RequestParam String name,@RequestParam String id){
        try {
            service.removeById(id);
            minioFileStorageService.deleteFile(name);
        }catch (Exception e){
            e.printStackTrace();
        }

        return Result.ok();
    }

    @Log(title = "文件信息-查询文件")
    @GetMapping("/selectFile")
    @ApiOperation(value = "查询文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "对应表单id", required = true, paramType = "path", allowMultiple = false, dataType = "String"),
    })
    public Result selectFile(@RequestParam String formId){
        return Result.ok(service.selectFile(formId));
    }
}

