package com.ktg.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.system.domain.SysUploadFile;
import com.ktg.system.service.ISysUploadFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 文件Controller
 *
 * @author cgj
 * @date 2024-10-22
 */
@Api(tags = "文件")
@RestController
@RequestMapping("/system/file")
public class SysUploadFileController extends BaseController
{
    @Autowired
    private ISysUploadFileService sysUploadFileService;

    /**
     * 查询文件分页
     */
    @ApiOperation("查询文件-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "sysUploadFile", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/getSysUploadFilePage")
    public CommonResult<Page<SysUploadFile>> getSysUploadFilePage(Page<SysUploadFile> page, SysUploadFile sysUploadFile)
    {
        Page<SysUploadFile> result = sysUploadFileService.page(page, Wrappers.<SysUploadFile>lambdaQuery()
                .orderByDesc(SysUploadFile::getId));
        return CommonResult.success(result);
    }

    /**
     * 导出文件列表
     */
    @ApiOperation("导出文件列表")
    @Parameter(name = "sysUploadFile", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSysUploadFile")
    public void exportSysUploadFile(HttpServletResponse response, SysUploadFile sysUploadFile)
    {
        List<SysUploadFile> list = sysUploadFileService.selectSysUploadFileList(sysUploadFile);
        ExcelUtil<SysUploadFile> util = new ExcelUtil<SysUploadFile>(SysUploadFile.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @ApiOperation("获取文件详细信息")
    @Parameter(name = "id", description = "id")
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/selectSysUploadFileById")
    public CommonResult<SysUploadFile> selectSysUploadFileById(Long id)
    {
        return CommonResult.success(sysUploadFileService.selectSysUploadFileById(id));
    }

    /**
     * 新增文件
     */
    @ApiOperation("新增文件")
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping("/insertSysUploadFile")
    public CommonResult<Boolean> insertSysUploadFile(@RequestBody @Parameter(name = "sysUploadFile", description = "新增数据类,放到body") SysUploadFile sysUploadFile)
    {
        return CommonResult.success(sysUploadFileService.insertSysUploadFile(sysUploadFile) == 1);
    }

    /**
     * 修改文件
     */
    @ApiOperation("修改文件")
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSysUploadFile")
    public CommonResult<Boolean> updateSysUploadFile(@RequestBody @Parameter(name = "sysUploadFile", description = "修改数据类,放到body") SysUploadFile sysUploadFile)
    {
        return CommonResult.success(sysUploadFileService.updateSysUploadFile(sysUploadFile) == 1);
    }

    /**
     * 删除文件
     */
    @ApiOperation("删除文件")
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
	@PostMapping("/deleteSysUploadFileByIds")
    public CommonResult<Boolean> deleteSysUploadFileByIds(String ids)
    {
        return CommonResult.success(sysUploadFileService.deleteSysUploadFileByIds(ids) != 0);
    }
}
