package com.hzs.bonus.achieve.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.bonus.achieve.param.MemberAchieveParam;
import com.hzs.bonus.achieve.param.MemberParentCountParam;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 会员网体历史记录 服务类
 */
public interface ICuMemberTreeService extends IService<CuMemberTree> {

    /**
     * 创建会员网体记录表，每天凌晨12点初始化昨天数据
     *
     * @param date 结算日期，初始化哪天的值 格式是 yyyyMM
     */
    void createCuMemberTree(String date);

    /**
     * 初始用来结算奖金的表，
     *
     * @param yesterdaySettleTableName 昨天的结算表
     * @param currentSettleTableName   结算日的结算表
     */
    void initCuMemberTreeTable(String yesterdaySettleTableName, String currentSettleTableName, String yesterdayTableNameDetail, String currentTableNameDetail,
                               Integer period, Integer endPeriod, String sourceCountryTable, String targetCountryTable);

    /**
     * 重新结算会员网体
     *
     * @param currentSettleTableName 结算日的表名
     */
    void updateCuMemberSettle(Map<Long, MemberAchieveParam> cuMemberAchieveHashMap, String currentSettleTableName, String settleTableNameDetail);

    /**
     * 重新结算会员网体
     *
     * @param currentSettleTableName 结算日的表名
     */
    void updateCuMemberBackSettle(Map<Long, MemberAchieveParam> cuMemberAchieveHashMap, String currentSettleTableName, String settleTableNameDetail);

    /**
     * 更新会员网体，将会员网体改为死点，检查网体是否有问题
     *
     * @param revokeRegSaOrderList 注册撤销订单
     * @param revokeUpdSaOrderList 升级撤销订单
     * @param settleDate           计算日期
     * @param settleTableName      结算表名
     */
    void updateCuMemberTreeByCancelOrder(List<SaOrderExt> revokeRegSaOrderList, List<SaOrderExt> revokeUpdSaOrderList,
                                         String settleDate, String settleTableName);

    /**
     * 判断是否存在临时表
     *
     * @param tableName 表名
     */
    Integer queryExistOracleTable(String tableName);

    /**
     * 创建会员网体推荐人索引
     *
     * @param tableName 日期
     */
    void createCuMemberTableUniqueIndex(String tableName);

    /**
     * 创建表的序列
     *
     * @param tableName    表名
     * @param tableNameSeq 序列名
     */
    void createCuMemberTableSequence(String tableName, String tableNameSeq);

    /**
     * 查询出符合下一等级足够业绩的会员
     *
     * @param settleTableName 结算表
     */
    List<CuMemberSettleExt> queryCuMemberSettleEnoughAwardAchieve(String settleTableName, String settleTableNameDetail);

    /*
     *  查询出符合下一等级足够血缘业绩的会员，验证血缘业绩是否满足
     **/
    List<CuMemberSettleExt> queryCuMemberSettleBloodEnoughAwardAchieve(String settleTableName, List<CuMemberSettleExt> cuMemberSettleExtList);

    /*
     *  回退会员奖衔
     **/
    void updateBackCuMemberSettleAward(String settleTableName, Integer period);

    /**
     * 查询会员结算表，查询出结算表中会员伞下左右区满足等级
     *
     * @param settleTableName 结算表
     * @param placeDept       部门
     * @param awardsValue     奖衔对应的值
     * @param pkMember        会员
     */
    Boolean queryCuMemberSettlePlaceUnderUmbrella(String settleTableName, Integer awardsValue, Integer placeDept, Long pkMember);

    /**
     * 查询会员结算表，查询出结算表中会员血缘关系满足等级
     *
     * @param settleTableName 结算表
     * @param pkMember        会员
     */
    Boolean queryCuMemberSettleBloodUnderUmbrella(String settleTableName, Integer awardsValue, Integer placeDept, Long pkMember);

    /*
     *  查询会员安置伞下部门数量
     **/
    List<Integer> queryCuMemberSettlePlaceDeptUnderUmbrella(String settleTableName, Long pkMember);

    /**
     * 更新会员网体奖衔数据，更新月表、结算表
     */
    void updateCuMemberSettleByPeriod(Integer period);

    /**
     * 根据订单，查询出网体中记录的会员数据，推荐人、安置人
     *
     * @param settleTableName 结算表
     * @param saOrderExtList  订单
     */
    List<CuMemberSettleExt> batchQueryMemberSettleBySaOrder(String settleTableName, String settleTableNameDetail, List<SaOrderExt> saOrderExtList);

    /**
     * 根据订单，查询出网体中记录的会员数据及推荐人
     *
     * @param settleTableName 结算表
     * @param saOrderExtList  会员编号
     */
    List<CuMemberSettleExt> queryMemberSettleTenParentBySaOrder(String settleTableName, List<SaOrderExt> saOrderExtList);

    /**
     * 根据订单，查询出网体中记录的会员数据，推荐人、安置人
     *
     * @param settleTableName  结算表
     * @param serviceMemberSet 会员编号
     */
    List<CuMemberSettleExt> batchQueryMemberSettleByMemberPk(String settleTableName, String settleTableNameDetail, Set<Long> serviceMemberSet);

    /**
     * 批量根据订单信息查询会员结算表中的父级安置数据
     *
     * @param settleTableName      结算表
     * @param firPurchaseOrderList 首单列表
     */
    Map<Long, List<CuMemberSettleExt>> batchQueryCuMemberSettlePlaceParent(String settleTableName, String settleTableNameDetail, List<SaOrderExt> firPurchaseOrderList);

    /**
     * 批量根据量奖信息查询会员结算表中的父级血缘数据
     *
     * @param settleTableName 结算表
     * @param expandMemberSet 有量奖的会员
     */
    List<CuMemberSettleExt> batchQueryCuMemberSettleParent(String settleTableName, String settleTableNameDetail, Set<Long> expandMemberSet);

    /**
     * 批量根据量奖信息查询会员结算表中的父级血缘数据
     *
     * @param settleTableName 结算表
     * @param expandMemberSet 有量奖的会员列表
     */
    List<CuMemberSettleExt> batchCuMemberSettlePlaceParent(String settleTableName, String settleTableNameDetail, Set<Long> expandMemberSet);

    /*
     *  批量根据订单信息查询会员结算表中的数据,查找每个人的前区后区
     **/
    List<CuMemberSettleExt> queryEnoughLeaderCuMemberSettle(String settleTableName, String settleTableNameDetail);

    /**
     * 更新会员树的累计业绩
     *
     * @param settleTableName 结算日期
     * @param cuMemberTreeMap 树
     */
    void updateCuMemberSettle(String settleTableName, String settleTableNameDetail, String settleTableCountry, Map<Long, CuMemberSettleExt> cuMemberTreeMap);

    /**
     * 查询会员直推业绩
     *
     * @param settleTableName
     * @param memberIds
     * @return
     */
    List<MemberParentCountParam> queryByMemberParentSumPv(String settleTableName, List<Long> memberIds);

    /**
     * 删除日表数据
     *
     * @param tableName 表名
     */
    void truncateCuMemberTreeTable(String tableName);

    /**
     * 创建会员网体推荐人索引
     *
     * @param tableName 日期
     */
    void createCuMemberTreeDayOnlyIndex(String tableName);

    /**
     * 创建会员网体推荐人索引
     *
     * @param tableName 日期
     */
    void createCuMemberTreeParentIndex(String tableName);

    /**
     * 创建会员网体安置人索引
     *
     * @param tableName 日期
     */
    void createCuMemberTreePlaceIndex(String tableName);
}
