package com.kexue.skills.controller;

import com.github.pagehelper.PageInfo;
import com.kexue.skills.annotation.RequireAuth;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.WithdrawalRecord;
import com.kexue.skills.entity.dto.WithdrawalRecordDto;
import com.kexue.skills.service.WithdrawalRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigDecimal;

/**
 * 提现记录控制器
 *
 * @author 王志维
 * @since 2026-03-25
 */
@Tag(name = "提现记录管理 Api")
@RestController
@RequestMapping("/api/withdrawalRecord")
public class WithdrawalRecordController {
    /**
     * 服务对象
     */
    @Resource
    private WithdrawalRecordService withdrawalRecordService;

    /**
     * 分页查询
     *
     * @param queryDto 查询参数
     * @return 分页结果
     */
    @Operation(summary = "分页查询提现记录", description = "分页查询提现记录")
    @PostMapping("/pageList")
    @RequireAuth
    public CommonResult<PageInfo<WithdrawalRecord>> getPageList(@RequestBody WithdrawalRecordDto queryDto) {
        return CommonResult.success(this.withdrawalRecordService.getPageList(queryDto));
    }

    /**
     * 查询列表
     *
     * @param queryDto 查询参数
     * @return 列表结果
     */
    @Operation(summary = "查询提现记录列表", description = "查询提现记录列表")
    @PostMapping("/list")
    @RequireAuth
    public CommonResult<List<WithdrawalRecord>> getList(@RequestBody WithdrawalRecordDto queryDto) {
        return CommonResult.success(this.withdrawalRecordService.getList(queryDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param recordId 主键
     * @return 单条数据
     */
    @Operation(summary = "通过ID查询提现记录", description = "通过ID查询提现记录")
    @PostMapping("/queryById/{recordId}")
    @RequireAuth
    public CommonResult<WithdrawalRecord> queryById(@Parameter(description = "提现记录ID") @PathVariable("recordId") Long recordId) {
        return CommonResult.success(this.withdrawalRecordService.queryById(recordId));
    }

    /**
     * 通过用户ID查询提现记录
     *
     * @param userId 用户ID
     * @return 提现记录列表
     */
    @Operation(summary = "通过用户ID查询提现记录", description = "通过用户ID查询提现记录")
    @PostMapping("/queryByUserId/{userId}")
    @RequireAuth
    public CommonResult<List<WithdrawalRecord>> queryByUserId(@Parameter(description = "用户ID") @PathVariable("userId") Long userId) {
        return CommonResult.success(this.withdrawalRecordService.queryByUserId(userId));
    }

    /**
     * 提交提现申请
     *
     * @param userId 用户ID
     * @param amount 提现金额
     * @param bankName 银行名称
     * @param bankAccount 银行账号
     * @param bankCardholder 持卡人姓名
     * @param remark 备注
     * @return 提现记录
     */
    @Operation(summary = "提交提现申请", description = "提交提现申请")
    @PostMapping("/submit")
    @RequireAuth
    public CommonResult<WithdrawalRecord> submitWithdrawal(
            @Parameter(description = "用户ID") @RequestParam("userId") Long userId,
            @Parameter(description = "提现金额") @RequestParam("amount") BigDecimal amount,
            @Parameter(description = "银行名称") @RequestParam("bankName") String bankName,
            @Parameter(description = "银行账号") @RequestParam("bankAccount") String bankAccount,
            @Parameter(description = "持卡人姓名") @RequestParam("bankCardholder") String bankCardholder,
            @Parameter(description = "备注") @RequestParam("remark") String remark) {
        WithdrawalRecord record = this.withdrawalRecordService.submitWithdrawal(userId, amount, bankName, bankAccount, bankCardholder, remark);
        return CommonResult.success(record);
    }

    /**
     * 处理提现
     *
     * @param recordId 记录ID
     * @param status 状态
     * @param remark 备注
     * @return 处理结果
     */
    @Operation(summary = "处理提现", description = "处理提现")
    @PostMapping("/process")
    @RequireAuth
    public CommonResult<Integer> processWithdrawal(
            @Parameter(description = "提现记录ID") @RequestParam("recordId") Long recordId,
            @Parameter(description = "状态：3.成功 4.失败") @RequestParam("status") Integer status,
            @Parameter(description = "备注") @RequestParam("remark") String remark) {
        int result = this.withdrawalRecordService.processWithdrawal(recordId, status, remark);
        return CommonResult.success(result);
    }
}