package cn.iocoder.yudao.module.system.controller.admin.config;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.config.vo.CommonConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.config.vo.CommonConfigRespVO;
import cn.iocoder.yudao.module.system.controller.admin.config.vo.CommonConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.config.CommonConfigDO;
import cn.iocoder.yudao.module.system.service.config.CommonConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 通用配置")
@RestController
@RequestMapping("/system/common/config")
@Validated
public class CommonConfigController {

    @Resource
    private CommonConfigService configService;

    @PostMapping("/create")
    @Operation(summary = "创建通用配置")
    @PreAuthorize("@ss.hasPermission('common:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody CommonConfigSaveReqVO createReqVO) {
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通用配置")
    @PreAuthorize("@ss.hasPermission('common:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody CommonConfigSaveReqVO updateReqVO) {
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通用配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通用配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:config:query')")
    public CommonResult<CommonConfigDO> getConfig(@RequestParam("configType") Long configType) {
        CommonConfigDO config = configService.getConfig(configType);
        return success(config);
    }

    @GetMapping("/page")
    @Operation(summary = "获得通用配置分页")
    @PreAuthorize("@ss.hasPermission('common:config:query')")
    public CommonResult<PageResult<CommonConfigRespVO>> getConfigPage(@Valid CommonConfigPageReqVO pageReqVO) {
        PageResult<CommonConfigDO> pageResult = configService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommonConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通用配置 Excel")
    @PreAuthorize("@ss.hasPermission('common:config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigExcel(@Valid CommonConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommonConfigDO> list = configService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通用配置.xls", "数据", CommonConfigRespVO.class,
                        BeanUtils.toBean(list, CommonConfigRespVO.class));
    }

}
