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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.ICuMemberAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.bonus.param.BonusExpandParam;
import com.hzs.bonus.bonus.service.ICuBonusExpandService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.bonus.achieve.mapper.CuMemberTreeMapper;
import com.hzs.bonus.achieve.param.MemberAchieveParam;
import com.hzs.bonus.detail.service.ICuMemberAssessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 会员网体历史记录 服务实现类
 */
@Slf4j
@Service
public class CuMemberTreeServiceImpl extends ServiceImpl<CuMemberTreeMapper, CuMemberTree> implements ICuMemberTreeService {

    @Autowired
    private ICuMemberAchieveService iCuMemberAchieveService;
    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    private ICuMemberAssessService iCuMemberAssessService;
    @Autowired
    private ICuBonusExpandService iCuBonusExpandService;
    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCuMemberTree(String date) {
        // 创建日结算（cu_member_settle）
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(date).getPkId();
        String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
        Integer number = baseMapper.queryExistOracleTable(settleTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberSettleTable(settleTableName);
            baseMapper.createCuMemberTreeDayOnlyIndex(settleTableName);
            baseMapper.createCuMemberTreeParentIndex(settleTableName);
            baseMapper.createCuMemberTreePlaceIndex(settleTableName);
        }
        number = baseMapper.queryExistOrderTableCount(settleTableName);
        if (number == null || number <= 0) {
            // 初始化日表数据，将cu_member数据插入到最新的日表记录表
            baseMapper.insertCuMemberSettleByMember(settleTableName);
            // 结算日期月份的第一天
            // TODO new 复购考核按月新増业绩走，晋升奖衔首月不处理复购免考
//            Date settleDate = DateUtils.parseStringToDate(date);
//            Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS, settleDate);
//            Date startDate = DateUtils.beforeMonthFirstDate(settleDate);
//            updateCuMemberSettleExemptByAward(settleTableName, startDate, endDate);
        }

//        // 创建极差结算记录表
//        String rangeTableName = TableNameConstants.CU_MEMBER_RANGE + period;
//        number = baseMapper.queryExistOracleTable(rangeTableName);
//        if (number == null || number <= 0) {
//            baseMapper.createCuMemberRangeTable(rangeTableName);
//            baseMapper.createCuMemberTreeDayOnlyIndex(rangeTableName);
//            baseMapper.createCuMemberTreeParentIndex(rangeTableName);
//        }
//        number = baseMapper.queryExistOrderTableCount(rangeTableName);
//        if (number == null || number <= 0) {
//            // 初始化日表数据，将cu_member数据插入到最新的日表记录表
//            // 获取当前月份第一天
//            Date firstDate = DateUtils.currentMonthFirstDate(DateUtils.parseStringToDate(date));
//            Integer firstPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, firstDate)).getPkId();
//            String year = DateUtils.getYear(date);
//            baseMapper.insertCuMemberRangeByMember(rangeTableName, year, firstPeriod, firstDate);
//        }

        // 创建会员网体月表（cu_member_tree）
        createMonthTreeTableName(date);
        // 创建月表业绩记录表（cu_member_achieve）
        iCuMemberAchieveService.createCuMemberAchieve(date);

        // 创建秒接表，秒结表只需要表结构，跟日表一模一样，秒结表期间是date+1期间
        Date tomorrowDate = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.parseStringToDate(date));
        String tomorrow = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, tomorrowDate);
        int nextPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(tomorrow).getPkId();
        // 创建会员秒表（cu_member_second）
        String secondTableName = TableNameConstants.CU_MEMBER_SECOND + nextPeriod;
        number = baseMapper.queryExistOracleTable(secondTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberSecondTable(secondTableName);
            baseMapper.createCuMemberTreeDayOnlyIndex(secondTableName);
        }

//        // 创建会员级差秒结表（cu_member_second_s）
//        String secondRangeTableName = TableNameConstants.CU_MEMBER_RANGE_S + nextPeriod;
//        number = baseMapper.queryExistOracleTable(secondRangeTableName);
//        if (number == null || number <= 0) {
//            baseMapper.createCuMemberRangeSecondTable(secondRangeTableName);
//            baseMapper.createCuMemberTreeDayOnlyIndex(secondRangeTableName);
//        }

        // 删除60天前的结算日表，结算秒表
        Date beforeDate = DateUtils.beforeDate(60, ChronoUnit.DAYS, DateUtils.parseStringToDate(date));
        try {
            // 上个月最后一天
            int beforePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, beforeDate)).getPkId();
            // 不删除每月最后一天数据
            String afterDate = DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, DateUtils.afterDate(1, ChronoUnit.DAYS, beforeDate));
            log.info("afterDate: {}", afterDate);
            if (!afterDate.endsWith("01")) {
                String beforeSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + beforePeriod;
                number = baseMapper.queryExistOracleTable(beforeSettleTableName);
                if (number != null && number > 0) {
                    baseMapper.dropCuMemberSettleTable(beforeSettleTableName);
                }
            }
            String beforeSecondTableName = TableNameConstants.CU_MEMBER_SECOND + beforePeriod;
            number = baseMapper.queryExistOracleTable(beforeSecondTableName);
            if (number != null && number > 0) {
                baseMapper.dropCuMemberSettleTable(beforeSecondTableName);
            }
        } catch (Exception e) {
            log.error("删除历史结算表失败, beforeDate: {}", beforeDate, e);
        }

        // 4-N 增加碰次需求
        BonusExpandParam bonusExpandParam = BonusExpandParam.builder().settleDate(DateUtils.parseStringToDate(date)).build();
        iCuBonusExpandService.firstSaveCuBonusExpand(bonusExpandParam);
    }

    @Override
    public void initCuMemberAssessData(String settleDate) {
        // 创建日表记录表
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(settleDate).getPkId();
        // 创建月表考核记录表
        iCuMemberAssessService.initCuMemberAssessData(settleDate, period);
    }

    private String createMonthTreeTableName(String date) {
        Integer number;
        String monthTableName = TableNameConstants.CU_MEMBER_TREE + DateUtils.getMonth(date);
        number = baseMapper.queryExistOracleTable(monthTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberTreeTable(monthTableName);
            baseMapper.createCuMemberTableUniqueIndex(monthTableName);
            baseMapper.createCuMemberTreeParentIndex(monthTableName);
            baseMapper.createCuMemberTreePlaceIndex(monthTableName);
            baseMapper.createCuMemberTableSequence(monthTableName, TableNameConstants.CU_MEMBER_TREE_SEQ);
        }
        return monthTableName;
    }

    @Override
    public void initCuMemberTreeTable(String yesterdaySettleTableName, String currentSettleTableName) {
        // 更新所有值为0
        iCuMemberAchieveService.updateCuMemberSettleInitAchieve(currentSettleTableName);
        // 初始化结算日的数据，将日期-1的数据作为当天的数据
        iCuMemberAchieveService.updateCuMemberSettleAchieveByBeforeDay(yesterdaySettleTableName, currentSettleTableName);
    }

    @Override
    public void initCuMemberExpandTreeTable(String yesterdaySettleTableName, String currentSettleTableName) {
        iCuMemberAchieveService.updateCuMemberExpandSettleAchieveByBeforeDay(yesterdaySettleTableName, currentSettleTableName);
    }

    @Override
    public void updateCuMemberSettle(Map<Long, MemberAchieveParam> cuMemberAchieveHashMap, String currentSettleTableName) {
        iCuMemberAchieveService.updateCuMemberAchieveEveryDay(cuMemberAchieveHashMap, currentSettleTableName);
    }

    @Override
    public void updateCuMemberNetAchieve(List<CuMemberAchieveExt> updMemberAchieveExtList, String currentSettleTableName) {
        iCuMemberAchieveService.updateCuMemberNetAchieve(updMemberAchieveExtList, currentSettleTableName);
    }

    @Override
    public void updateCuMemberBackSettle(Map<Long, MemberAchieveParam> cuMemberAchieveHashMap, String currentSettleTableName) {
        iCuMemberAchieveService.updateCuMemberBackOldAchieveEveryDay(cuMemberAchieveHashMap, currentSettleTableName);
    }

    @Override
    public void updateCuMemberTreeByCancelOrder(List<SaOrderExt> revokeRegSaOrderList, List<SaOrderExt> revokeUpdSaOrderList, String settleDate, String settleTableName) {
        // 更新网体，将撤单的注册订单，会员数据修改为死点
        if (revokeRegSaOrderList.size() > 0) {
            List<List<?>> revokeSaOrderList = cuMemberBonusSettle.handleCutList(revokeRegSaOrderList);
            revokeSaOrderList.forEach(saOrderList -> {
                baseMapper.updateCuMemberTreeByCancelOrder(saOrderList, settleTableName);
            });
        }
        // 更新网体，将撤销的升级订单，会员等级降级
        if (revokeRegSaOrderList.size() > 0) {
            Date tomorrow = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.parseStringToDate(settleDate));
            List<List<?>> revokeSaOrderList = cuMemberBonusSettle.handleCutList(revokeRegSaOrderList);
            revokeSaOrderList.forEach(saOrderList ->
                    baseMapper.updateCuMemberTreeGradeByCancelOrder(saOrderList, tomorrow, settleTableName));
        }
    }

    @Override
    public Integer queryExistOracleTable(String tableName) {
        return baseMapper.queryExistOracleTable(tableName);
    }

    @Override
    public void createCuMemberTableUniqueIndex(String tableName) {
        baseMapper.createCuMemberTableUniqueIndex(tableName);
    }

    @Override
    public void createCuMemberTableSequence(String tableName, String tableNameSeq) {
        baseMapper.createCuMemberTableSequence(tableName, tableNameSeq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCuMemberTreeBySettleDate(String settleDate, String settleTableName, Integer period) {
        // 创建月表记录表（cu_member_achieve）
        String achieveTableName = iCuMemberAchieveService.createCuMemberAchieve(settleDate);
        // 创建会员历史网体月表（cu_member_tree）
        String treeTableName = createMonthTreeTableName(settleDate);

        // 删除之前的数据
//        baseMapper.deleteCuMemberTreeByPeriod(treeTableName, period);
//        baseMapper.deleteCuMemberTreeByPeriod(achieveTableName, period);
        // 迁移新的数据
//        baseMapper.insertCuMemberTreeBySettleDate(treeTableName, settleTableName, period);
        // 结算日表网体添加到网体月表中
        baseMapper.mergeCuMemberTreeBySettleDate(treeTableName, settleTableName, period);

//        iCuMemberAchieveService.insertCuMemberAchieveBySettleDate(achieveTableName, settleTableName, period);
        // 结算日表的值插入到结算月表中
        iCuMemberAchieveService.mergeCuMemberAchieveBySettleDate(achieveTableName, settleTableName, period);
    }

    @Override
    public List<CuMemberSettleExt> queryCuMemberSettleEnoughAwardAchieve(String settleTableName) {
        return baseMapper.queryCuMemberSettleEnoughAwardAchieve(settleTableName);
    }

    @Override
    public void updateBackCuMemberSettleAward(String settleTableName, Integer period) {
        baseMapper.updateBackCuMemberSettleAward(settleTableName, period);
    }

    @Override
    public Boolean queryCuMemberSettlePlaceUnderUmbrella(String settleTableName, Integer awardsValue, Integer placeDept, Long pkMember) {
        return baseMapper.queryCuMemberSettlePlaceUnderUmbrella(settleTableName, awardsValue, placeDept, pkMember) > 0;
    }

    @Override
    public Boolean queryCuMemberSettleBloodUnderUmbrella(String settleTableName, Integer awardsValue, Integer placeDept, Long pkMember) {
        return baseMapper.queryCuMemberSettleBloodUnderUmbrella(settleTableName, awardsValue, placeDept, pkMember) > 0;
    }

    @Override
    public void updateCuMemberSettleByPeriod(List<Integer> periodList) {
        periodList.forEach(period -> {
            String updateTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
            baseMapper.updateCuMemberSettleByPeriod(updateTableName, period);
        });
    }

    @Override
    public List<CuMemberSettleExt> batchQueryMemberSettleBySaOrder(String settleTableName, List<SaOrderExt> saOrderExtList) {
        // 查询出推荐人
        Set<Long> serviceMemberSet = new HashSet<>();
        saOrderExtList.forEach(saOrderExt -> {
            serviceMemberSet.add(saOrderExt.getPkReference());
            // 查出自己跟推荐人
            serviceMemberSet.add(saOrderExt.getPkMember());
        });
        return getCuMemberSettleExtList(settleTableName, serviceMemberSet);
    }

    private List<CuMemberSettleExt> getCuMemberSettleExtList(String settleTableName, Set<Long> serviceMemberSet) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        if (serviceMemberSet.size() > 0) {
            List<Long> memberPkList = Arrays.asList(serviceMemberSet.toArray(new Long[]{}));
            List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList);
            listList.forEach(list ->
                    cuMemberSettleExtList.addAll(baseMapper.queryMemberSettleBySaOrder(settleTableName, list)));
        }
        return cuMemberSettleExtList;
    }

    @Override
    public List<CuMemberSettleExt> batchQueryMemberSettleByMemberPk(String settleTableName, Set<Long> serviceMemberSet) {
        return getCuMemberSettleExtList(settleTableName, serviceMemberSet);
    }

    @Override
    public List<CuMemberSettleExt> queryMemberSettleByAwards(String settleTableName, String rangeTableName, Integer startPeriod, Integer endPeriod) {
        return baseMapper.queryMemberSettleByAwards(settleTableName, rangeTableName, startPeriod, endPeriod);
    }

    @Override
    public List<CuMemberSettleExt> batchQueryMemberSettleParentByMemberPk(String settleTableName, Set<Long> serviceMemberSet) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        if (serviceMemberSet.size() > 0) {
            List<Long> memberPkList = Arrays.asList(serviceMemberSet.toArray(new Long[]{}));
            List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList);
            listList.forEach(list ->
                    cuMemberSettleExtList.addAll(baseMapper.batchQueryMemberSettleParentByMemberPk(settleTableName, list)));
        }
        return cuMemberSettleExtList;
    }

    @Override
    public Map<Long, List<CuMemberSettleExt>> batchQueryCuMemberSettlePlaceParent(String settleTableName, Integer period, List<SaOrderExt> firPurchaseOrderList) {
        // 查询出所有父级安置人
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(firPurchaseOrderList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list ->
                cuMemberSettleExtList.addAll(baseMapper.batchQueryCuMemberSettlePlaceParent(settleTableName, period, list)));
        // key：订单ID，value：订单向上找安置人列表
        Map<Long, List<CuMemberSettleExt>> memberSettleMap = new HashMap<>(ComputeUtil.mapInitCapacity(firPurchaseOrderList.size()));
        cuMemberSettleExtList.forEach(cuMemberSettleExt -> {
            if (memberSettleMap.containsKey(cuMemberSettleExt.getPkOrder())) {
                memberSettleMap.get(cuMemberSettleExt.getPkOrder()).add(cuMemberSettleExt);
            } else {
                List<CuMemberSettleExt> cuMemberSettles = new ArrayList<>();
                cuMemberSettles.add(cuMemberSettleExt);
                memberSettleMap.put(cuMemberSettleExt.getPkOrder(), cuMemberSettles);
            }
        });
        return memberSettleMap;
    }

    @Override
    public List<CuMemberSettleExt> batchQuerySpecialCuMemberSettleParent(String settleTableName, Set<Long> expandMemberSet) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        if (expandMemberSet == null || expandMemberSet.size() == 0) {
            return cuMemberSettleExtList;
        }
        List<Long> memberPkList = Arrays.asList(expandMemberSet.toArray(new Long[]{}));
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list ->
                cuMemberSettleExtList.addAll(baseMapper.batchQuerySpecialCuMemberSettleParent(settleTableName, list)));
        return cuMemberSettleExtList;
    }

    @Override
    public List<CuMemberSettleExt> batchQueryCuMemberSettleParent(String settleTableName, Set<Long> expandMemberSet) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        if (expandMemberSet == null || expandMemberSet.size() == 0) {
            return cuMemberSettleExtList;
        }
        List<Long> memberPkList = Arrays.asList(expandMemberSet.toArray(new Long[]{}));
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list ->
                cuMemberSettleExtList.addAll(baseMapper.batchQueryCuMemberSettleParent(settleTableName, list)));
        return cuMemberSettleExtList;
    }

    @Override
    public List<CuMemberSettleExt> listMemberMonthAssess(String settleTableName, String settleTableNameMonthLast, List<Long> pkMemberList) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        if (CollectionUtil.isEmpty(pkMemberList)) {
            return cuMemberSettleExtList;
        }
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(pkMemberList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list ->
                cuMemberSettleExtList.addAll(baseMapper.listMemberMonthAssess(settleTableName, settleTableNameMonthLast, list)));
        return cuMemberSettleExtList;
    }

    @Override
    public List<CuMemberSettleExt> batchQueryCuMemberSettleParentForSpecial(String settleTableName, Set<Long> expandMemberSet) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        List<Long> memberPkList = Arrays.asList(expandMemberSet.toArray(new Long[]{}));
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list ->
                cuMemberSettleExtList.addAll(baseMapper.batchQueryCuMemberSettleParentForSpecial(settleTableName, list)));
        return cuMemberSettleExtList;
    }

    @Override
    public void updateCuMemberSettle(String settleTableName, Map<Long, CuMemberSettleExt> cuMemberTreeMap) {
        List<List<?>> treeList = setUpdCuMemberTreeLists(cuMemberTreeMap);
        treeList.forEach(list -> baseMapper.updateCuMemberSettle(settleTableName, list));
    }

    @Override
    public void updateCuMemberExpandSettle(String settleTableName, Map<Long, CuMemberSettleExt> cuMemberTreeMap) {
        List<List<?>> treeList = setUpdCuMemberTreeLists(cuMemberTreeMap);
        treeList.forEach(list -> baseMapper.updateCuMemberExpandSettle(settleTableName, list));
    }

    @Override
    public void updateCuMemberSecond(String settleTableName, Map<Long, CuMemberSettleExt> cuMemberTreeMap) {
        List<List<?>> treeList = setUpdCuMemberTreeLists(cuMemberTreeMap);
        treeList.forEach(list -> baseMapper.updateCuMemberSecond(settleTableName, list));
    }

    @Override
    public void updateCuMemberSecondPlaceParent(String secondTableName, String settleTableName, String yesSettleTableName, Long pkMember, List<?> cuMemberTreeList, Integer isPlace) {
        if (DateUtils.getCurrentHour() < 1) {
            settleTableName = yesSettleTableName;
        }
        baseMapper.updateCuMemberSecondPlaceParent(secondTableName, settleTableName, pkMember, cuMemberTreeList, isPlace);
    }

    @Override
    public List<CuMemberSettleExt> queryCuMemberSecondPlaceParent(String secondTableName, Long pkMember) {
        return baseMapper.queryCuMemberSecondPlaceParent(secondTableName, pkMember);
    }

    @Override
    public List<CuMemberSettleExt> queryCuMemberSecondParent(String secondTableName, List<?> cuMemberTreeList) {
        List<CuMemberSettleExt> cuMemberSettleExtList = new ArrayList<>();
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(cuMemberTreeList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        listList.forEach(list -> cuMemberSettleExtList.addAll(baseMapper.queryCuMemberSecondParent(secondTableName, list)));
        return cuMemberSettleExtList;
    }

    private List<List<?>> setUpdCuMemberTreeLists(Map<Long, CuMemberSettleExt> cuMemberTreeMap) {
        List<CuMemberSettleExt> cuMemberTreeList = new ArrayList<>();
        cuMemberTreeMap.forEach((key, value) -> cuMemberTreeList.add(value));
        return cuMemberBonusSettle.handleCutList(cuMemberTreeList);
    }

    @Override
    public void truncateCuMemberTreeTable(String tableName) {
        baseMapper.truncateCuMemberTreeTable(tableName);
    }

    @Override
    public void createCuMemberTreeDayOnlyIndex(String tableName) {
        baseMapper.createCuMemberTreeDayOnlyIndex(tableName);
    }

    @Override
    public void createCuMemberTreeParentIndex(String tableName) {
        baseMapper.createCuMemberTreeParentIndex(tableName);
    }

    @Override
    public void createCuMemberTreePlaceIndex(String tableName) {
        baseMapper.createCuMemberTreePlaceIndex(tableName);
    }

    @Override
    public void updateBackCuMemberSettleGrade(String settleTableName, Integer period) {
        baseMapper.updateBackCuMemberSettleGrade(settleTableName, period);
    }

    @Override
    public void updateCuMemberSettleGradeByPeriod(List<Integer> periodList) {
        periodList.forEach(period -> {
            String updateTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
            baseMapper.updateCuMemberSettleGradeByPeriod(updateTableName, period);
        });
    }

    @Override
    public List<CuMemberSettleExt> listUpgradeAwards(String settleTableName, Integer awardsValue) {
        return baseMapper.listUpgradeAwards(settleTableName, awardsValue);
    }

}
