1 package com.supwisdom.institute.backend.system.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.common.framework.repo.BaseJpaRepository;
27 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
28 import com.supwisdom.institute.backend.system.domain.entity.Permission;
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 = 9071470982419099273L;
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, "parentId"))) {
75 predicates.add(criteriaBuilder.equal(root.get("parentId"), MapBeanUtils.getString(mapBean, "parentId")));
78 // if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeBegin"))) {
79 // String grantTimeBegin = MapBeanUtils.getString(mapBean, "grantTimeBegin");
80 // Date d = DateUtil.parseDate(grantTimeBegin+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
83 // predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("grantTime"), d));
87 // if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "grantTimeEnd"))) {
88 // String grantTimeEnd = MapBeanUtils.getString(mapBean, "grantTimeEnd");
89 // Date d = DateUtil.parseDate(grantTimeEnd+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
92 // predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("grantTime"), d));
96 List<Predicate> predicatesKeyword = new ArrayList<>();
97 if (!StringUtils.isEmpty(MapBeanUtils.getString(mapBean, "keyword"))) {
98 predicatesKeyword.add(criteriaBuilder.like(root.get("username"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
99 predicatesKeyword.add(criteriaBuilder.like(root.get("name"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
100 predicatesKeyword.add(criteriaBuilder.like(root.get("memo"), "%" + MapBeanUtils.getString(mapBean, "keyword") + "%"));
102 predicates.add(criteriaBuilder.or(predicatesKeyword.toArray(new Predicate[predicatesKeyword.size()])));
106 return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
115 public default Page<Permission> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
118 pageSize = Integer.MAX_VALUE;
121 Permission probe = new Permission();
122 if (mapBean != null) {
123 probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
124 probe.setCode(MapBeanUtils.getString(mapBean, "code"));
125 probe.setName(MapBeanUtils.getString(mapBean, "name"));
126 probe.setMemo(MapBeanUtils.getString(mapBean, "memo"));
127 probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
128 probe.setType(MapBeanUtils.getString(mapBean, "type"));
131 ExampleMatcher matcher = ExampleMatcher.matching()
132 .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
133 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.contains())
134 .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
135 .withMatcher("memo", ExampleMatcher.GenericPropertyMatchers.contains())
136 .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
137 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
139 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
140 Example<Permission> example = Example.of(probe, matcher);
142 Page<Permission> page = this.findAll(example, pageRequest);
149 @Query(value = "select max(p.rgt) from Permission p")
150 public int selectMaxRgt();
152 @Query(value = "select p from Permission p where p.lft>:lft and p.rgt<:rgt order by p.lft")
153 public List<Permission> selectBetweenLftRgt(@Param("lft") int lft, @Param("rgt") int rgt);
156 @Query(value = "update TB_U_PERMISSION "
158 + " LFT = (case when LFT >= :rgt then LFT + :offset else LFT end), "
159 + " RGT = RGT + :offset "
160 + "where RGT >= :rgt", nativeQuery = true)
161 public int updateLftRgtWhenInsert(@Param("rgt") int rgt, @Param("offset") int offset);
164 @Query(value = "update TB_U_PERMISSION "
166 + " LFT = (case when LFT >= :rgt then LFT - :offset else LFT end), "
167 + " RGT = RGT - :offset "
168 + "where RGT >= :rgt", nativeQuery = true)
169 public int updateLftRgtWhenDelete(@Param("rgt") int rgt, @Param("offset") int offset);
173 public default Permission insert(Permission entity) {
175 if (entity.getParentId() == null) {
176 entity.setParentId("0");
179 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
180 int maxRgt = selectMaxRgt();
181 entity.setLft((maxRgt+1));
182 entity.setRgt((maxRgt+1) + 1);
186 Permission parentEntity = this.selectById(entity.getParentId());
187 if (parentEntity == null) {
188 throw new RuntimeException(String.format("父级对象不存在!"));
190 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
191 int rgt = parentEntity.getRgt();
193 updateLftRgtWhenInsert(rgt, offset);
196 entity.setRgt(rgt + 1);
198 entity.setLevel(parentEntity.getLevel() + 1);
202 return BaseJpaRepository.super.insert(entity);
206 public default Permission update(Permission entity) {
208 Permission originEntity = this.selectById(entity.getId());
209 if (originEntity == null) {
213 //if (!this.checkFieldExists("code", entity.getCode(), entity.getId())) {
214 // throw new RuntimeException(String.format("代码重复!"));
217 if (originEntity.getParentId() == null) {
218 originEntity.setParentId("0");
221 if (entity.getParentId() == null) {
222 entity.setParentId("0");
225 if (!originEntity.getParentId().equals(entity.getParentId()) ) {
227 int lft = originEntity.getLft();
228 int rgt = originEntity.getRgt();
229 int level = originEntity.getLevel();
230 int offset = rgt - lft +1;
232 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
234 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
235 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
236 updateLftRgtWhenDelete(rgt, offset);
238 int maxRgt = selectMaxRgt();
239 entity.setLft((maxRgt+1));
240 entity.setRgt((maxRgt+1) + 1 +offset-2);
244 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
245 updateLftRgtWhenDelete(rgt, offset);
247 Permission parentEntity = this.selectById(entity.getParentId());
248 if (parentEntity == null) {
249 throw new RuntimeException(String.format("父级对象不存在!"));
251 //System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
252 if (parentEntity.getLft() >= originEntity.getLft() && parentEntity.getRgt() <= originEntity.getRgt()) {
253 throw new RuntimeException(String.format("不能设置自身或自身的子节点作为父级!"));
256 //parentEntity = this.selectById(entity.getParentId()); System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
257 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
258 //int pLft = parentEntity.getLft();
259 int pRgt = parentEntity.getRgt();
260 updateLftRgtWhenInsert(pRgt, offset);
263 entity.setRgt(pRgt + 1 + offset-2);
265 entity.setLevel(parentEntity.getLevel() + 1);
268 int newLft = entity.getLft();
269 int newRgt = entity.getRgt();
270 int newLevel = entity.getLevel();
271 //System.out.println(String.format("newLft %s, newRgt %s, newLevel %s", newLft, newRgt, newLevel));
272 //System.out.println(String.format("lft %s, rgt %s, level %s", lft, rgt, level));
273 for (Permission childEntity : childEntities) {
274 //Permission pEntity = this.selectById(childEntity.getParentId());
276 int cLft = childEntity.getLft();
277 int cRgt = childEntity.getRgt();
278 int cLevel = childEntity.getLevel();
280 childEntity.setLft(cLft + (newLft - lft));
281 childEntity.setRgt(cRgt + (newRgt - rgt));
283 childEntity.setLevel(cLevel + (newLevel - level));
285 BaseJpaRepository.super.update(childEntity);
290 return BaseJpaRepository.super.update(entity);
294 public default void delete(String id) {
296 Permission originEntity = this.selectById(id);
297 if (originEntity == null) {
301 int lft = originEntity.getLft();
302 int rgt = originEntity.getRgt();
303 int offset = rgt - lft +1;
306 //if (lft + 1 != rgt) {
310 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
311 for (Permission childEntity : childEntities) {
312 BaseJpaRepository.super.delete(childEntity.getId());
315 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
316 updateLftRgtWhenDelete(rgt, offset);
318 BaseJpaRepository.super.delete(id);
321 public default Permission selectApplicationPermissionByCode(String code) {
322 Permission probe = new Permission();
326 ExampleMatcher matcher = ExampleMatcher.matching()
327 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact())
328 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
330 Example<Permission> example = Example.of(probe, matcher);
332 Optional<Permission> o = this.findOne(example);
343 @Query(value = "select p from Permission p "
344 + "inner join RolePermission rp on p.id=rp.permissionId "
345 + "inner join Role r on rp.roleId=r.id "
346 + "inner join AccountRole ar on r.id=ar.roleId "
347 + "inner join Account a on ar.accountId=a.id "
348 + "where a.username=:username "
349 + "and p.lft >= :lft and p.rgt <= :rgt "
350 + "and (:type is null or p.type=:type) "
351 + "and p.status='1' and r.status='1' and a.status='1' and a.enabled=1 ")
352 public List<Permission> selectAccountRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
354 @Query(value = "select p from Permission p "
355 + "inner join RolePermission rp on p.id=rp.permissionId "
356 + "inner join Role r on rp.roleId=r.id "
357 + "inner join GroupRole gr on r.id=gr.roleId "
358 + "inner join Group_ g on gr.groupId=g.id "
359 + "inner join AccountGroup ag on g.id=ag.groupId "
360 + "inner join Account a on ag.accountId=a.id "
361 + "where a.username=:username "
362 + "and p.lft >= :lft and p.rgt <= :rgt "
363 + "and (:type is null or p.type=:type) "
364 + "and p.status='1' and r.status='1' and g.status='1' and a.status='1' and a.enabled=1 ")
365 public List<Permission> selectAccountGroupRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
367 public default List<Permission> selectByUsername(String username, String applicationCode, String type) {
368 List<Permission> permissions = new ArrayList<Permission>();
370 Permission applicationPermission = selectApplicationPermissionByCode(applicationCode);
371 if (applicationPermission == null) {
375 int lft = applicationPermission.getLft();
376 int rgt = applicationPermission.getRgt();
378 List<Permission> accountRolePermissions = selectAccountRolePermissionByUsername(username, lft, rgt, type);
379 permissions.addAll(accountRolePermissions);
381 List<Permission> accountGroupRolePermissions = selectAccountGroupRolePermissionByUsername(username, lft, rgt, type);
382 permissions.addAll(accountGroupRolePermissions);
390 public default List<Permission> selectList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
392 Specification<Permission> spec = convertToSpec(mapBean);
396 pageSize = Integer.MAX_VALUE;
399 Sort sort = convertToSort(orderBy);
402 return this.findAll(spec);
404 return this.findAll(spec, sort);