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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.member.achieve.mapper.CuMemberTreeMapper;
import com.hzs.member.achieve.param.MemberParentCountParam;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.achieve.vo.CuMemberAwardsUnderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

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

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

    @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;
    }

    /**
     * 根据pkmember查询
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public CuMemberSettleExt queryCuMemberTreeByPkMemberTable(String settleTableName, Long pkMember) {
        return baseMapper.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
    }

    /**
     * 查询推荐关系
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public List<CuMemberSettleExt> queryCuMemberTreeParentTable(String settleTableName, Long pkMember, Integer level, List<Integer> vertexIdList) {
        List<CuMemberSettleExt> memberSettleExtList = baseMapper.queryCuMemberTreeParentTable(settleTableName, pkMember, level, vertexIdList);
        List<Long> ids = memberSettleExtList.stream().map(CuMemberTree::getPkMember).collect(Collectors.toList());
        // 查询会员直推人数
        List<MemberParentCountParam> memberParentCountParamList = new ArrayList<>();
        if (ids.size() > 0) {
            if (settleTableName.contains(TableNameConstants.CU_MEMBER_SECOND)) {
                // 秒结表查询当天的推荐上下级
                memberParentCountParamList = baseMapper.queryByMemberParentCount(ids);
            } else {
                memberParentCountParamList = baseMapper.queryByMemberParentAndMemberSettleCount(ids, settleTableName);
            }
        }
        Map<Long, Long> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));
        // 查询直推人数
        memberSettleExtList.stream().peek(a -> a.setDirectPushNumber(memberParentCountMap.getOrDefault(a.getPkMember(), 0L))).collect(Collectors.toList());
        //查询直推业绩
        if (StringUtils.isNotBlank(settleTableName) && ids.size() > 0) {
            List<MemberParentCountParam> memberParentCountParamPvList;
            if (settleTableName.contains(TableNameConstants.CU_MEMBER_SECOND)) {
                // 查询秒结表
                memberParentCountParamPvList = this.queryByMemberParentSumPv(settleTableName, ids);
            } else {
                memberParentCountParamPvList = this.queryByMemberParentSettleSumPv(settleTableName, ids);
            }
            Map<Long, BigDecimal> memberParentPvMap = memberParentCountParamPvList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberSumPv));
            // 查询直推业绩
            memberSettleExtList.stream().peek(a -> a.setHistoryPerformancePv(memberParentPvMap.getOrDefault(a.getPkMember(), BigDecimal.ZERO))).collect(Collectors.toList());
        }
        return memberSettleExtList;
    }

    /**
     * 查询直推人数
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public Long queryCuMemberTreeParentTableCount(String settleTableName, Long pkMember) {
        return baseMapper.queryCuMemberTreeParentTableCount(settleTableName, pkMember);
    }

    /**
     * 查询历史安置上下级
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public List<CuMemberSettleExt> queryCuMemberTreePlaceParentHistoryTable(String settleTableName, Long pkMember, List<Long> memberIdList, Integer level) {
        return baseMapper.queryCuMemberTreePlaceParentHistoryTable(settleTableName, pkMember, memberIdList, level);
    }

    /**
     * 查询秒结表会员信息
     *
     * @param pkMember
     * @param settleTableName
     * @param level
     * @return
     */
    @Override
    public List<CuMemberSettleExt> selectByTreeMemberList(Long pkMember, String settleTableName, Integer level, List<Integer> vertexIdList) {
        return baseMapper.selectByTreeMemberList(pkMember, settleTableName, level, vertexIdList);
    }

    /**
     * 查询秒结表会员信息
     *
     * @param pkMember
     * @param level
     * @return
     */
    @Override
    public List<CuMemberSettleExt> selectByTreeMemberListFive(Long pkMember, Integer level, List<Integer> vertexIdList) {
        return baseMapper.selectByTreeMemberListFive(pkMember, level, vertexIdList);
    }

    /**
     * 查询秒结表是否有值
     *
     * @param settleTableName
     * @return
     */
    @Override
    public Long selectByselectByTreeMemberCount(String settleTableName, Long pkMember) {
        return baseMapper.selectByselectByTreeMemberCount(settleTableName, pkMember);
    }

    /**
     * 查询伞下安置新增业绩
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public List<CuMemberSettleExt> brollyBelowResettle(String settleTableName, Long pkMember) {
        return baseMapper.brollyBelowResettle(settleTableName, pkMember);
    }

    /**
     * 根据pkmember查询 关联parent
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public CuMemberSettleExt queryCuMemberTreeByPlacePkMemberTable(String settleTableName, Long pkMember) {
        CuMemberSettleExt cuMemberSettleExt = baseMapper.queryCuMemberTreeByPlacePkMemberTable(settleTableName, pkMember);
        List<Long> memberIds = Collections.singletonList(cuMemberSettleExt.getPkMember());
        // 查询会员直推人数
        List<MemberParentCountParam> memberParentCountParamList = baseMapper.queryByMemberParentCount(memberIds);
        Map<Long, Long> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));
        // 查询直推人数
        cuMemberSettleExt.setDirectPushNumber(memberParentCountMap.getOrDefault(cuMemberSettleExt.getPkMember(), 0L));

        //查询直推业绩
        if (StringUtils.isNotBlank(settleTableName)) {
            List<MemberParentCountParam> memberParentCountParamPvList = this.queryByMemberParentSumPv(settleTableName, memberIds);
            Map<Long, BigDecimal> memberParentPvMap = memberParentCountParamPvList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberSumPv));
            cuMemberSettleExt.setHistoryPerformancePv(memberParentPvMap.getOrDefault(cuMemberSettleExt.getPkMember(), BigDecimal.ZERO));
        }
        return cuMemberSettleExt;
    }

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


    /**
     * 查询历史安置架构
     *
     * @param settleTableName
     * @param pkMember
     * @param memberIdList
     * @param level
     * @return
     */
    @Override
    public List<CuMemberSettleExt> queryCuMemberTreePlaceParentHistoryFramework(String settleTableName, Long pkMember, List<Long> memberIdList, Integer level) {
        return baseMapper.queryCuMemberTreePlaceParentHistoryFramework(settleTableName, pkMember, memberIdList, level);
    }

    /**
     * 查询会员历史直推业绩
     *
     * @param settleTableName
     * @param memberIds
     * @return
     */
    @Override
    public List<MemberParentCountParam> queryByMemberParentSettleSumPv(String settleTableName, List<Long> memberIds) {
        List<MemberParentCountParam> memberParentCountParamList = new ArrayList<>();
        // in 如果超过1000 报错
        if (memberIds.size() > 1000) {
            List<List<Long>> partitionList = Lists.partition(memberIds, 1000);
            for (List<Long> pkMembers : partitionList) {
                memberParentCountParamList.addAll(baseMapper.queryByMemberParentSettleSumPv(settleTableName, pkMembers));
            }
        } else {
            memberParentCountParamList.addAll(baseMapper.queryByMemberParentSettleSumPv(settleTableName, memberIds));
        }
        return memberParentCountParamList;
    }

    @Override
    public CuMemberAwardsUnderVO queryMemberPv(String settleTable, Long pkMember, String monthTable) {
        return baseMapper.queryMemberPv(settleTable, pkMember, monthTable);
    }

    @Override
    public CuMemberAwardsUnderVO queryMemberTodayPv(Long pkMember) {
        return baseMapper.queryMemberTodayPv(pkMember);
    }

}
