package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dto.UserBankCardDto;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.thirdparty.user.realname.RealnameAuthenticationPlugin;
import com.yonge.toolset.utils.idcard.IdcardInfoExtractor;
import com.yonge.toolset.utils.idcard.IdcardValidator;
import com.yonge.toolset.utils.validator.CommonValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonge.cooleshow.biz.dal.entity.UserBankCard;
import com.yonge.cooleshow.biz.dal.vo.UserBankCardVo;
import com.yonge.cooleshow.biz.dal.dto.search.UserBankCardSearch;
import com.yonge.cooleshow.biz.dal.dao.UserBankCardDao;
import com.yonge.cooleshow.biz.dal.service.UserBankCardService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserBankCardServiceImpl extends ServiceImpl<UserBankCardDao, UserBankCard> implements UserBankCardService {
    @Autowired
    private RealnameAuthenticationPlugin realnameAuthenticationPlugin;
	@Override
    public UserBankCardVo detail(Long id) {
        UserBankCardVo detail = baseMapper.detail(id);
        return detail;
    }
    
     @Override
    public IPage<UserBankCardVo> selectPage(IPage<UserBankCardVo> page, UserBankCardSearch query){
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult<UserBankCard> bankCard(UserBankCardDto bankCard) {
        //验证身份证合法性
        IdcardValidator idcardValidator = new IdcardValidator();
        //验证身份证号合法性
        boolean validatedAllIdcard = idcardValidator.isValidatedAllIdcard(bankCard.getIdCardNo());
        if (!validatedAllIdcard) {
            return HttpResponseResult.failed("身份证号不合法");
        }
        boolean isMobileNo = CommonValidator.isMobileNo(bankCard.getPhone());
        if (!isMobileNo) {
            return HttpResponseResult.failed("手机号不合法");
        }
        boolean isBankCard = CommonValidator.isBankCard(bankCard.getBankCard());
        if (!isBankCard) {
            return HttpResponseResult.failed("银行卡号不合法");
        }
        //验证重复
        UserBankCard oldBankCard = baseMapper.selectOne(Wrappers.<UserBankCard>lambdaQuery()
                .eq(UserBankCard::getUserId, bankCard.getUserId())
                .eq(UserBankCard::getBankCard, bankCard.getBankCard())
                .eq(UserBankCard::getDelFlag,false)
        );
        if(null != oldBankCard){
            return HttpResponseResult.failed("已经绑定该银行卡");
        }
        boolean verify = realnameAuthenticationPlugin.verify(bankCard.getName(), bankCard.getIdCardNo());
        if (!verify) {
            return HttpResponseResult.failed("未通过验证");
        }
	    //当前用户只有一张银行卡
        baseMapper.deleteByUserId(bankCard.getUserId());
        bankCard.setDefaultFlag(true);
        baseMapper.insert(bankCard);
        return HttpResponseResult.succeed(bankCard);
    }

    @Override
    public HttpResponseResult<UserBankCard> unBind(UserBankCardDto bankCard) {
        UserBankCardVo detail = baseMapper.detail(bankCard.getId());
        if(null == detail){
            return HttpResponseResult.failed("未找到银行卡信息");
        }
        //验证身份证合法性
        IdcardValidator idcardValidator = new IdcardValidator();
        //验证身份证号合法性
        boolean validatedAllIdcard = idcardValidator.isValidatedAllIdcard(bankCard.getIdCardNo());
        if (!validatedAllIdcard) {
            return HttpResponseResult.failed("身份证号不合法");
        }
        boolean verify = realnameAuthenticationPlugin.verify(bankCard.getName(), bankCard.getIdCardNo());
        if (!verify) {
            return HttpResponseResult.failed("未通过验证");
        }

        int i = baseMapper.deleteById(bankCard.getId());
        return HttpResponseResult.succeed(detail);
    }

}