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.constant.CacheConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EBusinessModule;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
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.domain.system.config.BdTradeWhiteConfig;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.member.account.dto.CuMemberBankParam;
import com.hzs.member.account.service.ICuMemberBankService;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.account.vo.CuMemberBankVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员银行卡 前端控制器
 *
 * @author hzs
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/api/bank")
public class ApiCuMemberBankController extends BaseController {

    @Autowired
    private ICuMemberBankService cuMemberBankService;

    @Autowired
    private ICuMemberBaseService iCuMemberBaseService;

    @Autowired
    private RedisService redisService;


    /**
     * 是否绑定银行卡
     *
     * @return AjaxResult
     */
    @GetMapping("/is-bind")
    public AjaxResult isBindBank() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuMemberBankVO cuMemberBank = cuMemberBankService.isBindBank(pkMember, pkCountry);
        String flag = null == cuMemberBank.getDefaultCard() ? EYesNo.NO.getValue() : EYesNo.YES.getValue();
        return AjaxResult.success().put("flag", flag);
    }

    /**
     * 解绑银行卡
     *
     * @return AjaxResult
     */
    @GetMapping("/unbind-bank")
    public AjaxResult unbindBank(Long pkId) {
        Assert.notNull(pkId, TransactionUtils.getContent(FinanceMsgConstants.BANK_CARD_CANNOT_EMPTY));
        return AjaxResult.success(cuMemberBankService.unbindBank(pkId));
    }


    /**
     * 校验开户名
     *
     * @param accountName 开户名
     * @return AjaxResult
     */
    @GetMapping("/check-account-name")
    public AjaxResult checkAccountName(String accountName) {
        Assert.notNull(accountName, TransactionUtils.getContent(FinanceMsgConstants.ACCOUNT_NAME_CANNOT_EMPTY));
        String memberCode = SecurityUtils.getMemberCode();
        String flag = memberCode.equals(accountName) ? EYesNo.YES.getValue() : EYesNo.NO.getValue();
        return AjaxResult.success().put("flag", flag);
    }


    /**
     * 绑定银行卡
     *
     * @param cuMemberBankParam 银行卡入参
     * @return AjaxResult
     */
    @PostMapping("/add")
    @RepeatSubmitSimple
    public AjaxResult addBankInfo(@RequestBody CuMemberBankParam cuMemberBankParam) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();
        cuMemberBankParam.setPkCountry(pkCountry);
        cuMemberBankParam.setPkMember(pkMember);
        //开户支行
        if (StringUtils.isEmpty(cuMemberBankParam.getSubBankName())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.OPENING_BRANCH_CANNOT_EMPTY));
        }
        //开户姓名
        if (StringUtils.isEmpty(cuMemberBankParam.getAccountName())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ACCOUNT_NAME_CANNOT_EMPTY));
        }
        //银行卡号
        if (StringUtils.isEmpty(cuMemberBankParam.getCardNumber())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BANK_CARD_CANNOT_EMPTY));
        }
        //银行
        if (null == cuMemberBankParam.getPkBank()) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.PLEASE_SELECT_BANK));
        }
        //证件号码
        if (StringUtils.isEmpty(cuMemberBankParam.getIdCard())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.ID_CARD_CANNOT_EMPTY));
        }

        // 锁定的key
        String bankKey = CacheConstants.CU_MEMBER_BANK_LOCK + pkMember;
        try {
            // 锁定8秒，防止出现重复操作
            if (redisService.lockKeyOnce(bankKey, 8)) {
                List<CuMemberBank> cuMemberBanks = cuMemberBankService.selectRepeatBank(cuMemberBankParam);
                if (CollUtil.isNotEmpty(cuMemberBanks)) {
                    return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.BANK_CARD_ALREADY_EXISTS));
                }
                return toAjax(cuMemberBankService.saveMemberBank(cuMemberBankParam));
            }
        } finally {
            // 执行结束释放锁
            redisService.unlock(bankKey);
        }
        return AjaxResult.error("数据正在操作中，请稍后刷新重试");
    }


    @GetMapping("/list")
    public AjaxResult getBankCardList() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(cuMemberBankService.selectBankCardByPkMember(pkMember, pkCountry));
    }

    @GetMapping("/default-bank")
    public AjaxResult getDefaultBankCard() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(cuMemberBankService.isBindBank(pkMember, pkCountry));
    }

    /**
     * 修改默认银行卡
     *
     * @param pkId ID
     * @return AjaxResult
     */
    @GetMapping("/update-default")
    public AjaxResult updateDefault(Long pkId) {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return toAjax(cuMemberBankService.updateDefault(pkId, pkMember, pkCountry));
    }


    /**
     * 查询是否设置提现白名单
     *
     * @return AjaxResult
     */
    @GetMapping("/is-white-list")
    public AjaxResult isWhiteList() {
        String flag = getConfigWhite(SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success().put("flag", flag);
    }

    /**
     * 查询提现白名单配置
     *
     * @param pkCountry 国家ID
     * @param pkMember  会员ID
     * @return String
     */
    private String getConfigWhite(Integer pkCountry, Long pkMember) {
        BdTradeWhiteConfig bdTradeWhiteConfig = iCuMemberBaseService.selectConfigWhite(pkMember, pkCountry, EBusinessModule.WITHDRAWAL.getValue());
        String flag = null == bdTradeWhiteConfig ? EYesNo.NO.getValue() : EYesNo.YES.getValue();
        return flag;
    }

}
