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

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.bonus.achieve.mapper.CuMemberRetailDetailMapper;
import com.hzs.bonus.achieve.mapper.CuMemberRetailRangeMapper;
import com.hzs.bonus.achieve.service.ICuMemberRetailDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.enums.EBonusStage;
import com.hzs.common.core.enums.EBonusStageStatus;
import com.hzs.common.domain.member.achieve.CuMemberRetailDetail;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 日结算阶段表 服务实现类
 */
@Service
public class CuMemberRetailDetailServiceImpl extends ServiceImpl<CuMemberRetailDetailMapper, CuMemberRetailDetail> implements ICuMemberRetailDetailService {

    @Resource
    private CuMemberRetailRangeMapper cuMemberRetailRangeMapper;

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    public void listMemberRetailDetail(String retailDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                       EBonusStage eBonusStage, EBonusStageStatus eBonusStageStatus, Collection<Long> stageMemberList) {
        List<List<?>> pkMemberList;
        if (CollectionUtil.isNotEmpty(stageMemberList)) {
            pkMemberList = cuMemberBonusSettle.handleCutList(stageMemberList, MagicNumberConstants.BATCH_QUERY_NUM);
        } else {
            pkMemberList = cuMemberBonusSettle.handleCutList(memberRangeExtMap.keySet(), MagicNumberConstants.BATCH_QUERY_NUM);
        }
        pkMemberList.forEach(tmpIdList -> {
            // 查询明细数据（指定阶段，未完成的）
            List<CuMemberRetailDetail> tmpList = baseMapper.listMemberRetailDetail(retailDetailTableName, tmpIdList, eBonusStage.getValue(), null != eBonusStageStatus ? eBonusStageStatus.getValue() : null);
            packageThreeDetailMap(memberRangeExtMap, eBonusStage, tmpList);
        });
    }

    @Override
    public List<CuMemberRetailDetail> listMemberRetailDetailByQuery(String retailDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                                                    List<Long> queryMemberList, EBonusStage eBonusStage) {
        // 查询明细数据（指定阶段，未完成的）
        List<CuMemberRetailDetail> tmpDetailList = baseMapper.listMemberRetailDetail(retailDetailTableName, queryMemberList, eBonusStage.getValue(), EBonusStageStatus.NO.getValue());
        packageThreeDetailMap(memberRangeExtMap, eBonusStage, tmpDetailList);
        return tmpDetailList;
    }

    /**
     * 封装三个明细map
     */
    private void packageThreeDetailMap(Map<Long, CuMemberRetailRangeExt> memberRangeExtMap, EBonusStage eBonusStage, List<CuMemberRetailDetail> tmpDetailList) {
        if (CollectionUtil.isNotEmpty(tmpDetailList)) {
            // 明细数据放到对应的会员数据中
            for (CuMemberRetailDetail cuMemberRetailDetail : tmpDetailList) {
                CuMemberRetailRangeExt cuMemberRetailRangeExt = memberRangeExtMap.get(cuMemberRetailDetail.getPkMember());
                if (EBonusStage.ONE.getValue() == eBonusStage.getValue()) {
                    // 阶段一点位列表（使用有序map，为了后续业务处理方便）
                    if (null == cuMemberRetailRangeExt.getOneDetailMap()) {
                        // 使用有序map，为了后续业务处理方便
                        cuMemberRetailRangeExt.setOneDetailMap(new LinkedHashMap<>());
                    }
                    // 阶段一点位列表
                    cuMemberRetailRangeExt.getOneDetailMap().put(cuMemberRetailDetail.getChildNode(), cuMemberRetailDetail);
                } else if (EBonusStage.TWO.getValue() == eBonusStage.getValue()) {
                    if (null == cuMemberRetailRangeExt.getTwoDetailMap()) {
                        // 使用有序map，为了后续业务处理方便
                        cuMemberRetailRangeExt.setTwoDetailMap(new LinkedHashMap<>());
                    }
                    // 阶段二点位列表
                    cuMemberRetailRangeExt.getTwoDetailMap().put(cuMemberRetailDetail.getChildNode(), cuMemberRetailDetail);
                } else if (EBonusStage.THREE.getValue() == eBonusStage.getValue()) {
                    if (null == cuMemberRetailRangeExt.getThreeDetailMap()) {
                        // 使用有序map，为了后续业务处理方便
                        cuMemberRetailRangeExt.setThreeDetailMap(new LinkedHashMap<>());
                    }
                    // 阶段三点位列表
                    cuMemberRetailRangeExt.getThreeDetailMap().put(cuMemberRetailDetail.getChildNode(), cuMemberRetailDetail);
                }
            }
        }
    }

    @Override
    public void listChildRetailDetail(String rangeTableName, String retailDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                      CuMemberRetailRangeExt sourceMember, EBonusStage eBonusStage) {
        if (null != sourceMember.getChildRetailMap()) {
            // 直推会员列表存在，则直接结束
            return;
        }
        sourceMember.setChildRetailMap(new LinkedHashMap<>());

        // 会员直推数据
        List<CuMemberRetailRangeExt> retailRangeList = cuMemberRetailRangeMapper.listChildRetail(rangeTableName, Collections.singletonList(sourceMember.getPkMember()));
        if (CollectionUtil.isNotEmpty(retailRangeList)) {
            // 直推数据列表
            LinkedHashMap<Date, CuMemberRetailRangeExt> childRetailMap = sourceMember.getChildRetailMap();

            List<Long> queryMemberList = new ArrayList<>();
            for (CuMemberRetailRangeExt cuMemberRetailRangeExt : retailRangeList) {
                CuMemberRetailRangeExt tmpMemberRetailRange = memberRangeExtMap.get(cuMemberRetailRangeExt.getPkMember());
                if (null != tmpMemberRetailRange) {
                    // 直推会员已经存在
                    childRetailMap.put(tmpMemberRetailRange.getPayTime(), tmpMemberRetailRange);
                } else {
                    // 直推会员不存在
                    childRetailMap.put(cuMemberRetailRangeExt.getPayTime(), cuMemberRetailRangeExt);
                    memberRangeExtMap.put(cuMemberRetailRangeExt.getPkMember(), cuMemberRetailRangeExt);
                    queryMemberList.add(cuMemberRetailRangeExt.getPkMember());
                }
            }
            if (queryMemberList.size() > 0) {
                List<List<?>> pkMemberList = cuMemberBonusSettle.handleCutList(queryMemberList, MagicNumberConstants.BATCH_QUERY_NUM);
                pkMemberList.forEach(tmpIdList -> {
                    // 查询明细数据（指定阶段，未完成的）
                    List<CuMemberRetailDetail> tmpList = baseMapper.listMemberRetailDetail(retailDetailTableName, tmpIdList, eBonusStage.getValue(), EBonusStageStatus.NO.getValue());
                    packageThreeDetailMap(memberRangeExtMap, eBonusStage, tmpList);
                });
            }
        }
    }

}
