package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
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.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author zhangchenbiao
 * @FileName: RegionDAO
 * @Description:
 * @date 2023-06-07 14:22
 */
@Repository
public class RegionDAO {

    @Resource
    private RegionMapper regionMapper;

    /**
     * 插入或者更新组织架构
     * @param insertOrUpdateList
     * @return
     */
    public Integer batchInsertOrUpdate(List<RegionDO> insertOrUpdateList){
        if(CollectionUtils.isEmpty(insertOrUpdateList)){
            return CommonConstants.ZERO;
        }
        return regionMapper.batchInsertOrUpdate(insertOrUpdateList);
    }

    public Integer deleteNotExistRegion(List<String> regionIds){
        return regionMapper.deleteNotExistRegion(regionIds);
    }

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

    public List<RegionDO> getRegionBaseInfoList(){
        return regionMapper.getRegionBaseInfoList();
    }

    public Map<String, String> getRegionMap(List<String> regionIds){
        if(CollectionUtils.isEmpty(regionIds)){
            return Maps.newHashMap();
        }
        List<RegionDO> regionList = regionMapper.getRegionNameByRegionIds(regionIds);
        return ListUtils.emptyIfNull(regionList).stream().collect(Collectors.toMap(RegionDO::getRegionId, RegionDO::getName));
    }

    /**
     * 获取区域
     * @param regionId
     * @return
     */
    public RegionDO getRegionInfoByRegionId(String regionId){
        if(StringUtils.isBlank(regionId)){
            return null;
        }
        return regionMapper.getRegionInfoByRegionId(regionId);
    }

    /**
     * 判断一个节点是否是叶子节点
     * @param regionId
     * @return
     */
    public boolean isLeafNode(String regionId){
        Integer subCount = regionMapper.getSubNodeCountByRegionId(regionId);
        return subCount <= CommonConstants.ZERO;
    }

    /**
     * 删除区域
     * @param regionId
     * @return
     */
    public Integer deleteRegionByRegionId(String regionId){
        if(StringUtils.isBlank(regionId)){
            return CommonConstants.ZERO;
        }
        return regionMapper.deleteRegionByRegionId(regionId);
    }

    /**
     * 获取所有的子节点
     * @param leadDeptIds
     */
    public List<String> getSubRegionIds(List<String> leadDeptIds) {
        if(CollectionUtils.isEmpty(leadDeptIds)){
            return Lists.newArrayList();
        }
        List<RegionDO> regionList = getRegionByRegionIds(leadDeptIds);
        List<String> regionPathList = regionList.stream().map(RegionDO::getRegionPath).collect(Collectors.toList());
        return regionMapper.getSubRegionIds(regionPathList);
    }

    /**
     * 获取子部门
     * @param regionId
     * @return
     */
    public List<RegionDO> getSubRegion(String regionId){
        if(StringUtils.isBlank(regionId)){
            return Lists.newArrayList();
        }
        return regionMapper.getSubRegion(regionId);
    }
}
