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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.Date;
import java.util.List;

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

    @Override
    public CuMemberBankVO selectCuMemberBankById(Long pkId) {
        LambdaQueryWrapper<CuMemberBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuMemberBank::getPkId, pkId);
        queryWrapper.eq(CuMemberBank::getDefaultCard, EDefault.YES.getValue());
        CuMemberBank cuMemberBank = this.getOne(queryWrapper);
        return cuMemberBank == null ? null : BeanUtil.copyProperties(cuMemberBank, CuMemberBankVO.class);
    }

    @Override
    public CuMemberBankVO isBindBank(Long pkMember, Integer pkCountry) {
        LambdaQueryWrapper<CuMemberBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuMemberBank::getPkCountry, pkCountry);
        queryWrapper.eq(CuMemberBank::getPkMember, pkMember);
        queryWrapper.eq(CuMemberBank::getDefaultCard, EDefault.YES.getValue());
        return BeanUtil.copyProperties(this.getOne(queryWrapper), CuMemberBankVO.class);
    }

    @Override
    public Boolean unbindBank(Long pkId, Long pkMember) {
        LambdaUpdateWrapper<CuMemberBank> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CuMemberBank::getDelFlag, EDelFlag.DELETE.getValue())
                .set(CuMemberBank::getModifiedTime, new Date())
                .set(CuMemberBank::getPkModified, pkMember)
                .eq(CuMemberBank::getPkId, pkId);
        return update(updateWrapper);
    }

    @Override
    public Integer saveMemberBank(CuMemberBankParam cuMemberBankParam) {
        CuMemberBankVO bindCard = isBindBank(cuMemberBankParam.getPkMember(), cuMemberBankParam.getPkCountry());
        int isDefault = null != bindCard.getDefaultCard() ? EBankCardStatus.NO.getValue() : EBankCardStatus.YES.getValue();
        CuMemberBank cuMemberBank = BeanUtil.copyProperties(cuMemberBankParam, CuMemberBank.class);
        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);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateDefault(Long pkId, Long pkMember, Integer pkCountry) {
        LambdaUpdateWrapper<CuMemberBank> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CuMemberBank::getDefaultCard, EBankCardStatus.NO.getValue())
                .eq(CuMemberBank::getDefaultCard, EBankCardStatus.YES.getValue())
                .eq(CuMemberBank::getPkMember, pkMember)
                .eq(CuMemberBank::getPkCountry, pkCountry);

        LambdaUpdateWrapper<CuMemberBank> newUpdateWrapper = new LambdaUpdateWrapper<>();
        newUpdateWrapper.set(CuMemberBank::getDefaultCard, EBankCardStatus.YES.getValue())
                .eq(CuMemberBank::getPkId, pkId);
        return update(newUpdateWrapper);
    }

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

}
