package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.jsowell.pile.domain.MemberWalletInfo;
import com.jsowell.pile.domain.PileMerchantInfo;
import com.jsowell.pile.mapper.MemberWalletInfoMapper;
import com.jsowell.pile.service.PileMerchantInfoService;
import com.jsowell.pile.service.MemberWalletInfoService;
import com.jsowell.pile.vo.base.MemberWalletVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class MemberWalletInfoServiceImpl implements MemberWalletInfoService {

    @Resource
    private MemberWalletInfoMapper memberWalletInfoMapper;

    @Resource
    private PileMerchantInfoService pileMerchantInfoService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return memberWalletInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(MemberWalletInfo record) {
        return memberWalletInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(MemberWalletInfo record) {
        return memberWalletInfoMapper.insertSelective(record);
    }

    @Override
    public MemberWalletInfo selectByPrimaryKey(Integer id) {
        return memberWalletInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public MemberWalletInfo selectByMemberId(String memberId, String merchantId) {
        return memberWalletInfoMapper.selectByMemberId(memberId, merchantId);
    }

    @Override
    public MemberWalletInfo selectByWalletCode(String walletCode) {
        return memberWalletInfoMapper.selectByWalletCode(walletCode);
    }

    @Override
    public int updateByPrimaryKeySelective(MemberWalletInfo record) {
        return memberWalletInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MemberWalletInfo record) {
        return memberWalletInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<MemberWalletVO> selectByMemberWalletList(String memberId) {
        List<MemberWalletVO> resultList = Lists.newArrayList();
        List<MemberWalletInfo> list = memberWalletInfoMapper.selectByMemberWalletList(memberId);
        if (CollectionUtils.isNotEmpty(list)) {
            for (MemberWalletInfo memberWalletInfo : list) {
                BigDecimal principalBalance = memberWalletInfo.getPrincipalBalance() == null
                        ? BigDecimal.ZERO
                        : memberWalletInfo.getPrincipalBalance();
                BigDecimal giftBalance = memberWalletInfo.getGiftBalance() == null
                        ? BigDecimal.ZERO
                        : memberWalletInfo.getGiftBalance();
                MemberWalletVO memberWalletVO = MemberWalletVO.builder()
                        .memberId(memberWalletInfo.getMemberId())
                        .merchantId(memberWalletInfo.getMerchantId())
                        .walletCode(memberWalletInfo.getWalletCode())
                        .principalBalance(principalBalance)
                        .giftBalance(giftBalance)
                        .totalBalance(principalBalance.add(giftBalance))
                        .build();
                // 获取运营商名称
                PileMerchantInfo merchantInfo = pileMerchantInfoService.selectPileMerchantInfoById(memberWalletInfo.getMerchantId());
                if (Objects.nonNull(merchantInfo)) {
                    memberWalletVO.setMerchantName(merchantInfo.getMerchantName());
                }
                resultList.add(memberWalletVO);
            }
        }
        return resultList;
    }

    /**
     * 查询会员钱包信息
     * @param walletCode
     * @return
     */
    @Override
    public MemberWalletVO selectMemberWalletInfo(String walletCode) {
        // 查询数据库，字段为null默认返回0
        MemberWalletVO memberWalletVO = memberWalletInfoMapper.selectMemberWalletInfo(walletCode);

        // 总余额
        memberWalletVO.setTotalBalance(memberWalletVO.getPrincipalBalance().add(memberWalletVO.getGiftBalance()));

        // 总充值金额 = 累计本金充值 + 累计赠送金额
        memberWalletVO.setTotalRechargeAmount(memberWalletVO.getAccumulatedRechargePrincipal().add(memberWalletVO.getAccumulatedRechargeGift()));

        // 累计消费金额 = 总充值金额 - 本金余额 - 赠送金余额
        BigDecimal accumulatedConsumptionAmount = memberWalletVO.getTotalRechargeAmount()
                .subtract(memberWalletVO.getPrincipalBalance())
                .subtract(memberWalletVO.getGiftBalance());
        memberWalletVO.setAccumulatedConsumptionAmount(accumulatedConsumptionAmount);

        return memberWalletVO;
    }

}
