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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.mapper.CuMemberRetailRangeMapper;
import com.hzs.bonus.achieve.service.ICuMemberRetailRangeService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.domain.member.achieve.CuMemberRetailDetail;
import com.hzs.common.domain.member.achieve.CuMemberRetailRange;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 服务实现类
 */
@Service
public class CuMemberRetailRangeServiceImpl extends ServiceImpl<CuMemberRetailRangeMapper, CuMemberRetailRange> implements ICuMemberRetailRangeService {

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;

    @Override
    public void mergeMemberRetailRangeByYesterday(String rangeTableName, String yesterdayRangeTableName, Boolean isSameMonth) {
        // 当天数据全部更新为0
        baseMapper.mergeMemberRetailRangeInit(rangeTableName);
        if (isSameMonth) {
            // 结算期间和结算期间前一期是同月
            baseMapper.mergeMemberRetailRangeByYesterday(rangeTableName, yesterdayRangeTableName, 0);
        } else {
            baseMapper.mergeMemberRetailRangeByYesterday(rangeTableName, yesterdayRangeTableName, 1);
        }
    }

    @Override
    public void mergeMemberRetailRangeDetailByYesterday(String detailTableName, String yesterdayDetailTableName) {
        // 清空当前明细表
        iCuMemberTreeService.truncateCuMemberTreeTable(detailTableName);
        // 用昨天的数据初始化当前明细表
        baseMapper.mergeMemberRetailRangeDetailByYesterday(detailTableName, yesterdayDetailTableName);
    }

    @Override
    public List<CuMemberRetailRangeExt> queryCuMemberRetailRangeParent(String tableName, List<SaOrderExt> saOrderExtList) {
        List<CuMemberRetailRangeExt> memberRetailRangeExtList = new ArrayList<>();
        Set<Long> orderIdSet = new HashSet<>();
        for (SaOrderExt saOrderExt : saOrderExtList) {
            orderIdSet.add(saOrderExt.getPkMember());
        }
        List<List<?>> cutOrderList = cuMemberBonusSettle.handleCutList(orderIdSet, MagicNumberConstants.BATCH_QUERY_NUM);
        cutOrderList.forEach(orderList -> {
            List<CuMemberRetailRangeExt> rangeExtList = baseMapper.queryCuMemberRetailRangeParent(tableName, orderList);
            if (rangeExtList.size() > 0) {
                memberRetailRangeExtList.addAll(rangeExtList);
            }
        });
        return memberRetailRangeExtList;
    }

    @Override
    public List<CuMemberRetailRangeExt> queryCuMemberRetailRangeExtByParent(String tableName, Long pkParent) {
        return baseMapper.queryCuMemberRetailRangeExtByParent(tableName, pkParent);
    }

    @Override
    public List<CuMemberRetailRangeExt> queryCuMemberRetailRangeEnoughAchieve(String tableName, Long pkParent, BigDecimal achievePv) {
        return baseMapper.queryCuMemberRetailRangeEnoughAchieve(tableName, pkParent, achievePv);
    }

    @Override
    public List<CuMemberRetailRangeExt> queryCuMemberSecondRangeParent(String rangeTableName, String secondTableName, Long pkMember) {
        // 查询伞上
        return baseMapper.queryCuMemberSecondRangeParent(rangeTableName, secondTableName, pkMember);
    }

    @Override
    public List<CuMemberRetailRangeExt> queryCuMemberSecondRangeExtByParent(String rangeTableName, String secondTableName, Long pkMember) {
        return baseMapper.queryCuMemberSecondRangeExtByParent(rangeTableName, secondTableName, pkMember);
    }

    @Override
    public void updateCuMemberRetailRange(String settleTableName, Map<Long, CuMemberRetailRangeExt> cuMemberTreeMap) {
        List<List<?>> treeList = setUpdCuMemberTreeLists(cuMemberTreeMap);
        treeList.forEach(list -> baseMapper.mergeCuMemberRetailRangeExt(settleTableName, list));
    }

    @Override
    public void updateCuMemberRetailDetail(String rangeDetailTableName, Map<Long, CuMemberRetailRangeExt> memberRangeExtMap) {
        Collection<CuMemberRetailDetail> retailDetailColl = new ArrayList<>();
        memberRangeExtMap.forEach((key, value) -> {
            if (null != value.getOneDetailMap() && value.getOneDetailMap().size() > 0) {
                Collection<CuMemberRetailDetail> tmpDetailColl = value.getOneDetailMap().values();
                for (CuMemberRetailDetail cuMemberRetailDetail : tmpDetailColl) {
                    // 只有需要更新的才进行入库操作
                    if (null != cuMemberRetailDetail.getUpdateBool()) {
                        retailDetailColl.add(cuMemberRetailDetail);
                    }
                }
            }
        });
        List<List<?>> list = cuMemberBonusSettle.handleCutList(retailDetailColl);
        list.forEach(tmpList -> {
            if (CollectionUtil.isNotEmpty(tmpList)) {
                baseMapper.mergeCuMemberRetailDetail(rangeDetailTableName, tmpList);
            }
        });
    }

    @Override
    public void mergeMemberSecondRetailRangeByRange(String rangeTableName, String secondRangeTableName, Long pkMember) {
        // 当前网体更新极差秒接表（血缘伞上）
        baseMapper.mergeMemberSecondRetailRangeByRange(secondRangeTableName, pkMember);
        // 昨天的结算表更新极差秒结（激活状态和等级）
        baseMapper.mergeMemberSecondRetailRangeByRangeEnable(rangeTableName, secondRangeTableName, pkMember);
    }

    @Override
    public void mergeMemberRetailGradeByMember(String rangeTableName, Long pkMember, Integer isToday) {
        baseMapper.mergeMemberRetailGradeByMember(rangeTableName, pkMember, isToday);
    }

    @Override
    public void updateCuMemberSecondRange(String secondTableName, Map<Long, CuMemberRetailRangeExt> cuMemberTreeMap) {
        List<List<?>> treeList = setUpdCuMemberTreeLists(cuMemberTreeMap);
        treeList.forEach(list -> baseMapper.mergeCuMemberSecondRange(secondTableName, list));
    }

    @Override
    public List<CuMemberExt> findParentMemberList(Long pkMember) {
        return baseMapper.findParentMemberList(pkMember);
    }

    @Override
    public void updateMemberRetailSecondRangeSelf(String settleTableName, SaOrderExt orderExt, CuMember cuMember, Integer updateType, String symbol) {
        baseMapper.updateMemberRetailSecondRangeSelf(settleTableName, orderExt, cuMember, updateType, symbol);
    }

    @Override
    public void updateMemberRetailSecondRangeParent(String settleTableName, SaOrderExt orderExt, List<CuMemberExt> pkMemberList, Integer updateType, String symbol) {
        baseMapper.updateMemberRetailSecondRangeParent(settleTableName, orderExt, pkMemberList, updateType, symbol);
    }

    @Override
    public void initMemberRetailSecondRange(String tableName, CuMemberExt member) {
        baseMapper.initMemberRetailSecondRange(tableName, member);
    }

    private List<List<?>> setUpdCuMemberTreeLists(Map<Long, CuMemberRetailRangeExt> cuMemberTreeMap) {
        List<CuMemberRetailRangeExt> cuMemberTreeList = new ArrayList<>();
        cuMemberTreeMap.forEach((key, value) -> cuMemberTreeList.add(value));
        return cuMemberBonusSettle.handleCutList(cuMemberTreeList);
    }
}
