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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailSExt;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

    @Override
    public CuMemberSettleExt queryCuMemberTreeByPkMemberTable(String settleTableName, Long pkMember) {
        return baseMapper.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
    }

    @Override
    public List<CuMemberRetailSExt> queryCuMemberTreeParentTable(String settleTableName, Long pkMember, List<Integer> vertexIdList) {
        List<CuMemberRetailSExt> memberRetailExtList = baseMapper.queryCuMemberTreeParentTable(settleTableName, pkMember, vertexIdList);
        List<Long> ids = memberRetailExtList.stream().map(CuMemberRetailSExt::getPkMember).collect(Collectors.toList());
        // 查询会员直推人数
        List<MemberParentCountParam> memberParentCountParamList = new ArrayList<>();
        if (ids.size() > 0) {
            if (settleTableName.contains(TableNameConstants.CU_MEMBER_RETAIL_S)) {
                // 秒结表查询当天的推荐上下级
                memberParentCountParamList = baseMapper.queryByMemberParentCount(ids);
            } else {
                memberParentCountParamList = baseMapper.queryByMemberParentAndMemberSettleCount(ids, settleTableName);
            }

        }
        Map<Long, Integer> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));
        // 查询直推人数
        memberRetailExtList.stream().peek(a -> a.setPushNumber(memberParentCountMap.getOrDefault(a.getPkMember(), 0)));
        return memberRetailExtList;
    }

    @Override
    public Long queryCuMemberTreeParentTableCount(String settleTableName, Long pkMember) {
        return baseMapper.queryCuMemberTreeParentTableCount(settleTableName, pkMember);
    }

    @Override
    public Long selectByTreeMemberCount(String settleTableName, Long pkMember) {
        return baseMapper.selectByTreeMemberCount(settleTableName, pkMember);
    }

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

    @Override
    public CuMemberRetailSExt queryCuMemberTreeByPlacePkMemberTable(String settleTableName, Long pkMember) {
        CuMemberRetailSExt memberRetailExt = baseMapper.queryCuMemberTreeByPlacePkMemberTable(settleTableName, pkMember);
        List<Long> memberIds = Collections.singletonList(memberRetailExt.getPkMember());
        // 查询会员直推人数
        List<MemberParentCountParam> memberParentCountParamList = baseMapper.queryByMemberParentCount(memberIds);
        Map<Long, Integer> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));
        // 查询直推人数
        memberRetailExt.setPushNumber(memberParentCountMap.getOrDefault(memberRetailExt.getPkMember(), 0));
        return memberRetailExt;
    }

}
