package com.aegis.web.controller.file;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.file.domain.FileInfo;
import com.aegis.file.service.IFileService;

/**
 * 文件管理Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {
    
    @Autowired
    private IFileService fileService;
    
    /**
     * 上传文件（单个）
     */
    @PreAuthorize("@ss.hasPermi('file:upload')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) {
        // Service层已处理异常并抛出ServiceException，这里直接调用即可
        // 全局异常处理器会统一处理ServiceException
        FileInfo fileInfo = fileService.uploadFile(file);
        return success(fileInfo);
    }
    
    /**
     * 根据ID获取文件信息
     */
    @PreAuthorize("@ss.hasPermi('file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        FileInfo fileInfo = fileService.selectFileById(id);
        if (fileInfo == null) {
            return error("文件不存在或已删除");
        }
        return success(fileInfo);
    }
    
    /**
     * 批量获取文件信息
     */
    @PreAuthorize("@ss.hasPermi('file:query')")
    @GetMapping("/batch")
    public AjaxResult getBatch(@RequestParam("fileIds") Long[] fileIds) {
        List<FileInfo> files = fileService.selectFilesByIds(fileIds);
        return success(files);
    }
    
    /**
     * 下载文件
     */
    @PreAuthorize("@ss.hasPermi('file:download')")
    @GetMapping("/{id}/download")
    public void downloadFile(@PathVariable Long id, HttpServletResponse response) {
        fileService.downloadFile(id, response);
    }
    
    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('file:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileInfo fileInfo) {
        startPage();
        List<FileInfo> list = fileService.selectFileList(fileInfo);
        return getDataTable(list);
    }
    
    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(fileService.deleteFile(id));
    }
}