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.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.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 java.util.*;

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

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    public void listMemberRetailDetail(String retailDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap, EBonusStage eBonusStage) {
        List<List<?>> pkMemberList = cuMemberBonusSettle.handleCutList(memberRangeExtMap.keySet(), MagicNumberConstants.BATCH_QUERY_NUM);
        pkMemberList.forEach(tmpIdList -> {
            // 查询明细数据（指定阶段，未完成的）
            List<CuMemberRetailDetail> tmpList = baseMapper.listMemberRetailDetail(retailDetailTableName, tmpIdList, eBonusStage.getValue());
            if (CollectionUtil.isNotEmpty(tmpList)) {
                // 明细数据放到对应的会员数据中
                for (CuMemberRetailDetail cuMemberRetailDetail : tmpList) {
                    CuMemberRetailRangeExt cuMemberRetailRangeExt = memberRangeExtMap.get(cuMemberRetailDetail.getPkMember());
                    if (null == cuMemberRetailRangeExt.getOneDetailMap()) {
                        cuMemberRetailRangeExt.setOneDetailMap(new LinkedHashMap<>());
                    }
                    if (null == cuMemberRetailRangeExt.getTwoDetailMap()) {
                        cuMemberRetailRangeExt.setTwoDetailMap(new LinkedHashMap<>());
                    }
                    if (null == cuMemberRetailRangeExt.getThreeDetailMap()) {
                        cuMemberRetailRangeExt.setThreeDetailMap(new LinkedHashMap<>());
                    }
                    if (EBonusStage.ONE.getValue() == eBonusStage.getValue()) {
                        // 阶段一点位列表（使用有序map，为了后续业务处理方便）
                        cuMemberRetailRangeExt.getOneDetailMap().put(cuMemberRetailDetail.getChildNode(), cuMemberRetailDetail);
                    }
                }
            }
        });
    }

    @Override
    public void listMemberRetailDetail(String retailDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap,
                                       List<Long> queryMemberList, EBonusStage eBonusStage) {
        // 查询明细数据（指定阶段，未完成的）
        List<CuMemberRetailDetail> tmpDetailList = baseMapper.listMemberRetailDetail(retailDetailTableName, queryMemberList, eBonusStage.getValue());
        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()) {
                        cuMemberRetailRangeExt.setOneDetailMap(new LinkedHashMap<>());
                    }
                    cuMemberRetailRangeExt.getOneDetailMap().put(cuMemberRetailDetail.getChildNode(), cuMemberRetailDetail);
                }
            }
        }
    }

}
