GroupServiceImpl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.service.SuperServiceImpl;
  8. import jnpf.constant.CodeConst;
  9. import jnpf.permission.entity.GroupEntity;
  10. import jnpf.permission.mapper.GroupMapper;
  11. import jnpf.permission.model.usergroup.GroupPagination;
  12. import jnpf.permission.service.CodeNumService;
  13. import jnpf.permission.service.GroupService;
  14. import jnpf.util.DateUtil;
  15. import jnpf.util.RandomUtil;
  16. import jnpf.util.StringUtil;
  17. import jnpf.util.UserProvider;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.stream.Collectors;
  25. /**
  26. * 分组管理业务类实现类
  27. *
  28. * @author :JNPF开发平台组
  29. * @version: V3.1.0
  30. * @copyright 引迈信息技术有限公司
  31. * @date :2022/3/10 18:00
  32. */
  33. @Service
  34. public class GroupServiceImpl extends SuperServiceImpl<GroupMapper, GroupEntity> implements GroupService {
  35. @Autowired
  36. private CodeNumService codeNumService;
  37. @Override
  38. public List<GroupEntity> getList(GroupPagination pagination) {
  39. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  40. // 定义变量判断是否需要使用修改时间倒序
  41. boolean flag = false;
  42. // 判断关键字
  43. String keyword = pagination.getKeyword();
  44. if (StringUtil.isNotEmpty(keyword)) {
  45. flag = true;
  46. queryWrapper.lambda().and(
  47. t -> t.like(GroupEntity::getFullName, keyword)
  48. .or().like(GroupEntity::getEnCode, keyword)
  49. .or().like(GroupEntity::getDescription, keyword)
  50. );
  51. }
  52. if (pagination.getEnabledMark() != null) {
  53. queryWrapper.lambda().eq(GroupEntity::getEnabledMark, pagination.getEnabledMark());
  54. }
  55. // 获取列表
  56. queryWrapper.lambda().orderByAsc(GroupEntity::getSortCode).orderByAsc(GroupEntity::getCreatorTime);
  57. if (flag) {
  58. queryWrapper.lambda().orderByDesc(GroupEntity::getLastModifyTime);
  59. }
  60. //不分页
  61. if (Objects.equals(pagination.getDataType(), 1)) {
  62. return list(queryWrapper);
  63. }
  64. //有分页
  65. Page<GroupEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  66. IPage<GroupEntity> iPage = this.page(page, queryWrapper);
  67. return pagination.setData(iPage.getRecords(), iPage.getTotal());
  68. }
  69. @Override
  70. public List<GroupEntity> list() {
  71. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  72. queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1);
  73. queryWrapper.lambda().orderByAsc(GroupEntity::getSortCode).orderByAsc(GroupEntity::getCreatorTime);
  74. return this.list(queryWrapper);
  75. }
  76. @Override
  77. public Map<String, Object> getGroupMap() {
  78. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  79. queryWrapper.lambda().select(GroupEntity::getId, GroupEntity::getFullName);
  80. return this.list(queryWrapper).stream().collect(Collectors.toMap(GroupEntity::getId, GroupEntity::getFullName));
  81. }
  82. @Override
  83. public Map<String, Object> getGroupEncodeMap() {
  84. return getGroupEncodeMap(false);
  85. }
  86. @Override
  87. public Map<String, Object> getGroupEncodeMap(boolean enabledMark) {
  88. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  89. if (enabledMark) {
  90. queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1);
  91. }
  92. queryWrapper.lambda().select(GroupEntity::getId, GroupEntity::getFullName, GroupEntity::getEnCode);
  93. return this.list(queryWrapper).stream().collect(Collectors.toMap(group -> group.getFullName() + "/" + group.getEnCode(), GroupEntity::getId));
  94. }
  95. @Override
  96. public GroupEntity getInfo(String id) {
  97. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  98. queryWrapper.lambda().eq(GroupEntity::getId, id);
  99. return this.getOne(queryWrapper);
  100. }
  101. @Override
  102. public GroupEntity getInfo(String fullName, String enCode) {
  103. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  104. queryWrapper.lambda().eq(GroupEntity::getFullName, fullName);
  105. queryWrapper.lambda().eq(GroupEntity::getEnCode, enCode);
  106. return this.getOne(queryWrapper);
  107. }
  108. @Override
  109. public void crete(GroupEntity entity) {
  110. entity.setId(RandomUtil.uuId());
  111. if (StringUtil.isEmpty(entity.getEnCode())) {
  112. entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.YHZ), code -> this.isExistByEnCode(code, null)));
  113. }
  114. entity.setCreatorUserId(UserProvider.getUser().getUserId());
  115. entity.setCreatorTime(DateUtil.getNowDate());
  116. entity.setEnabledMark(1);
  117. this.save(entity);
  118. }
  119. @Override
  120. public Boolean update(String id, GroupEntity entity) {
  121. entity.setId(id);
  122. if (StringUtil.isEmpty(entity.getEnCode())) {
  123. entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.YHZ), code -> this.isExistByEnCode(code, id)));
  124. }
  125. entity.setLastModifyUserId(UserProvider.getUser().getUserId());
  126. entity.setLastModifyTime(DateUtil.getNowDate());
  127. entity.setEnabledMark(1);
  128. return this.updateById(entity);
  129. }
  130. @Override
  131. public void delete(GroupEntity entity) {
  132. this.removeById(entity.getId());
  133. }
  134. @Override
  135. public Boolean isExistByFullName(String fullName, String id) {
  136. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  137. queryWrapper.lambda().eq(GroupEntity::getFullName, fullName);
  138. if (!StringUtil.isEmpty(id)) {
  139. queryWrapper.lambda().ne(GroupEntity::getId, id);
  140. }
  141. return this.count(queryWrapper) > 0;
  142. }
  143. @Override
  144. public Boolean isExistByEnCode(String enCode, String id) {
  145. if (StringUtil.isEmpty(enCode)) return false;
  146. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  147. queryWrapper.lambda().eq(GroupEntity::getEnCode, enCode);
  148. if (!StringUtil.isEmpty(id)) {
  149. queryWrapper.lambda().ne(GroupEntity::getId, id);
  150. }
  151. return this.count(queryWrapper) > 0;
  152. }
  153. @Override
  154. public List<GroupEntity> getListByIds(List<String> idList) {
  155. return getListByIds(idList, true);
  156. }
  157. @Override
  158. public List<GroupEntity> getListByIds(List<String> idList, Boolean filterEnabledMark) {
  159. if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
  160. QueryWrapper<GroupEntity> queryWrapper = new QueryWrapper<>();
  161. queryWrapper.lambda().in(GroupEntity::getId, idList);
  162. if (idList.size() > 1000) {
  163. List<List<String>> lists = Lists.partition(idList, 1000);
  164. queryWrapper.lambda().and(t -> {
  165. for (List<String> list : lists) {
  166. t.in(GroupEntity::getId, list).or();
  167. }
  168. });
  169. } else {
  170. queryWrapper.lambda().in(GroupEntity::getId, idList);
  171. }
  172. if (filterEnabledMark) {
  173. queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1);
  174. }
  175. return this.list(queryWrapper);
  176. }
  177. }