| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- 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.google.common.collect.Lists;
- import jnpf.base.Pagination;
- import jnpf.base.UserInfo;
- import jnpf.base.service.SuperServiceImpl;
- import jnpf.constant.CodeConst;
- import jnpf.constant.PermissionConst;
- import jnpf.permission.entity.AuthorizeEntity;
- import jnpf.permission.entity.RoleEntity;
- import jnpf.permission.entity.RoleRelationEntity;
- import jnpf.permission.mapper.RoleMapper;
- import jnpf.permission.model.position.PosConModel;
- import jnpf.permission.model.role.RolePagination;
- import jnpf.permission.service.AuthorizeService;
- import jnpf.permission.service.CodeNumService;
- import jnpf.permission.service.RoleRelationService;
- import jnpf.permission.service.RoleService;
- import jnpf.util.JsonUtil;
- import jnpf.util.RandomUtil;
- import jnpf.util.StringUtil;
- import jnpf.util.UserProvider;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * 系统角色
- *
- * @author JNPF开发平台组
- * @version V3.1.0
- * @copyright 引迈信息技术有限公司
- * @date 2019年9月26日 上午9:18
- */
- @Service
- public class RoleServiceImpl extends SuperServiceImpl<RoleMapper, RoleEntity> implements RoleService {
- @Autowired
- private AuthorizeService authorizeService;
- @Autowired
- private CodeNumService codeNumService;
- @Autowired
- private RoleRelationService roleRelationService;
- @Override
- public List<RoleEntity> getList(RolePagination pagination) {
- boolean flag = false;
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- if (StringUtil.isNotEmpty(pagination.getKeyword())) {
- flag = true;
- queryWrapper.lambda().and(
- t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
- .or().like(RoleEntity::getEnCode, pagination.getKeyword())
- );
- }
- if (StringUtil.isNotEmpty(pagination.getType())) {
- queryWrapper.lambda().eq(RoleEntity::getType, pagination.getType());
- }
- if (pagination.getEnabledMark() != null) {
- queryWrapper.lambda().eq(RoleEntity::getEnabledMark, pagination.getEnabledMark());
- }
- //排序
- queryWrapper.lambda().orderByAsc(RoleEntity::getGlobalMark).orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
- if (flag) {
- queryWrapper.lambda().orderByDesc(RoleEntity::getLastModifyTime);
- }
- //不分页
- if (Objects.equals(pagination.getDataType(), 1)) {
- return this.list(queryWrapper);
- }
- //分页
- long count = this.count(queryWrapper);
- Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize(), count, false);
- page.setOptimizeCountSql(false);
- IPage<RoleEntity> iPage = this.page(page, queryWrapper);
- return pagination.setData(iPage.getRecords(), page.getTotal());
- }
- @Override
- public Boolean isExistByFullName(String fullName, String id, String type) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
- queryWrapper.lambda().eq(RoleEntity::getType, type);
- if (!StringUtil.isEmpty(id)) {
- queryWrapper.lambda().ne(RoleEntity::getId, id);
- }
- return this.count(queryWrapper) > 0 ? true : false;
- }
- @Override
- public Boolean isExistByEnCode(String enCode, String id) {
- if (StringUtil.isEmpty(enCode)) return false;
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(RoleEntity::getEnCode, enCode);
- if (!StringUtil.isEmpty(id)) {
- queryWrapper.lambda().ne(RoleEntity::getId, id);
- }
- return this.count(queryWrapper) > 0 ? true : false;
- }
- @Override
- public void create(RoleEntity entity) {
- if (StringUtil.isEmpty(entity.getId())) {
- entity.setId(RandomUtil.uuId());
- }
- if (StringUtil.isEmpty(entity.getEnCode())) {
- String codeType = CodeConst.YHJS;
- if (PermissionConst.ORGANIZE.equals(entity.getType())) {
- codeType = CodeConst.ZZJS;
- } else if (PermissionConst.POSITION.equals(entity.getType())) {
- codeType = CodeConst.GWJS;
- }
- final String codeTypeP = codeType;
- entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(codeTypeP), code -> isExistByEnCode(code, null)));
- }
- entity.setGlobalMark(2);
- entity.setEnabledMark(1);
- entity.setCreatorTime(new Date());
- entity.setCreatorUserId(UserProvider.getUser().getUserId());
- this.save(entity);
- }
- @Override
- public Boolean update(String id, RoleEntity entity) {
- entity.setId(id);
- if (StringUtil.isEmpty(entity.getEnCode())) {
- String codeType = CodeConst.YHJS;
- if (PermissionConst.ORGANIZE.equals(entity.getType())) {
- codeType = CodeConst.ZZJS;
- } else if (PermissionConst.POSITION.equals(entity.getType())) {
- codeType = CodeConst.GWJS;
- }
- final String codeTypeP = codeType;
- entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(codeTypeP), code -> isExistByEnCode(code, id)));
- }
- entity.setEnabledMark(1);
- entity.setLastModifyTime(new Date());
- entity.setLastModifyUserId(UserProvider.getUser().getUserId());
- return this.updateById(entity);
- }
- @Override
- public RoleEntity getInfo(String id) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(RoleEntity::getId, id);
- return this.getOne(queryWrapper);
- }
- @Override
- @Transactional
- public void delete(RoleEntity entity) {
- if (entity != null) {
- this.removeById(entity.getId());
- QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, entity.getId());
- authorizeService.remove(queryWrapper);
- QueryWrapper<RoleRelationEntity> wrapper = new QueryWrapper<>();
- wrapper.lambda().eq(RoleRelationEntity::getRoleId, entity.getId());
- roleRelationService.remove(wrapper);
- }
- }
- @Override
- public RoleEntity getByEnCode(String enCode) {
- if (StringUtil.isEmpty(enCode)) return null;
- QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
- wrapper.lambda().eq(RoleEntity::getEnCode, enCode);
- return this.getOne(wrapper);
- }
- @Override
- public List<RoleEntity> getList(boolean filterEnabledMark, String type, Integer isSystem) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- if (filterEnabledMark) {
- queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
- }
- if (StringUtil.isNotEmpty(type)) {
- queryWrapper.lambda().eq(RoleEntity::getType, type);
- }
- if (Objects.nonNull(isSystem)) {
- if (Objects.equals(isSystem, 1)) {
- queryWrapper.lambda().eq(RoleEntity::getGlobalMark, 1);
- } else {
- queryWrapper.lambda().ne(RoleEntity::getGlobalMark, 1);
- }
- }
- queryWrapper.lambda().orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
- return this.list(queryWrapper);
- }
- @Override
- public List<RoleEntity> getListByIds(List<String> id, String keyword, boolean filterEnabledMark) {
- if (CollectionUtil.isEmpty(id)) return Collections.EMPTY_LIST;
- List<RoleEntity> roleList = new ArrayList<>();
- if (id.size() > 0) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().in(RoleEntity::getId, id);
- if (filterEnabledMark) {
- queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
- }
- if (StringUtil.isNotEmpty(keyword)) {
- queryWrapper.lambda().and(
- t -> t.like(RoleEntity::getFullName, keyword)
- .or().like(RoleEntity::getEnCode, keyword)
- );
- }
- roleList = this.list(queryWrapper);
- }
- return roleList;
- }
- @Override
- public List<RoleEntity> getListByIds(List<String> idList) {
- if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- List<List<String>> lists = Lists.partition(idList, 1000);
- for (List<String> list : lists) {
- queryWrapper.lambda().in(RoleEntity::getId, list);
- }
- return this.list(queryWrapper);
- }
- @Override
- public List<RoleEntity> getListByIds(Pagination pagination, List<String> idList) {
- if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- List<List<String>> lists = Lists.partition(idList, 1000);
- for (List<String> list : lists) {
- queryWrapper.lambda().in(RoleEntity::getId, list);
- }
- if (StringUtil.isNotEmpty(pagination.getKeyword())) {
- if (StringUtil.isNotEmpty(pagination.getKeyword())) {
- queryWrapper.lambda().and(
- t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
- .or().like(RoleEntity::getEnCode, pagination.getKeyword())
- );
- }
- }
- Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleEntity> iPage = this.page(page, queryWrapper);
- return pagination.setData(iPage.getRecords(), page.getTotal());
- }
- @Override
- public Map<String, Object> getRoleMap() {
- QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
- roleWrapper.lambda().select(RoleEntity::getFullName, RoleEntity::getId);
- List<RoleEntity> list = this.list(roleWrapper);
- return list.stream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getFullName));
- }
- @Override
- public Map<String, Object> getRoleNameAndIdMap() {
- return getRoleNameAndIdMap(false);
- }
- @Override
- public Map<String, Object> getRoleNameAndIdMap(boolean enabledMark) {
- QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
- if (enabledMark) {
- roleWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
- }
- List<RoleEntity> list = this.list(roleWrapper);
- Map<String, Object> roleNameMap = new HashMap<>();
- list.stream().forEach(role -> roleNameMap.put(role.getFullName() + "/" + role.getEnCode(), role.getId()));
- return roleNameMap;
- }
- @Override
- public RoleEntity getInfoByFullName(String fullName) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
- return this.getOne(queryWrapper);
- }
- @Override
- public List<RoleEntity> getCurRolesByOrgId() {
- UserInfo user = UserProvider.getUser();
- //同组织下所有角色
- List<RoleRelationEntity> roleRelations = new ArrayList<>();
- roleRelations.addAll(roleRelationService.getListByObjectId(user.getOrganizeId(), PermissionConst.ORGANIZE));
- roleRelations.addAll(roleRelationService.getListByObjectId(user.getPositionId(), PermissionConst.POSITION));
- roleRelations.addAll(roleRelationService.getListByObjectId(user.getId(), PermissionConst.USER));
- if (CollectionUtil.isEmpty(roleRelations)) {
- return Collections.EMPTY_LIST;
- }
- List<String> roleIds = roleRelations.stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().in(RoleEntity::getId, roleIds);
- queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
- return this.list(queryWrapper);
- }
- @Override
- public List<RoleEntity> getList(List<String> idList, Pagination pagination, boolean filterEnabledMark) {
- if (idList.size() > 0) {
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().in(RoleEntity::getId, idList);
- if (filterEnabledMark) {
- queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
- }
- if (StringUtil.isNotEmpty(pagination.getKeyword())) {
- queryWrapper.lambda().and(
- t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
- .or().like(RoleEntity::getEnCode, pagination.getKeyword())
- );
- }
- Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
- IPage<RoleEntity> iPage = this.page(page, queryWrapper);
- return pagination.setData(iPage.getRecords(), iPage.getTotal());
- }
- return Collections.emptyList();
- }
- @Override
- public Map<String, Integer> roleUserCount() {
- Map<String, Integer> map = new HashMap<>();
- List<RoleEntity> list = this.getList(true, PermissionConst.USER, null);
- List<RoleRelationEntity> listByRoleId = roleRelationService.getListByRoleId("", PermissionConst.USER);
- Map<String, List<RoleRelationEntity>> roleGroup = listByRoleId.stream().collect(Collectors.groupingBy(RoleRelationEntity::getRoleId));
- for (RoleEntity role : list) {
- map.put(role.getFullName(), CollectionUtil.isEmpty(roleGroup.get(role.getId())) ? 0 : roleGroup.get(role.getId()).size());
- }
- return map;
- }
- @Override
- public List<RoleEntity> getUserRoles(String userId) {
- List<String> roleIds = roleRelationService.getListByObjectId(userId, PermissionConst.USER).stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
- return this.getListByIds(roleIds);
- }
- @Override
- public void linkUpdate(String id, PosConModel posConModel) {
- //联动修改互斥对象
- List<String> muEList = new ArrayList<>();
- if (posConModel.getMutualExclusionFlag()) {
- muEList.addAll(posConModel.getMutualExclusion());
- }
- //muEList 互斥对象。除了这个列表外其他角色里不能包含该互斥
- QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().like(RoleEntity::getConditionJson, id);
- if (CollectionUtil.isNotEmpty(muEList)) {
- queryWrapper.lambda().or().in(RoleEntity::getId, muEList);
- }
- List<RoleEntity> list = this.list(queryWrapper);
- for (RoleEntity item : list) {
- if (muEList.contains(item.getId())) {
- //添加
- item.setIsCondition(1);
- PosConModel psModel = StringUtil.isEmpty(item.getConditionJson()) ? new PosConModel() : JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
- List<Integer> constraintType = psModel.getConstraintType() == null ? new ArrayList<>() : psModel.getConstraintType();
- if (!constraintType.contains(0)) {
- constraintType.add(0);
- psModel.setConstraintType(constraintType);
- }
- List<String> mutualExclusion = psModel.getMutualExclusion() == null ? new ArrayList<>() : psModel.getMutualExclusion();
- if (!mutualExclusion.contains(id)) {
- mutualExclusion.add(id);
- psModel.setMutualExclusion(mutualExclusion);
- item.setConditionJson(JsonUtil.getObjectToString(psModel));
- }
- this.update(item.getId(), item);
- } else {
- //移除
- if (Objects.equals(item.getIsCondition(), 1)) {
- PosConModel psModel = JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
- psModel.init();
- if (psModel.getMutualExclusionFlag()) {
- List<String> mutualExclusion = psModel.getMutualExclusion();
- if (mutualExclusion.contains(id)) {
- mutualExclusion.remove(id);
- if (mutualExclusion.size() == 0) {
- List<Integer> constraintType = psModel.getConstraintType();
- constraintType.remove(Integer.valueOf(0));
- psModel.setConstraintType(constraintType);
- if (constraintType.size() == 0) {
- item.setIsCondition(0);
- }
- }
- item.setConditionJson(JsonUtil.getObjectToString(psModel));
- this.update(item.getId(), item);
- }
- }
- }
- }
- }
- }
- }
|