RoleServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package jnpf.permission.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.google.common.collect.Lists;
  7. import jnpf.base.Pagination;
  8. import jnpf.base.UserInfo;
  9. import jnpf.base.service.SuperServiceImpl;
  10. import jnpf.constant.CodeConst;
  11. import jnpf.constant.PermissionConst;
  12. import jnpf.permission.entity.AuthorizeEntity;
  13. import jnpf.permission.entity.RoleEntity;
  14. import jnpf.permission.entity.RoleRelationEntity;
  15. import jnpf.permission.mapper.RoleMapper;
  16. import jnpf.permission.model.position.PosConModel;
  17. import jnpf.permission.model.role.RolePagination;
  18. import jnpf.permission.service.AuthorizeService;
  19. import jnpf.permission.service.CodeNumService;
  20. import jnpf.permission.service.RoleRelationService;
  21. import jnpf.permission.service.RoleService;
  22. import jnpf.util.JsonUtil;
  23. import jnpf.util.RandomUtil;
  24. import jnpf.util.StringUtil;
  25. import jnpf.util.UserProvider;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 系统角色
  33. *
  34. * @author JNPF开发平台组
  35. * @version V3.1.0
  36. * @copyright 引迈信息技术有限公司
  37. * @date 2019年9月26日 上午9:18
  38. */
  39. @Service
  40. public class RoleServiceImpl extends SuperServiceImpl<RoleMapper, RoleEntity> implements RoleService {
  41. @Autowired
  42. private AuthorizeService authorizeService;
  43. @Autowired
  44. private CodeNumService codeNumService;
  45. @Autowired
  46. private RoleRelationService roleRelationService;
  47. @Override
  48. public List<RoleEntity> getList(RolePagination pagination) {
  49. boolean flag = false;
  50. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  51. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  52. flag = true;
  53. queryWrapper.lambda().and(
  54. t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
  55. .or().like(RoleEntity::getEnCode, pagination.getKeyword())
  56. );
  57. }
  58. if (StringUtil.isNotEmpty(pagination.getType())) {
  59. queryWrapper.lambda().eq(RoleEntity::getType, pagination.getType());
  60. }
  61. if (pagination.getEnabledMark() != null) {
  62. queryWrapper.lambda().eq(RoleEntity::getEnabledMark, pagination.getEnabledMark());
  63. }
  64. //排序
  65. queryWrapper.lambda().orderByAsc(RoleEntity::getGlobalMark).orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
  66. if (flag) {
  67. queryWrapper.lambda().orderByDesc(RoleEntity::getLastModifyTime);
  68. }
  69. //不分页
  70. if (Objects.equals(pagination.getDataType(), 1)) {
  71. return this.list(queryWrapper);
  72. }
  73. //分页
  74. long count = this.count(queryWrapper);
  75. Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize(), count, false);
  76. page.setOptimizeCountSql(false);
  77. IPage<RoleEntity> iPage = this.page(page, queryWrapper);
  78. return pagination.setData(iPage.getRecords(), page.getTotal());
  79. }
  80. @Override
  81. public Boolean isExistByFullName(String fullName, String id, String type) {
  82. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  83. queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
  84. queryWrapper.lambda().eq(RoleEntity::getType, type);
  85. if (!StringUtil.isEmpty(id)) {
  86. queryWrapper.lambda().ne(RoleEntity::getId, id);
  87. }
  88. return this.count(queryWrapper) > 0 ? true : false;
  89. }
  90. @Override
  91. public Boolean isExistByEnCode(String enCode, String id) {
  92. if (StringUtil.isEmpty(enCode)) return false;
  93. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  94. queryWrapper.lambda().eq(RoleEntity::getEnCode, enCode);
  95. if (!StringUtil.isEmpty(id)) {
  96. queryWrapper.lambda().ne(RoleEntity::getId, id);
  97. }
  98. return this.count(queryWrapper) > 0 ? true : false;
  99. }
  100. @Override
  101. public void create(RoleEntity entity) {
  102. if (StringUtil.isEmpty(entity.getId())) {
  103. entity.setId(RandomUtil.uuId());
  104. }
  105. if (StringUtil.isEmpty(entity.getEnCode())) {
  106. String codeType = CodeConst.YHJS;
  107. if (PermissionConst.ORGANIZE.equals(entity.getType())) {
  108. codeType = CodeConst.ZZJS;
  109. } else if (PermissionConst.POSITION.equals(entity.getType())) {
  110. codeType = CodeConst.GWJS;
  111. }
  112. final String codeTypeP = codeType;
  113. entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(codeTypeP), code -> isExistByEnCode(code, null)));
  114. }
  115. entity.setGlobalMark(2);
  116. entity.setEnabledMark(1);
  117. entity.setCreatorTime(new Date());
  118. entity.setCreatorUserId(UserProvider.getUser().getUserId());
  119. this.save(entity);
  120. }
  121. @Override
  122. public Boolean update(String id, RoleEntity entity) {
  123. entity.setId(id);
  124. if (StringUtil.isEmpty(entity.getEnCode())) {
  125. String codeType = CodeConst.YHJS;
  126. if (PermissionConst.ORGANIZE.equals(entity.getType())) {
  127. codeType = CodeConst.ZZJS;
  128. } else if (PermissionConst.POSITION.equals(entity.getType())) {
  129. codeType = CodeConst.GWJS;
  130. }
  131. final String codeTypeP = codeType;
  132. entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(codeTypeP), code -> isExistByEnCode(code, id)));
  133. }
  134. entity.setEnabledMark(1);
  135. entity.setLastModifyTime(new Date());
  136. entity.setLastModifyUserId(UserProvider.getUser().getUserId());
  137. return this.updateById(entity);
  138. }
  139. @Override
  140. public RoleEntity getInfo(String id) {
  141. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  142. queryWrapper.lambda().eq(RoleEntity::getId, id);
  143. return this.getOne(queryWrapper);
  144. }
  145. @Override
  146. @Transactional
  147. public void delete(RoleEntity entity) {
  148. if (entity != null) {
  149. this.removeById(entity.getId());
  150. QueryWrapper<AuthorizeEntity> queryWrapper = new QueryWrapper<>();
  151. queryWrapper.lambda().eq(AuthorizeEntity::getObjectId, entity.getId());
  152. authorizeService.remove(queryWrapper);
  153. QueryWrapper<RoleRelationEntity> wrapper = new QueryWrapper<>();
  154. wrapper.lambda().eq(RoleRelationEntity::getRoleId, entity.getId());
  155. roleRelationService.remove(wrapper);
  156. }
  157. }
  158. @Override
  159. public RoleEntity getByEnCode(String enCode) {
  160. if (StringUtil.isEmpty(enCode)) return null;
  161. QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
  162. wrapper.lambda().eq(RoleEntity::getEnCode, enCode);
  163. return this.getOne(wrapper);
  164. }
  165. @Override
  166. public List<RoleEntity> getList(boolean filterEnabledMark, String type, Integer isSystem) {
  167. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  168. if (filterEnabledMark) {
  169. queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
  170. }
  171. if (StringUtil.isNotEmpty(type)) {
  172. queryWrapper.lambda().eq(RoleEntity::getType, type);
  173. }
  174. if (Objects.nonNull(isSystem)) {
  175. if (Objects.equals(isSystem, 1)) {
  176. queryWrapper.lambda().eq(RoleEntity::getGlobalMark, 1);
  177. } else {
  178. queryWrapper.lambda().ne(RoleEntity::getGlobalMark, 1);
  179. }
  180. }
  181. queryWrapper.lambda().orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
  182. return this.list(queryWrapper);
  183. }
  184. @Override
  185. public List<RoleEntity> getListByIds(List<String> id, String keyword, boolean filterEnabledMark) {
  186. if (CollectionUtil.isEmpty(id)) return Collections.EMPTY_LIST;
  187. List<RoleEntity> roleList = new ArrayList<>();
  188. if (id.size() > 0) {
  189. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  190. queryWrapper.lambda().in(RoleEntity::getId, id);
  191. if (filterEnabledMark) {
  192. queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
  193. }
  194. if (StringUtil.isNotEmpty(keyword)) {
  195. queryWrapper.lambda().and(
  196. t -> t.like(RoleEntity::getFullName, keyword)
  197. .or().like(RoleEntity::getEnCode, keyword)
  198. );
  199. }
  200. roleList = this.list(queryWrapper);
  201. }
  202. return roleList;
  203. }
  204. @Override
  205. public List<RoleEntity> getListByIds(List<String> idList) {
  206. if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
  207. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  208. List<List<String>> lists = Lists.partition(idList, 1000);
  209. for (List<String> list : lists) {
  210. queryWrapper.lambda().in(RoleEntity::getId, list);
  211. }
  212. return this.list(queryWrapper);
  213. }
  214. @Override
  215. public List<RoleEntity> getListByIds(Pagination pagination, List<String> idList) {
  216. if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
  217. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  218. List<List<String>> lists = Lists.partition(idList, 1000);
  219. for (List<String> list : lists) {
  220. queryWrapper.lambda().in(RoleEntity::getId, list);
  221. }
  222. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  223. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  224. queryWrapper.lambda().and(
  225. t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
  226. .or().like(RoleEntity::getEnCode, pagination.getKeyword())
  227. );
  228. }
  229. }
  230. Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  231. IPage<RoleEntity> iPage = this.page(page, queryWrapper);
  232. return pagination.setData(iPage.getRecords(), page.getTotal());
  233. }
  234. @Override
  235. public Map<String, Object> getRoleMap() {
  236. QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
  237. roleWrapper.lambda().select(RoleEntity::getFullName, RoleEntity::getId);
  238. List<RoleEntity> list = this.list(roleWrapper);
  239. return list.stream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getFullName));
  240. }
  241. @Override
  242. public Map<String, Object> getRoleNameAndIdMap() {
  243. return getRoleNameAndIdMap(false);
  244. }
  245. @Override
  246. public Map<String, Object> getRoleNameAndIdMap(boolean enabledMark) {
  247. QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
  248. if (enabledMark) {
  249. roleWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
  250. }
  251. List<RoleEntity> list = this.list(roleWrapper);
  252. Map<String, Object> roleNameMap = new HashMap<>();
  253. list.stream().forEach(role -> roleNameMap.put(role.getFullName() + "/" + role.getEnCode(), role.getId()));
  254. return roleNameMap;
  255. }
  256. @Override
  257. public RoleEntity getInfoByFullName(String fullName) {
  258. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  259. queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
  260. return this.getOne(queryWrapper);
  261. }
  262. @Override
  263. public List<RoleEntity> getCurRolesByOrgId() {
  264. UserInfo user = UserProvider.getUser();
  265. //同组织下所有角色
  266. List<RoleRelationEntity> roleRelations = new ArrayList<>();
  267. roleRelations.addAll(roleRelationService.getListByObjectId(user.getOrganizeId(), PermissionConst.ORGANIZE));
  268. roleRelations.addAll(roleRelationService.getListByObjectId(user.getPositionId(), PermissionConst.POSITION));
  269. roleRelations.addAll(roleRelationService.getListByObjectId(user.getId(), PermissionConst.USER));
  270. if (CollectionUtil.isEmpty(roleRelations)) {
  271. return Collections.EMPTY_LIST;
  272. }
  273. List<String> roleIds = roleRelations.stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  274. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  275. queryWrapper.lambda().in(RoleEntity::getId, roleIds);
  276. queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
  277. return this.list(queryWrapper);
  278. }
  279. @Override
  280. public List<RoleEntity> getList(List<String> idList, Pagination pagination, boolean filterEnabledMark) {
  281. if (idList.size() > 0) {
  282. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  283. queryWrapper.lambda().in(RoleEntity::getId, idList);
  284. if (filterEnabledMark) {
  285. queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
  286. }
  287. if (StringUtil.isNotEmpty(pagination.getKeyword())) {
  288. queryWrapper.lambda().and(
  289. t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
  290. .or().like(RoleEntity::getEnCode, pagination.getKeyword())
  291. );
  292. }
  293. Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  294. IPage<RoleEntity> iPage = this.page(page, queryWrapper);
  295. return pagination.setData(iPage.getRecords(), iPage.getTotal());
  296. }
  297. return Collections.emptyList();
  298. }
  299. @Override
  300. public Map<String, Integer> roleUserCount() {
  301. Map<String, Integer> map = new HashMap<>();
  302. List<RoleEntity> list = this.getList(true, PermissionConst.USER, null);
  303. List<RoleRelationEntity> listByRoleId = roleRelationService.getListByRoleId("", PermissionConst.USER);
  304. Map<String, List<RoleRelationEntity>> roleGroup = listByRoleId.stream().collect(Collectors.groupingBy(RoleRelationEntity::getRoleId));
  305. for (RoleEntity role : list) {
  306. map.put(role.getFullName(), CollectionUtil.isEmpty(roleGroup.get(role.getId())) ? 0 : roleGroup.get(role.getId()).size());
  307. }
  308. return map;
  309. }
  310. @Override
  311. public List<RoleEntity> getUserRoles(String userId) {
  312. List<String> roleIds = roleRelationService.getListByObjectId(userId, PermissionConst.USER).stream().map(RoleRelationEntity::getRoleId).collect(Collectors.toList());
  313. return this.getListByIds(roleIds);
  314. }
  315. @Override
  316. public void linkUpdate(String id, PosConModel posConModel) {
  317. //联动修改互斥对象
  318. List<String> muEList = new ArrayList<>();
  319. if (posConModel.getMutualExclusionFlag()) {
  320. muEList.addAll(posConModel.getMutualExclusion());
  321. }
  322. //muEList 互斥对象。除了这个列表外其他角色里不能包含该互斥
  323. QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
  324. queryWrapper.lambda().like(RoleEntity::getConditionJson, id);
  325. if (CollectionUtil.isNotEmpty(muEList)) {
  326. queryWrapper.lambda().or().in(RoleEntity::getId, muEList);
  327. }
  328. List<RoleEntity> list = this.list(queryWrapper);
  329. for (RoleEntity item : list) {
  330. if (muEList.contains(item.getId())) {
  331. //添加
  332. item.setIsCondition(1);
  333. PosConModel psModel = StringUtil.isEmpty(item.getConditionJson()) ? new PosConModel() : JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
  334. List<Integer> constraintType = psModel.getConstraintType() == null ? new ArrayList<>() : psModel.getConstraintType();
  335. if (!constraintType.contains(0)) {
  336. constraintType.add(0);
  337. psModel.setConstraintType(constraintType);
  338. }
  339. List<String> mutualExclusion = psModel.getMutualExclusion() == null ? new ArrayList<>() : psModel.getMutualExclusion();
  340. if (!mutualExclusion.contains(id)) {
  341. mutualExclusion.add(id);
  342. psModel.setMutualExclusion(mutualExclusion);
  343. item.setConditionJson(JsonUtil.getObjectToString(psModel));
  344. }
  345. this.update(item.getId(), item);
  346. } else {
  347. //移除
  348. if (Objects.equals(item.getIsCondition(), 1)) {
  349. PosConModel psModel = JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
  350. psModel.init();
  351. if (psModel.getMutualExclusionFlag()) {
  352. List<String> mutualExclusion = psModel.getMutualExclusion();
  353. if (mutualExclusion.contains(id)) {
  354. mutualExclusion.remove(id);
  355. if (mutualExclusion.size() == 0) {
  356. List<Integer> constraintType = psModel.getConstraintType();
  357. constraintType.remove(Integer.valueOf(0));
  358. psModel.setConstraintType(constraintType);
  359. if (constraintType.size() == 0) {
  360. item.setIsCondition(0);
  361. }
  362. }
  363. item.setConditionJson(JsonUtil.getObjectToString(psModel));
  364. this.update(item.getId(), item);
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }