package com.hzs.member.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EBankCardStatus;
import com.hzs.common.core.enums.EDefault;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.domain.member.account.CuMemberBank;
import com.hzs.member.account.dto.CuMemberBankParam;
import com.hzs.member.account.mapper.CuMemberBankMapper;
import com.hzs.member.account.service.ICuMemberBankService;
import com.hzs.member.account.vo.CuMemberBankVO;
import com.hzs.member.handlebusiness.vo.BusinessMemberVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 会员信息-银行卡信息 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-31
 */
@Service
public class CuMemberBankServiceImpl extends ServiceImpl<CuMemberBankMapper, CuMemberBank> implements ICuMemberBankService {


    @Override
    public CuMemberBankVO selectCuMemberBankById(Long pkId) {
        QueryWrapper<CuMemberBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_ID", pkId);
        queryWrapper.eq("DEFAULT_CARD", EDefault.YES.getValue());
        return BeanUtil.copyProperties(this.getOne(queryWrapper), CuMemberBankVO.class);
    }


    @Override
    public CuMemberBankVO isBindBank(Long pkMember, Integer pkCountry) {
        QueryWrapper<CuMemberBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("PK_MEMBER", pkMember);
        queryWrapper.eq("DEFAULT_CARD", EDefault.YES.getValue());
        return BeanUtil.copyProperties(this.getOne(queryWrapper), CuMemberBankVO.class);
    }


    @Override
    public Boolean unbindBank(Long pkId) {
        UpdateWrapper<CuMemberBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue()).eq("PK_ID", pkId);
        return update(updateWrapper);
    }

    @Override
    public Integer saveMemberBank(CuMemberBankParam cuMemberBankParam) {
        CuMemberBank cuMemberBank = new CuMemberBank();
        BeanUtil.copyProperties(cuMemberBankParam, cuMemberBank);
        //是否绑定银行卡
        Long pkMember = cuMemberBankParam.getPkMember();
        Integer pkCountry = cuMemberBankParam.getPkCountry();
        CuMemberBankVO bindCard = isBindBank(pkMember, pkCountry);
        int isDefault = null != bindCard.getDefaultCard() ? EBankCardStatus.NO.getValue() : EBankCardStatus.YES.getValue();
        cuMemberBank.setDefaultCard(isDefault);
        cuMemberBank.setPkCreator(cuMemberBankParam.getPkMember());
        return baseMapper.insert(cuMemberBank);
    }

    @Override
    public List<CuMemberBankVO> selectBankCardByPkMember(Long pkMember, Integer pkCountry) {
        return BeanUtil.copyToList(baseMapper.selectBankCardByPkMember(pkMember, pkCountry), CuMemberBankVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDefault(Long pkId, Long pkMember, Integer pkCountry) {
        updateMemberBankCardToDefault(pkMember,pkCountry);
        UpdateWrapper<CuMemberBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("DEFAULT_CARD", EBankCardStatus.YES.getValue()).eq("PK_ID", pkId);
        return update(updateWrapper);
    }

    /**
     * 绑定的银行卡更新为非默认
     *
     * @param pkMember  会员ID
     * @param pkCountry 国家ID
     * @return Boolean
     */
    public Boolean updateMemberBankCardToDefault(Long pkMember, Integer pkCountry) {
        UpdateWrapper<CuMemberBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("DEFAULT_CARD", EBankCardStatus.NO.getValue())
                .eq("DEFAULT_CARD", EBankCardStatus.YES.getValue())
                .eq("PK_MEMBER", pkMember)
                .eq("PK_COUNTRY", pkCountry);
        return update(updateWrapper);
    }

    @Override
    public Integer batchUpdateBankBypkMembers(List<BusinessMemberVO> businessMemberList, Long updatePkMember) {
        return baseMapper.batchUpdateBankBypkMembers(businessMemberList, updatePkMember);
    }


    @Override
    public List<CuMemberBank> selectRepeatBank(CuMemberBankParam cuMemberBankParam) {
        QueryWrapper<CuMemberBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", cuMemberBankParam.getPkCountry());
        queryWrapper.eq("PK_MEMBER", cuMemberBankParam.getPkMember());
        queryWrapper.eq("SUB_BANK_NAME", cuMemberBankParam.getSubBankName());
        queryWrapper.eq("ACCOUNT_NAME", cuMemberBankParam.getAccountName());
        queryWrapper.eq("CARD_NUMBER", cuMemberBankParam.getCardNumber());
        queryWrapper.eq("PK_BANK", cuMemberBankParam.getPkBank());
        queryWrapper.eq("ID_CARD", cuMemberBankParam.getIdCard());
        queryWrapper.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
        return this.list(queryWrapper);
    }
}
