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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.detail.service.ICuMemberAssessDetailService;
import com.hzs.bonus.detail.service.ICuMemberAssessService;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.detail.CuMemberAssess;
import com.hzs.common.domain.member.detail.CuMemberAssessDetail;
import com.hzs.common.domain.member.ext.CuMemberAssessExt;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.bonus.detail.mapper.CuMemberAssessMapper;
import com.hzs.bonus.detail.vo.CuMemberAssessVO;
import com.hzs.sale.order.ISaOrderServiceApi;
import com.hzs.system.config.IAwardsServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 会员信息-会员考核记录 服务实现类
 *
 * @author hzs
 * @since 2022-08-31
 */
@Service
public class CuMemberAssessServiceImpl extends ServiceImpl<CuMemberAssessMapper, CuMemberAssess> implements ICuMemberAssessService {

    private ICuMemberTreeService cuMemberTreeService;

    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;

    private ICuMemberAssessDetailService cuMemberAssessDetailService;

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @DubboReference
    ISaOrderServiceApi saOrderServiceApi;
    @DubboReference
    IAwardsServiceApi awardsServiceApi;

    @Autowired
    public void setCuMemberAssessDetailService(ICuMemberAssessDetailService cuMemberAssessDetailService) {
        this.cuMemberAssessDetailService = cuMemberAssessDetailService;
    }

    @Autowired
    public void setCuMemberSettlePeriodService(ICuMemberSettlePeriodService cuMemberSettlePeriodService) {
        this.cuMemberSettlePeriodService = cuMemberSettlePeriodService;
    }

    @Autowired
    public void setCuMemberTreeService(ICuMemberTreeService cuMemberTreeService) {
        this.cuMemberTreeService = cuMemberTreeService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void initCuMemberAssessData(String settleDate, int period) {
        // 复购考核，初始化值，值的来源是cu_member_tree上月最后一天的那一期的网体数据
        // 初始化值跟最终运算需要分开，防止凌晨12点正好有人支付。
        // 根据网体获得需要考核的人员，所有人员
        // 免考状态为 免考的顺道修改为考核通过
        // 每年一张表，查询的时候只能查询进2年的值
        // 结算的是月底那天需要进行考核
        Date currentDate = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.parseStringToDate(settleDate));
        // 每年1月1日开始创建表
        // 判断当前时间是每年第一天，需要初始化表
        if (DateUtils.currentDay(currentDate) == 1) {
            // 考核表
            String tableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.currentYear(currentDate);
            Integer num = cuMemberTreeService.queryExistOracleTable(tableName);
            if (num == null || num <= 0) {
                // 判断是否存在年表
                baseMapper.createCuMemberAssessTable(tableName);
                baseMapper.createCuMemberTableUniqueIndex(tableName);
                cuMemberTreeService.createCuMemberTableSequence(tableName, TableNameConstants.CU_MEMBER_ASSESS_SEQ);
            }
            // TODO cabbage 目前没有月考核了，此处不需要创建月考核数据
//            calculationCuMemberAssess(currentDate);
//            String sourceTable = TableNameConstants.CU_MEMBER_SETTLE + period;
            // 判断当前时间是月初第一天，需要初始化数据,判断表中是否有数据,没有数据可以进行初始化
//            String assessDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, currentDate);
//            period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(assessDate).getPkId();
//            num = baseMapper.queryCuMemberAssessCount(tableName, period);
//            if (num == null || num <= 0) {
//                // 初始化复购考核
//                String month = DateUtils.getMonth(assessDate);
//                // 结算日期后一天
//                baseMapper.insertRepCuMemberAssess(tableName, sourceTable, period, month);
//                // 初始化分红考核
//                baseMapper.insertShareCuMemberAssess(tableName, sourceTable, period, month);
//            }
        }

    }

    /**
     * 复购考核每月第一次考核调用此方法
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calculationCuMemberAssess(Date currentDate) {
        // 查询当前时间的第一天跟最后一天
        // 需要验证考核国家是否有复购考核 分红考核
        // 考核日期不能跨越考核，只能当月考核，当月考核上月
        if (DateUtils.currentDay(currentDate) == 1) {
            String currentMonthFirstDate = DateUtils.currentMonthFirstDateStr(currentDate);
            Integer currentPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthFirstDate).getPkId();
            String currentTableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(currentMonthFirstDate);
            // 清空数据  根据期数清空cu_member_assess  cu_member_assess_detail表数据
            initCurrentAssess(currentDate, currentTableName, currentPeriod);
            // 初始化复购考核的上月结余
            initCurrentAssessByBeforeMonth(currentDate, currentTableName, currentPeriod, EAssessmentType.REPURCHASE_ASSESS.getValue());
            // 初始化分红考核的上月结余
//            initCurrentAssessByBeforeMonth(currentDate, currentTableName, currentPeriod, EAssessmentType.SHARE_ASSESS.getValue());
            // 更新当月结余 结余会同时更新复购跟分红，只需要更新一次
            // 1 有免考状态的 本月使用是 0，考核状态是免考通过，免考将所有的值更改一下期末 期末= 期初+本月
            // 注意 不修改考核状态为通过的值，可能存在之前考核通过，又手动免考的人
            baseMapper.updateAssessPeriodExemptBalanceByUsed(currentTableName, currentPeriod);
            // 2 需要扣除通过的，需要记录流水
            // 记录流水，新增或修改
            cuMemberAssessDetailService.updateCuMemberAssessDetailByPeriod(currentTableName, currentPeriod);
            baseMapper.updateAssessPeriodCheckBalanceByUsed(currentTableName, currentPeriod);
        }
    }

    @Override
    public Map<Long, CuMemberAssess> queryCuMemberAssessByMember(String assessTableName, Integer period, Integer assessType, List<CuMemberSettleExt> cuMemberSettleExtList) {
        if (CollectionUtil.isEmpty(cuMemberSettleExtList)) {
            return new HashMap<>();
        }
        List<CuMemberAssess> cuMemberAssessList = new ArrayList<>();
        List<List<?>> listList = cuMemberBonusSettle.handleCutList(cuMemberSettleExtList);
        listList.forEach(list ->
                cuMemberAssessList.addAll(baseMapper.queryCuMemberAssessByMember(assessTableName, period, assessType, list)));
        Map<Long, CuMemberAssess> cuMemberAssessMap = new HashMap<>(ComputeUtil.mapInitCapacity(cuMemberAssessList.size()));
        cuMemberAssessList.forEach(cuMemberAssess -> cuMemberAssessMap.put(cuMemberAssess.getPkMember(), cuMemberAssess));
        return cuMemberAssessMap;
    }

    @Override
    public List<CuMemberAssess> queryCuMemberShareAssess(String tableName, Integer period) {
        return baseMapper.queryCuMemberShareAssess(tableName, period);
    }

    /**
     * @description: 会员-考核明细列表
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> selectAssessList(CuMemberAssessVO cuMemberAssessVO, List<Integer> orderTypeList) {
        Integer year;
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
        } else {
            year = DateUtils.currentYear(new Date());
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if (StringUtils.isNotEmpty(tableName)) {
            return baseMapper.selectAssessList(tableName, cuMemberAssessVO.getPkMember(),
                    cuMemberAssessVO.getPkCountry(), cuMemberAssessVO.getAssessStatus(),
                    cuMemberAssessVO.getAssessType(), cuMemberAssessVO.getChangeType(), cuMemberAssessVO.getStartDate(),
                    cuMemberAssessVO.getEndDate(), orderTypeList
            );
        }
        return null;
    }

    /**
     * @description: 查询会员月度考核(查询列表用)
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> getMemberAssessMonthList(CuMemberAssessVO cuMemberAssessVO) {
        Integer year;
        //获取期数
        Date date = new Date();
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
        } else {
            year = DateUtils.currentYear(date);
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if (StringUtils.isNotEmpty(tableName)) {
            return baseMapper.getMemberAssessMonthList(tableName, cuMemberAssessVO.getPkMember(), cuMemberAssessVO.getAssessType(),
                    cuMemberAssessVO.getPkCountry(), null);
        }
        return null;
    }

    /**
     * @description: 查询会员月度考核(查询积分用)
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> getMemberAssessMonth(CuMemberAssessVO cuMemberAssessVO) {
        Integer year;
        //获取期数
        Date date = new Date();
        int period;
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
            if (DateUtils.compareDateBefore(date, cuMemberAssessVO.getStartDate()) || DateUtils.compareDateEqual(date, cuMemberAssessVO.getStartDate())) {
                //获取当前月第一天
                Date date1 = DateUtils.currentMonthFirstDate(DateUtils.beforeDate(1, ChronoUnit.DAYS, date));
                period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                        DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date1)).getPkId();
            } else {
                Date date1 = DateUtils.currentMonthFirstDate(cuMemberAssessVO.getStartDate());
                period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                        DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date1)).getPkId();
            }
        } else {
            year = DateUtils.currentYear(date);
            Date date1 = DateUtils.currentMonthFirstDate(DateUtils.beforeDate(1, ChronoUnit.DAYS, date));
            period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date1)).getPkId();
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if (StringUtils.isNotEmpty(tableName)) {
            return baseMapper.getMemberAssessMonthList(tableName, cuMemberAssessVO.getPkMember(), cuMemberAssessVO.getAssessType(),
                    cuMemberAssessVO.getPkCountry(), period);
        }
        return null;
    }


    /**
     * 奖金计算时重算考核 （复购考核、分红考核）,只重算免考的、撤单的
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void retryCalculationAssess(Date currentDate, Boolean flag) {
//        String beforeDateStr = DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, currentDate);
//        Integer beforePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(beforeDateStr).getPkId();
        Integer currentPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, currentDate)).getPkId();
//        String beforeTableName = TableNameConstants.CU_MEMBER_SETTLE + beforePeriod;
////        a)更新结算表会员前2部分会员免考状态 满足首次注册的会员 满足满足首次升级的会员
//        String tableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.currentYear(currentDate);
//        String currentMonthFirstDateStr = DateUtils.currentMonthFirstDateStr(currentDate);
//        Integer monthFirstPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthFirstDateStr).getPkId();
//        // 结算日期月份的第一天
        Date startDate = DateUtils.beforeMonthFirstDate(currentDate);
//        // 结算日期后一天
        Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.currentMonthLastDate(currentDate));
        String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + currentPeriod;
//        // 首次晋升、首次升级都是当月加下月考核，查询出上月1号到考核日期的值，
//        // 等级看会员表支付时间，奖衔看奖衔变更表是否免考（分组取）,手动免考查看 最新的结算表手动考核
////        d)前一天不免考，后一天免考，需要做重新考核，直接变考核通过，原来考核通过的不回退考核金额，且考核状态为考核通过，不是免考通过
//        // 所有的海粉会员设置复购考核失败，存在任意订单的海粉会员复购考核成功
//        // 复购免考通过
        Integer startPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, startDate)).getPkId();
        Integer endPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, endDate)).getPkId();
//        baseMapper.updateAssessCuMemberToRepExempt(tableName, settleTableName, monthFirstPeriod, startDate, endDate, startPeriod, endPeriod);
//        // 奖金重算时更新结算表,flag=true代表奖金重算时，重算考核需要更新网体 flag=false 代表重算考核
        if (flag) {
            // 目前永远传入的都是false
            baseMapper.updateCuMemberSettleToRepExempt(settleTableName, startDate, endDate, startPeriod, endPeriod);
        }
//        // 分红免考通过
//        baseMapper.updateAssessCuMemberToShareExempt(tableName, settleTableName, monthFirstPeriod);
//        // 查询出考核表免考的数据
//        List<CuMemberAssess> oldCuMemberAssessList = baseMapper.queryExemptAssessCuMember(tableName, monthFirstPeriod);
////        b)查询前一天的会员免考记录，查询结算日会员免考记录
//        List<CuMemberSettleExt> oldCuMemberSettleExtList = cuMemberTreeService.queryExemptCuMemberSettle(beforeTableName);
////        c)比较会员2天免考发生的状态，只需要计算变化的值，去掉没有发生变化的值
//        List<CuMemberSettleExt> newCuMemberSettleExtList = new ArrayList<>();
//        if (oldCuMemberAssessList.size() > 0 && oldCuMemberSettleExtList != null && oldCuMemberSettleExtList.size() > 0) {
//            Map<String, CuMemberAssess> cuMemberAssessMap = new HashMap<>(ComputeUtil.mapInitCapacity(oldCuMemberAssessList.size()));
//            oldCuMemberAssessList.forEach(cuMemberAssess ->
//                    cuMemberAssessMap.put(cuMemberAssess.getPkMember().toString() + cuMemberAssess.getAssessType().toString(), cuMemberAssess));
//            oldCuMemberSettleExtList.forEach(cuMemberSettleExt -> {
//                String key = cuMemberSettleExt.getPkMember().toString() + cuMemberSettleExt.getAssessType().toString();
//                if (!cuMemberAssessMap.containsKey(key)) {
//                    newCuMemberSettleExtList.add(cuMemberSettleExt);
//                }
//            });
//        }
////        e)查询有没有撤单，更新撤单的数据
//        List<CuMemberAssessExt> cuMemberAssessExtList = handleCuMemberAssessByRevokeOrder(currentDate, newCuMemberSettleExtList, tableName, monthFirstPeriod, settleTableName);
//        if (cuMemberAssessExtList.size() > 0) {
//            // 将变更的插入临时表
//            baseMapper.insertTmpCuMemberAssess(cuMemberAssessExtList);
//            // 根据临时表里面的值进行更新会员表
//            baseMapper.updateAssessPeriodByRevokeOrder(tableName);
//        }
////        f)前一天免考后一天不免考，需要做重新考核，看上月是否可以考核通过
//        if (newCuMemberSettleExtList.size() > 0) {
//            //  处理考核数据
//            // 记录流水，新增或修改
//            cuMemberAssessDetailService.insertCuMemberAssessDetailByPeriod(tableName, monthFirstPeriod, newCuMemberSettleExtList);
//            List<List<?>> cutCuMemberSettleExtList = cuMemberBonusSettle.handleCutList(newCuMemberSettleExtList);
//            cutCuMemberSettleExtList.forEach(assessExtList ->
//                    baseMapper.updateAssessPeriodCheckBalanceByTmp(tableName, monthFirstPeriod, assessExtList));
//        }
        // 网体数据修改，修改复购免考类型 0=正常 1=手动免考 2=注册免考 3=晋升免考，修改是否复购免考（0=是,1=否）
    }

    private void initCurrentAssess(Date currentDate, String currentTableName, Integer currentPeriod) {
        // 获取上月最后一天
        String beforeLastDate = DateUtils.beforeMonthLastDateStr(currentDate);
        CuMemberSettlePeriod cuMemberSettlePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(beforeLastDate);
        int period = cuMemberSettlePeriod.getPkId();
        String sourceTable = TableNameConstants.CU_MEMBER_SETTLE + period;
        String rangeSourceTable = TableNameConstants.CU_MEMBER_RANGE + period;
        // 清空子表、主表数据
        cuMemberAssessDetailService.deleteCuMemberAssessDetailByPeriod(currentTableName, currentPeriod);
        baseMapper.deleteCuMemberAssessByPeriod(currentTableName, currentPeriod);
        // 初始化复购考核
        String month = DateUtils.getMonth(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, currentDate));
        // 结算日期后一天
        baseMapper.insertRepCuMemberAssess(currentTableName, sourceTable, currentPeriod, month);
        baseMapper.insertRepCuMemberRangeAssess(currentTableName, rangeSourceTable, currentPeriod, month);
        // 初始化分红考核
//        baseMapper.insertShareCuMemberAssess(currentTableName, sourceTable, currentPeriod, month);

//        a)更新结算表会员前2部分会员免考状态 满足首次注册的会员 满足满足首次升级的会员
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.currentYear(currentDate);
        String currentMonthFirstDateStr = DateUtils.currentMonthFirstDateStr(currentDate);
        Integer monthFirstPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthFirstDateStr).getPkId();
        // 结算日期月份的第一天
        Date startDate = DateUtils.beforeMonthFirstDate(currentDate);
        // 结算日期后一天
        Date endDate = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.currentMonthLastDate(currentDate));
        String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + currentPeriod;
        if (DateUtils.currentDay(DateUtils.currentDate()) == 1) {
            String currentMonthFirstDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, currentDate));
            Integer yesterdayPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthFirstDate).getPkId();
            settleTableName = TableNameConstants.CU_MEMBER_SETTLE + yesterdayPeriod;
        }
        // 首次晋升、首次升级都是当月加下月考核，查询出上月1号到考核日期的值，
        // 等级看会员表支付时间，奖衔看奖衔变更表是否免考（分组取）,手动免考查看 最新的结算表手动考核
//        d)前一天不免考，后一天免考，需要做重新考核，直接变考核通过，原来考核通过的不回退考核金额，且考核状态为考核通过，不是免考通过
        // 复购免考通过
        Integer startPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, startDate)).getPkId();
        Integer endPeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, endDate)).getPkId();
        baseMapper.updateAssessCuMemberToRepExempt(tableName, settleTableName, monthFirstPeriod, startDate, endDate, startPeriod, endPeriod);

        // 初始化订单数据，将当月新增加入数据库
        // 上月第一天，上月最后一天
        Date beforeFirstDate = DateUtils.beforeMonthFirstDate(currentDate);
        Date currentFirstDate = DateUtils.currentMonthFirstDate(currentDate);
        cuMemberAssessDetailService.insertCuMemberAssesDetailByOrder(currentTableName, currentPeriod, beforeFirstDate, currentFirstDate);
    }

    /**
     * 初始化期初数据，将上月结余作为本月期初，更新当月新增
     *
     * @param currentDate      结算日期
     * @param currentTableName 当前月结算表
     * @param currentPeriod    当前结算期间
     * @param assessmentType   考核类型 0=复购考核 1=分红考核
     */
    private void initCurrentAssessByBeforeMonth(Date currentDate, String currentTableName, Integer currentPeriod, Integer assessmentType) {
        List<Integer> orderTypeList = new ArrayList<>();
        if (EAssessmentType.REPURCHASE_ASSESS.getValue() == assessmentType) {
            orderTypeList.add(EOrderType.REPURCHASE_ORDER.getValue());
            orderTypeList.add(EOrderType.SPECIAL_REPURCHASE_ORDER.getValue());
        } else {
            orderTypeList.add(EOrderType.WELFARE_ORDER.getValue());
        }
        // 更新考核值，期初是上月考核表的当月剩余业绩
        // 获取结算日月初第一天，获取期间
        // 获取结算日上月最后一天，获取期间
        Date beforeMonthFirstDate = DateUtils.beforeMonthFirstDate(currentDate);
        String beforeMonthFirstDateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, beforeMonthFirstDate);
        Integer beforePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(beforeMonthFirstDateStr).getPkId();
        String beforeTableName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(beforeMonthFirstDateStr);
        Date currentMonthFirstDate = DateUtils.currentMonthFirstDate(currentDate);
        // 初始化复购考核的上月结余 // 更新当月结余
        baseMapper.updateAssessPeriodBeginByBeforeMonth(beforeTableName, currentTableName, beforePeriod, currentPeriod, assessmentType);
        // 更新当月新增
        baseMapper.updateAssessPeriodAddBySaOrder(currentTableName, currentPeriod, assessmentType,
                beforeMonthFirstDate, currentMonthFirstDate, orderTypeList);
    }

    /**
     * @description: 查询会员订单考核积分
     * @author: zhang jing
     * @date: 2023/3/14 17:52
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public CuMemberAssessVO getOrderAssessList(CuMemberAssessVO cuMemberAssessVO) {
        return baseMapper.getOrderAssessList(cuMemberAssessVO);
    }

    /**
     * @description: 根据考核类型查询会员订单信息
     * @author: zhang jing
     * @date: 2023/4/23 19:45
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> getOrderByAssessList(CuMemberAssessVO cuMemberAssessVO) {
        return baseMapper.getOrderByAssessList(cuMemberAssessVO);
    }

    @Override
    public List<CuMemberAssess> queryCuMemberAssTotal(String startDate, Integer pkCountry) {
        int period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(startDate).getPkId();
        String tableTable = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(startDate);
        return baseMapper.queryCuMemberAssTotal(tableTable, period, pkCountry);
    }
}
