package cn.pluss.platform.merchantBankCard.impl;

import cn.pluss.platform.BankCardService;
import cn.pluss.platform.IdCardService;
import cn.pluss.platform.entity.BankCard;
import cn.pluss.platform.entity.IdCard;
import cn.pluss.platform.entity.MerchantBankCard;
import cn.pluss.platform.mapper.MerchantBankCardMapper;
import cn.pluss.platform.merchantBankCard.MerchantBankCardService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("merchantBankCardService")
public class MerchantBankCardServiceImpl extends ServiceImpl<MerchantBankCardMapper, MerchantBankCard> implements MerchantBankCardService {

    @Autowired
    private IdCardService idCardService;
    @Autowired
    private BankCardService bankCardService;

    @Override
    public MerchantBankCard queryMerchantBankCard(String userId) {
        MerchantBankCard result = new MerchantBankCard();

        IdCard legalIdCard = idCardService.getCertIdCard(userId);
        BankCard bankCard = new BankCard().setUserId(userId).setAccountType(BankCard.ACCOUNT_TYPE_CERT);
        bankCard = bankCardService.getOne(new QueryWrapper<>(bankCard));

        if (legalIdCard != null) {
            result.setId(legalIdCard.getId());
            result.setBankCertName(legalIdCard.getCertName());
            result.setUserId(userId);
            result.setCertNo(legalIdCard.getCertNo());
            result.setCertType(legalIdCard.getCertType());
            result.setCertStartTime(legalIdCard.getCertStartTime());
            result.setCertEndTime(legalIdCard.getCertEndTime());
            result.setCertBacKPhoto(legalIdCard.getImgNegative());
            result.setCertFullPhoto(legalIdCard.getImgPositive());
        }

        if (bankCard != null) {
            result.setBankName(bankCard.getBankName());
            result.setBankCertName(bankCard.getBankHolder());
            result.setBankCardFrontPic(bankCard.getImgUrl());
            result.setBankCardNo(bankCard.getBankCardNo());
            result.setBankAddressNo(bankCard.getBankAddressNo());
        }

        return result;
    }

    @Override
    public void updateMerchantBankCard(MerchantBankCard merchantBankCard) {
        baseMapper.updateMerchantBankCard(merchantBankCard);
    }

    @Override
    public void updateMerchantBankCardUserId(MerchantBankCard merchantBankCard) {
        IdCard legalIdCard = idCardService.getCertIdCard(merchantBankCard.getUserId());
        if (legalIdCard != null) {
            legalIdCard.setCertNo(merchantBankCard.getCertNo());
            legalIdCard.setCertName(merchantBankCard.getBankCertName());
            idCardService.updateById(legalIdCard);
        }

        BankCard bankCard = new BankCard().setUserId(legalIdCard.getUserId()).setAccountType(BankCard.ACCOUNT_TYPE_PRIVATE);
        bankCard = bankCardService.getOne(new QueryWrapper<>(bankCard));
        if (bankCard != null) {
            bankCard.setBankName(merchantBankCard.getBankName());
            bankCard.setBankHolder(merchantBankCard.getBankCertName());
            bankCard.setBankCardNo(merchantBankCard.getBankCardNo());
            bankCardService.updateById(bankCard);
        }

        baseMapper.updateMerchantBankCardUserId(merchantBankCard);
    }

    @Override
    public void deleteMerchantBankCard(MerchantBankCard merchantBankCard) {
        baseMapper.deleteMerchantBankCard(merchantBankCard);
    }

}

