package com.cmvr.web.controller.system;

import com.cmvr.common.annotation.Log;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.core.domain.entity.SysFileInfo;
import com.cmvr.common.core.page.TableDataInfo;
import com.cmvr.common.enums.BusinessType;
import com.cmvr.system.service.ISysFileService;
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 SysFileInfoController extends BaseController {

    private final ISysFileService sysFileService;

    @ApiOperation("查询文件")
    @GetMapping("/list")
    public TableDataInfo query(SysFileInfo sysFileInfo) {
        startPage();
        List<SysFileInfo> list = sysFileService.selectFileList(sysFileInfo);
        return getDataTable(list);
    }

    @ApiOperation(value = "上传文件，上传后返回文件URL", notes = "0:其他、1:普通文件、2:图片、3:音频、4:视频")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(
            @RequestParam("fileType") Integer fileType,
            @RequestPart("file") MultipartFile file
    ) {
        return AjaxResult.success(sysFileService.uploadFile(file, fileType));
    }


    @ApiOperation("删除文件")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(sysFileService.deleteFileByIds(groupIds));
    }
}
