package com.wteam.controller.oss;

import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.oss.service.OssService;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Tag(name = "文件相关接口管理")
@Slf4j
@RestController
@RequestMapping("/v1/oss")
public class OssController {

    @Autowired
    private OssService ossService;

    /**
     * 上传图片到oss
     *
     * @param moudle 属于哪一个模块的图片
     * @return 图片地址
     * @throws IOException
     */
    @PostMapping("batch/upload/{moudle}")
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage<List<String>> upload(@RequestParam("file") MultipartFile[] multipartFiles, @PathVariable String moudle) throws IOException {
        //文件对象,文件上传到哪个目录
//        InputStream inputStream = file.getInputStream();
//        String originalFileName = file.getOriginalFilename();
//        String uploadUrl = ossService.upload(inputStream, moudle, originalFileName);
        List<String> list = ossService.upFiles(moudle, multipartFiles);

        return ResultUtil.data(list);
    }


    @PostMapping("/upload/{moudle}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("oss上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, paramType = "query", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "moudle", value = "模块名，其实也就是上传到oss哪个文件夹", required = false, paramType = "path", dataType = "string"),
    })
    public String upload(@RequestParam("file") MultipartFile multipartFiles, @PathVariable String moudle) {
        //文件对象,文件上传到哪个目录
        try {
            InputStream inputStream = multipartFiles.getInputStream();
            String originalFileName = multipartFiles.getOriginalFilename();
            String uploadUrl = ossService.upload(inputStream, moudle, originalFileName);
            return uploadUrl;
        } catch (Exception e) {
//            System.out.println(e);
            return "";
        }
    }

    /**
     * 删除oss上的相关图片
     *
     * @param objectName 全限定路径文件名
     * @return
     */
    @GetMapping("/del")
    public String delete(@RequestParam String objectName) {//文件名
        return ossService.delete(objectName);
    }

}
