package com.ruoyi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.annotations.UserLoginToken;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.code.Token;
import com.ruoyi.controller.req.WithApplyReq;
import com.ruoyi.enums.process.ProcessResultEnums;
import com.ruoyi.frequency.auth.entity.Auth;
import com.ruoyi.frequency.auth.service.AuthService;
import com.ruoyi.frequency.bankcard.entity.BankCard;
import com.ruoyi.frequency.bankcard.service.BankCardService;
import com.ruoyi.frequency.bondwithdrawalrecord.service.BondWithdrawalRecordService;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.highendauth.entity.HighendAuth;
import com.ruoyi.frequency.highendauth.service.HighendAuthService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.BodyIdReq;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 保证金提现相关
 * @author liwenlong
 */
@Slf4j
@RestController
@RequestMapping("/api/bondWithdrawal")
@Api(tags = "保证金提现相关")
public class ApiBondWithdrawalController extends RyController {


    @Autowired
    private BondWithdrawalRecordService bondWithdrawalRecordService;

    @Autowired
    private AuthService authService;

    @Autowired
    private HighendAuthService highendAuthService;

    @Autowired
    private UserService userService;

    @Autowired
    private BankCardService bankCardService;

    @UserLoginToken(userTokenPre = {Token.store,Token.enterprise})
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ApiOperation(value = "申请提现")
    public ResponseData apply(@RequestBody @Valid WithApplyReq req) {

        UserVo userVo = getUserVo();

        userService.storeByEnterprise(userVo);

        if (ObjectUtil.isNotEmpty(req.getBankCardId())){
            //查询用户银行卡信息
            BankCard bankCard = bankCardService.getById(req.getBankCardId());

            if (bankCard != null){
                req.setBank(bankCard.getBank());
                req.setSubbranch(bankCard.getOpeningBranch());
                req.setBankCard(bankCard.getBankCard());
                req.setFullName(bankCard.getFullName());
            }
        }
        return bondWithdrawalRecordService.apply(req,userVo);
    }

    @UserLoginToken
    @RequestMapping(value = "/withdrawalRecordPage", method = RequestMethod.POST)
    @ApiOperation(value = "提现记录")
    public ResponseData withdrawalRecordPage(@RequestBody @Valid PageBasic req) {
        return bondWithdrawalRecordService.withdrawalRecordPage(req,getUserVo());
    }

    @UserLoginToken
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ApiOperation(value = "提现记录详情")
    public ResponseData detail(@RequestBody @Valid BodyIdReq req) {
        return bondWithdrawalRecordService.getDetail(req.getId());
    }

}


