All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.didiglobal.logi.security.dao.impl.UserRoleDaoImpl Maven / Gradle / Ivy

Go to download

logi-security 提供项目大多都需要的一些基础功能(用户、角色、权限、登录、注册、操作记录)

There is a newer version: 2.10.19
Show newest version
package com.didiglobal.logi.security.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.didiglobal.logi.security.common.constant.FieldConstant;
import com.didiglobal.logi.security.common.entity.UserRole;
import com.didiglobal.logi.security.common.po.UserRolePO;
import com.didiglobal.logi.security.dao.UserRoleDao;
import com.didiglobal.logi.security.dao.mapper.UserRoleMapper;
import com.didiglobal.logi.security.util.CopyBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cjm
 */
@Component
public class UserRoleDaoImpl extends BaseDaoImpl implements UserRoleDao {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List selectUserIdListByRoleId(Integer roleId) {
        if(roleId == null) {
            return new ArrayList<>();
        }
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.select( FieldConstant.USER_ID).eq(FieldConstant.ROLE_ID, roleId);
        List userIdList = userRoleMapper.selectObjs(queryWrapper);

        if(CollectionUtils.isEmpty(userIdList)){
            return new ArrayList<>();
        }

        return userIdList.stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public List selectRoleIdListByUserId(Integer userId) {
        if(userId == null) {
            return new ArrayList<>();
        }
        QueryWrapper userRoleWrapper = getQueryWrapperWithAppName();
        userRoleWrapper.select(FieldConstant.ROLE_ID).eq(FieldConstant.USER_ID, userId);
        List roleIdList = userRoleMapper.selectObjs(userRoleWrapper);
        return roleIdList.stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public void insertBatch(List userRoleList) {
        if(!CollectionUtils.isEmpty(userRoleList)) {
            List userRolePOList = CopyBeanUtil.copyList(userRoleList, UserRolePO.class);
            for(UserRolePO userRolePO : userRolePOList) {
                userRolePO.setAppName(logiSecurityProper.getAppName());
                userRoleMapper.insert(userRolePO);
            }
        }
    }

    @Override
    public void deleteByUserIdOrRoleId(Integer userId, Integer roleId) {
        if(userId == null && roleId == null) {
            return;
        }
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper
                .eq(userId != null, FieldConstant.USER_ID, userId)
                .eq(roleId != null, FieldConstant.ROLE_ID, roleId);
        userRoleMapper.delete(queryWrapper);
    }

    @Override
    public int selectCountByRoleId(Integer roleId) {
        if(roleId == null) {
            return 0;
        }
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.eq(FieldConstant.ROLE_ID, roleId);
        return userRoleMapper.selectCount(queryWrapper);
    }
}