package ink.wgink.module.file.controller.api;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.file.enums.UploadTypeEnum;
import ink.wgink.module.file.pojo.dtos.FileUploadSuccessDTO;
import ink.wgink.module.file.service.v2.IFileV2Service;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @ClassName: FileController
 * @Description: 文件处理
 * @Author: WangGeng
 * @Date: 2019/3/10 7:03 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "文件接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/file/v2")
public class FileV2Controller extends DefaultBaseController {

    @Autowired
    private IFileV2Service fileV2Service;

    @ApiOperation(value = "上传文件", notes = "上传文件接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件name", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-file")
    public SuccessResultData<FileUploadSuccessDTO> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, Object> params = requestParams();
        return uploadSingle(file, UploadTypeEnum.FILE, params);
    }

    @ApiOperation(value = "上传图片", notes = "上传图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "文件name", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-image")
    public SuccessResultData<FileUploadSuccessDTO> uploadImage(@RequestParam("image") MultipartFile image) {
        Map<String, Object> params = requestParams();
        return uploadSingle(image, UploadTypeEnum.IMAGE, params);
    }

    @ApiOperation(value = "上传视频", notes = "上传视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "video", value = "文件video", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-video")
    public SuccessResultData<FileUploadSuccessDTO> uploadVideo(@RequestParam("video") MultipartFile video) {
        Map<String, Object> params = requestParams();
        return uploadSingle(video, UploadTypeEnum.VIDEO, params);
    }

    @ApiOperation(value = "上传音频", notes = "上传音频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "audio", value = "文件audio", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("upload-audio")
    public SuccessResultData<FileUploadSuccessDTO> uploadAudio(@RequestParam("audio") MultipartFile audio) {
        Map<String, Object> params = requestParams();
        return uploadSingle(audio, UploadTypeEnum.AUDIO, params);
    }

    /**
     * 上传文件
     *
     * @param uploadFile
     * @param params
     * @return
     */
    private SuccessResultData<FileUploadSuccessDTO> uploadSingle(MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params) {
        return new SuccessResultData<>(fileV2Service.uploadSingle(uploadFile, uploadTypeEnum, params));
    }

}
