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

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.base.service.ICuMemberService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.domain.member.achieve.CuMemberRetailRange;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt;
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;

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

    @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 mergeCuMemberRetailRangeBySpecial(String settleTableName, Integer period) {
        baseMapper.mergeCuMemberRetailRangeBySpecial(settleTableName, period);
    }

    @Override
    public void mergeCuMemberRetailBackBoxBySpecial(String rangeTableName, String beforeTableName, Integer period) {
        baseMapper.mergeCuMemberRetailBackBoxBySpecial(rangeTableName, beforeTableName, period);
    }

    @Override
    public List<CuMemberRetailRangeExt> batchQueryMemberRetailRangeByMemberPk(String settleTableName, Set<Long> serviceMemberSet) {
        List<CuMemberRetailRangeExt> cuMemberSettleExtList = new ArrayList<>();
        if (serviceMemberSet.size() > 0) {
            // 做一下排序
            List<Long> memberPkList = Arrays.asList(serviceMemberSet.toArray(new Long[]{}));
            Collections.sort(memberPkList);
            List<List<?>> listList = cuMemberBonusSettle.handleCutList(memberPkList, MagicNumberConstants.BATCH_QUERY_NUM);
            listList.forEach(list ->
                    cuMemberSettleExtList.addAll(baseMapper.batchQueryMemberRetailRangeByMemberPk(settleTableName, list)));
        }
        return cuMemberSettleExtList;
    }

    @Override
    public List<CuMemberRetailRangeExt> batchQueryMemberRetailRangeByAwards(String rangeTableName) {
        return baseMapper.batchQueryMemberRetailRangeByAwards(rangeTableName);
    }

    @Override
    public List<CuMemberRetailRangeExt> batchQueryMemberRetailRangeByParent(String rangeTableName, List<CuMemberRetailRangeExt> memberRetailRangeExtList) {
        return baseMapper.batchQueryMemberRetailRangeByParent(rangeTableName, memberRetailRangeExtList);
    }

    @Override
    public List<CuMemberRetailRangeExt> batchQueryMemberRetailRangeByCounty(String rangeTableName) {
        return baseMapper.batchQueryMemberRetailRangeByCounty(rangeTableName);
    }

    @Override
    public List<CuMemberRetailRangeExt> batchQueryMemberRetailSecondRangeByCounty(String rangeTableName, String secondRangeTableName, Integer county) {
        return baseMapper.batchQueryMemberRetailSecondRangeByCounty(rangeTableName, secondRangeTableName, county);
    }

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

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