package com.hzs.retail.member.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberRetailRange;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberBaseService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.retail.member.mapper.CuMemberRetailRangeMapper;
import com.hzs.retail.member.param.MarketDynamicsQueryDateParam;
import com.hzs.retail.member.service.ICuMemberRetailAchieveService;
import com.hzs.retail.member.utils.DataStatisticsUtil;
import com.hzs.retail.member.utils.PageResult;
import com.hzs.retail.member.vo.CuMemberRetailAchieveVO;
import com.hzs.retail.member.vo.CuMemberRetailDataStatisticsDetailVO;
import com.hzs.retail.member.vo.CuMemberRetailDataStatisticsResultVO;
import com.hzs.retail.member.vo.MarketDynamicsDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 会员业绩记录 服务实现类
 */
@Slf4j
@Service
public class CuMemberRetailAchieveServiceImpl extends ServiceImpl<CuMemberRetailRangeMapper, CuMemberRetailRange> implements ICuMemberRetailAchieveService {

    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberBaseService iCuMemberBaseService;
    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;

    @Override
    public CuMemberRetailAchieveVO selectMemberAchieve(Long pkMember, Integer pkCountry, Integer systemType) {
        //查询会员昨日业绩
        String memberRetailTable = getTableName();
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        //根据国家查汇率
        BigDecimal rate = iCuMemberBaseService.getCurrency(pkCountry).getInExchangeRate();

        //查询自己业绩
        List<CuMemberRetailAchieveVO> memberSelfAchieveList = new ArrayList<>();

        //是否是当月第一天
        int isFirst = isFirstDayOfMonth() ? EYesNo.YES.getIntValue() : EYesNo.NO.getIntValue();
        //会员业绩
        CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder().memberRetailTable(memberRetailTable).pkMember(pkMember).systemType(systemType).isFirst(isFirst).build();
        CuMemberRetailAchieveVO orderAchieveVO = baseMapper.selectMemberOrderAchieveByPkMember(memberAchieveVO);
        if (null != orderAchieveVO && orderAchieveVO.getArealPv().compareTo(BigDecimal.ZERO) > 0) {
            orderAchieveVO.setPkMember(pkMember);
            memberSelfAchieveList.add(orderAchieveVO);
        }
        //查询伞下业绩
        if (CollUtil.isNotEmpty(pkMemberList)) {
            List<CuMemberRetailAchieveVO> memberPushAchieveList = getMemberPushAchieve(pkMemberList, memberRetailTable, systemType, isFirst);
            if (CollUtil.isNotEmpty(memberPushAchieveList)) {
                memberSelfAchieveList.addAll(memberPushAchieveList);
            }
        }
        if (CollUtil.isEmpty(memberSelfAchieveList)) {
            return CuMemberRetailAchieveVO.builder().smallAreaPv(BigDecimal.ZERO).bigArealPv(BigDecimal.ZERO).build();
        }
        return getMemberBigAndSmallAchieve(memberSelfAchieveList, rate);
    }

    @Override
    public CuMemberRetailAchieveVO selectExcludeWaterSmallAchieve(Long pkMember, Integer pkCountry, Integer systemType) {
        //查询会员昨日业绩表
        String memberRetailTable = getTableName();
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        if (CollUtil.isNotEmpty(pkMemberList)) {
            //根据国家查汇率
            BigDecimal rate = iCuMemberBaseService.getCurrency(pkCountry).getInExchangeRate();
            CuMember cuMember = iCuMemberService.queryMember(pkMember);
            //查询会员排除注水业绩
            CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder().memberRetailTable(memberRetailTable).pkMemberList(pkMemberList).memberCode(cuMember.getMemberCode()).systemType(systemType).build();
            CuMemberRetailAchieveVO excludeWaterAchieve = baseMapper.selectExcludeWaterSmallAchieve(memberAchieveVO);
            if (null == excludeWaterAchieve) {
                excludeWaterAchieve = CuMemberRetailAchieveVO.builder().smallAreaPv(BigDecimal.ZERO).build();
            } else {
                return CuMemberRetailAchieveVO.builder().smallAreaPv(getBigDecimal(excludeWaterAchieve.getSmallAreaPv(), rate)).build();
            }
            return excludeWaterAchieve;
        } else {
            return CuMemberRetailAchieveVO.builder().smallAreaPv(BigDecimal.ZERO).build();
        }
    }

    @Override
    public CuMemberRetailAchieveVO selectMemberBigAchieve(Long pkMember, Integer pkCountry, Integer systemType) {
        //查询会员昨日业绩表
        String memberRetailTable = getTableName();
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        if (CollUtil.isNotEmpty(pkMemberList)) {
            //根据国家查汇率
            BigDecimal rate = iCuMemberBaseService.getCurrency(pkCountry).getInExchangeRate();
            //是否是当月第一天
            int isFirst = isFirstDayOfMonth() ? EYesNo.YES.getIntValue() : EYesNo.NO.getIntValue();
            CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder().memberRetailTable(memberRetailTable).pkMemberList(pkMemberList).systemType(systemType).isFirst(isFirst).build();
            CuMemberRetailAchieveVO bigAchieve = baseMapper.selectMemberBigAchieve(memberAchieveVO);
            if (null != bigAchieve) {
                //查询上月业绩
                String retailUpMonthTable = getUpMonthTableName();
                CuMemberRetailAchieveVO upMonthAchieveParam = CuMemberRetailAchieveVO.builder().retailUpMonthTable(retailUpMonthTable).pkMember(bigAchieve.getPkBigMember()).build();
                CuMemberRetailAchieveVO upMonthAchieve = baseMapper.selectUpMonthAchieveByPkMember(upMonthAchieveParam);
                if (null != upMonthAchieve) {
                    bigAchieve.setUpMonthPv(getBigDecimal(upMonthAchieve.getUpMonthPv(), rate));
                } else {
                    bigAchieve.setUpMonthPv(BigDecimal.ZERO);
                }
                bigAchieve.setYesterdayPv(getBigDecimal(bigAchieve.getYesterdayPv(), rate));
                bigAchieve.setMonthPv(getBigDecimal(bigAchieve.getMonthPv(), rate));
                bigAchieve.setSumPv(getBigDecimal(bigAchieve.getSumPv(), rate));
                bigAchieve.setRealTimePv(getBigDecimal(bigAchieve.getRealTimePv(), rate));
            }
            return bigAchieve;
        }
        return null;
    }

    @Override
    public List<Long> selectDirectPushByPkMember(Long pkMember, Integer systemType) {
        return baseMapper.selectDirectPushByPkMember(pkMember, systemType);
    }

    @Override
    public List<CuMemberRetailAchieveVO> selectMemberAchieveDetail(CuMemberRetailAchieveVO retailAchieveVO) {
        List<CuMemberRetailAchieveVO> retailAchieveVOList = new ArrayList<>();
        //查询会员昨日业绩
        String memberRetailTable = getTableName();
        //查询直推会员
        List<Long> pkMemberList = retailAchieveVO.getPkMemberList();
        if (CollUtil.isNotEmpty(pkMemberList)) {
            //根据国家查汇率
            BigDecimal rate = iCuMemberBaseService.getCurrency(retailAchieveVO.getPkCountry()).getInExchangeRate();
            Long pkBigMember = retailAchieveVO.getPkBigMember();
            if (null != pkBigMember) {
                //查询业绩列表
                pkMemberList.remove(pkBigMember);
                retailAchieveVO.setPkMemberList(pkMemberList);
                //小区人数不为空时
                if (CollUtil.isNotEmpty(pkMemberList)) {
                    retailAchieveVO.setRetailUpMonthTable(getUpMonthTableName());
                    retailAchieveVO.setMemberRetailTable(memberRetailTable);
                    //是否是当月第一天
                    int isFirst = isFirstDayOfMonth() ? EYesNo.YES.getIntValue() : EYesNo.NO.getIntValue();
                    retailAchieveVO.setIsFirst(isFirst);
                    retailAchieveVOList = baseMapper.selectMemberAchieveDetail(retailAchieveVO);
                    //转化以万为单位
                    for (CuMemberRetailAchieveVO cuMemberRetailAchieveVO : retailAchieveVOList) {
                        cuMemberRetailAchieveVO.setYesterdayPv(getBigDecimal(cuMemberRetailAchieveVO.getYesterdayPv(), rate));
                        cuMemberRetailAchieveVO.setMonthPv(getBigDecimal(cuMemberRetailAchieveVO.getMonthPv(), rate));
                        cuMemberRetailAchieveVO.setSumPv(getBigDecimal(cuMemberRetailAchieveVO.getSumPv(), rate));
                        cuMemberRetailAchieveVO.setRealTimePv(getBigDecimal(cuMemberRetailAchieveVO.getRealTimePv(), rate));
                        cuMemberRetailAchieveVO.setUpMonthPv(getBigDecimal(cuMemberRetailAchieveVO.getUpMonthPv(), rate));
                    }
                }
            }

        }
        return retailAchieveVOList;
    }


    /**
     * 获取直推大区业绩、小区业绩
     *
     * @param memberAchieveList
     * @return CuMemberAchieveVO
     */
    private CuMemberRetailAchieveVO getMemberBigAndSmallAchieve(List<CuMemberRetailAchieveVO> memberAchieveList, BigDecimal rate) {
        //查询最大业绩
        CuMemberRetailAchieveVO maxAchieveVO = memberAchieveList.stream().max(Comparator.comparing(CuMemberRetailAchieveVO::getArealPv)).get();
        //查询小区业绩
        BigDecimal smallAreaPv = memberAchieveList.stream().filter(achieveVO -> !achieveVO.getPkMember().equals(maxAchieveVO.getPkMember())).map(vo -> null == vo.getArealPv() ? BigDecimal.ZERO : vo.getArealPv()).reduce(BigDecimal.ZERO, BigDecimal::add);
//        return CuMemberRetailAchieveVO.builder().smallAreaPv(getBigDecimal(smallAreaPv, rate)).pkBigMember(maxAchieveVO.getPkMember()).bigArealPv(getBigDecimal(maxAchieveVO.getArealPv(), rate)).build();
        return CuMemberRetailAchieveVO.builder()
                .smallAreaPv(smallAreaPv)
                .pkBigMember(maxAchieveVO.getPkMember())
                .bigArealPv(maxAchieveVO.getArealPv())
                .arealPv(maxAchieveVO.getArealPv())
                .build();
    }

    /**
     * 查询会员直推业绩
     *
     * @param pkMemberList 直推会员ID
     * @return CuMemberAchieveVO
     */
    private List<CuMemberRetailAchieveVO> getMemberPushAchieve(List<Long> pkMemberList, String memberRangeTable, Integer systemType, Integer isFist) {
        CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder().memberRetailTable(memberRangeTable).pkMemberList(pkMemberList).systemType(systemType).isFirst(isFist).build();
        return baseMapper.selectMemberAchieve(memberAchieveVO);
    }

    /**
     * 查询会员昨日极差表名
     *
     * @return String
     */
    private String getTableName() {
        String localDate = LocalDate.now().minus(1, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(localDate);
        if (ObjectUtil.isEmpty(cuMemberSettlePeriod)) {
            return null;
        }
        int period = cuMemberSettlePeriod.getPkId();
        return TableNameConstants.CU_MEMBER_RETAIL_RANGE + period;
    }

    private String getDayTableName() {
        String localDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(localDate);
        if (ObjectUtil.isEmpty(cuMemberSettlePeriod)) {
            return null;
        }
        int period = cuMemberSettlePeriod.getPkId();
        return TableNameConstants.CU_MEMBER_RETAIL_S + period;
    }


    /**
     * 查询上月最后一天业绩表名
     *
     * @return String
     */
    private String getUpMonthTableName() {
        //获取上月最后一天
        String upLastMonth = DateUtils.beforeMonthLastDateStr(new Date());
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(upLastMonth).getPkId();
        return TableNameConstants.CU_MEMBER_RETAIL_RANGE + period;
    }


    /**
     * 查询会员当日极差秒表名
     *
     * @return String
     */
    private String getTableSName() {
        String localDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(localDate).getPkId();
        return TableNameConstants.CU_MEMBER_RETAIL_S + period;
    }

    /**
     * 转化万单位、汇率换算
     *
     * @param value 值
     * @return BigDecimal
     */
    private BigDecimal getBigDecimal(BigDecimal value, BigDecimal rate) {
        BigDecimal oldValue = null != value ? value : BigDecimal.ZERO;
        return oldValue.divide(new BigDecimal("10000")).multiply(rate).setScale(4, BigDecimal.ROUND_FLOOR);
    }


    @Override
    public CuMemberRetailAchieveVO selectMemberBigSmallAchieveDetail(Long pkMember, Integer pkCountry, Integer systemType) {
        //查询会员昨日业绩
        String memberRetailTable = getTableName();
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        //根据国家查汇率
        BigDecimal rate = iCuMemberBaseService.getCurrency(pkCountry).getInExchangeRate();
        //查询业绩（自己的业绩算一个区）
        CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder().memberRetailTable(memberRetailTable).pkMemberList(pkMemberList).pkMember(pkMember).systemType(systemType).build();
        CuMemberRetailAchieveVO cuMemberRetailAchieveVO = baseMapper.selectMemberBigSmallAchieveDetail(memberAchieveVO);
        if (null != cuMemberRetailAchieveVO) {
            cuMemberRetailAchieveVO.setTotalYesterdayPv(getBigDecimal(cuMemberRetailAchieveVO.getTotalYesterdayPv(), rate));
            cuMemberRetailAchieveVO.setSmallYesterdayPv(getBigDecimal(cuMemberRetailAchieveVO.getSmallYesterdayPv(), rate));
            cuMemberRetailAchieveVO.setTotalMonthPv(getBigDecimal(cuMemberRetailAchieveVO.getTotalMonthPv(), rate));
            cuMemberRetailAchieveVO.setSmallMonthPv(getBigDecimal(cuMemberRetailAchieveVO.getSmallMonthPv(), rate));
            cuMemberRetailAchieveVO.setTotalSumPv(getBigDecimal(cuMemberRetailAchieveVO.getTotalSumPv(), rate));
            cuMemberRetailAchieveVO.setSmallSumPv(getBigDecimal(cuMemberRetailAchieveVO.getSmallSumPv(), rate));
        } else {
            cuMemberRetailAchieveVO = CuMemberRetailAchieveVO.builder().totalYesterdayPv(BigDecimal.ZERO).smallYesterdayPv(BigDecimal.ZERO)
                    .totalMonthPv(BigDecimal.ZERO).smallMonthPv(BigDecimal.ZERO).totalSumPv(BigDecimal.ZERO).smallSumPv(BigDecimal.ZERO).build();
        }
        return cuMemberRetailAchieveVO;
    }


    /**
     * 查询不前时间是否是本月第一天
     *
     * @return Boolean
     */
    private Boolean isFirstDayOfMonth() {
        LocalDate localDate = LocalDate.now();
        LocalDate firstMonthDay = localDate.with(TemporalAdjusters.firstDayOfMonth());
        // LocalDate firstMonthDay = LocalDate.of(2025, 02, 21);
        return localDate.isAfter(firstMonthDay);
    }


    @Override
    public CuMemberRetailAchieveVO selectMemberBigSmallBox(CuMemberRetailAchieveVO param) {
        Long pkBigMember = param.getPkBigMember();
        Long pkMember = param.getPkMember();
        Integer systemType = param.getSystemType();
        //判断会员是否激活
        if (!isActivate(pkMember)) {
            return CuMemberRetailAchieveVO.builder().bigAreaBox(0).smallAreaBox(0).build();
        }
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        if (CollUtil.isNotEmpty(pkMemberList) && null != pkBigMember) {
            //查询参数
            getParam(param);
            //查询自己的业绩
            Integer selfBox = baseMapper.selectSelfNewBox(param);
            pkMemberList.remove(pkBigMember);
            param.setPkMemberList(pkMemberList);
            CuMemberRetailAchieveVO cuMemberRetailAchieveVO = baseMapper.selectMemberBigSmallBox(param);
            if (null != cuMemberRetailAchieveVO) {
                cuMemberRetailAchieveVO.setPkBigMember(pkBigMember);
                Integer selfNewBox = null == selfBox ? 0 : selfBox;
                //判断大小市场增加自己新增的盒数
                if (!pkMember.equals(pkBigMember)) {
                    cuMemberRetailAchieveVO.setSmallAreaBox(cuMemberRetailAchieveVO.getSmallAreaBox() + selfNewBox);
                } else {
                    cuMemberRetailAchieveVO.setBigAreaBox(cuMemberRetailAchieveVO.getBigAreaBox() + selfNewBox);
                }
            }
            return cuMemberRetailAchieveVO;
        }
        return CuMemberRetailAchieveVO.builder().bigAreaBox(0).smallAreaBox(0).build();
    }


    /**
     * 获取会员金额、业绩、盒数表
     *
     * @return String
     */
    private String getAchieveAndBoxTableName(String date) {
        CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(date);
        if (ObjectUtil.isNotEmpty(cuMemberSettlePeriod)) {
            int period = cuMemberSettlePeriod.getPkId();
            return TableNameConstants.CU_MEMBER_AMOUNT_BOX + period;
        }
        return "";
    }

    @Override
    public CuMemberRetailAchieveVO selectMemberBigBoxList(CuMemberRetailAchieveVO param) {
        Long pkBigMember = param.getPkBigMember();
        if (null != pkBigMember && isActivate(param.getPkMember())) {
            //查询参数
            getParam(param);
            //上上月表名
            param.setSecondMonthLastDayTable(getSecondMontLastDayTableSName());
            param.setPkMemberList(Collections.singletonList(pkBigMember));
            CuMemberRetailAchieveVO cuMemberRetailAchieveVO = baseMapper.selectMemberBigBoxList(param);
            if (null != cuMemberRetailAchieveVO) {
                cuMemberRetailAchieveVO.setPkBigMember(pkBigMember);
            }
            return cuMemberRetailAchieveVO;
        }
        return null;
    }


    private CuMemberRetailAchieveVO getParam(CuMemberRetailAchieveVO param) {
        //上月最后一天的表名
        String upLastMonth = DateUtils.beforeMonthLastDateStr(new Date());
        param.setRetailUpMonthTable(getAchieveAndBoxTableName(upLastMonth));
        //昨天的表名
        String yesterdayTableName = LocalDate.now().minus(1, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        param.setMemberRetailTable(getAchieveAndBoxTableName(yesterdayTableName));
        //前天的表名
        String beforeYesterdayTableName = LocalDate.now().minus(2, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        param.setBeforeYesterdayTableName(getAchieveAndBoxTableName(beforeYesterdayTableName));
        //是否是当月第一天
        int isFirst = isFirstDayOfMonth() ? EYesNo.YES.getIntValue() : EYesNo.NO.getIntValue();
        param.setIsFirst(isFirst);
        //当天表名
        param.setMemberRetailSTable(getTableSName());
        return param;
    }

    @Override
    public List<CuMemberRetailAchieveVO> selectMemberSmallBoxList(CuMemberRetailAchieveVO param) {
        Long pkBigMember = param.getPkBigMember();
        List<Long> pkMemberList = param.getPkMemberList();
        if (CollUtil.isNotEmpty(pkMemberList) && null != pkBigMember && isActivate(param.getPkMember())) {
            //查询参数
            getParam(param);
            //上上月表名
            param.setSecondMonthLastDayTable(getSecondMontLastDayTableSName());
            pkMemberList.remove(pkBigMember);
            param.setPkMemberList(pkMemberList);
            //删除大区后，如果为空，则不查询
            if (CollUtil.isNotEmpty(pkMemberList)) {
                return baseMapper.selectMemberSmallBoxList(param);
            }
        }
        return new ArrayList<>();
    }

    @Override
    public CuMemberRetailDataStatisticsResultVO getRetailDataStatisticsDetail(Long pkMember, Integer pkCountry, Integer systemType) {
        Long userId = SecurityUtils.getUserId();
        String memberRetailTable = getTableName();
        String memberSecondTable = getDayTableName();
        //查询直推会员
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        pkMemberList.add(userId);
        CuMemberRetailAchieveVO memberAchieveVO = CuMemberRetailAchieveVO.builder()
                .memberRetailTable(memberRetailTable)
                .memberRetailSTable(memberSecondTable)
                .pkMemberList(pkMemberList).systemType(systemType).build();
        List<CuMemberRetailDataStatisticsDetailVO> statisticsDetailVOList = baseMapper.getRetailDataStatisticsDetail(memberAchieveVO);
        CuMember cuMember = iCuMemberService.queryMember(userId);
        CuMemberRetailDataStatisticsResultVO result = DataStatisticsUtil.getCalcMap(statisticsDetailVOList, cuMember);
        log.info("statisticsDetailVOList: {}", JSONUtil.toJsonStr(statisticsDetailVOList));
        log.info("calcMap -> {}", JSONUtil.toJsonStr(result));
        return result;
    }

    @Override
    public PageResult<MarketDynamicsDetailVO> getMarketDynamics(MarketDynamicsQueryDateParam param) {
        Long pkMember = SecurityUtils.getUserId();
        Integer systemType = SecurityUtils.getSystemType();

        DataStatisticsUtil.setQueryDateParam(param);
        param.setCurrentDayTableName(getDayTableName());
        param.setCurrentMonthTableName(getDayTableName(param.getYesterdayDate()));
        param.setLastMonthTableName(getDayTableName(param.getLastMonthDate()));
        log.info("MarketDynamicsQueryDateParam : {}", JSONUtil.toJsonStr(param));
        List<Long> pkMemberList = baseMapper.selectDirectPushByPkMember(pkMember, systemType);
        param.setPkIdList(pkMemberList);
        List<MarketDynamicsDetailVO> detailVOList = baseMapper.getMarketDynamicsDetail(param);
        log.info("detailVOList : {}", JSONUtil.toJsonStr(detailVOList));
        return DataStatisticsUtil.getMarketDynamicsAssemblyData(detailVOList, param);
    }

    private String getDayTableName(String localDate) {
        CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(localDate);
        if (ObjectUtil.isEmpty(cuMemberSettlePeriod)) {
            return TableNameConstants.CU_MEMBER_RETAIL_RANGE;
        }
        int period = cuMemberSettlePeriod.getPkId();
        return TableNameConstants.CU_MEMBER_RETAIL_RANGE + period;
    }

    /**
     * 查询会员是否激活
     *
     * @param pkMember 会员ID
     * @return Boolean
     */
    private Boolean isActivate(Long pkMember) {
        CuMember cuMember = iCuMemberService.queryMember(pkMember);
        Integer enableStatus = cuMember.getIsActivate();
        if (null != enableStatus && enableStatus.equals(EYesNo.YES.getIntValue())) {
            return true;
        }
        return false;
    }

    /**
     * 获取上上个月的表名
     *
     * @return String
     */
    private String getSecondMontLastDayTableSName() {
        //上上月的最后一天
        LocalDate secondMonthLastDay = DateUtils.beforeMonthLastDateStr(2L);
        //开网时间
        LocalDate openNetworkTime = LocalDate.of(2025, 01, 10);
        if (secondMonthLastDay.isBefore(openNetworkTime)) {
            secondMonthLastDay = openNetworkTime;
        }
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(secondMonthLastDay.toString()).getPkId();
        return TableNameConstants.CU_MEMBER_AMOUNT_BOX + period;
    }

}
