package com.ruoyi.cai.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.AccountBankcard;
import com.ruoyi.cai.domain.AccountCash;
import com.ruoyi.cai.domain.AccountRecharge;
import com.ruoyi.cai.dto.app.query.AccountAliBankCardRes;
import com.ruoyi.cai.dto.app.query.WithdrawReq;
import com.ruoyi.cai.dto.app.vo.AccountCashVo;
import com.ruoyi.cai.dto.app.vo.AccountRechargeVo;
import com.ruoyi.cai.dto.app.vo.user.UserAccountVo;
import com.ruoyi.cai.manager.CurrentUserManager;
import com.ruoyi.cai.service.AccountCashService;
import com.ruoyi.cai.service.AccountRechargeService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/wallet")
@Tag(name = "钱包操作相关的接口")
public class WalletController {

    @Autowired
    private CurrentUserManager currentUserManager;
    @Autowired
    private AccountCashService accountCashService;
    @Autowired
    private AccountRechargeService accountRechargeService;

    @GetMapping("/account")
    @Operation(summary = "快速获取当前用户账号余额信息")
    public R<UserAccountVo> currentAccount(){
        return R.ok(currentUserManager.currentAccount());
    }

    @GetMapping("/aliInfo")
    @Operation(summary = "获取绑定支付宝信息")
    public R<AccountBankcard> aliInfo(){
        AccountBankcard accountBankcard = currentUserManager.aliInfo();
        return R.ok(accountBankcard);
    }

    @PostMapping("/save-or-update/ali")
    @Operation(summary = "支付宝信息的新增和修改")
    public R<Boolean> updateAli(@RequestBody AccountAliBankCardRes res){
        currentUserManager.saveOrUpdateAliInfo(res);
        return R.ok(true);
    }

    @PostMapping("/withdraw/ali")
    @Operation(summary = "支付宝提现接口")
    public R<Boolean> withdraw(@RequestBody WithdrawReq res){
        res.setUserId(LoginHelper.getUserId());
        accountCashService.withdraw(res);
        return R.ok(true);
    }

    @GetMapping("/recharge/log")
    @Operation(summary = "充值记录-分页")
    public TableDataInfo<AccountRechargeVo> rechargeLog(PageQuery query){
        Long userId = LoginHelper.getUserId();
        Page<AccountRecharge> page = accountRechargeService.page(query.build(), Wrappers.lambdaQuery(AccountRecharge.class)
                .eq(AccountRecharge::getUserId, userId)
                .orderByDesc(AccountRecharge::getCreateTime));
        return TableDataInfo.build(page,AccountRechargeVo::new);
    }

    @GetMapping("/cash/log")
    @Operation(summary = "提现记录-分页")
    public TableDataInfo<AccountCashVo> cashLog(PageQuery query){
        Long userId = LoginHelper.getUserId();
        Page<AccountCash> page = accountCashService.page(query.build(), Wrappers.lambdaQuery(AccountCash.class)
                .eq(AccountCash::getUserId, userId)
                .orderByDesc(AccountCash::getCreateTime));
        return TableDataInfo.build(page,AccountCashVo::new);
    }

}
