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.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.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.member.ext.MemberSettleTreeExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.member.achieve.mapper.CuMemberTreeMapper;
import com.hzs.member.achieve.param.MemberAchieveParam;
import com.hzs.member.achieve.param.MemberParentCountParam;
import com.hzs.member.achieve.service.ICuMemberAchieveService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.census.param.CensusSummaryParam;
import com.hzs.member.census.vo.CensusSummaryVo;
import com.hzs.member.detail.service.ICuMemberAssessService;
import com.hzs.member.structure.controller.vo.CuMemberSettleVo;
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.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员网体历史记录 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-10-31
 */
@Slf4j
@Service
public class CuMemberTreeServiceImpl extends ServiceImpl<CuMemberTreeMapper, CuMemberTree> implements ICuMemberTreeService {
    @Autowired
    private ICuMemberTreeService memberTreeService;

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

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

    /**
     * 查询推荐关系
     *
     * @param settleTableName
     * @param pkMember
     * @return
     */
    @Override
    public List<CuMemberSettleExt> queryCuMemberTreeParentTable(String settleTableName, Long pkMember, Integer level) {
        Long b = System.currentTimeMillis();
        System.out.println("开始时间：" + b);
        List<CuMemberSettleExt> memberSettleExtList = baseMapper.queryCuMemberTreeParentIdTable(settleTableName, pkMember, level);
    //    List<CuMemberSettleExt> memberSettleExtList = baseMapper.queryCuMemberTreeParentTable(settleTableName, pkMember, level);
        System.out.println("结束时间：" + (System.currentTimeMillis() - b));
        List<Long> ids = memberSettleExtList.stream().map(a -> a.getPkMember()).collect(Collectors.toList());
        ids.add(pkMember);
        if (ids.size() >0){
            // 查询会员直推人数
            List<MemberParentCountParam> memberParentCountParamList = baseMapper.queryByMemberParentCount(ids);
            Map<Long, Long> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));

            //   List<MemberParentCountParam> memberParentCountParamList = baseMapper.queryByMemberParentCount(ids);
            //    Map<Long, Long> memberParentCountMap = memberParentCountParamList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberCount));
            // 查询直推人数
            memberSettleExtList.stream().map(a -> {
                a.setDirectPushNumber(memberParentCountMap.getOrDefault(a.getPkMember(), 0L));
                return a;
            }).collect(Collectors.toList());
        }

        //查询直推业绩
        if (StringUtils.isNotBlank(settleTableName) && ids.size() > 0) {
            List<MemberParentCountParam> memberParentCountParamPvList = new ArrayList<>();
            memberParentCountParamPvList = memberTreeService.queryByMemberParentSumPv(settleTableName, ids);
            Map<Long, BigDecimal> memberParentPvMap = memberParentCountParamPvList.stream().collect(Collectors.toMap(MemberParentCountParam::getPkMember, MemberParentCountParam::getParentMemberSumPv));
            // 查询直推业绩
            memberSettleExtList.stream().map(a -> {
                a.setHistoryPerformancePv(memberParentPvMap.getOrDefault(a.getPkMember(), BigDecimal.ZERO));
                return a;
            }).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> 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);
        CuMemberSettleExt cuMemberSettleExt = baseMapper.queryCuMemberTreeByParentPkMemberTable(settleTableName, pkMember);
        List<Long> memberIds = Arrays.asList(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 = memberTreeService.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;
    }

}
