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

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.userinvites.vo.*;
import com.yolo.keyboard.dal.dataobject.userinvites.KeyboardUserInvitesDO;
import com.yolo.keyboard.service.userinvites.KeyboardUserInvitesService;

@Tag(name = "管理后台 - 用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
@RestController
@RequestMapping("/keyboard/user-invites")
@Validated
public class KeyboardUserInvitesController {

    @Resource
    private KeyboardUserInvitesService userInvitesService;

    @PostMapping("/create")
    @Operation(summary = "创建用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:create')")
    public CommonResult<Long> createUserInvites(@Valid @RequestBody KeyboardUserInvitesSaveReqVO createReqVO) {
        return success(userInvitesService.createUserInvites(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:update')")
    public CommonResult<Boolean> updateUserInvites(@Valid @RequestBody KeyboardUserInvitesSaveReqVO updateReqVO) {
        userInvitesService.updateUserInvites(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:delete')")
    public CommonResult<Boolean> deleteUserInvites(@RequestParam("id") Long id) {
        userInvitesService.deleteUserInvites(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
                @PreAuthorize("@ss.hasPermission('keyboard:user-invites:delete')")
    public CommonResult<Boolean> deleteUserInvitesList(@RequestParam("ids") List<Long> ids) {
        userInvitesService.deleteUserInvitesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户邀请关系绑定台账表，记录新用户最终归属的邀请人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:query')")
    public CommonResult<KeyboardUserInvitesRespVO> getUserInvites(@RequestParam("id") Long id) {
        KeyboardUserInvitesDO userInvites = userInvitesService.getUserInvites(id);
        return success(BeanUtils.toBean(userInvites, KeyboardUserInvitesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户邀请关系绑定台账表，记录新用户最终归属的邀请人分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:query')")
    public CommonResult<PageResult<KeyboardUserInvitesRespVO>> getUserInvitesPage(@Valid KeyboardUserInvitesPageReqVO pageReqVO) {
        PageResult<KeyboardUserInvitesDO> pageResult = userInvitesService.getUserInvitesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserInvitesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户邀请关系绑定台账表，记录新用户最终归属的邀请人 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-invites:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserInvitesExcel(@Valid KeyboardUserInvitesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserInvitesDO> list = userInvitesService.getUserInvitesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户邀请关系绑定台账表，记录新用户最终归属的邀请人.xls", "数据", KeyboardUserInvitesRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserInvitesRespVO.class));
    }

}