package com.ruoyi.common.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.domain.TCommonFile;
import com.ruoyi.common.service.ITCommonFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 附件Controller
 * 
 * @author ruoyi
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/common/file")
public class TCommonFileController extends BaseController
{
    @Autowired
    private ITCommonFileService tCommonFileService;

    /**
     * 查询附件列表
     */
    @PreAuthorize("@ss.hasPermi('common:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCommonFile tCommonFile)
    {
        startPage();
        List<TCommonFile> list = tCommonFileService.selectTCommonFileList(tCommonFile);
        return getDataTable(list);
    }

    /**
     * 导出附件列表
     */
    @PreAuthorize("@ss.hasPermi('common:file:export')")
    @Log(title = "附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCommonFile tCommonFile)
    {
        List<TCommonFile> list = tCommonFileService.selectTCommonFileList(tCommonFile);
        ExcelUtil<TCommonFile> util = new ExcelUtil<TCommonFile>(TCommonFile.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('common:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(tCommonFileService.selectTCommonFileByFileId(fileId));
    }

    /**
     * 新增附件
     */
    @PreAuthorize("@ss.hasPermi('common:file:add')")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCommonFile tCommonFile)
    {
        return toAjax(tCommonFileService.insertTCommonFile(tCommonFile));
    }

    /**
     * 修改附件
     */
    @PreAuthorize("@ss.hasPermi('common:file:edit')")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCommonFile tCommonFile)
    {
        return toAjax(tCommonFileService.updateTCommonFile(tCommonFile));
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ss.hasPermi('common:file:remove')")
    @Log(title = "附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(tCommonFileService.deleteTCommonFileByFileIds(fileIds));
    }
}
