package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.UserRegionMappingDO;
import com.cool.store.enums.DataSourceEnum;
import com.cool.store.mapper.UserRegionMappingMapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: UserRegionMappingDAO
 * @Description:
 * @date 2023-06-08 13:58
 */
@Repository
public class UserRegionMappingDAO {

    @Resource
    private UserRegionMappingMapper userRegionMappingMapper;

    public Integer batchInsertOrUpdateUserRegion(List<UserRegionMappingDO> insertOrUpdateList){
        if(CollectionUtils.isEmpty(insertOrUpdateList)){
            return null;
        }
        return userRegionMappingMapper.batchInsertOrUpdateUserRegion(insertOrUpdateList);
    }

    public Integer deleteUserRegion(String regionId, DataSourceEnum dataSourceEnum, List<String> excludeUserIds){
        if(StringUtils.isBlank(regionId) || Objects.isNull(dataSourceEnum)){
            return null;
        }
        return userRegionMappingMapper.deleteUserRegion(regionId, dataSourceEnum.getCode(), excludeUserIds);
    }


    public Integer deleteUserRegionByUserId(String userId, DataSourceEnum dataSourceEnum, List<String> excludeRegionIds){
        if(StringUtils.isBlank(userId) || Objects.isNull(dataSourceEnum)){
            return null;
        }
        return userRegionMappingMapper.deleteUserRegionByExcludeRegionIds(userId, dataSourceEnum.getCode(), excludeRegionIds);
    }

    public Integer deleteRegionUserByExcludeRegionIds(List<String> excludeRegionIds){
        if(CollectionUtils.isEmpty(excludeRegionIds)){
            return null;
        }
        return userRegionMappingMapper.deleteRegionUserByExcludeRegionIds(excludeRegionIds);
    }

    public Integer deleteRegionUserByExcludeUserIds(List<String> excludeUserIds){
        if(CollectionUtils.isEmpty(excludeUserIds)){
            return null;
        }
        return userRegionMappingMapper.deleteRegionUserByExcludeUserIds(excludeUserIds);
    }

    public Integer deleteUserRegionByUserId(String userId){
        if(StringUtils.isBlank(userId)){
            return CommonConstants.ZERO;
        }
        return userRegionMappingMapper.deleteUserRegionByUserId(userId);
    }

    /**
     * 获取一个区域下直挂的人
     * @param regionId
     * @return
     */
    public List<String> getUserListByRegionId(String regionId){
        if(StringUtils.isBlank(regionId)){
            return Lists.newArrayList();
        }
        return userRegionMappingMapper.getUserListByRegionId(regionId);
    }

    /**
     * 获取一个人所属的部门
     * @param userId
     * @return
     */
    public List<String> getRegionIdsByUserId(String userId){
        if(StringUtils.isBlank(userId)){
            return Lists.newArrayList();
        }
        return userRegionMappingMapper.getRegionIdsByUserId(userId);
    }

    /**
     * 获取用户列表
     * @param regionIds
     * @return
     */
    public List<String> getUserListByRegionIds(List<String> regionIds){
        if(CollectionUtils.isEmpty(regionIds)){
            return Lists.newArrayList();
        }
        return userRegionMappingMapper.getUserListByRegionIds(regionIds);
    }

}
