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

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.userpurchaserecords.vo.*;
import com.yolo.keyboard.dal.dataobject.userpurchaserecords.KeyboardUserPurchaseRecordsDO;
import com.yolo.keyboard.service.userpurchaserecords.KeyboardUserPurchaseRecordsService;

@Tag(name = "管理后台 - 用户内购记录")
@RestController
@RequestMapping("/keyboard/user-purchase-records")
@Validated
public class KeyboardUserPurchaseRecordsController {

    @Resource
    private KeyboardUserPurchaseRecordsService userPurchaseRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户内购记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:create')")
    public CommonResult<Integer> createUserPurchaseRecords(@Valid @RequestBody KeyboardUserPurchaseRecordsSaveReqVO createReqVO) {
        return success(userPurchaseRecordsService.createUserPurchaseRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户内购记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:update')")
    public CommonResult<Boolean> updateUserPurchaseRecords(@Valid @RequestBody KeyboardUserPurchaseRecordsSaveReqVO updateReqVO) {
        userPurchaseRecordsService.updateUserPurchaseRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户内购记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:delete')")
    public CommonResult<Boolean> deleteUserPurchaseRecords(@RequestParam("id") Integer id) {
        userPurchaseRecordsService.deleteUserPurchaseRecords(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户内购记录")
                @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:delete')")
    public CommonResult<Boolean> deleteUserPurchaseRecordsList(@RequestParam("ids") List<Integer> ids) {
        userPurchaseRecordsService.deleteUserPurchaseRecordsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户内购记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:query')")
    public CommonResult<KeyboardUserPurchaseRecordsRespVO> getUserPurchaseRecords(@RequestParam("id") Integer id) {
        KeyboardUserPurchaseRecordsDO userPurchaseRecords = userPurchaseRecordsService.getUserPurchaseRecords(id);
        return success(BeanUtils.toBean(userPurchaseRecords, KeyboardUserPurchaseRecordsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户内购记录分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:query')")
    public CommonResult<PageResult<KeyboardUserPurchaseRecordsRespVO>> getUserPurchaseRecordsPage(@Valid KeyboardUserPurchaseRecordsPageReqVO pageReqVO) {
        PageResult<KeyboardUserPurchaseRecordsDO> pageResult = userPurchaseRecordsService.getUserPurchaseRecordsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserPurchaseRecordsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户内购记录 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-purchase-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPurchaseRecordsExcel(@Valid KeyboardUserPurchaseRecordsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserPurchaseRecordsDO> list = userPurchaseRecordsService.getUserPurchaseRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户内购记录.xls", "数据", KeyboardUserPurchaseRecordsRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserPurchaseRecordsRespVO.class));
    }

}