package com.fs.app.controller;

import com.fs.common.core.domain.AjaxResult;
import com.fs.file.domain.SysUploadFile;
import com.fs.file.service.ISysFileService;
import com.fs.file.vo.FileUploadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 通用文件上传
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/system/file")
@RequiredArgsConstructor
public class SysFileController {

    private final ISysFileService sysFileService;

    /**
     * 通用文件上传
     */
    @ApiOperation("通用文件上传")
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file,
                             @RequestParam("bizType") String bizType,
                             @RequestParam(value = "bizId", required = false) String bizId) {
        FileUploadVO vo = sysFileService.upload(file, bizType, bizId);
        return AjaxResult.success(vo);
    }

    /**
     * 批量文件上传
     */
    @ApiOperation("批量文件上传")
    @PostMapping("/upload/batch")
    public AjaxResult uploadBatch(@RequestParam("files") List<MultipartFile> files,
                                  @RequestParam("bizType") String bizType,
                                  @RequestParam(value = "bizId", required = false) String bizId) {
        List<FileUploadVO> voList = sysFileService.uploadBatch(files, bizType, bizId);
        return AjaxResult.success(voList);
    }

    /**
     * 秒传检查
     */
    @ApiOperation("秒传检查")
    @GetMapping("/fast-upload/check")
    public AjaxResult checkFastUpload(@RequestParam("md5") String md5,
                                      @RequestParam("bizType") String bizType,
                                      @RequestParam(value = "bizId", required = false) String bizId) {
        FileUploadVO vo = sysFileService.checkFastUpload(md5, bizType, bizId);
        if (vo != null) {
            return AjaxResult.success("秒传成功", vo);
        }
        return AjaxResult.error("文件不存在，需要上传");
    }

    /**
     * 绑定业务ID
     */
    @ApiOperation("绑定业务ID")
    @PostMapping("/bind")
    public AjaxResult bindBizId(@RequestParam("fileId") Long fileId,
                                @RequestParam("bizId") String bizId) {
        return AjaxResult.success(sysFileService.bindBizId(fileId, bizId));
    }

    /**
     * 批量绑定业务ID
     */
    @ApiOperation("批量绑定业务ID")
    @PostMapping("/bind/batch")
    public AjaxResult bindBizIdBatch(@RequestParam("fileIds") List<Long> fileIds,
                                     @RequestParam("bizId") String bizId) {
        return AjaxResult.success(sysFileService.bindBizIdBatch(fileIds, bizId));
    }

    /**
     * 根据业务查询文件列表
     */
    @ApiOperation("根据业务查询文件列表")
    @GetMapping("/list")
    public AjaxResult listByBiz(@RequestParam("bizType") String bizType,
                                @RequestParam("bizId") String bizId) {
        List<SysUploadFile> list = sysFileService.listByBiz(bizType, bizId);
        return AjaxResult.success(list);
    }

    /**
     * 删除文件
     */
    @ApiOperation("删除文件")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return AjaxResult.success(sysFileService.deleteById(id));
    }

    /**
     * 批量删除文件
     */
    @ApiOperation("批量删除文件")
    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        return AjaxResult.success(sysFileService.deleteByIds(ids));
    }
}
