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

com.didiglobal.logi.security.dao.impl.UserProjectDaoImpl 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.UserProject;
import com.didiglobal.logi.security.common.po.UserProjectPO;
import com.didiglobal.logi.security.dao.UserProjectDao;
import com.didiglobal.logi.security.dao.mapper.UserProjectMapper;
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 UserProjectDaoImpl extends BaseDaoImpl implements UserProjectDao {

    @Autowired
    private UserProjectMapper userProjectMapper;

    @Override
    public List selectUserIdListByProjectId(Integer projectId, int type) {
        if(projectId == null) {
            return new ArrayList<>();
        }
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.select(FieldConstant.USER_ID);
        queryWrapper.eq(FieldConstant.PROJECT_ID, projectId);
        queryWrapper.eq(FieldConstant.USER_TYPE, type);
        List userIdList = userProjectMapper.selectObjs(queryWrapper);
        return userIdList.stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public List selectProjectIdListByUserIdList(List userIdList) {
        if(CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>();
        }
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.select(FieldConstant.PROJECT_ID).in(FieldConstant.USER_ID, userIdList);
        List projectIdList = userProjectMapper.selectObjs(queryWrapper);
        return projectIdList.stream().map(Integer.class::cast).collect(Collectors.toList());
    }

    @Override
    public void insertBatch(List userProjectList) {
        if(!CollectionUtils.isEmpty(userProjectList)) {
            for(UserProject project : userProjectList){
                UserProjectPO userProjectPO = getByProjectAndUserId(project);
                if(null == userProjectPO){
                    addUserProject(project);
                }else {
                    updateUserProject(userProjectPO.getId(), project);
                }
            }
        }
    }

    @Override
    public int deleteUserProject(List userProjectList){
        int delNu = 0;
        if(!CollectionUtils.isEmpty(userProjectList)) {
            for(UserProject userProject : userProjectList){
                QueryWrapper queryWrapper = getQueryWrapperWithAppName();
                queryWrapper.eq(FieldConstant.PROJECT_ID, userProject.getProjectId());
                queryWrapper.eq(FieldConstant.USER_ID, userProject.getUserId());
                delNu += userProjectMapper.delete(queryWrapper);
            }
        }

        return delNu;
    }

    @Override
    public void deleteByProjectId(Integer projectId) {
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.eq(FieldConstant.PROJECT_ID, projectId);
        userProjectMapper.delete(queryWrapper);
    }

    /**************************************************** private method ****************************************************/

    private int addUserProject(UserProject userProject){
        UserProjectPO userProjectPO = CopyBeanUtil.copy(userProject, UserProjectPO.class);
        userProjectPO.setAppName(logiSecurityProper.getAppName());

        return userProjectMapper.insert(userProjectPO);
    }

    private UserProjectPO getByProjectAndUserId(UserProject userProject){
        QueryWrapper queryWrapper = getQueryWrapperWithAppName();
        queryWrapper.eq(FieldConstant.PROJECT_ID, userProject.getProjectId());
        queryWrapper.eq(FieldConstant.USER_ID, userProject.getUserId());

        return userProjectMapper.selectOne(queryWrapper);
    }

    private int updateUserProject(int id, UserProject userProject){
        UserProjectPO userProjectPO = CopyBeanUtil.copy(userProject, UserProjectPO.class);
        userProjectPO.setId(id);
        return userProjectMapper.updateById(userProjectPO);
    }
}