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

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.userquotatotal.vo.*;
import com.yolo.keyboard.dal.dataobject.userquotatotal.KeyboardUserQuotaTotalDO;
import com.yolo.keyboard.service.userquotatotal.KeyboardUserQuotaTotalService;

@Tag(name = "管理后台 - 用户免费功能永久总次数额度表（所有功能共用）")
@RestController
@RequestMapping("/keyboard/user-quota-total")
@Validated
public class KeyboardUserQuotaTotalController {

    @Resource
    private KeyboardUserQuotaTotalService userQuotaTotalService;

    @PostMapping("/create")
    @Operation(summary = "创建用户免费功能永久总次数额度表（所有功能共用）")
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:create')")
    public CommonResult<Long> createUserQuotaTotal(@Valid @RequestBody KeyboardUserQuotaTotalSaveReqVO createReqVO) {
        return success(userQuotaTotalService.createUserQuotaTotal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户免费功能永久总次数额度表（所有功能共用）")
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:update')")
    public CommonResult<Boolean> updateUserQuotaTotal(@Valid @RequestBody KeyboardUserQuotaTotalSaveReqVO updateReqVO) {
        userQuotaTotalService.updateUserQuotaTotal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户免费功能永久总次数额度表（所有功能共用）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:delete')")
    public CommonResult<Boolean> deleteUserQuotaTotal(@RequestParam("id") Long id) {
        userQuotaTotalService.deleteUserQuotaTotal(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户免费功能永久总次数额度表（所有功能共用）")
                @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:delete')")
    public CommonResult<Boolean> deleteUserQuotaTotalList(@RequestParam("ids") List<Long> ids) {
        userQuotaTotalService.deleteUserQuotaTotalListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户免费功能永久总次数额度表（所有功能共用）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:query')")
    public CommonResult<KeyboardUserQuotaTotalRespVO> getUserQuotaTotal(@RequestParam("id") Long id) {
        KeyboardUserQuotaTotalDO userQuotaTotal = userQuotaTotalService.getUserQuotaTotal(id);
        return success(BeanUtils.toBean(userQuotaTotal, KeyboardUserQuotaTotalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户免费功能永久总次数额度表（所有功能共用）分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:query')")
    public CommonResult<PageResult<KeyboardUserQuotaTotalRespVO>> getUserQuotaTotalPage(@Valid KeyboardUserQuotaTotalPageReqVO pageReqVO) {
        PageResult<KeyboardUserQuotaTotalDO> pageResult = userQuotaTotalService.getUserQuotaTotalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserQuotaTotalRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户免费功能永久总次数额度表（所有功能共用） Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-quota-total:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserQuotaTotalExcel(@Valid KeyboardUserQuotaTotalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserQuotaTotalDO> list = userQuotaTotalService.getUserQuotaTotalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户免费功能永久总次数额度表（所有功能共用）.xls", "数据", KeyboardUserQuotaTotalRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserQuotaTotalRespVO.class));
    }

}