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

import com.yolo.keyboard.dal.dataobject.userwallet.KeyboardWalletTransactionDO;
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.userwallet.vo.*;
import com.yolo.keyboard.dal.dataobject.userwallet.KeyboardUserWalletDO;
import com.yolo.keyboard.service.userwallet.KeyboardUserWalletService;

@Tag(name = "管理后台 - 用户钱包")
@RestController
@RequestMapping("/keyboard/user-wallet")
@Validated
public class KeyboardUserWalletController {

    @Resource
    private KeyboardUserWalletService userWalletService;

    @PostMapping("/create")
    @Operation(summary = "创建用户钱包")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:create')")
    public CommonResult<Long> createUserWallet(@Valid @RequestBody KeyboardUserWalletSaveReqVO createReqVO) {
        return success(userWalletService.createUserWallet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户钱包")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:update')")
    public CommonResult<Boolean> updateUserWallet(@Valid @RequestBody KeyboardUserWalletSaveReqVO updateReqVO) {
        userWalletService.updateUserWallet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户钱包")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:delete')")
    public CommonResult<Boolean> deleteUserWallet(@RequestParam("id") Long id) {
        userWalletService.deleteUserWallet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户钱包")
                @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:delete')")
    public CommonResult<Boolean> deleteUserWalletList(@RequestParam("ids") List<Long> ids) {
        userWalletService.deleteUserWalletListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户钱包")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:query')")
    public CommonResult<KeyboardUserWalletRespVO> getUserWallet(@RequestParam("id") Long id) {
        KeyboardUserWalletDO userWallet = userWalletService.getUserWallet(id);
        return success(BeanUtils.toBean(userWallet, KeyboardUserWalletRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户钱包分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:query')")
    public CommonResult<PageResult<KeyboardUserWalletRespVO>> getUserWalletPage(@Valid KeyboardUserWalletPageReqVO pageReqVO) {
        PageResult<KeyboardUserWalletDO> pageResult = userWalletService.getUserWalletPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserWalletRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户钱包 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserWalletExcel(@Valid KeyboardUserWalletPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserWalletDO> list = userWalletService.getUserWalletPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户钱包.xls", "数据", KeyboardUserWalletRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserWalletRespVO.class));
    }

    // ==================== 子表（钱包交易记录） ====================

    @GetMapping("/wallet-transaction/page")
    @Operation(summary = "获得钱包交易记录分页")
    @Parameter(name = "userId", description = "用户 Id")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:query')")
    public CommonResult<PageResult<KeyboardWalletTransactionDO>> getWalletTransactionPage(PageParam pageReqVO,
                                                                                          @RequestParam("userId") Long userId) {
        return success(userWalletService.getWalletTransactionPage(pageReqVO, userId));
    }

    @PostMapping("/wallet-transaction/create")
    @Operation(summary = "创建钱包交易记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:create')")
    public CommonResult<Long> createWalletTransaction(@Valid @RequestBody KeyboardWalletTransactionDO walletTransaction) {
        return success(userWalletService.createWalletTransaction(walletTransaction));
    }

    @PutMapping("/wallet-transaction/update")
    @Operation(summary = "更新钱包交易记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:update')")
    public CommonResult<Boolean> updateWalletTransaction(@Valid @RequestBody KeyboardWalletTransactionDO walletTransaction) {
        userWalletService.updateWalletTransaction(walletTransaction);
        return success(true);
    }

    @DeleteMapping("/wallet-transaction/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除钱包交易记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:delete')")
    public CommonResult<Boolean> deleteWalletTransaction(@RequestParam("id") Long id) {
        userWalletService.deleteWalletTransaction(id);
        return success(true);
    }

    @DeleteMapping("/wallet-transaction/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除钱包交易记录")
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:delete')")
    public CommonResult<Boolean> deleteWalletTransactionList(@RequestParam("ids") List<Long> ids) {
        userWalletService.deleteWalletTransactionListByIds(ids);
        return success(true);
    }

    @GetMapping("/wallet-transaction/get")
    @Operation(summary = "获得钱包交易记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-wallet:query')")
    public CommonResult<KeyboardWalletTransactionDO> getWalletTransaction(@RequestParam("id") Long id) {
        return success(userWalletService.getWalletTransaction(id));
    }

}