package com.hzs.bonus.bonus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.bonus.mapper.*;
import com.hzs.bonus.bonus.param.BonusParam;
import com.hzs.bonus.bonus.service.IBonusSettleService;
import com.hzs.bonus.bonus.service.ICuMemberBonusService;
import com.hzs.bonus.bonus.vo.MemberBonusDetailSerVO;
import com.hzs.bonus.bonus.vo.MemberBonusDetailVO;
import com.hzs.common.core.constant.*;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.ext.*;
import com.hzs.common.domain.system.config.BdBonusItems;
import com.hzs.common.domain.system.config.ext.BdBonusItemsExt;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import com.hzs.system.config.IBonusItemsServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 会员奖金-会员奖金汇总表 服务实现类
 */
@Service
@Slf4j
public class CuMemberBonusServiceImpl extends ServiceImpl<CuMemberBonusMapper, CuMemberBonus> implements ICuMemberBonusService {

    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;
    @DubboReference
    IBonusItemsServiceApi iBonusItemsServiceApi;

    @Autowired
    private CuMemberBonusDetailMapper cuMemberBonusDetailMapper;
    @Autowired
    private CuMemberBonusRangeMapper cuMemberBonusRangeMapper;
    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    private IBonusSettleService iBonusSettleService;


    @Override
    public List<Long> batchQueryCuMemberBonusSeq(Integer rowNum) {
        return baseMapper.batchQueryCuMemberBonusSeq(rowNum);
    }

    @Override
    public List<CuMemberBonusExt> queryMemberBonusVoByCondition(BonusParam bonusParam) {
        return handleMemberBonusPurchase(getCuMemberBonusExtList(bonusParam), bonusParam);
    }

    @Override
    public List<CuMemberBonusExt> listMemberBonus(BonusParam bonusParam) {
        return baseMapper.listMemberBonus(bonusParam);
    }

    private List<CuMemberBonusExt> handleMemberBonusPurchase(List<CuMemberBonusExt> cuMemberBonusExtList, BonusParam bonusParam) {
        List<CuMemberBonusExt> memberBonusExtList = new ArrayList<>();
        Map<String, CuMemberBonusExt> cuMemberBonusExtMap = new HashMap<>();
        cuMemberBonusExtList.forEach(cuMemberBonusExt -> cuMemberBonusExtMap.put(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cuMemberBonusExt.getSettleDate()), cuMemberBonusExt));
        Date endDate = DateUtils.beforeDate(1, ChronoUnit.DAYS, bonusParam.getEndDate());
        Date compareDate = DateUtils.parseStringToDateTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.currentDate()) + " 13:00:00");

        while (DateUtils.compareDateAfter(endDate, bonusParam.getStartDate())
                || DateUtils.compareDateEqual(endDate, bonusParam.getStartDate())) {
            String date = DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, endDate);
            if (cuMemberBonusExtMap.containsKey(date)) {
                memberBonusExtList.add(cuMemberBonusExtMap.get(date));
            } else {
                if (!DateUtils.compareDateEqual(endDate, DateUtils.beforeDate(1, ChronoUnit.DAYS, DateUtils.currentDate()))
                        || DateUtils.compareDateTimeAfter(DateUtils.currentDateTime(), compareDate)) {
                    CuMemberBonusExt cuMemberBonusExt = new CuMemberBonusExt();
                    cuMemberBonusExt.setRepurRangeIncome(BigDecimal.ZERO);
                    cuMemberBonusExt.setRetailRangeIncome(BigDecimal.ZERO);
                    cuMemberBonusExt.setRetailRealSubtotal(BigDecimal.ZERO);
                    cuMemberBonusExt.setRealIncomeTotal(BigDecimal.ZERO);
                    memberBonusExtList.add(cuMemberBonusExt);
                }
            }
            endDate = DateUtils.beforeDate(1, ChronoUnit.DAYS, endDate);
        }
        return memberBonusExtList;
    }

    private List<CuMemberBonusExt> getCuMemberBonusExtList(BonusParam bonusParam) {
        if (bonusParam.getGrantStatus() == null) {
            bonusParam.setGrantStatus(EGrantStatus.PUBLISHED.getValue());
        }
        return baseMapper.queryMemberBonusByCondition(bonusParam);
    }

    @Override
    public void calculateBonusTotal(CuMemberBonusExt cuMemberBonusExt, CuMemberBonusExt memberBonusExt) {
        cuMemberBonusExt.setDirectIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getDirectIncome(), memberBonusExt.getDirectIncome()));
        cuMemberBonusExt.setExpandIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getExpandIncome(), memberBonusExt.getExpandIncome()));
        cuMemberBonusExt.setCoachIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getCoachIncome(), memberBonusExt.getCoachIncome()));
        cuMemberBonusExt.setShareIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getShareIncome(), memberBonusExt.getShareIncome()));
        cuMemberBonusExt.setServiceIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getServiceIncome(), memberBonusExt.getServiceIncome()));
        cuMemberBonusExt.setBackPoints(ComputeUtil.computeAdd(cuMemberBonusExt.getBackPoints(), memberBonusExt.getBackPoints()));
        cuMemberBonusExt.setServiceSpend(ComputeUtil.computeAdd(cuMemberBonusExt.getServiceSpend(), memberBonusExt.getServiceSpend()));
        cuMemberBonusExt.setPurRealSubtotal(ComputeUtil.computeAdd(cuMemberBonusExt.getPurRealSubtotal(), memberBonusExt.getPurRealSubtotal()));
        cuMemberBonusExt.setRepurPushIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurPushIncome(), memberBonusExt.getRepurPushIncome()));
        cuMemberBonusExt.setRepurRangeIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurRangeIncome(), memberBonusExt.getRepurRangeIncome()));
        cuMemberBonusExt.setRepurExpandIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurExpandIncome(), memberBonusExt.getRepurExpandIncome()));
        cuMemberBonusExt.setRepurRealSubtotal(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurRealSubtotal(), memberBonusExt.getRepurRealSubtotal()));
        cuMemberBonusExt.setCloudDirectIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getCloudDirectIncome(), memberBonusExt.getCloudDirectIncome()));
        cuMemberBonusExt.setCloudPurIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getCloudPurIncome(), memberBonusExt.getCloudPurIncome()));
        cuMemberBonusExt.setCloudRepurIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getCloudRepurIncome(), memberBonusExt.getCloudRepurIncome()));
        cuMemberBonusExt.setCloudRealSubtotal(ComputeUtil.computeAdd(cuMemberBonusExt.getCloudRealSubtotal(), memberBonusExt.getCloudRealSubtotal()));
        cuMemberBonusExt.setRepurCoupon(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurCoupon(), memberBonusExt.getRepurCoupon()));
        cuMemberBonusExt.setRepurCouponShare(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurCouponShare(), memberBonusExt.getRepurCouponShare()));
        cuMemberBonusExt.setRepurCouponSubtotal(ComputeUtil.computeAdd(cuMemberBonusExt.getRepurCouponSubtotal(), memberBonusExt.getRepurCouponSubtotal()));
        cuMemberBonusExt.setMakerDirectIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getMakerDirectIncome(), memberBonusExt.getMakerDirectIncome()));
        cuMemberBonusExt.setMakerShareIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getMakerShareIncome(), memberBonusExt.getMakerShareIncome()));
        cuMemberBonusExt.setMakerRealSubtotal(ComputeUtil.computeAdd(cuMemberBonusExt.getMakerRealSubtotal(), memberBonusExt.getMakerRealSubtotal()));
        cuMemberBonusExt.setGlobalPoints(ComputeUtil.computeAdd(cuMemberBonusExt.getGlobalPoints(), memberBonusExt.getGlobalPoints()));
        cuMemberBonusExt.setCarAwardPoints(ComputeUtil.computeAdd(cuMemberBonusExt.getCarAwardPoints(), memberBonusExt.getCarAwardPoints()));
        cuMemberBonusExt.setHiFunIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getHiFunIncome(), memberBonusExt.getHiFunIncome()));
        cuMemberBonusExt.setHaiFunIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getHaiFunIncome(), memberBonusExt.getHaiFunIncome()));
        cuMemberBonusExt.setRealIncomeTotal(ComputeUtil.computeAdd(cuMemberBonusExt.getRealIncomeTotal(), memberBonusExt.getRealIncomeTotal()));
        cuMemberBonusExt.setStoreIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getStoreIncome(), memberBonusExt.getStoreIncome()));
        cuMemberBonusExt.setRetailRangeIncome(ComputeUtil.computeAdd(cuMemberBonusExt.getRetailRangeIncome(), memberBonusExt.getRetailRangeIncome()));
    }

    @Override
    public List<CuMemberBonusExt> queryMemberBonusTotalByCondition(BonusParam bonusParam) {
        return getCuMemberBonusExtList(bonusParam);
    }

    @Override
    public CuMemberBonusExt initCuMemberBonus() {
        CuMemberBonusExt cuMemberBonusExt = new CuMemberBonusExt();
        cuMemberBonusExt.setDirectIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setExpandIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setExpandNoCapping(BigDecimal.ZERO);
        cuMemberBonusExt.setExpandCapping(BigDecimal.ZERO);
        cuMemberBonusExt.setCoachIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setShareIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setServiceIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setBackPoints(BigDecimal.ZERO);
        cuMemberBonusExt.setServiceSpend(BigDecimal.ZERO);
        cuMemberBonusExt.setPurRealSubtotal(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurPushIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurRangeIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurExpandIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurRealSubtotal(BigDecimal.ZERO);
        cuMemberBonusExt.setCloudDirectIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setCloudPurIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setCloudRepurIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setCloudRealSubtotal(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurCoupon(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurCouponShare(BigDecimal.ZERO);
        cuMemberBonusExt.setRepurCouponSubtotal(BigDecimal.ZERO);
        cuMemberBonusExt.setMakerDirectIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setMakerShareIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setMakerRealSubtotal(BigDecimal.ZERO);
        cuMemberBonusExt.setStoreIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setGlobalPoints(BigDecimal.ZERO);
        cuMemberBonusExt.setCarAwardPoints(BigDecimal.ZERO);
        cuMemberBonusExt.setHiFunIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setHaiFunIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setRetailRangeIncome(BigDecimal.ZERO);
        cuMemberBonusExt.setRealIncomeTotal(BigDecimal.ZERO);
        return cuMemberBonusExt;
    }

    @Override
    public List<CuMemberBonusExt> queryMemberBonusVoByConditionForServer(BonusParam bonusParam) {
        if (bonusParam.getEndDate() != null) {
            bonusParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, bonusParam.getEndDate()));
        }
        return baseMapper.queryMemberBonusByConditionForServer(bonusParam);
    }

    @Override
    public CuMemberBonusExt queryMemberBonusByConditionForServerSum(BonusParam bonusParam) {
        return baseMapper.queryMemberBonusByConditionForServerSum(bonusParam);
    }

    @Override
    public List<CuMemberBonusExt> queryBonusTotalVoByConditionForServer(BonusParam bonusParam) {
        return baseMapper.queryBonusTotalVoByConditionForServer(bonusParam);
    }

    @Override
    public TableDataInfo queryMemberBonusDetailVoByCondition(BonusParam bonusParam) {
        List<MemberBonusDetailVO> memberBonusDetailVOList = new ArrayList<>();
        TableDataInfo dataTable;
        Integer bonusItemsValue = 0;
        if (bonusParam.getPkBonusItems() != null) {
            BdBonusItems bonusItems = iBonusItemsServiceApi.queryBonusItems(bonusParam.getPkCountry(), bonusParam.getPkBonusItems()).getData();
            bonusItemsValue = bonusItems.getBonusValue();
        }
        if (null == bonusParam.getPkBonusItems()) {
            List<CuMemberBonusDetailExt> cuMemberBonusDetailList = cuMemberBonusDetailMapper.queryMemberBonusDetailByCondition(bonusParam);
            dataTable = getDataTable(cuMemberBonusDetailList);
            cuMemberBonusDetailList.forEach(cuMemberBonusDetail -> {
                MemberBonusDetailVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusDetail, MemberBonusDetailVO.class);
                memberBonusDetailVO.setSourceMemberCode(cuMemberBonusDetail.getSourceMemberCode());
                memberBonusDetailVO.setSourceMemberName(cuMemberBonusDetail.getSourceMemberName());
                memberBonusDetailVOList.add(memberBonusDetailVO);
            });
        } else if (EBonusItems.RETAIL_RANGE_INCOME.getValue() == bonusItemsValue
                || EBonusItems.RETAIL_REPURCHASE_INCOME.getValue() == bonusItemsValue) {
            // 直推级差收益、复购级差收益
            List<CuMemberBonusRangeExt> cuMemberBonusRangeList = cuMemberBonusRangeMapper.queryMemberBonusRangeByBonusItems(bonusParam);
            dataTable = getDataTable(cuMemberBonusRangeList);
            cuMemberBonusRangeList.forEach(cuMemberBonusRange -> {
                MemberBonusDetailVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusRange, MemberBonusDetailVO.class);
                memberBonusDetailVO.setSourceMemberCode(cuMemberBonusRange.getSourceMemberCode());
                memberBonusDetailVO.setSourceMemberName(cuMemberBonusRange.getSourceMemberName());
                memberBonusDetailVOList.add(memberBonusDetailVO);
            });
        } else {
            // 其他收益
            List<CuMemberBonusDetailExt> cuMemberBonusDetailList = cuMemberBonusDetailMapper.queryMemberBonusDetailByBonusItems(bonusParam);
            dataTable = getDataTable(cuMemberBonusDetailList);
            cuMemberBonusDetailList.forEach(cuMemberBonusDetail -> {
                MemberBonusDetailVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusDetail, MemberBonusDetailVO.class);
                memberBonusDetailVO.setSourceMemberCode(cuMemberBonusDetail.getSourceMemberCode());
                memberBonusDetailVO.setSourceMemberName(cuMemberBonusDetail.getSourceMemberName());
                memberBonusDetailVOList.add(memberBonusDetailVO);
            });
        }
        Map<Integer, BdBonusItemsExt> bonusItemsMap = convertBonusItems(bonusParam);
        CurrencyDTO currencyDTO = iCurrencyServiceApi.getCurrency(bonusParam.getPkCountry()).getData();
        memberBonusDetailVOList.forEach(memberBonusDetailVO -> {
            if (bonusItemsMap.containsKey(memberBonusDetailVO.getPkBonusItems())) {
                memberBonusDetailVO.setBonusName(bonusItemsMap.get(memberBonusDetailVO.getPkBonusItems()).getBonusName());
            }
            // 转换汇率
            if (!Objects.equals(bonusParam.getPkCountry(), memberBonusDetailVO.getPkSourceCountry())) {
                memberBonusDetailVO.setOrderAmount(ComputeUtil.computeMultiply(ComputeUtil.computeDivide(memberBonusDetailVO.getOrderAmount(),
                        memberBonusDetailVO.getOutExchangeRate()), currencyDTO.getOutExchangeRate()));
            }
        });
        dataTable.setRows(memberBonusDetailVOList);
        return dataTable;
    }

    private Map<Integer, BdBonusItemsExt> convertBonusItems(BonusParam bonusParam) {
        List<BdBonusItemsExt> bonusItemsExtList = iBonusItemsServiceApi.queryBonusItems(bonusParam.getPkCountry()).getData();
        Map<Integer, BdBonusItemsExt> bonusItemsMap = new HashMap<>(ComputeUtil.mapInitCapacity(bonusItemsExtList.size()));
        bonusItemsExtList.forEach(bonusItems ->
                bonusItemsMap.put(bonusItems.getPkId(), bonusItems));

        return bonusItemsMap;
    }

    @Override
    public TableDataInfo queryMemberBonusDetailSerVoByCondition(BonusParam bonusParam) {
        List<MemberBonusDetailSerVO> memberBonusDetailSerVoS = new ArrayList<>();
        Integer bonusItemsValue = 0;
        if (bonusParam.getPkBonusItems() != null) {
            BdBonusItems bonusItems = iBonusItemsServiceApi.queryBonusItems(bonusParam.getPkCountry(), bonusParam.getPkBonusItems()).getData();
            bonusItemsValue = bonusItems.getBonusValue();
        }
        if (bonusParam.getEndDate() != null) {
            bonusParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, bonusParam.getEndDate()));
        }
        TableDataInfo dataTable;
        if (null == bonusParam.getPkBonusItems()) {
            List<CuMemberBonusDetailExt> cuMemberBonusDetailList = cuMemberBonusDetailMapper.queryMemberBonusDetailByConditionForServer(bonusParam);
            dataTable = getDataTable(cuMemberBonusDetailList);
            cuMemberBonusDetailList.forEach(cuMemberBonusDetail -> {
                MemberBonusDetailSerVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusDetail, MemberBonusDetailSerVO.class);
                memberBonusDetailSerVoS.add(memberBonusDetailVO);
            });
        } else if (EBonusItems.RETAIL_RANGE_INCOME.getValue() == bonusItemsValue
                || EBonusItems.RETAIL_REPURCHASE_INCOME.getValue() == bonusItemsValue) {
            // 直推级差收益、复购级差收益
            List<CuMemberBonusRangeExt> cuMemberBonusRangeList = cuMemberBonusRangeMapper.queryMemberBonusRangeByBonusItemsForServer(bonusParam);
            dataTable = getDataTable(cuMemberBonusRangeList);
            cuMemberBonusRangeList.forEach(cuMemberBonusRange -> {
                MemberBonusDetailSerVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusRange, MemberBonusDetailSerVO.class);
                memberBonusDetailSerVoS.add(memberBonusDetailVO);
            });
        } else {
            // 其他收益
            List<CuMemberBonusDetailExt> cuMemberBonusDetailList = cuMemberBonusDetailMapper.queryMemberBonusDetailByBonusItemsForServer(bonusParam);
            dataTable = getDataTable(cuMemberBonusDetailList);
            cuMemberBonusDetailList.forEach(cuMemberBonusDetail -> {
                MemberBonusDetailSerVO memberBonusDetailVO = BeanUtil.copyProperties(cuMemberBonusDetail, MemberBonusDetailSerVO.class);
                memberBonusDetailSerVoS.add(memberBonusDetailVO);
            });
        }
        Map<Integer, BdBonusItemsExt> bonusItemsMap = convertBonusItems(bonusParam);
        memberBonusDetailSerVoS.forEach(memberBonusDetailSerVO -> {
            if (bonusItemsMap.containsKey(memberBonusDetailSerVO.getPkBonusItems())) {
                memberBonusDetailSerVO.setBonusName(bonusItemsMap.get(memberBonusDetailSerVO.getPkBonusItems()).getBonusName());
            }
            memberBonusDetailSerVO.setOrderAmountDollar(ComputeUtil.computeDivide(memberBonusDetailSerVO.getOrderAmount(), memberBonusDetailSerVO.getOutExchangeRate()));
            memberBonusDetailSerVO.setOrderAchieveOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getOrderAchieve(), memberBonusDetailSerVO.getOutExchangeRate()));
            memberBonusDetailSerVO.setPretaxIncomeOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getPretaxIncome(), memberBonusDetailSerVO.getOutExchangeRate()));
            memberBonusDetailSerVO.setRealIncomeOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getRealIncome(), memberBonusDetailSerVO.getOutExchangeRate()));
            memberBonusDetailSerVO.setIncomeRatio(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getIncomeRatio(), 100));
            memberBonusDetailSerVO.setIncomeDialRatio(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getIncomeDialRatio(), 100));
            memberBonusDetailSerVO.setOrderDialRatio(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getOrderDialRatio(), 100));
            if (memberBonusDetailSerVO.getExpandNoCapping() != null || memberBonusDetailSerVO.getExpandCappingOri() != null) {
                memberBonusDetailSerVO.setExpandNoCappingOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getExpandNoCapping(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setExpandCappingOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getExpandCapping(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setExpandNormalHalfOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getExpandNormalHalf(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setExpandNormalRealOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getExpandNormalReal(), memberBonusDetailSerVO.getOutExchangeRate()));
            }
            if (memberBonusDetailSerVO.getCoachNoCapping() != null || memberBonusDetailSerVO.getCoachNormalHalf() != null) {
                memberBonusDetailSerVO.setCoachNoCappingOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getCoachNoCapping(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setCoachCappingOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getCoachCapping(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setCoachNormalHalfOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getCoachNormalHalf(), memberBonusDetailSerVO.getOutExchangeRate()));
                memberBonusDetailSerVO.setCoachNormalRealOri(ComputeUtil.computeMultiply(memberBonusDetailSerVO.getCoachNormalReal(), memberBonusDetailSerVO.getOutExchangeRate()));
            }
        });
        dataTable.setRows(memberBonusDetailSerVoS);
        return dataTable;
    }

    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo<>(list).getTotal());
        return rspData;
    }

    @Override
    public void deleteCuMemberBonus(Integer period) {
        baseMapper.deleteCuMemberBonus(period);
    }

    @Override
    public void batchInsertCuMemberBonus(List<CuMemberBonus> cuMemberBonusList) {
        if (cuMemberBonusList.size() == 0) {
            return;
        }
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(cuMemberBonusList);
        lists.forEach(list ->
                baseMapper.batchInsertCuMemberBonus(list));
    }

    @Override
    public Boolean updateCuMemberBonusStatus(Integer pkCountry, Integer grantStatus, Integer oldGrantStatus, Integer startPeriod, Integer endPeriod) {
        return baseMapper.updateCuMemberBonusStatus(pkCountry, grantStatus, oldGrantStatus, startPeriod, endPeriod) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public List<CuMemberBonus> queryCuMemberBonusByCondition(Integer pkCountry, Integer grantStatus, Integer startPeriod, Integer endPeriod) {
        QueryWrapper<CuMemberBonus> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pk_member,pk_rate,pk_country,sum(real_income_total) real_income_total");
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.eq(MemberBonusFieldConstants.GRANT_STATUS, grantStatus);
        queryWrapper.eq(MemberBonusFieldConstants.INCOME_STATUS, ENormalDisable.NORMAL.getValue());
        queryWrapper.between(MemberFieldConstants.PERIOD, startPeriod, endPeriod);
        queryWrapper.gt(MemberBonusFieldConstants.REAL_INCOME_TOTAL, 0);
        queryWrapper.groupBy("pk_member,pk_rate,pk_country");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean autoCalculateBonus(Date startDate, Date endDate) {
        String settleDate = DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, startDate);
        // 自动初始化表
        iCuMemberTreeService.createCuMemberTree(settleDate);
        log.info("初始化表完成");
        // 奖金结算
        iBonusSettleService.calculateCuMemberBonus(settleDate, null);
        return Boolean.TRUE;
    }

    @Override
    public void backupsSettleBonusEveryday() {
        iCuMemberTreeService.createCuMemberTree(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS)));
    }

    @Override
    public Set<Long> queryMemberBonusByPeriod(Integer period) {
        LambdaQueryWrapper<CuMemberBonus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuMemberBonus::getPeriod, period);
        queryWrapper.le(CuMemberBonus::getGrantStatus, EGrantStatus.PUBLISHED.getValue());
        List<CuMemberBonus> cuMemberBonusList = baseMapper.selectList(queryWrapper);
        return cuMemberBonusList.stream().distinct().map(CuMemberBonus::getPkId).collect(Collectors.toSet());
    }

}
