package cn.iocoder.byzs.module.system.controller.admin.invitecode;

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;
import cn.iocoder.byzs.module.system.controller.admin.invitecode.vo.InviteCodePageReqVO;
import cn.iocoder.byzs.module.system.controller.admin.invitecode.vo.InviteCodeRespVO;
import cn.iocoder.byzs.module.system.controller.admin.invitecode.vo.InviteCodeSaveReqVO;
import cn.iocoder.byzs.module.system.dal.dataobject.invitecode.InviteCodeDO;
import cn.iocoder.byzs.module.system.service.invitecode.InviteCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 邀请码")
@RestController
@RequestMapping("/system/invite-code")
@Validated
public class InviteCodeController {

    @Resource
    private InviteCodeService inviteCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建邀请码")
    @PreAuthorize("@ss.hasPermission('system:invite-code:create')")
    public CommonResult<Long> createInviteCode(@Valid @RequestBody InviteCodeSaveReqVO createReqVO) {
        return success(inviteCodeService.createInviteCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新邀请码")
    @PreAuthorize("@ss.hasPermission('system:invite-code:update')")
    public CommonResult<Boolean> updateInviteCode(@Valid @RequestBody InviteCodeSaveReqVO updateReqVO) {
        inviteCodeService.updateInviteCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邀请码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:invite-code:delete')")
    public CommonResult<Boolean> deleteInviteCode(@RequestParam("id") Long id) {
        inviteCodeService.deleteInviteCode(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除邀请码")
                @PreAuthorize("@ss.hasPermission('system:invite-code:delete')")
    public CommonResult<Boolean> deleteInviteCodeList(@RequestParam("ids") List<Long> ids) {
        inviteCodeService.deleteInviteCodeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邀请码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:invite-code:query')")
    public CommonResult<InviteCodeRespVO> getInviteCode(@RequestParam("id") Long id) {
        InviteCodeDO inviteCode = inviteCodeService.getInviteCode(id);
        return success(BeanUtils.toBean(inviteCode, InviteCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邀请码分页")
    @PreAuthorize("@ss.hasPermission('system:invite-code:query')")
    public CommonResult<PageResult<InviteCodeRespVO>> getInviteCodePage(@Valid InviteCodePageReqVO pageReqVO) {
        PageResult<InviteCodeRespVO> pageResult = inviteCodeService.getInviteCodePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邀请码 Excel")
    @PreAuthorize("@ss.hasPermission('system:invite-code:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInviteCodeExcel(@Valid InviteCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InviteCodeRespVO> list = inviteCodeService.getInviteCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "邀请码.xls", "数据", InviteCodeRespVO.class, list);
    }

}