package com.cool.store.dao;

import com.cool.store.entity.UserRegionMappingDO;
import com.cool.store.mapper.UserRegionMappingMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangchenbiao
 * @FileName: UserRegionMappingDAO
 * @Description:
 * @date 2022-03-03 9:50
 */
@Service
public class UserRegionMappingDAO {

    @Autowired
    private UserRegionMappingMapper userRegionMappingMapper;

    /**
     * 根据人员id列表查询人员所属部门
     *
     * @param userIds
     * @return
     */
    public List<UserRegionMappingDO> listUserRegionMappingByUserId( List<String> userIds){
        return userRegionMappingMapper.listUserRegionMappingByUserId( userIds);
    }


    /**
     * 查询部门下的人员
     *
     * @param regionIds
     * @return
     */
    public List<UserRegionMappingDO> selectUserListByRegionIds( List<String> regionIds){
       return userRegionMappingMapper.selectUserListByRegionIds(regionIds);
    }

    /**
     * 获取区域的直连人员数量
     *
     * @param regionIds
     * @return
     */
    public List<HashMap<String,Long>> getRegionUserCount(List<String> regionIds){
        return userRegionMappingMapper.getRegionUserCount(regionIds);
    }

    /**
     * 根据Userids和regionids查询数据
     *
     * @param userIds
     * @param regionIds
     * @return
     */
    public List<UserRegionMappingDO> listByUserIdsAndRegionIds(List<String> userIds,List<String> regionIds){
        return userRegionMappingMapper.listByUserIdsAndRegionIds(userIds,regionIds);
    }

    /**
     * 获取用户所在的部门
     *
     * @param userIds
     * @return
     */
    public List<UserRegionMappingDO> getRegionIdsByUserIds( List<String> userIds){
        if(CollectionUtils.isEmpty(userIds)){
            return Lists.newArrayList();
        }
        return userRegionMappingMapper.getRegionIdsByUserIds( userIds);
    }

    public List<String> getRegionIdsByUserId( String userId){
        if(StringUtils.isAnyBlank( userId)){
            return Lists.newArrayList();
        }
        List<UserRegionMappingDO> regionMappings = getRegionIdsByUserIds( Arrays.asList(userId));
        return ListUtils.emptyIfNull(regionMappings).stream().map(UserRegionMappingDO::getRegionId).distinct().collect(Collectors.toList());
    }

    /**
     * 获取部门有哪些用户
     *
     * @param regionIds
     * @return
     */
    public List<String> getUserIdsByRegionIds( List<String> regionIds){
        return userRegionMappingMapper.getUserIdsByRegionIds( regionIds);
    }
}
