package com.hzs.report.member.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
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.base.CuMember;
import com.hzs.common.domain.report.stat.CuLianchuangDataStat;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.achieve.ICuMemberSettlePeriodServiceApi;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.report.member.service.ICuLianchuangDataStatService;
import com.hzs.report.member.vo.AbroadDataStatVO;
import com.hzs.report.member.vo.CuLianchuangDataStatHistoryVO;
import com.hzs.report.member.vo.CuLianchuangDataStatVO;
import com.hzs.report.member.vo.HomeDataStatVO;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
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.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 联创报表统计 前端控制器
 */
@RestController
@RequestMapping("/manager/cu-lianchuang-data-stat")
public class CuLianchuangDataStatController extends BaseController {

    @Autowired
    private ICuLianchuangDataStatService iCuLianchuangDataStatService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    ICurrencyServiceApi iCurrencyServiceApi;
    @DubboReference
    ICuMemberSettlePeriodServiceApi iCuMemberSettlePeriodServiceApi;

    /**
     * 查询联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/oldList")
    public TableDataInfo oldList(CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            Date date = DateUtils.currentDate();
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.COMBINE.getValue());
        if (StringUtils.isNotEmpty(lcVO.getVertexCode())) {
            queryWrapper.eq(CuLianchuangDataStat::getVertexCode, lcVO.getVertexCode());
        }
        queryWrapper.orderByAsc(CuLianchuangDataStat::getCreationTime);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        String orderBusinessStr = "2,7,3";
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(itm -> {
                itm.setPayTime(lcVO.getPayTime());
                itm.setEndDate(lcVO.getPayTime());
                itm.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));

                if (StringUtils.isNotEmpty(itm.getAddRegion())) {
                    List<Integer> addRegionList = Arrays.stream(itm.getAddRegion().split(","))
                            .map(Integer::parseInt)
                            .collect(Collectors.toList());
                    itm.setAddRegionList(addRegionList);
                }

                if (StringUtils.isNotEmpty(itm.getReduceRegion())) {
                    List<Integer> reduceRegionList = Arrays.stream(itm.getReduceRegion().split(","))
                            .map(Integer::parseInt)
                            .collect(Collectors.toList());
                    itm.setReduceRegionList(reduceRegionList);
                }

                List<Integer> orderBusinessList = Arrays.stream(orderBusinessStr.split(","))
                        .map(Integer::parseInt)
                        .collect(Collectors.toList());

                CuLianchuangDataStatVO cdsVO = BeanUtil.copyProperties(itm, CuLianchuangDataStatVO.class);
                list.add(cdsVO);
                //多线程
                orderBusinessList.parallelStream().forEach(orderType -> {
                    List<Integer> orderTypeList = EOrderBusiness.getOrderTypeList(orderType);
                    List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.lcStatListByOrdeType(orderTypeList, orderType, itm);
                    if (CollectionUtil.isNotEmpty(sonList)) {
                        for (CuLianchuangDataStatVO co : sonList) {
                            switch (EOrderBusiness.getEnumByValue(co.getOrderType())) {
                                case FIRST_PURCHASE:
                                    cdsVO.setFirstPurchaseMo(co.getAmount());
                                    cdsVO.setFirstPurchasePv(co.getAchieve());
                                    cdsVO.setFirstPurchaseSumMo(co.getAmountSum());
                                    cdsVO.setFirstPurchaseSumPv(co.getAchieveSumPv());
                                    break;
                                case REPURCHASE_ORDER:
                                    cdsVO.setRepurchaseOrderMo(co.getAmount());
                                    cdsVO.setRepurchaseOrderPv(co.getAchieve());
                                    cdsVO.setRepurchaseOrderSumMo(co.getAmountSum());
                                    cdsVO.setRepurchaseOrderSumPv(co.getAchieveSumPv());
                                    break;
                                case REPURCHASE:
                                    cdsVO.setRepurchaseMo(co.getAmount());
                                    cdsVO.setRepurchasePv(co.getAchieve());
                                    cdsVO.setRepurchaseSumMo(co.getAmountSum());
                                    cdsVO.setRepurchaseSumPv(co.getAchieveSumPv());
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                });
            });
            if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
                if (CollectionUtil.isNotEmpty(list)) {
                    list.forEach(itm -> {
                        BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
                        itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(inExchangeRate));
                        itm.setRepurchaseOrderPv(itm.getRepurchaseOrderPv().multiply(inExchangeRate));
                        itm.setRepurchasePv(itm.getRepurchasePv().multiply(inExchangeRate));
                        itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(inExchangeRate));
                        itm.setRepurchaseOrderSumPv(itm.getRepurchaseOrderSumPv().multiply(inExchangeRate));
                        itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(inExchangeRate));
                    });
                }
            }
        }

        return getDataTable(list);
    }

    /**
     * 查询联创数据报表(优化后)
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        List<CuLianchuangDataStatVO> allList = new ArrayList<>();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            Date date = DateUtils.currentDate();
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.COMBINE.getValue());
        if (StringUtils.isNotEmpty(lcVO.getVertexCode())) {
            queryWrapper.eq(CuLianchuangDataStat::getVertexCode, lcVO.getVertexCode());
        }
        queryWrapper.orderByAsc(CuLianchuangDataStat::getCreationTime);

        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        List<Long> lcMemberStr = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(itm -> {
                if (StringUtils.isNotEmpty(itm.getAddRegion())) {
                    lcMemberStr.addAll(Arrays.stream(itm.getAddRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList()));
                }
                if (StringUtils.isNotEmpty(itm.getReduceRegion())) {
                    lcMemberStr.addAll(Arrays.stream(itm.getReduceRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList()));
                }
            });
        }
        //去重lcMemberStr
        List<Long> uniqueList = new ArrayList<>(new LinkedHashSet<>(lcMemberStr));
        lcVO.setEndDate(lcVO.getPayTime());
        lcVO.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
        lcVO.setPkCountry(SecurityUtils.getPkCountry());
        //多线程
        uniqueList.parallelStream().forEach(itm -> {
            List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.newLcStatListByOrdeType(lcVO, itm);
            allList.addAll(sonList);
        });
        //按照会员ID进行分组
        Map<Long, CuLianchuangDataStatVO> allListstMap = allList.stream()
                .collect(Collectors.toMap(CuLianchuangDataStatVO::getPkMember, Function.identity()));
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(item -> list.add(BeanUtil.copyProperties(item, CuLianchuangDataStatVO.class)));
            //根据匹配的规则添加业绩金额数据
            for (CuLianchuangDataStatVO clos : list) {
                clos.setPayTime(lcVO.getPayTime());
                if (StringUtils.isNotEmpty(clos.getAddRegion())) {
                    List<Long> addRegionList = Arrays.stream(clos.getAddRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList());
                    clos.setAddRegionList(addRegionList);
                }

                if (StringUtils.isNotEmpty(clos.getReduceRegion())) {
                    List<Long> reduceRegionList = Arrays.stream(clos.getReduceRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList());
                    clos.setReduceRegionList(reduceRegionList);
                }
                calculateAmount(clos, allListstMap);
            }
        }
        R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
                itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(inExchangeRate));
                itm.setRepurchaseOrderPv(itm.getRepurchaseOrderPv().multiply(inExchangeRate));
                itm.setRepurchasePv(itm.getRepurchasePv().multiply(inExchangeRate));
                itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(inExchangeRate));
                itm.setRepurchaseOrderSumPv(itm.getRepurchaseOrderSumPv().multiply(inExchangeRate));
                itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(inExchangeRate));
            });
        }

        return getDataTable(list);
    }

    //根据规则计算业绩或金额
    public void calculateAmount(CuLianchuangDataStatVO clos, Map<Long, CuLianchuangDataStatVO> allListstMap) {
        BigDecimal addFpm = BigDecimal.ZERO;
        BigDecimal redFpm = BigDecimal.ZERO;
        BigDecimal addFpp = BigDecimal.ZERO;
        BigDecimal redFpp = BigDecimal.ZERO;
        BigDecimal addFpmSum = BigDecimal.ZERO;
        BigDecimal redFpmSum = BigDecimal.ZERO;
        BigDecimal addFppSum = BigDecimal.ZERO;
        BigDecimal redFppSum = BigDecimal.ZERO;

        BigDecimal addRom = BigDecimal.ZERO;
        BigDecimal redRom = BigDecimal.ZERO;
        BigDecimal addRop = BigDecimal.ZERO;
        BigDecimal redRop = BigDecimal.ZERO;
        BigDecimal addRomSum = BigDecimal.ZERO;
        BigDecimal redRomSum = BigDecimal.ZERO;
        BigDecimal addRopSum = BigDecimal.ZERO;
        BigDecimal redRopSum = BigDecimal.ZERO;

        BigDecimal addRm = BigDecimal.ZERO;
        BigDecimal redRm = BigDecimal.ZERO;
        BigDecimal addRp = BigDecimal.ZERO;
        BigDecimal redRp = BigDecimal.ZERO;
        BigDecimal addRmSum = BigDecimal.ZERO;
        BigDecimal redRmSum = BigDecimal.ZERO;
        BigDecimal addRpSum = BigDecimal.ZERO;
        BigDecimal redRpSum = BigDecimal.ZERO;
        //加区
        for (Long ad : clos.getAddRegionList()) {
            CuLianchuangDataStatVO data = allListstMap.get(ad);
            addFpm = addFpm.add(data.getFirstPurchaseMo());
            addFpp = addFpp.add(data.getFirstPurchasePv());
            addFpmSum = addFpmSum.add(data.getFirstPurchaseSumMo());
            addFppSum = addFppSum.add(data.getFirstPurchaseSumPv());
            addRom = addRom.add(data.getRepurchaseOrderMo());
            addRop = addRop.add(data.getRepurchaseOrderPv());
            addRomSum = addRomSum.add(data.getRepurchaseOrderSumMo());
            addRopSum = addRopSum.add(data.getRepurchaseOrderSumPv());
            addRm = addRm.add(data.getRepurchaseMo());
            addRp = addRp.add(data.getRepurchasePv());
            addRmSum = addRmSum.add(data.getRepurchaseSumMo());
            addRpSum = addRpSum.add(data.getRepurchaseSumPv());
        }
        //减区
        if (CollectionUtil.isNotEmpty(clos.getReduceRegionList())) {
            for (Long re : clos.getReduceRegionList()) {
                CuLianchuangDataStatVO data = allListstMap.get(re);
                redFpm = redFpm.add(data.getFirstPurchaseMo());
                redFpp = redFpp.add(data.getFirstPurchasePv());
                redFpmSum = redFpmSum.add(data.getFirstPurchaseSumMo());
                redFppSum = redFppSum.add(data.getFirstPurchaseSumPv());
                redRom = redRom.add(data.getRepurchaseOrderMo());
                redRop = redRop.add(data.getRepurchaseOrderPv());
                redRomSum = redRomSum.add(data.getRepurchaseOrderSumMo());
                redRopSum = redRopSum.add(data.getRepurchaseOrderSumPv());
                redRm = redRm.add(data.getRepurchaseMo());
                redRp = redRp.add(data.getRepurchasePv());
                redRmSum = redRmSum.add(data.getRepurchaseSumMo());
                redRpSum = redRpSum.add(data.getRepurchaseSumPv());
            }
        }

        clos.setFirstPurchaseMo(addFpm.subtract(redFpm));
        clos.setFirstPurchasePv(addFpp.subtract(redFpp));
        clos.setRepurchaseOrderMo(addRom.subtract(redRom));
        clos.setRepurchaseOrderPv(addRop.subtract(redRop));
        clos.setRepurchaseMo(addRm.subtract(redRm));
        clos.setRepurchasePv(addRp.subtract(redRp));
        clos.setFirstPurchaseSumMo(addFpmSum.subtract(redFpmSum));
        clos.setFirstPurchaseSumPv(addFppSum.subtract(redFppSum));
        clos.setRepurchaseOrderSumMo(addRomSum.subtract(redRomSum));
        clos.setRepurchaseOrderSumPv(addRopSum.subtract(redRopSum));
        clos.setRepurchaseSumMo(addRmSum.subtract(redRmSum));
        clos.setRepurchaseSumPv(addRpSum.subtract(redRpSum));
    }

    /**
     * 查询联创数据报表(历史数据)
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/listHistory")
    public TableDataInfo listHistory(CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            return getDataTable(list);
        }
        //获取之前月的最后一天
        lcVO.setStartDate(DateUtils.beforeMonthLastDateStr(date));
        lcVO.setEndDate(lcVO.getPayTime());
        //结束日期前一天
        lcVO.setBeforeDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, lcVO.getPayTime()));
        lcVO.setPkCountry(SecurityUtils.getPkCountry());
        //设置表名和期数
        setTableNames(lcVO);
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.COMBINE.getValue());
        if (StringUtils.isNotEmpty(lcVO.getVertexCode())) {
            queryWrapper.eq(CuLianchuangDataStat::getVertexCode, lcVO.getVertexCode());
        }
        queryWrapper.orderByAsc(CuLianchuangDataStat::getCreationTime);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
        if (CollectionUtil.isNotEmpty(lcList)) {
            //多线程操作
            list = lcList.parallelStream()
                    .map(itm -> {
                        itm.setPayTime(lcVO.getPayTime());
                        itm.setEndDate(lcVO.getPayTime());
                        processRegions(itm);
                        CuLianchuangDataStatVO cdsVO = BeanUtil.copyProperties(itm, CuLianchuangDataStatVO.class);
                        // 查询首购新增复购新增
                        List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.lcStatOrdeHistoryList(lcVO, itm);
                        cdsVO.setFirstPurchaseMo(getStatistic(sonList, CuLianchuangDataStatVO::getFirstPurchaseMo));
                        cdsVO.setFirstPurchasePv(getStatistic(sonList, CuLianchuangDataStatVO::getFirstPurchasePv));
                        cdsVO.setRepurchaseMo(getStatistic(sonList, CuLianchuangDataStatVO::getRepurchaseMo));
                        cdsVO.setRepurchasePv(getStatistic(sonList, CuLianchuangDataStatVO::getRepurchasePv));
                        // 查询首购新增复购累计
                        List<CuLianchuangDataStatVO> sonSumList = iCuLianchuangDataStatService.lcStatOrderHistorySumList(lcVO, itm);
                        cdsVO.setFirstPurchaseSumMo(getStatistic(sonSumList, CuLianchuangDataStatVO::getFirstPurchaseSumMo));
                        cdsVO.setFirstPurchaseSumPv(getStatistic(sonSumList, CuLianchuangDataStatVO::getFirstPurchaseSumPv));
                        cdsVO.setRepurchaseSumMo(getStatistic(sonSumList, CuLianchuangDataStatVO::getRepurchaseSumMo));
                        cdsVO.setRepurchaseSumPv(getStatistic(sonSumList, CuLianchuangDataStatVO::getRepurchaseSumPv));
                        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                            cdsVO.setFirstPurchasePv(cdsVO.getFirstPurchasePv().multiply(inExchangeRate));
                            cdsVO.setRepurchasePv(cdsVO.getRepurchasePv().multiply(inExchangeRate));
                            cdsVO.setFirstPurchaseSumPv(cdsVO.getFirstPurchaseSumPv().multiply(inExchangeRate));
                            cdsVO.setRepurchaseSumPv(cdsVO.getRepurchaseSumPv().multiply(inExchangeRate));
                        }
                        return cdsVO;
                    })
                    .collect(Collectors.toList());
        }
        return getDataTable(list);
    }

    /**
     * 导出联创数据报表(历史数据)
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.EXPORT)
    @PostMapping("/exportHistory")
    public void exportHistory(HttpServletResponse response, CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatHistoryVO> list = new ArrayList<>();
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            return;
        }
        //获取之前月的最后一天
        lcVO.setStartDate(DateUtils.beforeMonthLastDateStr(date));
        lcVO.setEndDate(lcVO.getPayTime());
        //结束日期前一天
        lcVO.setBeforeDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, lcVO.getPayTime()));
        lcVO.setPkCountry(SecurityUtils.getPkCountry());
        //设置表名和期数
        setTableNames(lcVO);
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.COMBINE.getValue());
        if (StringUtils.isNotEmpty(lcVO.getVertexCode())) {
            queryWrapper.eq(CuLianchuangDataStat::getVertexCode, lcVO.getVertexCode());
        }
        queryWrapper.orderByAsc(CuLianchuangDataStat::getCreationTime);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
        BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
        if (CollectionUtil.isNotEmpty(lcList)) {
            //多线程操作
            list = lcList.parallelStream()
                    .map(itm -> {
                        itm.setPayTime(lcVO.getPayTime());
                        itm.setEndDate(lcVO.getPayTime());
                        processRegions(itm);
                        CuLianchuangDataStatHistoryVO cdsVO = BeanUtil.copyProperties(itm, CuLianchuangDataStatHistoryVO.class);
                        // 查询首购新增复购新增
                        List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.lcStatOrdeHistoryList(lcVO, itm);
                        cdsVO.setFirstPurchaseMo(getStatistic(sonList, CuLianchuangDataStatVO::getFirstPurchaseMo));
                        cdsVO.setFirstPurchasePv(getStatistic(sonList, CuLianchuangDataStatVO::getFirstPurchasePv));
                        cdsVO.setRepurchaseMo(getStatistic(sonList, CuLianchuangDataStatVO::getRepurchaseMo));
                        cdsVO.setRepurchasePv(getStatistic(sonList, CuLianchuangDataStatVO::getRepurchasePv));
                        // 查询首购新增复购累计
                        List<CuLianchuangDataStatVO> sonSumList = iCuLianchuangDataStatService.lcStatOrderHistorySumList(lcVO, itm);
                        cdsVO.setFirstPurchaseSumMo(getStatistic(sonSumList, CuLianchuangDataStatVO::getFirstPurchaseSumMo));
                        cdsVO.setFirstPurchaseSumPv(getStatistic(sonSumList, CuLianchuangDataStatVO::getFirstPurchaseSumPv));
                        cdsVO.setRepurchaseSumMo(getStatistic(sonSumList, CuLianchuangDataStatVO::getRepurchaseSumMo));
                        cdsVO.setRepurchaseSumPv(getStatistic(sonSumList, CuLianchuangDataStatVO::getRepurchaseSumPv));
                        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                            cdsVO.setFirstPurchasePv(cdsVO.getFirstPurchasePv().multiply(inExchangeRate));
                            cdsVO.setRepurchasePv(cdsVO.getRepurchasePv().multiply(inExchangeRate));
                            cdsVO.setFirstPurchaseSumPv(cdsVO.getFirstPurchaseSumPv().multiply(inExchangeRate));
                            cdsVO.setRepurchaseSumPv(cdsVO.getRepurchaseSumPv().multiply(inExchangeRate));
                        }
                        return cdsVO;
                    })
                    .collect(Collectors.toList());
        }
        ExcelUtil<CuLianchuangDataStatHistoryVO> util = new ExcelUtil<>(CuLianchuangDataStatHistoryVO.class);
        util.exportExcel(response, list, "联创数据报表");
    }

    // 辅助方法，用于从列表中获取某个属性的汇总值（例如，如果列表为空，则返回0或null）
    private <T, R> R getStatistic(List<T> list, Function<T, R> extractor) {
        if (CollectionUtil.isEmpty(list)) {
            return null; // 或者根据具体情况返回0或其他默认值
        }
        // 假设我们只需要列表中的第一个元素的值（例如，所有元素的值都应该是相同的）
        // 或者，您可能需要根据具体需求实现更复杂的聚合逻辑
        return extractor.apply(list.get(0));
    }

    //设置表名和期数
    private void setTableNames(CuLianchuangDataStatVO lcVO) {
        String startDateMonth = DateUtils.getMonth(lcVO.getStartDate());
        String endDateMonth = DateUtils.getMonth(lcVO.getEndDate());
        String beforeDateMonth = DateUtils.getMonth(lcVO.getBeforeDate());
        lcVO.setAchieveTableNameSta(TableNameConstants.CU_MEMBER_ACHIEVE + startDateMonth);
        lcVO.setAchieveTableNameBefore(TableNameConstants.CU_MEMBER_ACHIEVE + beforeDateMonth);
        lcVO.setAchieveTableNameEnd(TableNameConstants.CU_MEMBER_ACHIEVE + endDateMonth);
        lcVO.setSettleTableNameSta(TableNameConstants.CU_MEMBER_TREE + startDateMonth);
        lcVO.setSettleTableNameBefore(TableNameConstants.CU_MEMBER_TREE + beforeDateMonth);
        lcVO.setSettleTableNameEnd(TableNameConstants.CU_MEMBER_TREE + endDateMonth);
        lcVO.setPeriodSta(iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(
                DateUtils.parseDateToStr(lcVO.getStartDate())).getData().getPkId());
        lcVO.setPeriodBefore(iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(
                DateUtils.parseDateToStr(lcVO.getBeforeDate())).getData().getPkId());
        lcVO.setPeriodEnd(iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(
                DateUtils.parseDateToStr(lcVO.getEndDate())).getData().getPkId());
    }

    //设置加区和减区
    private void processRegions(CuLianchuangDataStat itm) {
        if (StringUtils.isNotEmpty(itm.getAddRegion())) {
            List<Integer> addRegionList = Arrays.stream(itm.getAddRegion().split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            itm.setAddRegionList(addRegionList);
        }

        if (StringUtils.isNotEmpty(itm.getReduceRegion())) {
            List<Integer> reduceRegionList = Arrays.stream(itm.getReduceRegion().split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            itm.setReduceRegionList(reduceRegionList);
        }
    }


    /**
     * 查询国内联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_HOME_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/home-list")
    public TableDataInfo homeList(CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }

        //查询联创数据报表条件表
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.HOME.getValue());
        queryWrapper.orderByAsc(CuLianchuangDataStat::getPkId);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(itm -> {
                itm.setEndDate(lcVO.getPayTime());
                itm.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
                if (StringUtils.isNotEmpty(itm.getAddRegion())) {
                    List<Integer> addRegionList = Arrays.stream(itm.getAddRegion().split(","))
                            .map(Integer::parseInt)
                            .collect(Collectors.toList());
                    itm.setAddRegionList(addRegionList);
                }
                List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.lcStatList(Arrays.asList(itm));
                list.addAll(sonList);
            });
            if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
                R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
                if (CollectionUtil.isNotEmpty(list)) {
                    list.forEach(itm -> {
                        itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(currency.getData().getInExchangeRate()));
                        itm.setRepurchaseOrderPv(itm.getRepurchaseOrderPv().multiply(currency.getData().getInExchangeRate()));
                        itm.setRepurchasePv(itm.getRepurchasePv().multiply(currency.getData().getInExchangeRate()));
                        itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                        itm.setRepurchaseOrderSumPv(itm.getRepurchaseOrderSumPv().multiply(currency.getData().getInExchangeRate()));
                        itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    });
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询国外联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_ABROAD_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/abroad-list")
    public TableDataInfo abroadList(CuLianchuangDataStatVO lcVO) {
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }
        lcVO.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
        lcVO.setEndDate(lcVO.getPayTime());
        //查询国家列表
        List<CuLianchuangDataStatVO> pkCountryList = iCuLianchuangDataStatService.countryList();
        startPage();
        List<AbroadDataStatVO> list = iCuLianchuangDataStatService.abroadStatList(pkCountryList, lcVO.getStartDate(), lcVO.getEndDate());
        //中国汇率
        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(itm -> {
                    pkCountryList.forEach(itmm -> {
                        //国外金额换算成RMB
                        if (itm.getPkCountry().equals(itmm.getPkCountry())) {
                            itm.setRegisterPurchaseMo(itm.getRegisterPurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setUpgradePurchaseMo(itm.getUpgradePurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setFirstPurchaseMo(itm.getFirstPurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRepurchaseMo(itm.getRepurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRegisterPurchaseSumMo(itm.getRegisterPurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setUpgradePurchaseSumMo(itm.getUpgradePurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setFirstPurchaseSumMo(itm.getFirstPurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRepurchaseSumMo(itm.getRepurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                        }
                    });
                    itm.setRegisterPurchasePv(itm.getRegisterPurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setUpgradePurchasePv(itm.getUpgradePurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRegisterPurchaseSumPv(itm.getRegisterPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setUpgradePurchaseSumPv(itm.getUpgradePurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchasePv(itm.getRepurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                });
            }
        }
        return getDataTable(list);
    }


    /**
     * 导出联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        List<CuLianchuangDataStatVO> allList = new ArrayList<>();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            Date date = DateUtils.currentDate();
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.COMBINE.getValue());
        if (StringUtils.isNotEmpty(lcVO.getVertexCode())) {
            queryWrapper.eq(CuLianchuangDataStat::getVertexCode, lcVO.getVertexCode());
        }
        queryWrapper.orderByAsc(CuLianchuangDataStat::getCreationTime);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        List<Long> lcMemberStr = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(itm -> {
                if (StringUtils.isNotEmpty(itm.getAddRegion())) {
                    lcMemberStr.addAll(Arrays.stream(itm.getAddRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList()));
                }
                if (StringUtils.isNotEmpty(itm.getReduceRegion())) {
                    lcMemberStr.addAll(Arrays.stream(itm.getReduceRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList()));
                }
            });
        }
        //去重lcMemberStr
        List<Long> uniqueList = new ArrayList<>(new LinkedHashSet<>(lcMemberStr));
        lcVO.setEndDate(lcVO.getPayTime());
        lcVO.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
        lcVO.setPkCountry(SecurityUtils.getPkCountry());
        //多线程
        uniqueList.parallelStream().forEach(itm -> {
            List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.newLcStatListByOrdeType(lcVO, itm);
            allList.addAll(sonList);
        });
        //按照会员ID进行分组
        Map<Long, CuLianchuangDataStatVO> allListstMap = allList.stream()
                .collect(Collectors.toMap(CuLianchuangDataStatVO::getPkMember, Function.identity()));
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(item -> list.add(BeanUtil.copyProperties(item, CuLianchuangDataStatVO.class)));
            //根据匹配的规则添加业绩金额数据
            for (CuLianchuangDataStatVO clos : list) {
                clos.setPayTime(lcVO.getPayTime());
                if (StringUtils.isNotEmpty(clos.getAddRegion())) {
                    List<Long> addRegionList = Arrays.stream(clos.getAddRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList());
                    clos.setAddRegionList(addRegionList);
                }

                if (StringUtils.isNotEmpty(clos.getReduceRegion())) {
                    List<Long> reduceRegionList = Arrays.stream(clos.getReduceRegion().split(","))
                            .map(Long::parseLong)
                            .collect(Collectors.toList());
                    clos.setReduceRegionList(reduceRegionList);
                }
                calculateAmount(clos, allListstMap);
            }
        }
        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(itm -> {
                    BigDecimal inExchangeRate = currency.getData().getInExchangeRate();
                    itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(inExchangeRate));
                    itm.setRepurchaseOrderPv(itm.getRepurchaseOrderPv().multiply(inExchangeRate));
                    itm.setRepurchasePv(itm.getRepurchasePv().multiply(inExchangeRate));
                    itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(inExchangeRate));
                    itm.setRepurchaseOrderSumPv(itm.getRepurchaseOrderSumPv().multiply(inExchangeRate));
                    itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(inExchangeRate));
                });
            }
        }
        ExcelUtil<CuLianchuangDataStatVO> util = new ExcelUtil<>(CuLianchuangDataStatVO.class);
        util.exportExcel(response, list, "联创数据报表");
    }

    /**
     * 导出国内联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_HOME_DATA_STAT, method = EOperationMethod.EXPORT)
    @PostMapping("/home-export")
    public void homeExport(HttpServletResponse response, CuLianchuangDataStatVO lcVO) {
        List<CuLianchuangDataStatVO> list = new ArrayList<>();
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            Date date = DateUtils.currentDate();
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, date)));
        }

        //查询联创数据报表条件表
        LambdaQueryWrapper<CuLianchuangDataStat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuLianchuangDataStat::getType, ESheetType.HOME.getValue());
        queryWrapper.orderByAsc(CuLianchuangDataStat::getPkId);
        List<CuLianchuangDataStat> lcList = iCuLianchuangDataStatService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(lcList)) {
            lcList.forEach(itm -> {
                itm.setEndDate(lcVO.getPayTime());
                itm.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
                if (StringUtils.isNotEmpty(itm.getAddRegion())) {
                    List<Integer> addRegionList = Arrays.stream(itm.getAddRegion().split(","))
                            .map(Integer::parseInt)
                            .collect(Collectors.toList());
                    itm.setAddRegionList(addRegionList);
                }
                List<CuLianchuangDataStatVO> sonList = iCuLianchuangDataStatService.lcStatList(Arrays.asList(itm));
                list.addAll(sonList);
            });

        }
        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(SecurityUtils.getPkCountry());
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(itm -> {
                    itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchaseOrderPv(itm.getRepurchaseOrderPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchasePv(itm.getRepurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchaseOrderSumPv(itm.getRepurchaseOrderSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                });
            }
        }
        List<HomeDataStatVO> list1 = new ArrayList<>();
        list.forEach(item -> list1.add(BeanUtil.copyProperties(item, HomeDataStatVO.class)));
        ExcelUtil<HomeDataStatVO> util = new ExcelUtil<>(HomeDataStatVO.class);
        util.exportExcel(response, list1, "国内数据报表");
    }

    /**
     * 导出国外联创数据报表
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_ABROAD_DATA_STAT, method = EOperationMethod.EXPORT)
    @PostMapping("/abroad-export")
    public void abroadExport(HttpServletResponse response, CuLianchuangDataStatVO lcVO) {
        if (StringUtils.isEmpty(lcVO.getPayTime())) {
            Date date = DateUtils.currentDate();
            lcVO.setPayTime(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, date));
        }

        lcVO.setStartDate(DateUtils.currentMonthFirstDateStr(DateUtils.parseStringToDate(lcVO.getPayTime())));
        lcVO.setEndDate(lcVO.getPayTime());
        //查询国家列表
        List<CuLianchuangDataStatVO> pkCountryList = iCuLianchuangDataStatService.countryList();
        List<AbroadDataStatVO> list = iCuLianchuangDataStatService.abroadStatList(pkCountryList, lcVO.getStartDate(), lcVO.getEndDate());
        if (SecurityUtils.getPkCountry().equals(CountryConstants.CHINA_COUNTRY)) {
            //中国汇率
            R<CurrencyDTO> currency = iCurrencyServiceApi.getCurrency(CountryConstants.CHINA_COUNTRY);
            if (CollectionUtil.isNotEmpty(list)) {
                list.forEach(itm -> {
                    pkCountryList.forEach(itmm -> {
                        //国外金额换算成RMB
                        if (itm.getPkCountry().equals(itmm.getPkCountry())) {
                            itm.setRegisterPurchaseMo(itm.getRegisterPurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setUpgradePurchaseMo(itm.getUpgradePurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setFirstPurchaseMo(itm.getFirstPurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRepurchaseMo(itm.getRepurchaseMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRegisterPurchaseSumMo(itm.getRegisterPurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setUpgradePurchaseSumMo(itm.getUpgradePurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setFirstPurchaseSumMo(itm.getFirstPurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                            itm.setRepurchaseSumMo(itm.getRepurchaseSumMo().multiply(currency.getData().getInExchangeRate()).divide(itmm.getRate(), 2, BigDecimal.ROUND_HALF_UP));
                        }
                    });
                    itm.setRegisterPurchasePv(itm.getRegisterPurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setUpgradePurchasePv(itm.getUpgradePurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRegisterPurchaseSumPv(itm.getRegisterPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setUpgradePurchaseSumPv(itm.getUpgradePurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setFirstPurchasePv(itm.getFirstPurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchasePv(itm.getRepurchasePv().multiply(currency.getData().getInExchangeRate()));
                    itm.setFirstPurchaseSumPv(itm.getFirstPurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                    itm.setRepurchaseSumPv(itm.getRepurchaseSumPv().multiply(currency.getData().getInExchangeRate()));
                });
            }
        }
        ExcelUtil<AbroadDataStatVO> util = new ExcelUtil<>(AbroadDataStatVO.class);
        util.exportExcel(response, list, "国外数据报表");
    }

    /**
     * 新增联创查询条件
     **/
    @Log(module = EOperationModule.LEVEL_CONFIG, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody CuLianchuangDataStatVO lcVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.currentDate();
        StringBuilder addRegion = new StringBuilder();
        StringBuilder reduceRegion = new StringBuilder();
        if (StringUtils.isNotEmpty(lcVO.getAddRegion())) {
            String[] addCodeArray = lcVO.getAddRegion().split(",");
            if (addCodeArray.length > 0) {
                for (String s : addCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    addRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setAddRegion(addRegion.toString());
        }
        if (StringUtils.isNotEmpty(lcVO.getReduceRegion())) {
            String[] reduceCodeArray = lcVO.getReduceRegion().split(",");
            if (reduceCodeArray.length > 0) {
                for (String s : reduceCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    reduceRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setReduceRegion(reduceRegion.toString());
        }
        lcVO.setPkCountry(pkCountry);
        lcVO.setPkCreator(userId);
        lcVO.setCreationTime(date);
        CuLianchuangDataStat cld = BeanUtil.copyProperties(lcVO, CuLianchuangDataStat.class);
        return AjaxResult.success(iCuLianchuangDataStatService.save(cld));
    }

    /**
     * 国内数据报表查询
     **/
    @Log(module = EOperationModule.LEVEL_CONFIG, business = EOperationBusiness.LIANCHUANG_HOME_DATA_STAT, method = EOperationMethod.INSERT)
    @PostMapping("/home-save")
    public AjaxResult homeSave(@RequestBody CuLianchuangDataStatVO lcVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.currentDate();
        lcVO.setType(ESheetType.HOME.getValue());
        StringBuilder addRegion = new StringBuilder();
        if (StringUtils.isEmpty(lcVO.getVertexCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_CODE));
        }
        if (StringUtils.isEmpty(lcVO.getVertexName())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_NAME));
        }
        if (StringUtils.isEmpty(lcVO.getAddRegion())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_ADD_REGION));
        }
        if (StringUtils.isNotEmpty(lcVO.getAddRegion())) {
            String[] addCodeArray = lcVO.getAddRegion().split(",");
            if (addCodeArray.length > 0) {
                for (String s : addCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    addRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setAddRegion(addRegion.toString());
        }
        lcVO.setPkCountry(pkCountry);
        lcVO.setPkCreator(userId);
        lcVO.setCreationTime(date);
        CuLianchuangDataStat cld = BeanUtil.copyProperties(lcVO, CuLianchuangDataStat.class);
        return AjaxResult.success(iCuLianchuangDataStatService.save(cld));
    }


    /**
     * 修改联创数据报表查询条件
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CuLianchuangDataStatVO lcVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.currentDate();
        StringBuilder addRegion = new StringBuilder();
        StringBuilder reduceRegion = new StringBuilder();
        if (StringUtils.isEmpty(lcVO.getVertexCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_CODE));
        }
        if (StringUtils.isEmpty(lcVO.getVertexName())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_NAME));
        }
        if (StringUtils.isEmpty(lcVO.getAddRegion())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LIANCHUANG_STAT_ADD_REGION));
        }
        if (StringUtils.isNotEmpty(lcVO.getAddRegion())) {
            String[] addCodeArray = lcVO.getAddRegion().split(",");
            if (addCodeArray.length > 0) {
                for (String s : addCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    addRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setAddRegion(addRegion.toString());
        } else {
            lcVO.setAddRegion(addRegion.toString());
        }
        if (StringUtils.isNotEmpty(lcVO.getReduceRegion())) {
            String[] reduceCodeArray = lcVO.getReduceRegion().split(",");
            if (reduceCodeArray.length > 0) {
                for (String s : reduceCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    reduceRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setReduceRegion(reduceRegion.toString());
        } else {
            lcVO.setReduceRegion(reduceRegion.toString());
        }
        lcVO.setPkCountry(pkCountry);
        lcVO.setPkModified(userId);
        lcVO.setModifiedTime(date);
        CuLianchuangDataStat cld = BeanUtil.copyProperties(lcVO, CuLianchuangDataStat.class);
        return AjaxResult.success(iCuLianchuangDataStatService.updateById(cld));
    }

    /**
     * 修改国内联创数据报表查询条件
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_HOME_DATA_STAT, method = EOperationMethod.UPDATE)
    @PostMapping("/home-update")
    public AjaxResult homeUpdate(@RequestBody CuLianchuangDataStatVO lcVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.currentDate();
        lcVO.setType(ESheetType.HOME.getValue());
        StringBuilder addRegion = new StringBuilder();
        if (StringUtils.isNotEmpty(lcVO.getAddRegion())) {
            String[] addCodeArray = lcVO.getAddRegion().split(",");
            if (addCodeArray.length > 0) {
                for (String s : addCodeArray) {
                    R<CuMember> member = iMemberServiceApi.getMember(s);
                    addRegion.append(member.getData().getPkId()).append(",");
                }
            }
            lcVO.setAddRegion(addRegion.toString());
        } else {
            lcVO.setAddRegion(addRegion.toString());
        }
        lcVO.setPkCountry(pkCountry);
        lcVO.setPkModified(userId);
        lcVO.setModifiedTime(date);
        CuLianchuangDataStat cld = BeanUtil.copyProperties(lcVO, CuLianchuangDataStat.class);
        return AjaxResult.success(iCuLianchuangDataStatService.updateById(cld));
    }


    /**
     * 查询单条联创数据报表查询条件
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        CuLianchuangDataStat cld = iCuLianchuangDataStatService.getById(pkId);
        CuLianchuangDataStatVO lcVO = BeanUtil.copyProperties(cld, CuLianchuangDataStatVO.class);
        StringBuilder addRegion = new StringBuilder();
        StringBuilder reduceRegion = new StringBuilder();
        if (StringUtils.isNotEmpty(lcVO.getAddRegion())) {
            String[] addCodeArray = lcVO.getAddRegion().split(",");
            if (addCodeArray.length > 0) {
                for (String s : addCodeArray) {
                    int pkMember = Integer.parseInt(s);
                    R<CuMember> member = iMemberServiceApi.getMember((long) pkMember);
                    addRegion.append(member.getData().getMemberCode()).append(",");
                }
            }
            lcVO.setAddRegion(addRegion.toString());
        }
        if (StringUtils.isNotEmpty(lcVO.getReduceRegion())) {
            String[] reduceCodeArray = lcVO.getReduceRegion().split(",");
            if (reduceCodeArray.length > 0) {
                for (String s : reduceCodeArray) {
                    int pkMember = Integer.parseInt(s);
                    R<CuMember> member = iMemberServiceApi.getMember((long) pkMember);
                    reduceRegion.append(member.getData().getMemberCode()).append(",");
                }
            }
            lcVO.setReduceRegion(reduceRegion.toString());
        }
        return AjaxResult.success(lcVO);
    }


    /**
     * 删除联创数据报表查询条件
     **/
    @Log(module = EOperationModule.STATISTIC_ANALYSIS, business = EOperationBusiness.LIANCHUANG_DATA_STAT, method = EOperationMethod.DELETE)
    @PostMapping("/delete-lcStat")
    public AjaxResult delete(@RequestBody CuLianchuangDataStatVO lcVO) {
        if (CollectionUtil.isNotEmpty(lcVO.getPkIdList())) {
            iCuLianchuangDataStatService.deleteLcStat(lcVO.getPkIdList());
        }
        return AjaxResult.success();
    }

}
