package cn.iocoder.yudao.module.member.controller.admin.configdata;

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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.configdata.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.configdata.ConfigDataDO;
import cn.iocoder.yudao.module.member.service.configdata.ConfigDataService;

@Tag(name = "管理后台 - 配置数据")
@RestController
@RequestMapping("/member/config-data")
@Validated
public class ConfigDataController {

    @Resource
    private ConfigDataService configDataService;

    @PostMapping("/create")
    @Operation(summary = "创建配置数据")
    @PreAuthorize("@ss.hasPermission('member:config-data:create')")
    public CommonResult<Long> createConfigData(@Valid @RequestBody ConfigDataSaveReqVO createReqVO) {
        return success(configDataService.createConfigData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配置数据")
    @PreAuthorize("@ss.hasPermission('member:config-data:update')")
    public CommonResult<Boolean> updateConfigData(@Valid @RequestBody ConfigDataSaveReqVO updateReqVO) {
        configDataService.updateConfigData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配置数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:config-data:delete')")
    public CommonResult<Boolean> deleteConfigData(@RequestParam("id") Long id) {
        configDataService.deleteConfigData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得配置数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:config-data:query')")
    public CommonResult<ConfigDataRespVO> getConfigData(@RequestParam("id") Long id) {
        ConfigDataDO configData = configDataService.getConfigData(id);
        return success(BeanUtils.toBean(configData, ConfigDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配置数据分页")
    @PreAuthorize("@ss.hasPermission('member:config-data:query')")
    public CommonResult<PageResult<ConfigDataRespVO>> getConfigDataPage(@Valid ConfigDataPageReqVO pageReqVO) {
        PageResult<ConfigDataDO> pageResult = configDataService.getConfigDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配置数据 Excel")
    @PreAuthorize("@ss.hasPermission('member:config-data:export')")
    @OperateLog(type = EXPORT)
    public void exportConfigDataExcel(@Valid ConfigDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigDataDO> list = configDataService.getConfigDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配置数据.xls", "数据", ConfigDataRespVO.class,
                        BeanUtils.toBean(list, ConfigDataRespVO.class));
    }

}