package com.yolo.keyboard.controller.admin.userinvitecodes;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.userinvitecodes.vo.*;
import com.yolo.keyboard.dal.dataobject.userinvitecodes.KeyboardUserInviteCodesDO;
import com.yolo.keyboard.service.userinvitecodes.KeyboardUserInviteCodesService;

@Tag(name = "管理后台 - 用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
@RestController
@RequestMapping("/keyboard/user-invite-codes")
@Validated
public class KeyboardUserInviteCodesController {

    @Resource
    private KeyboardUserInviteCodesService userInviteCodesService;

    @PostMapping("/create")
    @Operation(summary = "创建用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:create')")
    public CommonResult<Long> createUserInviteCodes(@Valid @RequestBody KeyboardUserInviteCodesSaveReqVO createReqVO) {
        return success(userInviteCodesService.createUserInviteCodes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:update')")
    public CommonResult<Boolean> updateUserInviteCodes(@Valid @RequestBody KeyboardUserInviteCodesSaveReqVO updateReqVO) {
        userInviteCodesService.updateUserInviteCodes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:delete')")
    public CommonResult<Boolean> deleteUserInviteCodes(@RequestParam("id") Long id) {
        userInviteCodesService.deleteUserInviteCodes(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
                @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:delete')")
    public CommonResult<Boolean> deleteUserInviteCodesList(@RequestParam("ids") List<Long> ids) {
        userInviteCodesService.deleteUserInviteCodesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:query')")
    public CommonResult<KeyboardUserInviteCodesRespVO> getUserInviteCodes(@RequestParam("id") Long id) {
        KeyboardUserInviteCodesDO userInviteCodes = userInviteCodesService.getUserInviteCodes(id);
        return success(BeanUtils.toBean(userInviteCodes, KeyboardUserInviteCodesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:query')")
    public CommonResult<PageResult<KeyboardUserInviteCodesRespVO>> getUserInviteCodesPage(@Valid KeyboardUserInviteCodesPageReqVO pageReqVO) {
        PageResult<KeyboardUserInviteCodesDO> pageResult = userInviteCodesService.getUserInviteCodesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserInviteCodesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invite-codes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserInviteCodesExcel(@Valid KeyboardUserInviteCodesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserInviteCodesDO> list = userInviteCodesService.getUserInviteCodesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户生成的邀请码表，用于邀请新用户注册/安装并建立邀请关系.xls", "数据", KeyboardUserInviteCodesRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserInviteCodesRespVO.class));
    }

}