1 package com.supwisdom.institute.backend.base.domain.repo;
3 import java.util.ArrayList;
6 import java.util.Optional;
8 import javax.persistence.criteria.CriteriaBuilder;
9 import javax.persistence.criteria.CriteriaQuery;
10 import javax.persistence.criteria.Predicate;
11 import javax.persistence.criteria.Root;
12 import javax.transaction.Transactional;
14 import org.springframework.data.domain.Example;
15 import org.springframework.data.domain.ExampleMatcher;
16 import org.springframework.data.domain.Page;
17 import org.springframework.data.domain.PageRequest;
18 import org.springframework.data.domain.Sort;
19 import org.springframework.data.jpa.domain.Specification;
20 import org.springframework.data.jpa.repository.Modifying;
21 import org.springframework.data.jpa.repository.Query;
22 import org.springframework.data.repository.query.Param;
23 import org.springframework.stereotype.Repository;
24 import org.springframework.util.StringUtils;
26 import com.supwisdom.institute.backend.base.domain.entity.Permission;
27 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
28 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
32 public interface PermissionRepository extends BaseJpaRepository<Permission> {
36 public default Specification<Permission> convertToSpec(Map<String, Object> mapBean) {
38 Specification<Permission> spec = new Specification<Permission>() {
43 private static final long serialVersionUID = 6195601104797641573L;
46 public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
47 List<Predicate> predicates = new ArrayList<>();
49 if (mapBean != null) {
51 if (MapBeanUtils.getBoolean(mapBean, "deleted") != null) {
52 predicates.add(criteriaBuilder.equal(root.get("deleted"), MapBeanUtils.getBoolean(mapBean, "deleted")));
55 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "code"))) {
56 predicates.add(criteriaBuilder.like(root.get("code"), "%" + MapBeanUtils.getString(mapBean, "code") + "%"));
58 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "name"))) {
59 predicates.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "name") + "%"));
62 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "status"))) {
63 predicates.add(criteriaBuilder.equal(root.get("status"), MapBeanUtils.getString(mapBean, "status")));
66 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "type"))) {
67 predicates.add(criteriaBuilder.equal(root.get("type"), MapBeanUtils.getString(mapBean, "type")));
70 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "url"))) {
71 predicates.add(criteriaBuilder.like(root.get("url"), "%" + MapBeanUtils.getString(mapBean, "url") + "%"));
74 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "applicationId"))) {
75 predicates.add(criteriaBuilder.equal(root.get("applicationId"), MapBeanUtils.getString(mapBean, "applicationId")));
78 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "parentId"))) {
79 predicates.add(criteriaBuilder.equal(root.get("parentId"), MapBeanUtils.getString(mapBean, "parentId")));
82 // if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeBegin"))) {
83 // String grantTimeBegin = MapBeanUtils.getString(mapBean, "grantTimeBegin");
84 // Date d = DateUtil.parseDate(grantTimeBegin+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
87 // predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("grantTime"), d));
91 // if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeEnd"))) {
92 // String grantTimeEnd = MapBeanUtils.getString(mapBean, "grantTimeEnd");
93 // Date d = DateUtil.parseDate(grantTimeEnd+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
96 // predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("grantTime"), d));
100 List<Predicate> predicatesKeyword = new ArrayList<>();
101 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "keyword"))) {
102 predicatesKeyword.add(criteriaBuilder.like(root.get("code"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
103 predicatesKeyword.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
104 predicatesKeyword.add(criteriaBuilder.like(root.get("memo"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
106 predicates.add(criteriaBuilder.or(predicatesKeyword.toArray(new Predicate[predicatesKeyword.size()])));
110 return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
119 public default Page<Permission> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
122 pageSize = Integer.MAX_VALUE;
125 Permission probe = new Permission();
126 if (mapBean != null) {
127 probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
128 probe.setCode(MapBeanUtils.getString(mapBean, "code"));
129 probe.setName(MapBeanUtils.getString(mapBean, "name"));
130 probe.setMemo(MapBeanUtils.getString(mapBean, "memo"));
131 probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
132 probe.setType(MapBeanUtils.getString(mapBean, "type"));
135 ExampleMatcher matcher = ExampleMatcher.matching()
136 .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
137 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.contains())
138 .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
139 .withMatcher("memo", ExampleMatcher.GenericPropertyMatchers.contains())
140 .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
141 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
143 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
144 Example<Permission> example = Example.of(probe, matcher);
146 Page<Permission> page = this.findAll(example, pageRequest);
153 @Query(value = "select max(p.rgt) from Permission p")
154 public int selectMaxRgt();
156 @Query(value = "select p from Permission p where p.lft>:lft and p.rgt<:rgt order by p.lft")
157 public List<Permission> selectBetweenLftRgt(@Param("lft") int lft, @Param("rgt") int rgt);
160 @Query(value = "update TB_BASE_PERMISSION "
162 + " LFT = (case when LFT >= :rgt then LFT + :offset else LFT end), "
163 + " RGT = RGT + :offset "
164 + "where RGT >= :rgt", nativeQuery = true)
165 public int updateLftRgtWhenInsert(@Param("rgt") int rgt, @Param("offset") int offset);
168 @Query(value = "update TB_BASE_PERMISSION "
170 + " LFT = (case when LFT >= :rgt then LFT - :offset else LFT end), "
171 + " RGT = RGT - :offset "
172 + "where RGT >= :rgt", nativeQuery = true)
173 public int updateLftRgtWhenDelete(@Param("rgt") int rgt, @Param("offset") int offset);
177 public default Permission insert(Permission entity) {
179 if (entity.getParentId() == null) {
180 entity.setParentId("0");
183 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
184 int maxRgt = selectMaxRgt();
185 entity.setLft((maxRgt+1));
186 entity.setRgt((maxRgt+1) + 1);
190 Permission parentEntity = this.selectById(entity.getParentId());
191 if (parentEntity == null) {
192 throw new RuntimeException(String.format("父级对象不存在!"));
194 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
195 int rgt = parentEntity.getRgt();
197 updateLftRgtWhenInsert(rgt, offset);
200 entity.setRgt(rgt + 1);
202 entity.setLevel(parentEntity.getLevel() + 1);
206 return BaseJpaRepository.super.insert(entity);
210 public default Permission update(Permission entity) {
212 Permission originEntity = this.selectById(entity.getId());
213 if (originEntity == null) {
217 //if (!this.checkFieldExists("code", entity.getCode(), entity.getId())) {
218 // throw new RuntimeException(String.format("代码重复!"));
221 if (originEntity.getParentId() == null) {
222 originEntity.setParentId("0");
225 if (entity.getParentId() == null) {
226 entity.setParentId("0");
229 if (!originEntity.getParentId().equals(entity.getParentId()) ) {
231 int lft = originEntity.getLft();
232 int rgt = originEntity.getRgt();
233 int level = originEntity.getLevel();
234 int offset = rgt - lft +1;
236 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
238 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
239 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
240 updateLftRgtWhenDelete(rgt, offset);
242 int maxRgt = selectMaxRgt();
243 entity.setLft((maxRgt+1));
244 entity.setRgt((maxRgt+1) + 1 +offset-2);
248 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
249 updateLftRgtWhenDelete(rgt, offset);
251 Permission parentEntity = this.selectById(entity.getParentId());
252 if (parentEntity == null) {
253 throw new RuntimeException(String.format("父级对象不存在!"));
255 //System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
256 if (parentEntity.getLft() >= originEntity.getLft() && parentEntity.getRgt() <= originEntity.getRgt()) {
257 throw new RuntimeException(String.format("不能设置自身或自身的子节点作为父级!"));
260 //parentEntity = this.selectById(entity.getParentId()); System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
261 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
262 //int pLft = parentEntity.getLft();
263 int pRgt = parentEntity.getRgt();
264 updateLftRgtWhenInsert(pRgt, offset);
267 entity.setRgt(pRgt + 1 + offset-2);
269 entity.setLevel(parentEntity.getLevel() + 1);
272 int newLft = entity.getLft();
273 int newRgt = entity.getRgt();
274 int newLevel = entity.getLevel();
275 //System.out.println(String.format("newLft %s, newRgt %s, newLevel %s", newLft, newRgt, newLevel));
276 //System.out.println(String.format("lft %s, rgt %s, level %s", lft, rgt, level));
277 for (Permission childEntity : childEntities) {
278 //Permission pEntity = this.selectById(childEntity.getParentId());
280 int cLft = childEntity.getLft();
281 int cRgt = childEntity.getRgt();
282 int cLevel = childEntity.getLevel();
284 childEntity.setLft(cLft + (newLft - lft));
285 childEntity.setRgt(cRgt + (newRgt - rgt));
287 childEntity.setLevel(cLevel + (newLevel - level));
289 BaseJpaRepository.super.update(childEntity);
294 return BaseJpaRepository.super.update(entity);
298 public default void delete(String id) {
300 Permission originEntity = this.selectById(id);
301 if (originEntity == null) {
305 int lft = originEntity.getLft();
306 int rgt = originEntity.getRgt();
307 int offset = rgt - lft +1;
310 //if (lft + 1 != rgt) {
314 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
315 for (Permission childEntity : childEntities) {
316 BaseJpaRepository.super.delete(childEntity.getId());
319 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
320 updateLftRgtWhenDelete(rgt, offset);
322 BaseJpaRepository.super.delete(id);
325 public default Permission selectApplicationPermissionByCode(String code) {
326 Permission probe = new Permission();
330 ExampleMatcher matcher = ExampleMatcher.matching()
331 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact())
332 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
334 Example<Permission> example = Example.of(probe, matcher);
336 Optional<Permission> o = this.findOne(example);
347 @Query(value = "select p from Permission p "
348 + "inner join RolePermission rp on p.id=rp.permissionId "
349 + "inner join Role r on rp.roleId=r.id "
350 + "inner join AccountRole ar on r.id=ar.roleId "
351 + "inner join Account a on ar.accountId=a.id "
352 + "where a.username=:username "
353 + "and p.lft >= :lft and p.rgt <= :rgt "
354 + "and (:type is null or p.type=:type) "
355 + "and p.status='1' and r.status='1' and a.status='1' and a.enabled=1 ")
356 public List<Permission> selectAccountRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
358 @Query(value = "select p from Permission p "
359 + "inner join RolePermission rp on p.id=rp.permissionId "
360 + "inner join Role r on rp.roleId=r.id "
361 + "inner join GroupRole gr on r.id=gr.roleId "
362 + "inner join Group_ g on gr.groupId=g.id "
363 + "inner join AccountGroup ag on g.id=ag.groupId "
364 + "inner join Account a on ag.accountId=a.id "
365 + "where a.username=:username "
366 + "and p.lft >= :lft and p.rgt <= :rgt "
367 + "and (:type is null or p.type=:type) "
368 + "and p.status='1' and r.status='1' and g.status='1' and a.status='1' and a.enabled=1 ")
369 public List<Permission> selectAccountGroupRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
371 public default List<Permission> selectByUsername(String username, String applicationCode, String type) {
372 List<Permission> permissions = new ArrayList<Permission>();
374 Permission applicationPermission = selectApplicationPermissionByCode(applicationCode);
375 if (applicationPermission == null) {
379 int lft = applicationPermission.getLft();
380 int rgt = applicationPermission.getRgt();
382 List<Permission> accountRolePermissions = selectAccountRolePermissionByUsername(username, lft, rgt, type);
383 permissions.addAll(accountRolePermissions);
385 List<Permission> accountGroupRolePermissions = selectAccountGroupRolePermissionByUsername(username, lft, rgt, type);
386 permissions.addAll(accountGroupRolePermissions);
394 public default List<Permission> selectList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
396 Specification<Permission> spec = convertToSpec(mapBean);
400 pageSize = Integer.MAX_VALUE;
403 Sort sort = convertToSort(orderBy);
406 return this.findAll(spec);
408 return this.findAll(spec, sort);