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


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.TransactionUtils;
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.security.utils.SecurityUtils;
import com.hzs.member.statis.service.ICuBonusStatisService;
import com.hzs.member.statis.vo.CuBonusStatisChartVO;
import com.hzs.member.statis.vo.CuBonusStatisColumnarVO;
import com.hzs.member.statis.vo.CuBonusStatisRoundVO;
import com.hzs.member.statis.vo.CuBonusStatisVO;
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.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奖金拨比统计 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2023-05-31
 */
@Slf4j
@RestController
@RequestMapping("/manage/bonus-statis")
public class CuBonusStatisController extends BaseController {

    @Autowired
    private ICuBonusStatisService iCuBonusStatisService;
    @DubboReference
    private ICurrencyServiceApi currencyServiceApi;


    /**
     * @description: 奖金拨比列表
     * @author: zhang jing
     * @date: 2023/6/2 17:27
     * @param: [cuBonusStatisVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo list(CuBonusStatisVO cuBonusStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        BigDecimal cent = new BigDecimal(100);
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        List<CuBonusStatisVO> slList;
        cuBonusStatisVO.setPkCountry(pkCountry);

        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }
        startPage();
        slList = iCuBonusStatisService.queryForeignCountryCuBonusStatis(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                //订单金额转美元
                if (itm.getMainRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRegAmount(itm.getMainRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainUpAmount(itm.getMainUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFictitiousOrder().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFictitiousOrder(itm.getFictitiousOrder().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFirstPurchaseAll(itm.getFirstPurchaseAll().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRepurAmount(itm.getMainRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

            });
        }
        return getDataTable(slList);
    }

    /**
     * @description: 奖金拨比列表合计
     * @author: zhang jing
     * @date: 2023/9/15 14:04
     * @param: [cuBonusStatisVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/subtotal")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult subtotal(CuBonusStatisVO cuBonusStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuBonusStatisVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        BigDecimal cent = new BigDecimal(100);
        cuBonusStatisVO.setPkCountry(pkCountry);
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));

        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }

        startPage();
        subtotal = iCuBonusStatisService.queryForeignCountryCuBonusStatisSum(cuBonusStatisVO);
        if (subtotal != null) {
            //订单金额转美元
            if (subtotal.getMainRegAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRegAmountSum(subtotal.getMainRegAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainUpAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainUpAmountSum(subtotal.getMainUpAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFictitiousOrderSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFictitiousOrderSum(subtotal.getFictitiousOrderSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFirstPurchaseAllSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFirstPurchaseAllSum(subtotal.getFirstPurchaseAllSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainRepurAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRepurAmountSum(subtotal.getMainRepurAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
        }

        return AjaxResult.success(subtotal);
    }


    /**
     * @description: 奖金拨比图表统计
     * @author: zhang jing
     * @date: 2023/6/2 17:27
     * @param: [cuBonusStatisVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/chart")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo chart(CuBonusStatisVO chartVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        BigDecimal cent = new BigDecimal(100);
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        chartVO.setPerCent(exchangeRate.multiply(cent));
        List<CuBonusStatisVO> slList;
        chartVO.setPkCountry(pkCountry);

        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(chartVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            chartVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            chartVO.setBackRatio(BigDecimal.ONE);
        }
        startPage();
        slList = iCuBonusStatisService.queryForeignCountryCuBonusStatis(chartVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                //订单金额转美元
                if (itm.getMainRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRegAmount(itm.getMainRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainUpAmount(itm.getMainUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFictitiousOrder().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFictitiousOrder(itm.getFictitiousOrder().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFirstPurchaseAll(itm.getFirstPurchaseAll().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRepurAmount(itm.getMainRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

            });
        }
        return getDataTable(slList);
    }

    /**
     * @description: 奖金拨比图表合计
     * @author: zhang jing
     * @date: 2023/9/18 14:42
     * @param: [chartVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/chartSum")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult chartSum(CuBonusStatisVO chartVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuBonusStatisVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        BigDecimal cent = new BigDecimal(100);
        chartVO.setPkCountry(pkCountry);
        chartVO.setPerCent(exchangeRate.multiply(cent));

        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(chartVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            chartVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            chartVO.setBackRatio(BigDecimal.ONE);
        }

        startPage();
        subtotal = iCuBonusStatisService.queryForeignCountryCuBonusStatisSum(chartVO);
        if (subtotal != null) {
            //订单金额转美元
            if (subtotal.getMainRegAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRegAmountSum(subtotal.getMainRegAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainUpAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainUpAmountSum(subtotal.getMainUpAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFictitiousOrderSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFictitiousOrderSum(subtotal.getFictitiousOrderSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFirstPurchaseAllSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFirstPurchaseAllSum(subtotal.getFirstPurchaseAllSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainRepurAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRepurAmountSum(subtotal.getMainRepurAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
        }

        return AjaxResult.success(subtotal);
    }

    /**
     * @description: 导出奖金拨比列表
     * @author: zhang jing
     * @date: 2023/8/16 20:07
     * @param: [response, cuBonusStatisVO]
     * @return: void
     **/
    @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 = null;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal cent = new BigDecimal(100);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusStatisVO.setPkCountry(pkCountry);
        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);
        }
        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }

        startPage();
        {
            slList = iCuBonusStatisService.queryForeignCountryCuBonusStatis(cuBonusStatisVO);
            //合计
            subtotal = iCuBonusStatisService.queryForeignCountryCuBonusStatisSum(cuBonusStatisVO);
        }
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                if (pkCountry == CountryConstants.CHINA_COUNTRY) {
                    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()));
                    }
                    itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurBonus(itm.getRepurBonus().multiply(currency.getData().getInExchangeRate()));
                    itm.setTotalBonus(itm.getTotalBonus().multiply(currency.getData().getInExchangeRate()));
                }
                //订单金额
                if (itm.getMainRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRegAmount(itm.getMainRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainUpAmount(itm.getMainUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFictitiousOrder().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFictitiousOrder(itm.getFictitiousOrder().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFirstPurchaseAll(itm.getFirstPurchaseAll().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

                if (itm.getMainRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRepurAmount(itm.getMainRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
            });
        }

        //合计
        if (subtotal == null) {
            subtotal = new CuBonusStatisVO();
        }
        subtotal.setVertexName("合计");
        if (subtotal != null && pkCountry != CountryConstants.CHINA_COUNTRY) {
            //订单金额转美元
            if (subtotal.getMainRegAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRegAmountSum(subtotal.getMainRegAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainUpAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainUpAmountSum(subtotal.getMainUpAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFictitiousOrderSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFictitiousOrderSum(subtotal.getFictitiousOrderSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFirstPurchaseAllSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFirstPurchaseAllSum(subtotal.getFirstPurchaseAllSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainRepurAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRepurAmountSum(subtotal.getMainRepurAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            subtotal.setMainRegAmount(subtotal.getMainRegAmountSum());
            subtotal.setMainUpAmount(subtotal.getMainUpAmountSum());
            subtotal.setFictitiousOrder(subtotal.getFictitiousOrderSum());
            subtotal.setFirstPurchaseAll(subtotal.getFirstPurchaseAllSum());
            subtotal.setMainRepurAmount(subtotal.getMainRepurAmountSum());
            subtotal.setRepurRangeIncome(subtotal.getRepurRangeIncomeSum());
            subtotal.setSameLevelIncome(subtotal.getSameLevelIncomeSum());
            subtotal.setSeePointIncome(subtotal.getSeePointIncomeSum());
            subtotal.setShareIncome(subtotal.getShareIncomeSum());
            subtotal.setIntroduceBonus(subtotal.getIntroduceBonusSum());
            subtotal.setOrgBonus(subtotal.getOrgBonusSum());
            subtotal.setLeaderBonus(subtotal.getLeaderBonusSum());

            subtotal.setMainBonus(subtotal.getMainBonusSum());
            subtotal.setRepurBonus(subtotal.getRepurBonusSum());
            subtotal.setTotalBonus(subtotal.getTotalBonusSum());
            subtotal.setMainBonusRate(subtotal.getMainBonusRateSum());
            subtotal.setRepurBonusRate(subtotal.getRepurBonusRateSum());
            subtotal.setTotalBonusRate(subtotal.getTotalBonusRateSum());
        }
        slList.add(subtotal);
        ExcelUtil<CuBonusStatisVO> util = new ExcelUtil<>(CuBonusStatisVO.class);
        util.exportExcel(response, slList, "拨比列表");
    }


    /**
     * @description: 导出奖金图表
     * @author: zhang jing
     * @date: 2023/9/23 10:11
     * @param: [response, chartVO]
     * @return: void
     **/
    @PostMapping("/exportChart")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.EXPORT)
    public void exportChart(HttpServletResponse response, CuBonusStatisVO cuBonusStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusStatisVO> slList;
        List<CuBonusStatisChartVO> chartList = new ArrayList<>();
        CuBonusStatisVO subtotal = null;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal cent = new BigDecimal(100);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusStatisVO.setPkCountry(pkCountry);
        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);
        }
        //查询奖金扣项配置参数
        List<CuBonusStatisVO> bonus = iCuBonusStatisService.bonusDeductlist(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(bonus)) {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE.subtract(bonus.get(0).getBackRatio()));
        } else {
            cuBonusStatisVO.setBackRatio(BigDecimal.ONE);
        }

            slList = iCuBonusStatisService.queryForeignCountryCuBonusStatis(cuBonusStatisVO);
            //合计
            subtotal = iCuBonusStatisService.queryForeignCountryCuBonusStatisSum(cuBonusStatisVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                if (pkCountry == CountryConstants.CHINA_COUNTRY) {
                    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()));
                    }
                    itm.setMainBonus(itm.getMainBonus().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurBonus(itm.getRepurBonus().multiply(currency.getData().getInExchangeRate()));
                    itm.setTotalBonus(itm.getTotalBonus().multiply(currency.getData().getInExchangeRate()));
                }
                //订单金额
                if (itm.getMainRegAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRegAmount(itm.getMainRegAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getMainUpAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainUpAmount(itm.getMainUpAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
                if (itm.getFictitiousOrder().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFictitiousOrder(itm.getFictitiousOrder().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

                if (itm.getFirstPurchaseAll().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setFirstPurchaseAll(itm.getFirstPurchaseAll().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }

                if (itm.getMainRepurAmount().compareTo(BigDecimal.ZERO) > 0) {
                    itm.setMainRepurAmount(itm.getMainRepurAmount().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
                }
            });
        }

        //合计
        if (subtotal == null) {
            subtotal = new CuBonusStatisVO();
        }
        subtotal.setVertexName("合计");
        if (subtotal != null && pkCountry != CountryConstants.CHINA_COUNTRY) {
            //订单金额转美元
            if (subtotal.getMainRegAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRegAmountSum(subtotal.getMainRegAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainUpAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainUpAmountSum(subtotal.getMainUpAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFictitiousOrderSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFictitiousOrderSum(subtotal.getFictitiousOrderSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getFirstPurchaseAllSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setFirstPurchaseAllSum(subtotal.getFirstPurchaseAllSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            if (subtotal.getMainRepurAmountSum().compareTo(BigDecimal.ZERO) > 0) {
                subtotal.setMainRepurAmountSum(subtotal.getMainRepurAmountSum().divide(currency.getData().getInExchangeRate(), 4, RoundingMode.HALF_UP));
            }
            subtotal.setMainRegAmount(subtotal.getMainRegAmountSum());
            subtotal.setMainUpAmount(subtotal.getMainUpAmountSum());
            subtotal.setFictitiousOrder(subtotal.getFictitiousOrderSum());
            subtotal.setFirstPurchaseAll(subtotal.getFirstPurchaseAllSum());
            subtotal.setMainRepurAmount(subtotal.getMainRepurAmountSum());
            subtotal.setSeePointIncome(subtotal.getSeePointIncomeSum());
            subtotal.setShareIncome(subtotal.getShareIncomeSum());
            subtotal.setIntroduceBonus(subtotal.getIntroduceBonusSum());
            subtotal.setOrgBonus(subtotal.getOrgBonusSum());
            subtotal.setLeaderBonus(subtotal.getLeaderBonusSum());
            subtotal.setRepurRangeIncome(subtotal.getRepurRangeIncomeSum());
            subtotal.setSameLevelIncome(subtotal.getSameLevelIncomeSum());
            subtotal.setMainBonus(subtotal.getMainBonusSum());
            subtotal.setRepurBonus(subtotal.getRepurBonusSum());
            subtotal.setTotalBonus(subtotal.getTotalBonusSum());
            subtotal.setSeePointIncomeRate(subtotal.getSeePointIncomeRateSum());
            subtotal.setShareIncomeRate(subtotal.getShareIncomeRateSum());
            subtotal.setIntroduceBonusRate(subtotal.getIntroduceBonusRateSum());
            subtotal.setOrgBonusRate(subtotal.getOrgBonusRateSum());
            subtotal.setLeaderBonusRate(subtotal.getLeaderBonusRateSum());
            subtotal.setMainBonusRate(subtotal.getMainBonusRateSum());
            subtotal.setRepurBonusRate(subtotal.getRepurBonusRateSum());
            subtotal.setTotalBonusRate(subtotal.getTotalBonusRateSum());
        }
        slList.add(subtotal);
        slList.forEach(item -> chartList.add(BeanUtil.copyProperties(item, CuBonusStatisChartVO.class)));
        ExcelUtil<CuBonusStatisChartVO> util = new ExcelUtil<>(CuBonusStatisChartVO.class);
        util.exportExcel(response, chartList, "拨比图表表");
    }


    /**
     * @description: 奖金拨比柱形图
     * @author: zhang jing
     * @date: 2023/6/3 15:12
     * @param: [cuBonusStatisVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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);
    }

    /**
     * @description: 导出奖金拨比柱形图
     * @author: zhang jing
     * @date: 2023/9/23 10:14
     * @param: [columnarVO]
     * @return: void
     **/
    @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, "奖金拨比柱状图");
    }

    /**
     * @description: 奖金拨比饼状图
     * @author: zhang jing
     * @date: 2023/6/5 10:42
     * @param: [cuBonusStatisVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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);
    }

    /**
     * @description: 导出奖金拨比饼状图
     * @author: zhang jing
     * @date: 2023/9/23 10:20
     * @param: [response, roundVO]
     * @return: void
     **/
    @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, "奖金拨比饼状图");
    }

    /**
     * @description: 统计历史奖金拨比数据, 手动统计某一天
     * @author: zhang jing
     * @date: 2023/8/16 11:43
     * @param: []
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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);
    }

    /**
     * @description: 删除奖金拨比统计表数据
     * @author: zhang jing
     * @date: 2023/9/11 16:04
     * @param: []
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/delStatis")
    public AjaxResult delStatis(String startDate, String endDate) {
        Boolean b = iCuBonusStatisService.delStatis(startDate, endDate);
        return AjaxResult.success(b);
    }


}
