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

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.configtype.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.configtype.ConfigTypeDO;
import cn.iocoder.yudao.module.member.service.configtype.ConfigTypeService;

@Tag(name = "管理后台 - 配置类型")
@RestController
@RequestMapping("/member/config-type")
@Validated
public class ConfigTypeController {

    @Resource
    private ConfigTypeService configTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建配置类型")
    @PreAuthorize("@ss.hasPermission('member:config-type:create')")
    public CommonResult<Long> createConfigType(@Valid @RequestBody ConfigTypeSaveReqVO createReqVO) {
        return success(configTypeService.createConfigType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新配置类型")
    @PreAuthorize("@ss.hasPermission('member:config-type:update')")
    public CommonResult<Boolean> updateConfigType(@Valid @RequestBody ConfigTypeSaveReqVO updateReqVO) {
        configTypeService.updateConfigType(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得配置类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:config-type:query')")
    public CommonResult<ConfigTypeRespVO> getConfigType(@RequestParam("id") Long id) {
        ConfigTypeDO configType = configTypeService.getConfigType(id);
        return success(BeanUtils.toBean(configType, ConfigTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得配置类型分页")
    @PreAuthorize("@ss.hasPermission('member:config-type:query')")
    public CommonResult<PageResult<ConfigTypeRespVO>> getConfigTypePage(@Valid ConfigTypePageReqVO pageReqVO) {
        PageResult<ConfigTypeDO> pageResult = configTypeService.getConfigTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出配置类型 Excel")
    @PreAuthorize("@ss.hasPermission('member:config-type:export')")
    @OperateLog(type = EXPORT)
    public void exportConfigTypeExcel(@Valid ConfigTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigTypeDO> list = configTypeService.getConfigTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "配置类型.xls", "数据", ConfigTypeRespVO.class,
                        BeanUtils.toBean(list, ConfigTypeRespVO.class));
    }

}