package cn.ezhizao.project.tool.file.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.tool.file.domain.File;
import cn.ezhizao.project.tool.file.service.IFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件库Controller
 *
 * @author ruoyi
 * @date 2022-12-15
 */
@RestController
@RequestMapping("/tool/file")
public class FileController extends BaseController
{
    @Resource
    private IFileService fileService;

    /**
     * 查询文件库列表
     */
    @PreAuthorize("@ss.hasPermi('tool:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(File file) throws NoSuchFieldException, IllegalAccessException {
        startPage();
        List<File> list = fileService.getList(file);
        return getDataTable(list);
    }

    /**
     * 导出文件库列表
     */
    @PreAuthorize("@ss.hasPermi('tool:file:export')")
    @Log(title = "文件库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, File file) throws NoSuchFieldException, IllegalAccessException {
        List<File> list = fileService.getList(file);
        ExcelUtil<File> util = new ExcelUtil<File>(File.class);
        util.exportExcel(response, list, "文件库数据");
    }

    /**
     * 获取文件库详细信息
     */
    @PreAuthorize("@ss.hasPermi('tool:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fileService.getById(id));
    }

    /**
     * 新增文件库
     */
    @PreAuthorize("@ss.hasPermi('tool:file:add')")
    @Log(title = "文件库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody File file) throws NoSuchFieldException, IllegalAccessException {
        return toAjax(fileService.save(file));
    }

    /**
     * 修改文件库
     */
    @PreAuthorize("@ss.hasPermi('tool:file:edit')")
    @Log(title = "文件库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody File file)
    {
        return toAjax(fileService.updateById(file));
    }

    /**
     * 删除文件库
     */
    @PreAuthorize("@ss.hasPermi('tool:file:remove')")
    @Log(title = "文件库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(fileService.removeBatchByIds(ids));
    }
}
