package com.hzs.member.account.controller.api;

import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.account.CuMemberBank;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.dto.CuMemberBankParam;
import com.hzs.member.account.service.ICuMemberBankService;
import com.hzs.member.account.service.ICuMemberWithdrawService;
import com.hzs.member.account.vo.CuMemberBankVO;
import com.hzs.member.sms.service.IApiAliSmsService;
import com.hzs.third.bankcard.IBankCardServiceApi;
import com.hzs.third.bankcard.dto.BankCardParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员银行卡 前端控制器
 */
@RestController
@RequestMapping("/api/bank")
public class ApiCuMemberBankController extends BaseController {

    @Autowired
    private ICuMemberBankService iCuMemberBankService;
    @Autowired
    private ICuMemberWithdrawService iCuMemberWithdrawService;
    @Autowired
    private IApiAliSmsService iApiAliSmsService;

    @DubboReference
    IBankCardServiceApi iBankCardServiceApi;

    /**
     * 银行卡列表
     */
    @GetMapping("/list")
    public AjaxResult getBankCardList() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(iCuMemberBankService.selectBankCardByPkMember(pkMember, pkCountry));
    }

    /**
     * 银行卡四要素验证
     */
    @PostMapping("/verify-bank-card")
    public AjaxResult verifyBankCard(@RequestBody CuMemberBankParam memberBankParam) {
        if (StringUtils.isEmpty(memberBankParam.getCardNumber())) {
            return AjaxResult.error("银行卡号不能为空！");
        }
        if (StringUtils.isEmpty(memberBankParam.getIdCard())) {
            return AjaxResult.error("身份证号码不能为空！");
        }
        if (StringUtils.isEmpty(memberBankParam.getAccountName())) {
            return AjaxResult.error("开户姓名不能为空！");
        }
        if (StringUtils.isEmpty(memberBankParam.getPhone())) {
            return AjaxResult.error("手机号不能为空！");
        }

        BankCardParam bankCard = BankCardParam.builder()
                .name(memberBankParam.getAccountName())
                .idCard(memberBankParam.getIdCard())
                .mobile(memberBankParam.getPhone())
                .bankCard(StringUtils.deleteWhitespace(memberBankParam.getCardNumber()))
                .build();
        R<?> result = iBankCardServiceApi.verifyBankCard(bankCard);
        return result.isSuccess() ? AjaxResult.success() : AjaxResult.error(result.getMsg());
    }

    /**
     * 绑定银行卡
     */
    @PostMapping("/add")
    @RepeatSubmitSimple
    public AjaxResult addBankInfo(@RequestBody CuMemberBankParam cuMemberBankParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();

        if (null == cuMemberBankParam.getPkBank()) {
            return AjaxResult.error("银行名称不能为空");
        }
        if (StringUtils.isEmpty(cuMemberBankParam.getSubBankName())) {
            return AjaxResult.error("开户支行不能为空");
        }
        if (StringUtils.isEmpty(cuMemberBankParam.getCardNumber())) {
            return AjaxResult.error("银行卡号不能为空！");
        }
        if (StringUtils.isEmpty(cuMemberBankParam.getAccountName())) {
            return AjaxResult.error("开户姓名不能为空！");
        }
        if (StringUtils.isEmpty(cuMemberBankParam.getIdCard())) {
            return AjaxResult.error("证件号码不能为空！");
        }
        if (StringUtils.isEmpty(cuMemberBankParam.getPhone())) {
            return AjaxResult.error("联系方式不能为空！");
        }
        String verificationCode = cuMemberBankParam.getVerificationCode();
        if (StringUtils.isEmpty(verificationCode)) {
            return AjaxResult.error("验证码不能为空！");
        }
        String message = iApiAliSmsService.checkCode(cuMemberBankParam.getPhone(), verificationCode, null, pkMember);
        if (null != message) {
            return AjaxResult.error(message);
        }

        // 银行卡号去空格
        String cardNumber = cuMemberBankParam.getCardNumber();
        cuMemberBankParam.setCardNumber(StringUtils.deleteWhitespace(cardNumber));
        cuMemberBankParam.setPkCountry(pkCountry);
        cuMemberBankParam.setPkMember(pkMember);
        return toAjax(iCuMemberBankService.saveMemberBank(cuMemberBankParam));
    }

    /**
     * 解绑银行卡
     */
    @GetMapping("/unbind-bank")
    public AjaxResult unbindBank(Long pkId) {
        Assert.notNull(pkId, "银行卡号不能为空！");
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();
        // 查询默认银行卡
        CuMemberBankVO cuMemberBankVO = iCuMemberBankService.selectCuMemberBankById(pkId);
        // 解绑默认银行卡需校验是否有进行的提现
        if (null != cuMemberBankVO) {
            List<Long> pkIds = iCuMemberWithdrawService.selectWithdrawMember(pkCountry, pkMember);
            if (CollUtil.isNotEmpty(pkIds)) {
                return AjaxResult.error("提现正在审批中，请稍后再试");
            }
        }
        return AjaxResult.success(iCuMemberBankService.unbindBank(pkId, pkMember));
    }

    /**
     * 修改默认银行卡
     */
    @GetMapping("/update-default")
    public AjaxResult updateDefault(Long pkId) {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        // 查询银行卡
        CuMemberBank cuMemberBank = iCuMemberBankService.getById(pkId);
        // 解绑默认银行卡需校验是否有进行的提现
        if (null != cuMemberBank) {
            List<Long> pkIds = iCuMemberWithdrawService.selectWithdrawMember(pkCountry, pkMember);
            if (CollUtil.isNotEmpty(pkIds)) {
                return AjaxResult.error("提现正在审批中，请稍后再试");
            }
        }
        return toAjax(iCuMemberBankService.updateDefault(pkId, pkMember, pkCountry));
    }

    /**
     * 默认银行卡
     */
    @GetMapping("/default-bank")
    public AjaxResult getDefaultBankCard() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(iCuMemberBankService.isBindBank(pkMember, pkCountry));
    }

}
