package com.hzs.member.statis.controller.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.statis.service.ICuBonusStatisService;
import com.hzs.member.statis.vo.*;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 奖金拨比统计 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/bonus-statis")
public class CuBonusStatisController extends BaseController {

    @Autowired
    private ICuBonusStatisService iCuBonusStatisService;
    @Autowired
    private ICuMemberService iCuMemberService;

    @DubboReference
    ICurrencyServiceApi currencyServiceApi;


    /**
     * 奖金拨比列表
     **/
    @AccessPermissions("performanceList")
    @GetMapping("/list")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo list(CuBonusStatisVO cuBonusStatisVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusStatisVO> slList;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        R<CurrencyDTO> chCurrency = currencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
        BigDecimal chRate = chCurrency.getData().getInExchangeRate() == null ? BigDecimal.ONE : chCurrency.getData().getInExchangeRate();
        cuBonusStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusStatisVO.getEndDate())) {
            cuBonusStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkBdVertexStr())) {
            String[] split = cuBonusStatisVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkTeamCodeStr())) {
            String[] split = cuBonusStatisVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkTeamCodeList(pkTeamCodeList);
        }

        //查询订单业绩,查询分红配置每月10号分红
        BigDecimal dividendRatio = BigDecimal.ZERO;
        //查询奖金扣项配置参数 0.8
        if (pkCountry == CountryConstants.CHINA_COUNTRY) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisVO.setOrderTypeList(orderTypeList);
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {
            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            cuBonusStatisVO.setDividendRatio(dividendRatio);
        } else {
            cuBonusStatisVO.setDividendRatio(BigDecimal.ZERO);
        }
        startPage();
        if (pkCountry == CountryConstants.CHINA_COUNTRY) {
            //查询中国奖金拨比列表
            slList = iCuBonusStatisService.queryCuBonusStatis(cuBonusStatisVO);
        } else {
            //查询国外奖金拨比
            slList = iCuBonusStatisService.queryForeignCountryCuBonusStatis(cuBonusStatisVO);
        }
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                if (itm.getIntroduceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setIntroduceBonus(itm.getIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setOrgBonus(itm.getOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getLeaderBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setLeaderBonus(itm.getLeaderBonus().multiply(currency.getData().getInExchangeRate()));
                }
                //重新算首购拨比
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal mainBonusRate = cent.multiply((itm.getMainBonus()).multiply(currency.getData().getInExchangeRate()));
                    mainBonusRate = mainBonusRate.divide(itm.getFirstPurchaseAll(), 4, RoundingMode.HALF_UP);
                    itm.setMainBonusRate(mainBonusRate);
                    itm.setMainBonusPvRate(itm.getMainBonusPvRate().divide(itm.getFirstPurchaseAll(), 4, RoundingMode.HALF_UP));
                }
                //重新算总拨比
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 ||
                        itm.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal totalBonusRate = cent.multiply((itm.getTotalBonus()).multiply(currency.getData().getInExchangeRate()));
                    totalBonusRate = totalBonusRate.divide(itm.getFirstPurchaseAll().add(itm.getRepurchaseAllAmount()), 4, RoundingMode.HALF_UP);
                    itm.setTotalBonusRate(totalBonusRate);
                }
                //首购拨出金额
                itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setTotalBonus(itm.getTotalBonus().multiply(currency.getData().getInExchangeRate()));
                if (itm.getServiceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setServiceBonus(itm.getServiceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurRangeBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurRangeBonus(itm.getRepurRangeBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurOrgBonus(itm.getRepurOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                itm.setRepurBonus(itm.getRepurBonus().multiply(currency.getData().getInExchangeRate()));
            });
        }
        return getDataTable(slList);
    }


    /**
     * 查询单个人奖金拨比列表
     **/
    @GetMapping("/singlePersonList")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_SINGLE_PERSON_LIST, method = EOperationMethod.SELECT)
    public AjaxResult singlePersonList(CuBonusSinglePersonStatisVO csps) {
        CuBonusSinglePersonStatisVO cspsSum;
        if (StringUtils.isEmpty(csps.getMemberCode()) || StringUtils.isEmpty(csps.getStartDate()) || StringUtils.isEmpty(csps.getEndDate())) {
            return AjaxResult.success();
        }
        CuMemberExt cmex = iCuMemberService.getMemberByCode(csps.getMemberCode());
        if (StringUtils.isNull(cmex)) {
            return AjaxResult.success();
        }
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(cmex.getPkSettleCountry());
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        R<CurrencyDTO> chCurrency = currencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
        BigDecimal chRate = chCurrency.getData().getInExchangeRate() == null ? BigDecimal.ONE : chCurrency.getData().getInExchangeRate();
        csps.setRate(exchangeRate);

        csps.setPkMember(cmex.getPkId());
        BigDecimal cent = new BigDecimal(100);
        csps.setPkCountry(cmex.getPkSettleCountry());

        //查询订单业绩,查询分红配置每月10号分红
        BigDecimal dividendRatio = BigDecimal.ZERO;
        CuBonusStatisVO cuBonusStatisVO = new CuBonusStatisVO();
        cuBonusStatisVO.setPkCountry(cmex.getPkSettleCountry());
        //查询奖金扣项配置参数 0.8
        if (cmex.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {
            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            csps.setDividendRatio(dividendRatio);
        } else {
            csps.setDividendRatio(BigDecimal.ZERO);
        }
        startPage();
        cspsSum = iCuBonusStatisService.bonusSingleSum(csps);
        BigDecimal mainBonusRate;
        BigDecimal repurBonusRate;
        BigDecimal totalBonus;
        BigDecimal totalBonusRate;
        //国外转为人民币
        if (cspsSum != null && !cmex.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            //订单金额
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setFirstPurchaseAll(cspsSum.getFirstPurchaseAll().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setRepurchaseAllAmount(cspsSum.getRepurchaseAllAmount().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getMainBonus().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setMainBonus(cspsSum.getMainBonus().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getRepurBonus().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setRepurBonus(cspsSum.getRepurBonus().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 || cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    mainBonusRate = ((cspsSum.getMainBonus().add(cspsSum.getShareBonus())).divide(cspsSum.getFirstPurchaseAll(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setMainBonusRate(mainBonusRate);
                } else {
                    cspsSum.setMainBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    repurBonusRate = (cspsSum.getRepurBonus().divide(cspsSum.getRepurchaseAllAmount(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setRepurBonusRate(repurBonusRate);
                } else {
                    cspsSum.setRepurBonusRate(new BigDecimal(0));
                }
                totalBonus = cspsSum.getMainBonus().add(cspsSum.getRepurBonus()).add(cspsSum.getShareBonus());
                cspsSum.setTotalBonus(totalBonus);
                totalBonusRate = totalBonus.divide((cspsSum.getFirstPurchaseAll().add(cspsSum.getRepurchaseAllAmount())), 2, RoundingMode.HALF_UP).multiply(cent);
                cspsSum.setTotalBonusRate(totalBonusRate);
                cspsSum.setMainBonus(cspsSum.getMainBonus().add(cspsSum.getShareBonus()));
            }
        } else {
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 || cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    mainBonusRate = ((cspsSum.getMainBonus().add(cspsSum.getShareBonus())).divide(cspsSum.getFirstPurchaseAll(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setMainBonusRate(mainBonusRate);
                } else {
                    cspsSum.setMainBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    repurBonusRate = (cspsSum.getRepurBonus().divide(cspsSum.getRepurchaseAllAmount(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setRepurBonusRate(repurBonusRate);
                } else {
                    cspsSum.setRepurBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getShareBonus() == null) {
                    cspsSum.setShareBonus(new BigDecimal(0));
                }
                totalBonus = cspsSum.getMainBonus().add(cspsSum.getRepurBonus()).add(cspsSum.getShareBonus());
                cspsSum.setTotalBonus(totalBonus);
                totalBonusRate = totalBonus.divide((cspsSum.getFirstPurchaseAll().add(cspsSum.getRepurchaseAllAmount())), 2, RoundingMode.HALF_UP).multiply(cent);
                cspsSum.setTotalBonusRate(totalBonusRate);
                cspsSum.setMainBonus(cspsSum.getMainBonus().add(cspsSum.getShareBonus()));
            }
        }
        return AjaxResult.success(cspsSum);
    }

    @PostMapping("/export-singlePersonList")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_SINGLE_PERSON_LIST, method = EOperationMethod.EXPORT)
    public void exportSinglePersonList(HttpServletResponse response, CuBonusSinglePersonStatisVO csps) {
        CuBonusSinglePersonStatisVO cspsSum;
        if (StringUtils.isEmpty(csps.getMemberCode()) || StringUtils.isEmpty(csps.getStartDate()) || StringUtils.isEmpty(csps.getEndDate())) {
            List<CuBonusSinglePersonStatisVO> cspsList = new ArrayList<>();
            ExcelUtil<CuBonusSinglePersonStatisVO> util = new ExcelUtil<>(CuBonusSinglePersonStatisVO.class);
            util.exportExcel(response, cspsList, "个人拨比列表");
        }
        CuMemberExt cmex = iCuMemberService.getMemberByCode(csps.getMemberCode());
        if (StringUtils.isNull(cmex)) {
            List<CuBonusSinglePersonStatisVO> cspsList = new ArrayList<>();
            ExcelUtil<CuBonusSinglePersonStatisVO> util = new ExcelUtil<>(CuBonusSinglePersonStatisVO.class);
            util.exportExcel(response, cspsList, "个人拨比列表");
        }
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(cmex.getPkSettleCountry());
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        R<CurrencyDTO> chCurrency = currencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
        BigDecimal chRate = chCurrency.getData().getInExchangeRate() == null ? BigDecimal.ONE : chCurrency.getData().getInExchangeRate();
        csps.setRate(exchangeRate);

        csps.setPkMember(cmex.getPkId());
        BigDecimal cent = new BigDecimal(100);
        csps.setPkCountry(cmex.getPkSettleCountry());

        //查询订单业绩,查询分红配置每月10号分红
        BigDecimal dividendRatio = BigDecimal.ZERO;
        CuBonusStatisVO cuBonusStatisVO = new CuBonusStatisVO();
        cuBonusStatisVO.setPkCountry(cmex.getPkSettleCountry());
        //查询奖金扣项配置参数 0.8
        if (cmex.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {
            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            csps.setDividendRatio(dividendRatio);
        } else {
            csps.setDividendRatio(BigDecimal.ZERO);
        }
        startPage();
        cspsSum = iCuBonusStatisService.bonusSingleSum(csps);
        BigDecimal mainBonusRate;
        BigDecimal repurBonusRate;
        BigDecimal totalBonus;
        BigDecimal totalBonusRate;
        //国外转为人民币
        if (cspsSum != null && !cmex.getPkSettleCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            //订单金额
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setFirstPurchaseAll(cspsSum.getFirstPurchaseAll().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setRepurchaseAllAmount(cspsSum.getRepurchaseAllAmount().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getMainBonus().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setMainBonus(cspsSum.getMainBonus().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getRepurBonus().compareTo(BigDecimal.ZERO) > 0) {
                cspsSum.setRepurBonus(cspsSum.getRepurBonus().divide(exchangeRate, 2, RoundingMode.HALF_UP).multiply(chRate));
            }
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 || cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    mainBonusRate = ((cspsSum.getMainBonus().add(cspsSum.getShareBonus())).divide(cspsSum.getFirstPurchaseAll(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setMainBonusRate(mainBonusRate);
                } else {
                    cspsSum.setMainBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    repurBonusRate = (cspsSum.getRepurBonus().divide(cspsSum.getRepurchaseAllAmount(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setRepurBonusRate(repurBonusRate);
                } else {
                    cspsSum.setRepurBonusRate(new BigDecimal(0));
                }
                totalBonus = cspsSum.getMainBonus().add(cspsSum.getRepurBonus()).add(cspsSum.getShareBonus());
                cspsSum.setTotalBonus(totalBonus);
                totalBonusRate = totalBonus.divide((cspsSum.getFirstPurchaseAll().add(cspsSum.getRepurchaseAllAmount())), 2, RoundingMode.HALF_UP).multiply(cent);
                cspsSum.setTotalBonusRate(totalBonusRate);
                cspsSum.setMainBonus(cspsSum.getMainBonus().add(cspsSum.getShareBonus()));
            }
        } else {
            if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 || cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                if (cspsSum.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    mainBonusRate = ((cspsSum.getMainBonus().add(cspsSum.getShareBonus())).divide(cspsSum.getFirstPurchaseAll(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setMainBonusRate(mainBonusRate);
                } else {
                    cspsSum.setMainBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    repurBonusRate = (cspsSum.getRepurBonus().divide(cspsSum.getRepurchaseAllAmount(), 2, RoundingMode.HALF_UP)).multiply(cent);
                    cspsSum.setRepurBonusRate(repurBonusRate);
                } else {
                    cspsSum.setRepurBonusRate(new BigDecimal(0));
                }
                if (cspsSum.getShareBonus() == null) {
                    cspsSum.setShareBonus(new BigDecimal(0));
                }
                totalBonus = cspsSum.getMainBonus().add(cspsSum.getRepurBonus()).add(cspsSum.getShareBonus());
                cspsSum.setTotalBonus(totalBonus);
                totalBonusRate = totalBonus.divide((cspsSum.getFirstPurchaseAll().add(cspsSum.getRepurchaseAllAmount())), 2, RoundingMode.HALF_UP).multiply(cent);
                cspsSum.setTotalBonusRate(totalBonusRate);
                cspsSum.setMainBonus(cspsSum.getMainBonus().add(cspsSum.getShareBonus()));
            }
        }
        List<CuBonusSinglePersonStatisVO> cspsList = Collections.singletonList(cspsSum);
        ExcelUtil<CuBonusSinglePersonStatisVO> util = new ExcelUtil<>(CuBonusSinglePersonStatisVO.class);
        util.exportExcel(response, cspsList, "个人拨比列表");
    }

    /**
     * 奖金拨比列表合计
     **/
    @AccessPermissions("performanceList")
    @GetMapping("/subtotal")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult subtotal(CuBonusStatisVO cuBonusStatisVO) {
        int pkCountry = SecurityUtils.getPkCountry();
        CuBonusStatisVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        R<CurrencyDTO> chCurrency = currencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
        cuBonusStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusStatisVO.setPkCountry(pkCountry);
        Date date = DateUtils.currentDate();
        //默认查询当前日期的前一天
        if (StringUtils.isEmpty(cuBonusStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusStatisVO.getEndDate())) {
            cuBonusStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkBdVertexStr())) {
            String[] split = cuBonusStatisVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkTeamCodeStr())) {
            String[] split = cuBonusStatisVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkTeamCodeList(pkTeamCodeList);
        }
        //查询订单业绩,查询分红配置每月10号分红
        //查询奖金扣项配置参数 0.8
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }

        //查询体系订单分红业绩
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisVO.setOrderTypeList(orderTypeList);
        cuBonusStatisVO.setDividendRatio(BigDecimal.ZERO);
        startPage();
        subtotal = iCuBonusStatisService.queryCuBonusSumStatis(cuBonusStatisVO);
        if (subtotal != null && pkCountry == CountryConstants.CHINA_COUNTRY) {
            if (subtotal.getIntroduceBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setIntroduceBonusSum(subtotal.getIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getMakerDirectSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMakerDirectSum(subtotal.getMakerDirectSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getMakerShareSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMakerShareSum(subtotal.getMakerShareSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getOrgBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setOrgBonusSum(subtotal.getOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getLeaderBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setLeaderBonusSum(subtotal.getLeaderBonusSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (cuBonusStatisVO.getOrderAchieve().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setShareBonusSum(cuBonusStatisVO.getOrderAchieve().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getServiceBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setServiceBonusSum(subtotal.getServiceBonusSum().multiply(currency.getData().getInExchangeRate()));
            }

            if (subtotal.getRepurRangeBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setRepurRangeBonusSum(subtotal.getRepurRangeBonusSum().multiply(currency.getData().getInExchangeRate()));
            }
            if (subtotal.getRepurOrgBonusSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setRepurOrgBonusSum(subtotal.getRepurOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
            }
            subtotal.setMainBonusSum(subtotal.getMainBonusSum().multiply(currency.getData().getInExchangeRate()));
            subtotal.setRepurBonusSum(subtotal.getRepurBonusSum().multiply(currency.getData().getInExchangeRate()));
            subtotal.setTotalBonusSum(subtotal.getTotalBonusSum().multiply(currency.getData().getInExchangeRate()));
        }
        return AjaxResult.success(subtotal);
    }


    /**
     * 奖金拨比图表统计
     **/
    @AccessPermissions("performanceChart")
    @GetMapping("/chart")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo chart(CuBonusStatisChartVO chartVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        chartVO.setPkCountry(pkCountry);
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        chartVO.setRate(exchangeRate);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(chartVO.getStartDate()) ||
                StringUtils.isEmpty(chartVO.getEndDate())) {
            chartVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            chartVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        BigDecimal cent = new BigDecimal(100);
        chartVO.setPerCent(exchangeRate.multiply(cent));
        if (StringUtils.isNotEmpty(chartVO.getPkBdVertexStr())) {
            String[] split = chartVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(chartVO.getPkTeamCodeStr())) {
            String[] split = chartVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkTeamCodeList(pkTeamCodeList);
        }
        //查询奖金扣项配置参数
        CuBonusStatisVO cuBonusStatisVO = new CuBonusStatisVO();
        cuBonusStatisVO.setPkCountry(pkCountry);
        BigDecimal orderAchieve;
        BigDecimal dividendRatio = BigDecimal.ZERO;
        //查询奖金扣项配置参数 0.8
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }

        CuBonusStatisVO cuBonusStatisAchieve = new CuBonusStatisVO();
        cuBonusStatisAchieve.setStartDate(chartVO.getStartDate());
        cuBonusStatisAchieve.setEndDate(chartVO.getEndDate());
        cuBonusStatisAchieve.setPkCountry(pkCountry);
        cuBonusStatisAchieve.setPkBdVertexList(chartVO.getPkBdVertexList());
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisAchieve.setOrderTypeList(orderTypeList);
        orderAchieve = iCuBonusStatisService.queryOrderAchieveSum(cuBonusStatisAchieve).getOrderAchieve();
        if (orderAchieve == null) {
            orderAchieve = BigDecimal.ZERO;
        }
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {

            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            chartVO.setDividendRatio(dividendRatio);
        } else {
            chartVO.setDividendRatio(BigDecimal.ZERO);
        }
        chartVO.setOrderAchieve(orderAchieve.multiply(dividendRatio));

        startPage();
        List<CuBonusStatisChartVO> slList = iCuBonusStatisService.queryCuBonusStatisChart(chartVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                if (itm.getIntroduceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setIntroduceBonus(itm.getIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setOrgBonus(itm.getOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getLeaderBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setLeaderBonus(itm.getLeaderBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getShareBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setShareBonus(itm.getShareBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getServiceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setServiceBonus(itm.getServiceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getHiFunIncome().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setHiFunIncome(itm.getHiFunIncome().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurRangeBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurRangeBonus(itm.getRepurRangeBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurOrgBonus(itm.getRepurOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getAgentIntroduceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setAgentIntroduceBonus(itm.getAgentIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setRepurBonus(itm.getRepurBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setTotalBonus(itm.getTotalBonus().multiply(currency.getData().getInExchangeRate()));
            });
        }
        return getDataTable(slList);
    }

    /**
     * 奖金拨比图表合计
     **/
    @AccessPermissions("performanceChart")
    @GetMapping("/chartSum")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult chartSum(CuBonusStatisChartVO chartVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        chartVO.setPkCountry(pkCountry);
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        chartVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(chartVO.getStartDate()) ||
                StringUtils.isEmpty(chartVO.getEndDate())) {
            chartVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            chartVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        chartVO.setPerCent(exchangeRate.multiply(cent));
        if (StringUtils.isNotEmpty(chartVO.getPkBdVertexStr())) {
            String[] split = chartVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(chartVO.getPkTeamCodeStr())) {
            String[] split = chartVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkTeamCodeList(pkTeamCodeList);
        }
        //查询奖金扣项配置参数
        CuBonusStatisVO cuBonusStatisVO = new CuBonusStatisVO();
        cuBonusStatisVO.setPkCountry(pkCountry);
        BigDecimal orderAchieve;
        BigDecimal dividendRatio = BigDecimal.ZERO;
        //查询奖金扣项配置参数 0.8
        if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        CuBonusStatisVO cuBonusStatisAchieve = new CuBonusStatisVO();
        cuBonusStatisAchieve.setStartDate(chartVO.getStartDate());
        cuBonusStatisAchieve.setEndDate(chartVO.getEndDate());
        cuBonusStatisAchieve.setPkCountry(pkCountry);
        cuBonusStatisAchieve.setPkBdVertexList(chartVO.getPkBdVertexList());
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisAchieve.setOrderTypeList(orderTypeList);
        orderAchieve = iCuBonusStatisService.queryOrderAchieveSum(cuBonusStatisAchieve).getOrderAchieve();
        if (orderAchieve == null) {
            orderAchieve = BigDecimal.ZERO;
        }
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {

            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            chartVO.setDividendRatio(dividendRatio);
        } else {
            chartVO.setDividendRatio(BigDecimal.ZERO);
        }
        chartVO.setOrderAchieve(orderAchieve.multiply(dividendRatio));

        CuBonusStatisChartVO sumChart = iCuBonusStatisService.queryCuBonusSumStatisChart(chartVO);
        if (sumChart != null && pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            sumChart.setIntroduceBonusSum(sumChart.getIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setOrgBonusSum(sumChart.getOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setLeaderBonusSum(sumChart.getLeaderBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setShareBonusSum(sumChart.getShareBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setServiceBonusSum(sumChart.getServiceBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setRepurRangeBonusSum(sumChart.getRepurRangeBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setRepurOrgBonusSum(sumChart.getRepurOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setAgentIntroduceBonusSum(sumChart.getAgentIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setMainBonusSum(sumChart.getMainBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setRepurBonusSum(sumChart.getRepurBonusSum().multiply(currency.getData().getInExchangeRate()));
            sumChart.setTotalBonusSum(sumChart.getTotalBonusSum().multiply(currency.getData().getInExchangeRate()));
        }
        if (sumChart != null && !pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            if (sumChart.getMainRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setMainRegAmount(sumChart.getMainRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getMainUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setMainUpAmount(sumChart.getMainUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getAgentRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setAgentRegAmount(sumChart.getAgentRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getAgentUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setAgentUpAmount(sumChart.getAgentUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setFirstPurchaseAll(sumChart.getFirstPurchaseAll().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getAgentFirstAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setAgentFirstAmount(sumChart.getAgentFirstAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getAgentRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setAgentRepurAmount(sumChart.getAgentRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getMainRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setMainRepurAmount(sumChart.getMainRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (sumChart.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                sumChart.setRepurchaseAllAmount(sumChart.getRepurchaseAllAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
        }
        return AjaxResult.success(sumChart);
    }

    /**
     * 导出奖金拨比列表
     **/
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuBonusStatisVO cuBonusStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusStatisVO> slList;
        CuBonusStatisVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        R<CurrencyDTO> chCurrency = currencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
        BigDecimal chRate = chCurrency.getData().getInExchangeRate() == null ? BigDecimal.ONE : chCurrency.getData().getInExchangeRate();
        cuBonusStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusStatisVO.getEndDate())) {
            cuBonusStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkBdVertexStr())) {
            String[] split = cuBonusStatisVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(cuBonusStatisVO.getPkTeamCodeStr())) {
            String[] split = cuBonusStatisVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            cuBonusStatisVO.setPkTeamCodeList(pkTeamCodeList);
        }
        //查询奖金扣项配置参数 0.8
        if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisVO.setOrderTypeList(orderTypeList);
        cuBonusStatisVO.setDividendRatio(BigDecimal.ZERO);

        startPage();
        slList = iCuBonusStatisService.queryCuBonusStatis(cuBonusStatisVO);
        //合计
        subtotal = iCuBonusStatisService.queryCuBonusSumStatis(cuBonusStatisVO);

        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                if (itm.getIntroduceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setIntroduceBonus(itm.getIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setOrgBonus(itm.getOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getLeaderBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setLeaderBonus(itm.getLeaderBonus().multiply(currency.getData().getInExchangeRate()));
                }
                String creationTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, itm.getCreationTime());
                //重新算首购拨比
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal mainBonusRate = cent.multiply((itm.getMainBonus()).multiply(currency.getData().getInExchangeRate()));
                    mainBonusRate = mainBonusRate.divide(itm.getFirstPurchaseAll(), 4, RoundingMode.HALF_UP);
                    itm.setMainBonusRate(mainBonusRate);
                }
                //重新算总拨比
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0 ||
                        itm.getRepurchaseAllAmount().compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal totalBonusRate = cent.multiply((itm.getTotalBonus()).multiply(currency.getData().getInExchangeRate()));
                    totalBonusRate = totalBonusRate.divide(itm.getFirstPurchaseAll().add(itm.getRepurchaseAllAmount()), 4, RoundingMode.HALF_UP);
                    itm.setTotalBonusRate(totalBonusRate);
                }
                //首购拨出金额
                itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setTotalBonus((itm.getTotalBonus()).multiply(currency.getData().getInExchangeRate()));

                if (itm.getServiceBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setServiceBonus(itm.getServiceBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurRangeBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurRangeBonus(itm.getRepurRangeBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurOrgBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurOrgBonus(itm.getRepurOrgBonus().multiply(currency.getData().getInExchangeRate()));
                }
                if (itm.getRepurBonus().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setRepurBonus(itm.getRepurBonus().multiply(chRate));
                }
            });
        }

        //合计
        if (subtotal == null) {
            subtotal = new CuBonusStatisVO();
        }
        subtotal.setVertexName("合计");
        subtotal.setMainBonusRate(subtotal.getMainBonusRateSum());
        subtotal.setRepurBonusRate(subtotal.getRepurBonusRateSum());
        subtotal.setIntroduceBonus(subtotal.getIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setOrgBonus(subtotal.getOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setLeaderBonus(subtotal.getLeaderBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setShareBonus(subtotal.getShareBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setServiceBonus(subtotal.getServiceBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setRepurRangeBonus(subtotal.getRepurRangeBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setRepurOrgBonus(subtotal.getRepurOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setMainBonus(subtotal.getMainBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setRepurBonus(subtotal.getRepurBonusSum().multiply(currency.getData().getInExchangeRate()));
        subtotal.setTotalBonus((subtotal.getTotalBonusSum()).multiply(currency.getData().getInExchangeRate()));
        subtotal.setMainRegAmount(subtotal.getMainRegAmountSum());
        subtotal.setMainUpAmount(subtotal.getMainUpAmountSum());
        subtotal.setFirstPurchaseAll(subtotal.getFirstPurchaseAllSum());
        subtotal.setMainRepurAmount(subtotal.getMainRepurAmountSum());
        subtotal.setRepurchaseAllAmount(subtotal.getRepurchaseAllAmountSum());
        subtotal.setTotalBonusRate(subtotal.getTotalBonusRateSum());
        subtotal.setMainBonusPvRate(subtotal.getMainBonusPvRateSum());
        subtotal.setRepurBonusPvRate(subtotal.getRepurBonusPvRateSum());
        slList.add(subtotal);
        ExcelUtil<CuBonusStatisVO> util = new ExcelUtil<>(CuBonusStatisVO.class);
        util.exportExcel(response, slList, "拨比列表");
    }


    /**
     * 导出奖金图表
     **/
    @PostMapping("/exportChart")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.EXPORT)
    public void exportChart(HttpServletResponse response, CuBonusStatisChartVO chartVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        chartVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        chartVO.setPerCent(exchangeRate.multiply(cent));
        chartVO.setPkCountry(pkCountry);
        if (StringUtils.isNotEmpty(chartVO.getPkBdVertexStr())) {
            String[] split = chartVO.getPkBdVertexStr().split(",");
            List<String> pkBdVertexStr = Arrays.asList(split);
            final List<Integer> pkBdVertexList = pkBdVertexStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkBdVertexList(pkBdVertexList);
        }
        if (StringUtils.isNotEmpty(chartVO.getPkTeamCodeStr())) {
            String[] split = chartVO.getPkTeamCodeStr().split(",");
            List<String> pkTeamCodeStr = Arrays.asList(split);
            final List<Integer> pkTeamCodeList = pkTeamCodeStr.stream().map(Integer::valueOf).collect(Collectors.toList());
            chartVO.setPkTeamCodeList(pkTeamCodeList);
        }
        //查询奖金扣项配置参数
        CuBonusStatisVO cuBonusStatisVO = new CuBonusStatisVO();
        cuBonusStatisVO.setPkCountry(pkCountry);

        BigDecimal orderAchieve;
        BigDecimal dividendRatio = BigDecimal.ZERO;
        //查询奖金扣项配置参数 0.8
        if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
            if (CollectionUtil.isNotEmpty(bonus)) {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
            } else {
                cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
            }
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        CuBonusStatisVO cuBonusStatisAchieve = new CuBonusStatisVO();
        cuBonusStatisAchieve.setStartDate(cuBonusStatisVO.getStartDate());
        cuBonusStatisAchieve.setEndDate(cuBonusStatisVO.getEndDate());
        cuBonusStatisAchieve.setPkCountry(pkCountry);
        cuBonusStatisAchieve.setPkBdVertexList(cuBonusStatisVO.getPkBdVertexList());
        List<Integer> orderTypeList = new ArrayList<>();
        orderTypeList.add(EOrderType.REGISTER_ORDER.getValue());
        orderTypeList.add(EOrderType.UPGRADE_ORDER.getValue());
        cuBonusStatisAchieve.setOrderTypeList(orderTypeList);
        orderAchieve = iCuBonusStatisService.queryOrderAchieveSum(cuBonusStatisAchieve).getOrderAchieve();
        if (orderAchieve == null) {
            orderAchieve = BigDecimal.ZERO;
        }
        //查询分红比例配置
        List<CuBonusStatisVO> bdr = iCuBonusStatisService.bonusDividendRatiolist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bdr)) {

            BigDecimal ratio = new BigDecimal("0.01");
            for (CuBonusStatisVO cs : bdr) {
                dividendRatio = dividendRatio.add(cs.getDividendRatio());
            }
            dividendRatio = dividendRatio.multiply(ratio).multiply(cuBonusStatisVO.getBackRatio());
            chartVO.setDividendRatio(dividendRatio);
        } else {
            chartVO.setDividendRatio(BigDecimal.ZERO);
        }
        chartVO.setOrderAchieve(orderAchieve.multiply(dividendRatio));
        List<CuBonusStatisChartVO> slList = iCuBonusStatisService.queryCuBonusStatisChart(chartVO);
        CuBonusStatisChartVO sumChart = iCuBonusStatisService.queryCuBonusSumStatisChart(chartVO);
        if (sumChart == null) {
            sumChart = new CuBonusStatisChartVO();
        }
        sumChart.setVertexName("合计");
        sumChart.setIntroduceBonus(sumChart.getIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setOrgBonus(sumChart.getOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setLeaderBonus(sumChart.getLeaderBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setShareBonus(sumChart.getShareBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setServiceBonus(sumChart.getServiceBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setRepurRangeBonus(sumChart.getRepurRangeBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setRepurOrgBonus(sumChart.getRepurOrgBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setAgentIntroduceBonus(sumChart.getAgentIntroduceBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setMainBonus(sumChart.getMainBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setRepurBonus(sumChart.getRepurBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setTotalBonus(sumChart.getTotalBonusSum().multiply(currency.getData().getInExchangeRate()));
        sumChart.setTotalBonusRate(sumChart.getTotalBonusRateSum());
        sumChart.setMainRegAmount(sumChart.getMainRegAmountSum());
        sumChart.setMainUpAmount(sumChart.getMainUpAmountSum());
        sumChart.setAgentRegAmount(sumChart.getAgentRegAmountSum());
        sumChart.setAgentUpAmount(sumChart.getAgentUpAmountSum());
        sumChart.setFirstPurchaseAll(sumChart.getFirstPurchaseAllSum());
        sumChart.setAgentFirstAmount(sumChart.getAgentFirstAmountSum());
        sumChart.setAgentRepurAmount(sumChart.getAgentRepurAmountSum());
        sumChart.setMainRepurAmount(sumChart.getMainRepurAmountSum());
        sumChart.setRepurchaseAllAmount(sumChart.getRepurchaseAllAmountSum());

        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                itm.setIntroduceBonus(itm.getIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setOrgBonus(itm.getOrgBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setLeaderBonus(itm.getLeaderBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setShareBonus(itm.getShareBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setServiceBonus(itm.getServiceBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setHiFunIncome(itm.getHiFunIncome().multiply(currency.getData().getInExchangeRate()));
                itm.setRepurRangeBonus(itm.getRepurRangeBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setRepurOrgBonus(itm.getRepurOrgBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setAgentIntroduceBonus(itm.getAgentIntroduceBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setRepurBonus(itm.getRepurBonus().multiply(currency.getData().getInExchangeRate()));
                itm.setTotalBonus(itm.getTotalBonus().multiply(currency.getData().getInExchangeRate()));
            });
            slList.add(sumChart);
        }
        ExcelUtil<CuBonusStatisChartVO> util = new ExcelUtil<>(CuBonusStatisChartVO.class);
        util.exportExcel(response, slList, "拨比图表表");
    }


    /**
     * 奖金拨比柱形图
     **/
    @AccessPermissions("performanceRatio")
    @GetMapping("/pillarRatio")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_CHART, method = EOperationMethod.SELECT)
    public AjaxResult pillarRatio(CuBonusStatisColumnarVO columnarVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        columnarVO.setPkCountry(pkCountry);
        CuBonusStatisColumnarVO cbs = new CuBonusStatisColumnarVO();
        if (StringUtils.isNotEmpty(columnarVO.getStartDate()) &&
                StringUtils.isNotEmpty(columnarVO.getEndDate())) {
            cbs = iCuBonusStatisService.pillarRatio(columnarVO);
        }
        return AjaxResult.success(cbs);
    }

    /**
     * 导出奖金拨比柱形图
     **/
    @PostMapping("/export-pillarRatio")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_CHART, method = EOperationMethod.EXPORT)
    public void exportPillarRatio(HttpServletResponse response, CuBonusStatisColumnarVO columnarVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        columnarVO.setPkCountry(pkCountry);
        CuBonusStatisColumnarVO cbs = new CuBonusStatisColumnarVO();
        if (StringUtils.isNotEmpty(columnarVO.getStartDate()) &&
                StringUtils.isNotEmpty(columnarVO.getEndDate())) {
            cbs = iCuBonusStatisService.pillarRatio(columnarVO);
        }
        List<CuBonusStatisColumnarVO> list = new ArrayList<>();
        list.add(cbs);
        ExcelUtil<CuBonusStatisColumnarVO> util = new ExcelUtil<>(CuBonusStatisColumnarVO.class);
        util.exportExcel(response, list, "奖金拨比柱状图");
    }

    /**
     * 奖金拨比饼状图
     **/
    @AccessPermissions("performanceRatio")
    @GetMapping("/pieChartRatio")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_CHART, method = EOperationMethod.SELECT)
    public AjaxResult pieChartRatio(CuBonusStatisRoundVO roundVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        roundVO.setPkCountry(pkCountry);
        CuBonusStatisRoundVO cbs = iCuBonusStatisService.queryDiallingRatio(roundVO);
        if (null != cbs) {
            BigDecimal dialling = cbs.getIntroduceBonus()
                    .add(cbs.getOrgBonus())
                    .add(cbs.getLeaderBonus())
                    .add(cbs.getShareBonus()).add(cbs.getServiceBonus())
                    .add(cbs.getAgentIntroduceBonus()).add(cbs.getAgentFirstAmount())
                    .add(cbs.getAgentRepurAmount());
            BigDecimal dialling2 = cbs.getRepurRangeBonus().add(cbs.getRepurOrgBonus());
            BigDecimal dialling3 = dialling.add(dialling2);
            cbs.setFirstBalance(cbs.getFirstPurchaseAll().subtract(dialling));
            cbs.setRepurBalance(cbs.getRepurchaseAllAmount().subtract(dialling2));
            cbs.setTotalBalance(cbs.getTotalBonus().subtract(dialling3));
            if (cbs.getFirstBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setFirstBalance(BigDecimal.ZERO);
            }
            if (cbs.getRepurBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setRepurBalance(BigDecimal.ZERO);
            }
            if (cbs.getTotalBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setTotalBalance(BigDecimal.ZERO);
            }
        }
        return AjaxResult.success(cbs);
    }

    /**
     * 导出奖金拨比饼状图
     **/
    @PostMapping("/export-pieChartRatio")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_CHART, method = EOperationMethod.EXPORT)
    public void exportPieChartRatio(HttpServletResponse response, CuBonusStatisRoundVO roundVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        roundVO.setPkCountry(pkCountry);
        CuBonusStatisRoundVO cbs = iCuBonusStatisService.queryDiallingRatio(roundVO);
        if (null != cbs) {
            BigDecimal dialling = cbs.getIntroduceBonus()
                    .add(cbs.getOrgBonus())
                    .add(cbs.getLeaderBonus())
                    .add(cbs.getShareBonus()).add(cbs.getServiceBonus())
                    .add(cbs.getAgentIntroduceBonus()).add(cbs.getAgentFirstAmount())
                    .add(cbs.getAgentRepurAmount());
            BigDecimal dialling2 = cbs.getRepurRangeBonus().add(cbs.getRepurOrgBonus());
            BigDecimal dialling3 = dialling.add(dialling2);
            cbs.setFirstBalance(cbs.getFirstPurchaseAll().subtract(dialling));
            cbs.setRepurBalance(cbs.getRepurchaseAllAmount().subtract(dialling2));
            cbs.setTotalBalance(cbs.getTotalBonus().subtract(dialling3));
            if (cbs.getFirstBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setFirstBalance(BigDecimal.ZERO);
            }
            if (cbs.getRepurBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setRepurBalance(BigDecimal.ZERO);
            }
            if (cbs.getTotalBalance().compareTo(BigDecimal.ZERO) < 0) {
                cbs.setTotalBalance(BigDecimal.ZERO);
            }
        }
        List<CuBonusStatisRoundVO> list = new ArrayList<>();
        list.add(cbs);
        ExcelUtil<CuBonusStatisRoundVO> util = new ExcelUtil<>(CuBonusStatisRoundVO.class);
        util.exportExcel(response, list, "奖金拨比饼状图");
    }

    /**
     * 统计历史奖金拨比数据, 手动统计某一天
     **/
    @GetMapping("/statis-history")
    public AjaxResult statisHistoryBonus(String startDate, String endDate) {
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_DATE_NOT_NULL));
        }
        //删除数据
        iCuBonusStatisService.delStatis(startDate, endDate);
        //重新统计
        Boolean b = iCuBonusStatisService.statisHistoryBonus(startDate, endDate);
        return AjaxResult.success(b);
    }

    @GetMapping("/rerun-statis-history")
    public AjaxResult rerunCuBonusStatis(String endDateStr, String startDateStr) {
        //查询前一天的订单和奖金
        Date date = DateUtils.currentDate();
        date = DateUtils.beforeDate(2, ChronoUnit.DAYS, date);
        Date endDate = DateUtils.beforeDate(5, ChronoUnit.DAYS, date);
        if (StringUtils.isEmpty(endDateStr) ||
                StringUtils.isEmpty(startDateStr)) {
            endDateStr = (DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date));
            startDateStr = (DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endDate));
        }
        System.out.println(startDateStr + "--" + endDateStr);
        //删除前6天的数据
        Boolean delstatis = iCuBonusStatisService.delStatis(startDateStr, endDateStr);
        if (delstatis) {
            //重算前6天的数据
            Boolean statis = iCuBonusStatisService.statisHistoryBonus(startDateStr, endDateStr);
            if (!statis) {
                log.info("{}-重算7天定时统计奖金拨统计失败", DateUtils.currentDate());
            }
        }
        return AjaxResult.success();
    }

    /**
     * 删除奖金拨比统计表数据
     **/
    @GetMapping("/delStatis")
    public AjaxResult delStatis(String startDate, String endDate) {
        Boolean b = iCuBonusStatisService.delStatis(startDate, endDate);
        return AjaxResult.success(b);
    }

}
