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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.mapper.CuMemberRangeMapper;
import com.hzs.bonus.achieve.service.ICuMemberRangeService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.domain.member.achieve.CuMemberRange;
import com.hzs.common.domain.member.achieve.ext.CuMemberRangeExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class CuMemberRangeServiceImpl extends ServiceImpl<CuMemberRangeMapper, CuMemberRange> implements ICuMemberRangeService {

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    public void mergeMemberSecondRangeByRange(String rangeTableName, String secondRangeTableName, Long pkMember) {
        baseMapper.mergeMemberSecondRangeByRange(rangeTableName, secondRangeTableName, pkMember);
    }

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

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

    @Override
    public Map<Long, List<CuMemberRangeExt>> queryCuMemberRangeParent(String tableName, List<SaOrderExt> saOrderExtList) {
        List<CuMemberRangeExt> cuMemberRangeExtList = new ArrayList<>();
        List<List<?>> cutOrderList = cuMemberBonusSettle.handleCutList(saOrderExtList, MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        cutOrderList.forEach(orderList ->
                cuMemberRangeExtList.addAll(baseMapper.queryCuMemberRangeParent(tableName, orderList)));
        return packageMemberRangeMap(cuMemberRangeExtList);
    }

    private Map<Long, List<CuMemberRangeExt>> packageMemberRangeMap(List<CuMemberRangeExt> cuMemberRangeExtList) {
        Map<Long, List<CuMemberRangeExt>> memberRangeMap = new HashMap<>();
        for (CuMemberRangeExt cuMemberRangeExt : cuMemberRangeExtList) {
            if (memberRangeMap.containsKey(cuMemberRangeExt.getPkParent())) {
                memberRangeMap.get(cuMemberRangeExt.getPkParent()).add(cuMemberRangeExt);
            } else {
                List<CuMemberRangeExt> memberRangeExtList = new ArrayList<>();
                memberRangeExtList.add(cuMemberRangeExt);
                memberRangeMap.put(cuMemberRangeExt.getPkParent(), memberRangeExtList);
            }
        }
        return memberRangeMap;
    }

    @Override
    public Map<Long, List<CuMemberRangeExt>> queryCuMemberSecondRangeParent(String rangeTableName, String secondTableName, Long pkMember) {
        List<CuMemberRangeExt> cuMemberRangeExtList = baseMapper.queryCuMemberSecondRangeParent(rangeTableName, secondTableName, pkMember);
        return packageMemberRangeMap(cuMemberRangeExtList);
    }

    @Override
    public List<CuMemberRangeExt> queryCuMemberRangeExtByParent(String tableName, Long pkParent, Date orderDate) {
        return baseMapper.queryCuMemberRangeExtByParent(tableName, pkParent, orderDate);
    }

    @Override
    public List<CuMemberRangeExt> querySecondCuMemberRangeExtByParent(String tableName, String secondTableName, Long pkParent) {
        return baseMapper.querySecondCuMemberRangeExtByParent(tableName, secondTableName, pkParent);
    }

    @Override
    public void mergeCuMemberRangeExt(String rangeTableName, List<CuMemberRangeExt> cuMemberRangeExtList) {
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(cuMemberRangeExtList);
        for (List<?> list : lists) {
            baseMapper.mergeCuMemberRangeExt(rangeTableName, list);
        }
    }

    @Override
    public void mergeSecondCuMemberRangeExt(String secondTableName, List<CuMemberRangeExt> cuMemberRangeExtList) {
        baseMapper.mergeSecondCuMemberRangeExt(secondTableName, cuMemberRangeExtList);
    }

    @Override
    public Integer validateCuMemberPlaceAwards(String rangeTableName, List<CuMemberRangeExt> cuMemberRangeExtList, Integer pkRangeAwards, Date orderDate) {
        return baseMapper.validateCuMemberPlaceAwards(rangeTableName, cuMemberRangeExtList, pkRangeAwards, orderDate);
    }

    @Override
    public Integer validateSecondCuMemberPlaceAwards(String secondTableName, List<CuMemberRangeExt> cuMemberRangeExtList, Integer pkRangeAwards) {
        return baseMapper.validateSecondCuMemberPlaceAwards(secondTableName, cuMemberRangeExtList, pkRangeAwards);
    }
}
