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

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

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.didiglobal.logi.security.common.dto.resource.ControlLevelQueryDTO;
import com.didiglobal.logi.security.common.dto.resource.UserResourceQueryDTO;
import com.didiglobal.logi.security.common.entity.UserResource;
import com.didiglobal.logi.security.common.enums.resource.ControlLevelCode;
import com.didiglobal.logi.security.common.po.UserResourcePO;
import com.didiglobal.logi.security.dao.UserResourceDao;
import com.didiglobal.logi.security.dao.mapper.UserResourceMapper;
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.List;
import java.util.stream.Collectors;

/**
 * @author cjm
 */
@Component
public class UserResourceDaoImpl extends BaseDaoImpl implements UserResourceDao {

    @Autowired
    private UserResourceMapper userResourceMapper;

    /**
     * 封装UserResource的查询条件
     * @param userId 用户id
     * @param queryDTO 查询条件
     * @return QueryWrapper
     */
    private QueryWrapper wrapQueryCriteria(Integer userId, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = getQueryWrapper();
        Integer resourceTypeId = queryDTO.getResourceTypeId();
        queryWrapper
                .eq(FieldConstant.CONTROL_LEVEL, queryDTO.getControlLevel())
                .eq(userId != null, FieldConstant.USER_ID, userId)
                .eq(queryDTO.getProjectId() != null, FieldConstant.PROJECT_ID, queryDTO.getProjectId())
                .eq(resourceTypeId != null, FieldConstant.RESOURCE_TYPE_ID, resourceTypeId)
                .eq(queryDTO.getResourceId() != null, FieldConstant.RESOURCE_ID, queryDTO.getResourceId());
        return queryWrapper;
    }

    @Override
    public int selectCountByUserId(Integer userId, UserResourceQueryDTO queryDTO) {
        return userResourceMapper.selectCount(wrapQueryCriteria(userId, queryDTO));
    }

    @Override
    public void deleteByUserId(Integer userId, UserResourceQueryDTO queryDTO) {
        userResourceMapper.delete(wrapQueryCriteria(userId, queryDTO));
    }

    @Override
    public void deleteByControlLevel(ControlLevelCode controlLevel) {
        QueryWrapper queryWrapper = getQueryWrapper();
        queryWrapper.eq(FieldConstant.CONTROL_LEVEL, controlLevel.getType());
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void insert(UserResource userResource) {
        userResourceMapper.insert(CopyBeanUtil.copy(userResource, UserResourcePO.class));
    }

    @Override
    public void insertBatch(List userResourceList) {
        if(CollectionUtils.isEmpty(userResourceList)) {
            return;
        }
        List userResourcePOList = CopyBeanUtil.copyList(userResourceList, UserResourcePO.class);
        for(UserResourcePO userResourcePO : userResourcePOList) {
            userResourceMapper.insert(userResourcePO);
        }

    }

    @Override
    public void deleteByUserIdList(List userIdList, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.in(FieldConstant.USER_ID, userIdList);
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByProjectIdList(List projectIdList, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.in(FieldConstant.PROJECT_ID, projectIdList);
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByResourceTypeIdList(List resourceTypeIdList, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.in(FieldConstant.RESOURCE_TYPE_ID, resourceTypeIdList);
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByResourceIdList(List resourceIdList, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.in(FieldConstant.RESOURCE_ID, resourceIdList);
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public int selectCountByUserIdAndControlLevel(Integer userId, ControlLevelCode controlLevel) {
        QueryWrapper queryWrapper = getQueryWrapper();
        queryWrapper
                .eq(userId != null, FieldConstant.USER_ID, userId)
                .eq(FieldConstant.CONTROL_LEVEL, controlLevel.getType());
        return userResourceMapper.selectCount(queryWrapper);
    }

    @Override
    public int selectCount(UserResourceQueryDTO queryDTO) {
        return userResourceMapper.selectCount(wrapQueryCriteria(null, queryDTO));
    }

    @Override
    public List selectResourceIdListByUserId(Integer userId, UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(userId, queryDTO);
        queryWrapper.select(FieldConstant.RESOURCE_ID);
        List resourceIdList = userResourceMapper.selectObjs(queryWrapper);
        return resourceIdList.stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public void deleteWithoutUserIdList(UserResourceQueryDTO queryDTO, List excludeUserIdList) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        if(!CollectionUtils.isEmpty(excludeUserIdList)) {
            queryWrapper.notIn(FieldConstant.USER_ID, excludeUserIdList);
        }
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByUserIdWithoutProjectIdList(Integer userId, UserResourceQueryDTO queryDTO,
                                                   List excludeIdList) {
        QueryWrapper queryWrapper = wrapQueryCriteria(userId, queryDTO);
        if(!CollectionUtils.isEmpty(excludeIdList)) {
            queryWrapper.notIn(FieldConstant.PROJECT_ID, excludeIdList);
        }
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByUserIdWithoutResourceTypeIdList(Integer userId, UserResourceQueryDTO queryDTO,
                                                        List excludeIdList) {
        QueryWrapper queryWrapper = wrapQueryCriteria(userId, queryDTO);
        if(!CollectionUtils.isEmpty(excludeIdList)) {
            queryWrapper.notIn(FieldConstant.RESOURCE_TYPE_ID, excludeIdList);
        }
        userResourceMapper.delete(queryWrapper);
    }

    @Override
    public int selectCountGroupByUserId(UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.select("COUNT(*)").groupBy(FieldConstant.USER_ID);
        return userResourceMapper.selectObjs(queryWrapper).size();
    }

    @Override
    public List selectUserIdListGroupByUserId(UserResourceQueryDTO queryDTO) {
        QueryWrapper queryWrapper = wrapQueryCriteria(null, queryDTO);
        queryWrapper.select(FieldConstant.USER_ID).groupBy(FieldConstant.USER_ID);
        // 查询后,List -> List
        return userResourceMapper.selectObjs(queryWrapper)
                .stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public Integer selectControlLevel(ControlLevelQueryDTO queryDTO) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper
                .select("MAX(" + FieldConstant.CONTROL_LEVEL + ")")
                .eq(FieldConstant.USER_ID, queryDTO.getUserId())
                .eq(FieldConstant.PROJECT_ID, queryDTO.getProjectId())
                .eq(FieldConstant.RESOURCE_TYPE_ID, queryDTO.getResourceTypeId())
                .eq(FieldConstant.RESOURCE_ID, queryDTO.getResourceId());
        List objects = userResourceMapper.selectObjs(queryWrapper);
        return (Integer) objects.get(0);
    }
}