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

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.themepurchase.vo.*;
import com.yolo.keyboard.dal.dataobject.themepurchase.KeyboardThemePurchaseDO;
import com.yolo.keyboard.service.themepurchase.KeyboardThemePurchaseService;

@Tag(name = "管理后台 - 皮肤购买记录表（积分支付）")
@RestController
@RequestMapping("/keyboard/theme-purchase")
@Validated
public class KeyboardThemePurchaseController {

    @Resource
    private KeyboardThemePurchaseService themePurchaseService;

    @PostMapping("/create")
    @Operation(summary = "创建皮肤购买记录表（积分支付）")
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:create')")
    public CommonResult<Long> createThemePurchase(@Valid @RequestBody KeyboardThemePurchaseSaveReqVO createReqVO) {
        return success(themePurchaseService.createThemePurchase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新皮肤购买记录表（积分支付）")
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:update')")
    public CommonResult<Boolean> updateThemePurchase(@Valid @RequestBody KeyboardThemePurchaseSaveReqVO updateReqVO) {
        themePurchaseService.updateThemePurchase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除皮肤购买记录表（积分支付）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:delete')")
    public CommonResult<Boolean> deleteThemePurchase(@RequestParam("id") Long id) {
        themePurchaseService.deleteThemePurchase(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除皮肤购买记录表（积分支付）")
                @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:delete')")
    public CommonResult<Boolean> deleteThemePurchaseList(@RequestParam("ids") List<Long> ids) {
        themePurchaseService.deleteThemePurchaseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得皮肤购买记录表（积分支付）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:query')")
    public CommonResult<KeyboardThemePurchaseRespVO> getThemePurchase(@RequestParam("id") Long id) {
        KeyboardThemePurchaseDO themePurchase = themePurchaseService.getThemePurchase(id);
        return success(BeanUtils.toBean(themePurchase, KeyboardThemePurchaseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得皮肤购买记录表（积分支付）分页")
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:query')")
    public CommonResult<PageResult<KeyboardThemePurchaseRespVO>> getThemePurchasePage(@Valid KeyboardThemePurchasePageReqVO pageReqVO) {
        PageResult<KeyboardThemePurchaseDO> pageResult = themePurchaseService.getThemePurchasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardThemePurchaseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出皮肤购买记录表（积分支付） Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:theme-purchase:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportThemePurchaseExcel(@Valid KeyboardThemePurchasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardThemePurchaseDO> list = themePurchaseService.getThemePurchasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "皮肤购买记录表（积分支付）.xls", "数据", KeyboardThemePurchaseRespVO.class,
                        BeanUtils.toBean(list, KeyboardThemePurchaseRespVO.class));
    }

}