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

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.userexpand.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userexpand.UserExpandDO;
import cn.iocoder.yudao.module.member.service.userexpand.UserExpandService;

@Tag(name = "管理后台 - 用户拓展属性")
@RestController
@RequestMapping("/member/user-expand")
@Validated
public class UserExpandController {

    @Resource
    private UserExpandService userExpandService;

    @PostMapping("/create")
    @Operation(summary = "创建用户拓展属性")
    @PreAuthorize("@ss.hasPermission('member:user-expand:create')")
    public CommonResult<Long> createUserExpand(@Valid @RequestBody UserExpandSaveReqVO createReqVO) {
        return success(userExpandService.createUserExpand(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户拓展属性")
    @PreAuthorize("@ss.hasPermission('member:user-expand:update')")
    public CommonResult<Boolean> updateUserExpand(@Valid @RequestBody UserExpandSaveReqVO updateReqVO) {
        userExpandService.updateUserExpand(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户拓展属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-expand:delete')")
    public CommonResult<Boolean> deleteUserExpand(@RequestParam("id") Long id) {
        userExpandService.deleteUserExpand(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户拓展属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-expand:query')")
    public CommonResult<UserExpandRespVO> getUserExpand(@RequestParam("id") Long id) {
        UserExpandDO userExpand = userExpandService.getUserExpand(id);
        return success(BeanUtils.toBean(userExpand, UserExpandRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户拓展属性分页")
    @PreAuthorize("@ss.hasPermission('member:user-expand:query')")
    public CommonResult<PageResult<UserExpandRespVO>> getUserExpandPage(@Valid UserExpandPageReqVO pageReqVO) {
        PageResult<UserExpandDO> pageResult = userExpandService.getUserExpandPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserExpandRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户拓展属性 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-expand:export')")
    @OperateLog(type = EXPORT)
    public void exportUserExpandExcel(@Valid UserExpandPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserExpandDO> list = userExpandService.getUserExpandPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户拓展属性.xls", "数据", UserExpandRespVO.class,
                        BeanUtils.toBean(list, UserExpandRespVO.class));
    }

}