| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- 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<RoleRelationMapper, RoleRelationEntity> 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<RoleRelationEntity> getListPage(RoleListPage pagination) {
- String objectType = StringUtil.isNotEmpty(pagination.getPositionId()) ? PermissionConst.POSITION : PermissionConst.ORGANIZE;
- String objectId = StringUtil.isNotEmpty(pagination.getPositionId()) ? pagination.getPositionId() : pagination.getOrganizeId();
- QueryWrapper<RoleRelationEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(RoleRelationEntity::getObjectId, objectId);
- queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, objectType);
- queryWrapper.lambda().orderByDesc(RoleRelationEntity::getCreatorTime).orderByDesc(RoleRelationEntity::getId);
- Page<RoleRelationEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleRelationEntity> iPage = this.page(page, queryWrapper);
- return pagination.setData(iPage.getRecords(), iPage.getTotal());
- }
- @Override
- public List<RoleRelationUserVo> getUserPage(RoleRelationPage pagination) {
- MPJLambdaWrapper<RoleRelationEntity> 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<RoleRelationUserVo> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleRelationUserVo> data = this.selectJoinListPage(page, RoleRelationUserVo.class, queryWrapper);
- return pagination.setData(data.getRecords(), page.getTotal());
- }
- @Override
- public List<RoleRelationOrgVo> getOrgPage(RoleRelationPage pagination) {
- MPJLambdaWrapper<RoleRelationEntity> 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<RoleRelationOrgVo> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleRelationOrgVo> data = this.selectJoinListPage(page, RoleRelationOrgVo.class, queryWrapper);
- return pagination.setData(data.getRecords(), page.getTotal());
- }
- @Override
- public List<RoleRelationOrgVo> getPosPage(RoleRelationPage pagination) {
- MPJLambdaWrapper<RoleRelationEntity> 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<RoleRelationOrgVo> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleRelationOrgVo> data = this.selectJoinListPage(page, RoleRelationOrgVo.class, queryWrapper);
- return pagination.setData(data.getRecords(), page.getTotal());
- }
- @Override
- public List<RoleRelationEntity> getListByObjectId(String objectId, String objectType) {
- QueryWrapper<RoleRelationEntity> 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<RoleRelationEntity> getListByObjectId(List<String> objectId, String objectType) {
- if (CollectionUtil.isEmpty(objectId)) return Collections.EMPTY_LIST;
- QueryWrapper<RoleRelationEntity> queryWrapper = new QueryWrapper<>();
- if (objectType != null) {
- queryWrapper.lambda().eq(RoleRelationEntity::getObjectType, objectType);
- }
- List<List<String>> lists = Lists.partition(objectId, 1000);
- queryWrapper.lambda().and(t -> {
- for (List<String> ids : lists) {
- t.in(RoleRelationEntity::getObjectId, ids).or();
- }
- });
- queryWrapper.lambda().orderByAsc(RoleRelationEntity::getSortCode).orderByDesc(RoleRelationEntity::getCreatorTime);
- return this.list(queryWrapper);
- }
- @Override
- public List<RoleRelationEntity> getListByRoleId(String roleId, String objectType) {
- QueryWrapper<RoleRelationEntity> 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<RoleRelationEntity> getListByRoleId(List<String> roleId, String objectType) {
- if (CollectionUtil.isEmpty(roleId)) return Collections.EMPTY_LIST;
- QueryWrapper<RoleRelationEntity> query = new QueryWrapper<>();
- if (StringUtil.isNotEmpty(objectType)) {
- query.lambda().eq(RoleRelationEntity::getObjectType, objectType);
- }
- List<List<String>> lists = Lists.partition(roleId, 1000);
- query.lambda().and(t -> {
- for (List<String> 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<String> ids = new ArrayList<>(form.getIds());
- List<String> errList1 = new ArrayList<>();
- List<String> objectIds = new ArrayList<>();
- //角色-移除数据库已有数据。
- List<RoleRelationEntity> listByRoleId = this.getListByRoleId(roleId, type);
- Set<String> roleSet = listByRoleId.stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toSet());
- Set<String> 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<String> 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<RoleRelationEntity> userRoleList = this.getListByObjectId(objectId, PermissionConst.USER);
- List<String> thisUserRole = userRoleList.stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
- List<RoleEntity> 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<String> userIds = new ArrayList<>();
- if (PermissionConst.USER.equals(type)) {
- userIds.addAll(objectIds);
- }
- // if (PermissionConst.ORGANIZE.equals(type)) {
- // List<OrganizeEntity> listByIds = organizeService.getListByIds(objectIds);
- // List<PositionEntity> listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList()));
- // List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- // List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds);
- // userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
- // }
- if (PermissionConst.POSITION.equals(type)) {
- List<PositionEntity> listByOrgIds = positionService.getListByIds(objectIds);
- List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- List<UserRelationEntity> 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<RoleRelationEntity> 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<RoleRelationEntity> list = this.list(queryWrapper);
- if (CollectionUtil.isEmpty(list)) {
- return;
- }
- for (RoleRelationEntity item : list) {
- this.removeById(item);
- }
- //一次移除只能是用类型数据
- List<String> objectIds = list.stream().map(RoleRelationEntity::getObjectId).collect(Collectors.toList());
- //修改关系-相关用户踢下线
- List<String> userIds = new ArrayList<>();
- if (PermissionConst.USER.equals(type)) {
- userIds.addAll(objectIds);
- }
- // if (PermissionConst.ORGANIZE.equals(type)) {
- // List<OrganizeEntity> listByIds = organizeService.getListByIds(objectIds);
- // List<PositionEntity> listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList()));
- // List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- // List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds);
- // userIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
- // }
- if (PermissionConst.POSITION.equals(type)) {
- List<PositionEntity> listByOrgIds = positionService.getListByIds(objectIds);
- List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- List<UserRelationEntity> 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<String> roleIds = form.getIds();
- List<String> 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<String> userObjectIds = new ArrayList<>();
- // if (PermissionConst.ORGANIZE.equals(type)) {
- // List<OrganizeEntity> listByIds = organizeService.getListByIds(Arrays.asList(form.getObjectId()));
- // List<PositionEntity> listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList()));
- // List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- // List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds);
- // userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
- // }
- if (PermissionConst.POSITION.equals(type)) {
- List<PositionEntity> listByOrgIds = positionService.getListByIds(Arrays.asList(form.getObjectId()));
- List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- List<UserRelationEntity> 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<RoleRelationEntity> 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<RoleRelationEntity> list = this.list(queryWrapper);
- if (CollectionUtil.isEmpty(list)) {
- return;
- }
- for (RoleRelationEntity item : list) {
- this.removeById(item);
- }
- List<String> userObjectIds = new ArrayList<>();
- // if (PermissionConst.ORGANIZE.equals(type)) {
- // List<OrganizeEntity> listByIds = organizeService.getListByIds(Arrays.asList(form.getObjectId()));
- // List<PositionEntity> listByOrgIds = positionService.getListByOrgIds(listByIds.stream().map(OrganizeEntity::getId).collect(Collectors.toList()));
- // List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- // List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds);
- // userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
- // }
- if (PermissionConst.POSITION.equals(type)) {
- List<PositionEntity> listByOrgIds = positionService.getListByIds(Arrays.asList(form.getObjectId()));
- List<String> positionIds = listByOrgIds.stream().map(PositionEntity::getId).collect(Collectors.toList());
- List<UserRelationEntity> listByObjectIdAll = userRelationService.getListByObjectIdAll(positionIds);
- userObjectIds.addAll(listByObjectIdAll.stream().map(UserRelationEntity::getUserId).collect(Collectors.toList()));
- }
- if (CollectionUtil.isNotEmpty(userObjectIds)) {
- userService.delCurUser(MsgCode.PS010.get(), userObjectIds);
- }
- }
- }
|