package cn.iocoder.yudao.module.server.controller.admin.fileforconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.server.controller.admin.fileforconfig.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.fileforconfig.FileForConfigDO;
import cn.iocoder.yudao.module.server.service.fileforconfig.FileForConfigService;

@Tag(name = "管理后台 - 文件配置")
@RestController
@RequestMapping("/server/file-for-config")
@Validated
public class FileForConfigController {

    @Resource
    private FileForConfigService fileForConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建文件配置")
    @PreAuthorize("@ss.hasPermission('server:file-for-config:create')")
    public CommonResult<Long> createFileForConfig(@Valid @RequestBody FileForConfigSaveReqVO createReqVO) {
        return success(fileForConfigService.createFileForConfig(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新文件配置")
    @PreAuthorize("@ss.hasPermission('server:file-for-config:update')")
    public CommonResult<Boolean> updateFileForConfig(@Valid @RequestBody FileForConfigSaveReqVO updateReqVO) {
        fileForConfigService.updateFileForConfig(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除文件配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:file-for-config:delete')")
    public CommonResult<Boolean> deleteFileForConfig(@RequestParam("id") Long id) {
        fileForConfigService.deleteFileForConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('server:file-for-config:query')")
    public CommonResult<FileForConfigRespVO> getFileForConfig(@RequestParam("id") Long id) {
        FileForConfigDO fileForConfig = fileForConfigService.getFileForConfig(id);
        return success(BeanUtils.toBean(fileForConfig, FileForConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件配置分页")
//    @PreAuthorize("@ss.hasPermission('server:file-for-config:query')")
    public CommonResult<PageResult<FileForConfigRespVO>> getFileForConfigPage(@Valid FileForConfigPageReqVO pageReqVO) {
        PageResult<FileForConfigDO> pageResult = fileForConfigService.getFileForConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileForConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件配置 Excel")
    @PreAuthorize("@ss.hasPermission('server:file-for-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFileForConfigExcel(@Valid FileForConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FileForConfigDO> list = fileForConfigService.getFileForConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件配置.xls", "数据", FileForConfigRespVO.class,
                        BeanUtils.toBean(list, FileForConfigRespVO.class));
    }

}