package com.cool.store.dao;

import com.cool.store.dto.RegionNode;
import com.cool.store.entity.RegionDO;
import com.cool.store.mapper.RegionMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shoul
 * @ClassName RegionDao
 * @Description 用一句话描述什么
 */
@Repository
public class RegionDao {

    @Resource
    private RegionMapper regionMapper;


    public RegionNode getRegionByRegionId( String regionId) {
        return regionMapper.getRegionByRegionId( regionId);
    }

    public List<RegionDO> getRegionByRegionIds( List<String> regionIds) {
        if (CollectionUtils.isEmpty(regionIds)) {
            return Collections.emptyList();
        }
        return regionMapper.getRegionByRegionIds( regionIds);
    }


    public RegionNode getRootRegion(String eid) {
        return regionMapper.getRegionByRegionId( "1");
    }


    /**
     * 获取区域根据区域id列表（包含删除）
     *
     *        区域id
     * @param regionIds 区域id列表
     * @return List<RegionDO>
     */
    public List<RegionDO> getAllRegionByRegionIds( List<Long> regionIds) {
        if (CollectionUtils.isEmpty(regionIds)) {
            return Collections.emptyList();
        }
        return regionMapper.getByIds( regionIds);
    }

    /**
     * 获取区域根据区域id（包含删除）
     *
     *
     * @param regionId
     * @return
     */
    public RegionDO getRegionById( Long regionId) {
        if ( Objects.isNull(regionId)) {
            return null;
        }
        return regionMapper.getByRegionId( regionId);
    }

    /**
     * 根据id查询
     * @param id           区域id
     * @return RegionDO
     */
    public RegionDO selectById(Long id) {
        if(Objects.isNull(id)) {
            return null;
        }
        return regionMapper.getByRegionId( id);
    }

    /**
     * 获取所有的区域包含删除区域
     *
     *
     * @return
     */
    public List<RegionDO> getRegionsByEid( Long regionId) {
        return regionMapper.getRegionsByEid( regionId);
    }


    public List<RegionDO> getSubRegion( Long parentId) {
        if ( Objects.isNull(parentId)) {
            return Lists.newArrayList();
        }
        return regionMapper.getSubRegion( parentId);
    }

    public List<RegionDO> getRegionList( List<String> regionIds) {
        if ( CollectionUtils.isEmpty(regionIds)) {
            return Lists.newArrayList();
        }
        return regionMapper.getRegionByRegionIds( regionIds);
    }


    public RegionDO getByRegionIdExcludeDeleted( Long regionId) {
        if ( Objects.isNull(regionId)) {
            return null;
        }
        return regionMapper.getByRegionIdExcludeDeleted( regionId);
    }

    public List<String> getSubIdsByRegionIds( List<String> regionIds) {
        if (CollectionUtils.isEmpty(regionIds)) {
            return Lists.newArrayList();
        }
        List<String> regionPaths = regionMapper.getRegionPathByRegionIds( regionIds);
        regionPaths = regionPaths.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(regionPaths)) {
            return Lists.newArrayList();
        }
        return regionMapper.getSubIdsByRegionIds( regionPaths);
    }

    /**
     * 根据regionIds 的  全路径查下级
     *
     *
     * @param regionIds
     * @return
     */
    public List<String> listSubIdsByRegionIds( List<String> regionIds) {
        if (CollectionUtils.isEmpty(regionIds)) {
            return Lists.newArrayList();
        }
        List<Long> regionIdList = regionIds.stream()
                .map(a -> Long.valueOf(a)).collect(Collectors.toList());
        List<RegionDO> regionDOList = regionMapper.getRegionPathByIds( regionIdList);
        List<String> fullRegionPathList = ListUtils.emptyIfNull(regionDOList)
                .stream()
                .map(RegionDO::getFullRegionPath)
                .collect(Collectors.toList());
        fullRegionPathList = fullRegionPathList.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(fullRegionPathList)) {
            return Lists.newArrayList();
        }
        return regionMapper.getSubIdsByRegionIds( fullRegionPathList);
    }


    public Map<Long, String> getRegionNameMap(List<Long> regionIds) {
        List<RegionDO> regionList = getAllRegionByRegionIds( regionIds);
        if (CollectionUtils.isEmpty(regionList)) {
            return Maps.newHashMap();
        }
        return regionList.stream().collect(Collectors.toMap(k -> k.getId(), v -> v.getName(), (k1, k2) -> k1));
    }

    public List<RegionDO> getCompRegionByRegionIds(List<Long> compParentIdList) {
        return regionMapper.getCompRegionByRegionIds( compParentIdList);
    }


    public List<RegionDO> getRegionByParentIds(List<String> regionIdList) {
        return regionMapper.getRegionByParentIds(regionIdList);
    }

    public List<RegionDO> listByThirdRegionType(Long parentId, String thirdRegionType) {
        return regionMapper.listByThirdRegionType(parentId, thirdRegionType);
    }

    public List<RegionDO> getFightRegionByRegionIds(List<String> regionIds) {
        return regionMapper.getFightRegionByRegionIds(regionIds);
    }

    public RegionDO getRegionByStoreId(String storeId) {
        if (Objects.isNull(storeId)) {
            return null;
        }
        return regionMapper.getRegionByStoreId(storeId);
    }
}
