package jnpf.permission.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.toolkit.JoinWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.google.common.collect.Lists; import jnpf.base.ActionResult; import jnpf.base.service.SuperServiceImpl; import jnpf.constant.MsgCode; import jnpf.constant.PermissionConst; import jnpf.permission.entity.*; import jnpf.permission.mapper.RoleRelationMapper; import jnpf.permission.model.position.PosConModel; import jnpf.permission.model.rolerelaiton.*; import jnpf.permission.service.*; import jnpf.util.JsonUtil; import jnpf.util.RandomUtil; import jnpf.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; /** * 用户关系 * * @author JNPF开发平台组 * @version V3.1.0 * @copyright 引迈信息技术有限公司 * @date 2019年9月26日 上午9:18 */ @Service public class RoleRelationServiceImpl extends SuperServiceImpl implements RoleRelationService { @Autowired private UserService userService; @Autowired private OrganizeService organizeService; @Autowired private PositionService positionService; @Autowired private UserRelationService userRelationService; @Autowired private RoleService roleService; @Override public List getListPage(RoleListPage pagination) { String objectType = StringUtil.isNotEmpty(pagination.getPositionId()) ? PermissionConst.POSITION : PermissionConst.ORGANIZE; String objectId = StringUtil.isNotEmpty(pagination.getPositionId()) ? pagination.getPositionId() : pagination.getOrganizeId(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(RoleRelationEntity::getObjectId, objectId); queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, objectType); queryWrapper.lambda().orderByDesc(RoleRelationEntity::getCreatorTime).orderByDesc(RoleRelationEntity::getId); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage iPage = this.page(page, queryWrapper); return pagination.setData(iPage.getRecords(), iPage.getTotal()); } @Override public List getUserPage(RoleRelationPage pagination) { MPJLambdaWrapper queryWrapper = JoinWrappers.lambda(RoleRelationEntity.class); queryWrapper.selectAs(UserEntity::getId, RoleRelationUserVo::getId); queryWrapper.selectAs(UserEntity::getAccount, RoleRelationUserVo::getAccount); queryWrapper.selectAs(UserEntity::getRealName, RoleRelationUserVo::getRealName); queryWrapper.selectAs(UserEntity::getGender, RoleRelationUserVo::getGender); queryWrapper.selectAs(UserEntity::getMobilePhone, RoleRelationUserVo::getMobilePhone); queryWrapper.selectAs(UserEntity::getEnabledMark, RoleRelationUserVo::getEnabledMark); queryWrapper.leftJoin(UserEntity.class, UserEntity::getId, RoleRelationEntity::getObjectId); if (!StringUtil.isEmpty(pagination.getKeyword())) { queryWrapper.and( t -> t.like(UserEntity::getAccount, pagination.getKeyword()) .or().like(UserEntity::getRealName, pagination.getKeyword()) .or().like(UserEntity::getMobilePhone, pagination.getKeyword()) ); } queryWrapper.eq(RoleRelationEntity::getRoleId, pagination.getRoleId()); queryWrapper.eq(RoleRelationEntity::getObjectType, pagination.getType()); queryWrapper.orderByDesc(RoleRelationEntity::getCreatorTime).orderByDesc(RoleRelationEntity::getId); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage data = this.selectJoinListPage(page, RoleRelationUserVo.class, queryWrapper); return pagination.setData(data.getRecords(), page.getTotal()); } @Override public List getOrgPage(RoleRelationPage pagination) { MPJLambdaWrapper queryWrapper = JoinWrappers.lambda(RoleRelationEntity.class); queryWrapper.leftJoin(OrganizeEntity.class, OrganizeEntity::getId, RoleRelationEntity::getObjectId); queryWrapper.selectAs(OrganizeEntity::getId, RoleRelationOrgVo::getId); queryWrapper.selectAs(OrganizeEntity::getFullName, RoleRelationOrgVo::getFullName); queryWrapper.selectAs(OrganizeEntity::getEnCode, RoleRelationOrgVo::getEnCode); queryWrapper.selectAs(OrganizeEntity::getOrgNameTree, RoleRelationOrgVo::getOrgNameTree); queryWrapper.selectAs(OrganizeEntity::getDescription, RoleRelationOrgVo::getDescription); if (!StringUtil.isEmpty(pagination.getKeyword())) { queryWrapper.and( t -> t.like(OrganizeEntity::getEnCode, pagination.getKeyword()) .or().like(OrganizeEntity::getFullName, pagination.getKeyword()) ); } queryWrapper.eq(RoleRelationEntity::getRoleId, pagination.getRoleId()); queryWrapper.eq(RoleRelationEntity::getObjectType, pagination.getType()); queryWrapper.isNotNull(OrganizeEntity::getId); queryWrapper.orderByDesc(RoleRelationEntity::getCreatorTime).orderByDesc(RoleRelationEntity::getId); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage data = this.selectJoinListPage(page, RoleRelationOrgVo.class, queryWrapper); return pagination.setData(data.getRecords(), page.getTotal()); } @Override public List getPosPage(RoleRelationPage pagination) { MPJLambdaWrapper queryWrapper = JoinWrappers.lambda(RoleRelationEntity.class); queryWrapper.leftJoin(PositionEntity.class, PositionEntity::getId, RoleRelationEntity::getObjectId); queryWrapper.selectAs(PositionEntity::getId, RoleRelationOrgVo::getId); queryWrapper.selectAs(PositionEntity::getFullName, RoleRelationOrgVo::getFullName); queryWrapper.selectAs(PositionEntity::getEnCode, RoleRelationOrgVo::getEnCode); queryWrapper.selectAs(PositionEntity::getDescription, RoleRelationOrgVo::getDescription); queryWrapper.selectAs(PositionEntity::getOrganizeId, RoleRelationOrgVo::getOrganizeId); if (!StringUtil.isEmpty(pagination.getKeyword())) { queryWrapper.and( t -> t.like(PositionEntity::getEnCode, pagination.getKeyword()) .or().like(PositionEntity::getFullName, pagination.getKeyword()) ); } queryWrapper.eq(RoleRelationEntity::getRoleId, pagination.getRoleId()); queryWrapper.eq(RoleRelationEntity::getObjectType, pagination.getType()); queryWrapper.isNotNull(PositionEntity::getId); queryWrapper.orderByDesc(RoleRelationEntity::getCreatorTime).orderByDesc(RoleRelationEntity::getId); Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage data = this.selectJoinListPage(page, RoleRelationOrgVo.class, queryWrapper); return pagination.setData(data.getRecords(), page.getTotal()); } @Override public List getListByObjectId(String objectId, String objectType) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(RoleRelationEntity::getObjectId, objectId); if (objectType != null) { queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, objectType); } queryWrapper.lambda().orderByAsc(RoleRelationEntity::getSortCode).orderByDesc(RoleRelationEntity::getCreatorTime); return this.list(queryWrapper); } @Override public List getListByObjectId(List objectId, String objectType) { if (CollectionUtil.isEmpty(objectId)) return Collections.EMPTY_LIST; QueryWrapper queryWrapper = new QueryWrapper<>(); if (objectType != null) { queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, objectType); } List> lists = Lists.partition(objectId, 1000); queryWrapper.lambda().and(t -> { for (List ids : lists) { t.in(RoleRelationEntity::getObjectId, ids).or(); } }); queryWrapper.lambda().orderByAsc(RoleRelationEntity::getSortCode).orderByDesc(RoleRelationEntity::getCreatorTime); return this.list(queryWrapper); } @Override public List getListByRoleId(String roleId, String objectType) { QueryWrapper query = new QueryWrapper<>(); if (StringUtil.isNotEmpty(roleId)) { query.lambda().eq(RoleRelationEntity::getRoleId, roleId); } if (StringUtil.isNotEmpty(objectType)) { query.lambda().eq(RoleRelationEntity::getObjectType, objectType); } query.lambda().orderByAsc(RoleRelationEntity::getSortCode).orderByDesc(RoleRelationEntity::getCreatorTime); return this.list(query); } @Override public List getListByRoleId(List roleId, String objectType) { if (CollectionUtil.isEmpty(roleId)) return Collections.EMPTY_LIST; QueryWrapper query = new QueryWrapper<>(); if (StringUtil.isNotEmpty(objectType)) { query.lambda().eq(RoleRelationEntity::getObjectType, objectType); } List> lists = Lists.partition(roleId, 1000); query.lambda().and(t -> { for (List ids : lists) { t.in(RoleRelationEntity::getRoleId, ids).or(); } }); query.lambda().orderByAsc(RoleRelationEntity::getSortCode).orderByDesc(RoleRelationEntity::getCreatorTime); return this.list(query); } @Override public ActionResult roleAddObjectIds(RoleRelationForm form) { if (CollectionUtil.isEmpty(form.getIds())) { return ActionResult.fail(MsgCode.SYS134.get()); } String roleId = form.getRoleId(); RoleEntity info = roleService.getInfo(roleId); if (info == null) { return ActionResult.fail(MsgCode.FA001.get()); } String type = form.getType(); List ids = new ArrayList<>(form.getIds()); List errList1 = new ArrayList<>(); List objectIds = new ArrayList<>(); //角色-移除数据库已有数据。 List listByRoleId = this.getListByRoleId(roleId, type); Set roleSet = listByRoleId.stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toSet()); Set adminIds = userService.getAdminList().stream().map(UserEntity::getId).collect(Collectors.toSet()); ids = ids.stream().filter(t -> !roleSet.contains(t)).collect(Collectors.toList()); //用户角色约束判断 PosConModel posConModel = new PosConModel(); if (Objects.equals(info.getIsCondition(), 1)) { posConModel = JsonUtil.getJsonToBean(info.getConditionJson(), PosConModel.class); posConModel.init(); } if (posConModel.getNumFlag()) { //用户基数限制 if (ids.size() > 0 && posConModel.getUserNum() <= roleSet.size()) { return ActionResult.fail(MsgCode.SYS135.get(MsgCode.PS006.get())); } } for (String objectId : ids) { if (adminIds.contains(objectId)) { errList1.add("超管不能添加"); continue; } List errList2 = new ArrayList<>(); RoleRelationEntity entity = new RoleRelationEntity(); entity.setId(RandomUtil.uuId()); entity.setObjectId(objectId); entity.setRoleId(roleId); entity.setObjectType(type); //角色约束判断 if (PermissionConst.USER.equals(type)) { List userRoleList = this.getListByObjectId(objectId, PermissionConst.USER); List thisUserRole = userRoleList.stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList()); List roleList = roleService.getListByIds(thisUserRole); //用户现有角色和当前互斥 for (RoleEntity role : roleList) { if (Objects.equals(role.getIsCondition(), 1)) { PosConModel conModelP = JsonUtil.getJsonToBean(role.getConditionJson(), PosConModel.class); conModelP.init(); if (conModelP.getMutualExclusionFlag() && conModelP.getMutualExclusion().contains(roleId)) { errList2.add(MsgCode.SYS137.get()); } } } //互斥 if (posConModel.getMutualExclusionFlag()) { if (posConModel.getMutualExclusion().stream().filter(t -> thisUserRole.contains(t)).collect(Collectors.toList()).size() > 0) { errList2.add(MsgCode.SYS137.get()); } } //先决 if (posConModel.getPrerequisiteFlag() && !thisUserRole.containsAll(posConModel.getPrerequisite())) { errList2.add(MsgCode.SYS138.get()); } } if (errList2.size() == 0) { //达到用户基数限制跳出循环 if (posConModel.getNumFlag() && posConModel.getUserNum() <= (objectIds.size() + roleSet.size())) { errList1.add(MsgCode.SYS135.get(MsgCode.PS006.get())); break; } objectIds.add(objectId); this.save(entity); } else { errList1.addAll(errList2); } } //修改关系-相关用户踢下线 List userIds = new ArrayList<>(); if (PermissionConst.USER.equals(type)) { userIds.addAll(objectIds); } // if (PermissionConst.ORGANIZE.equals(type)) { // List listByIds = organizeService.getListByIds(objectIds); // List listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList())); // List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); // List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); // userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); // } if (PermissionConst.POSITION.equals(type)) { List listByOrgIds = positionService.getListByIds(objectIds); List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); } if (CollectionUtil.isNotEmpty(userIds)) { userService.delCurUser(MsgCode.PS010.get(), userIds); } if (CollectionUtil.isNotEmpty(errList1) && CollectionUtil.isNotEmpty(objectIds)) { return ActionResult.success(MsgCode.SYS139.get()); } else if (CollectionUtil.isNotEmpty(errList1)) { return ActionResult.fail(MsgCode.DB019.get()); } return ActionResult.success(MsgCode.SU018.get()); } @Override public void delete(RoleRelationForm form) { String type = form.getType(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(RoleRelationEntity::getRoleId, form.getRoleId()); queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, form.getType()); queryWrapper.lambda().in(RoleRelationEntity::getObjectId, form.getIds()); List list = this.list(queryWrapper); if (CollectionUtil.isEmpty(list)) { return; } for (RoleRelationEntity item : list) { this.removeById(item); } //一次移除只能是用类型数据 List objectIds = list.stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList()); //修改关系-相关用户踢下线 List userIds = new ArrayList<>(); if (PermissionConst.USER.equals(type)) { userIds.addAll(objectIds); } // if (PermissionConst.ORGANIZE.equals(type)) { // List listByIds = organizeService.getListByIds(objectIds); // List listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList())); // List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); // List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); // userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); // } if (PermissionConst.POSITION.equals(type)) { List listByOrgIds = positionService.getListByIds(objectIds); List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); } if (CollectionUtil.isNotEmpty(userIds)) { userService.delCurUser(MsgCode.PS010.get(), userIds); } } @Override public void objectAddRoles(AddRolesForm form) { String type = form.getType(); List roleIds = form.getIds(); List hasRRE = this.getListByObjectId(form.getObjectId(), type).stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList()); for (String roleId : roleIds) { if (!hasRRE.contains(roleId)) { RoleRelationEntity entity = new RoleRelationEntity(); entity.setId(RandomUtil.uuId()); entity.setObjectId(form.getObjectId()); entity.setRoleId(roleId); entity.setObjectType(type); this.save(entity); } } List userObjectIds = new ArrayList<>(); // if (PermissionConst.ORGANIZE.equals(type)) { // List listByIds = organizeService.getListByIds(Arrays.asList(form.getObjectId())); // List listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList())); // List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); // List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); // userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); // } if (PermissionConst.POSITION.equals(type)) { List listByOrgIds = positionService.getListByIds(Arrays.asList(form.getObjectId())); List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); } if (CollectionUtil.isNotEmpty(userObjectIds)) { userService.delCurUser(MsgCode.PS010.get(), userObjectIds); } } public void objectDeleteRoles(AddRolesForm form) { String type = form.getType(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(RoleRelationEntity::getObjectId, form.getObjectId()); queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, form.getType()); queryWrapper.lambda().in(RoleRelationEntity::getRoleId, form.getIds()); List list = this.list(queryWrapper); if (CollectionUtil.isEmpty(list)) { return; } for (RoleRelationEntity item : list) { this.removeById(item); } List userObjectIds = new ArrayList<>(); // if (PermissionConst.ORGANIZE.equals(type)) { // List listByIds = organizeService.getListByIds(Arrays.asList(form.getObjectId())); // List listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList())); // List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); // List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); // userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); // } if (PermissionConst.POSITION.equals(type)) { List listByOrgIds = positionService.getListByIds(Arrays.asList(form.getObjectId())); List positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList()); List listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds); userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList())); } if (CollectionUtil.isNotEmpty(userObjectIds)) { userService.delCurUser(MsgCode.PS010.get(), userObjectIds); } } }