1 package com.supwisdom.institute.backend.system.domain.repo;
3 import java.util.ArrayList;
6 import java.util.Optional;
8 import javax.transaction.Transactional;
10 import org.springframework.data.domain.Example;
11 import org.springframework.data.domain.ExampleMatcher;
12 import org.springframework.data.domain.Page;
13 import org.springframework.data.domain.PageRequest;
14 import org.springframework.data.jpa.repository.Modifying;
15 import org.springframework.data.jpa.repository.Query;
16 import org.springframework.data.repository.query.Param;
17 import org.springframework.stereotype.Repository;
19 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
20 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
21 import com.supwisdom.institute.backend.system.domain.entity.Permission;
25 public interface PermissionRepository extends BaseJpaRepository<Permission> {
28 public default Page<Permission> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
31 pageSize = Integer.MAX_VALUE;
34 Permission probe = new Permission();
35 if (mapBean != null) {
36 probe.setDeleted(MapBeanUtils.getBoolean(mapBean, "deleted"));
37 probe.setCode(MapBeanUtils.getString(mapBean, "code"));
38 probe.setName(MapBeanUtils.getString(mapBean, "name"));
39 probe.setMemo(MapBeanUtils.getString(mapBean, "memo"));
40 probe.setStatus(MapBeanUtils.getString(mapBean, "status"));
41 probe.setType(MapBeanUtils.getString(mapBean, "type"));
44 ExampleMatcher matcher = ExampleMatcher.matching()
45 .withMatcher("deleted", ExampleMatcher.GenericPropertyMatchers.exact())
46 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.contains())
47 .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
48 .withMatcher("memo", ExampleMatcher.GenericPropertyMatchers.contains())
49 .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
50 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
52 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
53 Example<Permission> example = Example.of(probe, matcher);
55 Page<Permission> page = this.findAll(example, pageRequest);
62 @Query(value = "select max(p.rgt) from Permission p")
63 public int selectMaxRgt();
65 @Query(value = "select p from Permission p where p.lft>:lft and p.rgt<:rgt order by p.lft")
66 public List<Permission> selectBetweenLftRgt(@Param("lft") int lft, @Param("rgt") int rgt);
69 @Query(value = "update TB_U_PERMISSION "
71 + " LFT = (case when LFT >= :rgt then LFT + :offset else LFT end), "
72 + " RGT = RGT + :offset "
73 + "where RGT >= :rgt", nativeQuery = true)
74 public int updateLftRgtWhenInsert(@Param("rgt") int rgt, @Param("offset") int offset);
77 @Query(value = "update TB_U_PERMISSION "
79 + " LFT = (case when LFT >= :rgt then LFT - :offset else LFT end), "
80 + " RGT = RGT - :offset "
81 + "where RGT >= :rgt", nativeQuery = true)
82 public int updateLftRgtWhenDelete(@Param("rgt") int rgt, @Param("offset") int offset);
86 public default Permission insert(Permission entity) {
88 if (entity.getParentId() == null) {
89 entity.setParentId("0");
92 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
93 int maxRgt = selectMaxRgt();
94 entity.setLft((maxRgt+1));
95 entity.setRgt((maxRgt+1) + 1);
99 Permission parentEntity = this.selectById(entity.getParentId());
100 if (parentEntity == null) {
101 throw new RuntimeException(String.format("父级对象不存在!"));
103 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
104 int rgt = parentEntity.getRgt();
106 updateLftRgtWhenInsert(rgt, offset);
109 entity.setRgt(rgt + 1);
111 entity.setLevel(parentEntity.getLevel() + 1);
115 return BaseJpaRepository.super.insert(entity);
119 public default Permission update(Permission entity) {
121 Permission originEntity = this.selectById(entity.getId());
122 if (originEntity == null) {
126 //if (!this.checkFieldExists("code", entity.getCode(), entity.getId())) {
127 // throw new RuntimeException(String.format("代码重复!"));
130 if (originEntity.getParentId() == null) {
131 originEntity.setParentId("0");
134 if (entity.getParentId() == null) {
135 entity.setParentId("0");
138 if (!originEntity.getParentId().equals(entity.getParentId()) ) {
140 int lft = originEntity.getLft();
141 int rgt = originEntity.getRgt();
142 int level = originEntity.getLevel();
143 int offset = rgt - lft +1;
145 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
147 if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
148 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
149 updateLftRgtWhenDelete(rgt, offset);
151 int maxRgt = selectMaxRgt();
152 entity.setLft((maxRgt+1));
153 entity.setRgt((maxRgt+1) + 1 +offset-2);
157 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
158 updateLftRgtWhenDelete(rgt, offset);
160 Permission parentEntity = this.selectById(entity.getParentId());
161 if (parentEntity == null) {
162 throw new RuntimeException(String.format("父级对象不存在!"));
164 //System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
165 if (parentEntity.getLft() >= originEntity.getLft() && parentEntity.getRgt() <= originEntity.getRgt()) {
166 throw new RuntimeException(String.format("不能设置自身或自身的子节点作为父级!"));
169 //parentEntity = this.selectById(entity.getParentId()); System.out.println(String.format("pLft %s, pRgt %s", parentEntity.getLft(), parentEntity.getRgt()));
170 // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
171 //int pLft = parentEntity.getLft();
172 int pRgt = parentEntity.getRgt();
173 updateLftRgtWhenInsert(pRgt, offset);
176 entity.setRgt(pRgt + 1 + offset-2);
178 entity.setLevel(parentEntity.getLevel() + 1);
181 int newLft = entity.getLft();
182 int newRgt = entity.getRgt();
183 int newLevel = entity.getLevel();
184 //System.out.println(String.format("newLft %s, newRgt %s, newLevel %s", newLft, newRgt, newLevel));
185 //System.out.println(String.format("lft %s, rgt %s, level %s", lft, rgt, level));
186 for (Permission childEntity : childEntities) {
187 //Permission pEntity = this.selectById(childEntity.getParentId());
189 int cLft = childEntity.getLft();
190 int cRgt = childEntity.getRgt();
191 int cLevel = childEntity.getLevel();
193 childEntity.setLft(cLft + (newLft - lft));
194 childEntity.setRgt(cRgt + (newRgt - rgt));
196 childEntity.setLevel(cLevel + (newLevel - level));
198 BaseJpaRepository.super.update(childEntity);
203 return BaseJpaRepository.super.update(entity);
207 public default void delete(String id) {
209 Permission originEntity = this.selectById(id);
210 if (originEntity == null) {
214 int lft = originEntity.getLft();
215 int rgt = originEntity.getRgt();
216 int offset = rgt - lft +1;
219 //if (lft + 1 != rgt) {
223 List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
224 for (Permission childEntity : childEntities) {
225 BaseJpaRepository.super.delete(childEntity.getId());
228 // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
229 updateLftRgtWhenDelete(rgt, offset);
231 BaseJpaRepository.super.delete(id);
234 public default Permission selectApplicationPermissionByCode(String code) {
235 Permission probe = new Permission();
239 ExampleMatcher matcher = ExampleMatcher.matching()
240 .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact())
241 .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
243 Example<Permission> example = Example.of(probe, matcher);
245 Optional<Permission> o = this.findOne(example);
256 @Query(value = "select p from Permission p "
257 + "inner join RolePermission rp on p.id=rp.permissionId "
258 + "inner join Role r on rp.roleId=r.id "
259 + "inner join AccountRole ar on r.id=ar.roleId "
260 + "inner join Account a on ar.accountId=a.id "
261 + "where a.username=:username "
262 + "and p.lft >= :lft and p.rgt <= :rgt "
263 + "and (:type is null or p.type=:type) "
264 + "and p.status='1' and r.status='1' and a.status='1' and a.enabled=1 ")
265 public List<Permission> selectAccountRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
267 @Query(value = "select p from Permission p "
268 + "inner join RolePermission rp on p.id=rp.permissionId "
269 + "inner join Role r on rp.roleId=r.id "
270 + "inner join GroupRole gr on r.id=gr.roleId "
271 + "inner join Group_ g on gr.groupId=g.id "
272 + "inner join AccountGroup ag on g.id=ag.groupId "
273 + "inner join Account a on ag.accountId=a.id "
274 + "where a.username=:username "
275 + "and p.lft >= :lft and p.rgt <= :rgt "
276 + "and (:type is null or p.type=:type) "
277 + "and p.status='1' and r.status='1' and g.status='1' and a.status='1' and a.enabled=1 ")
278 public List<Permission> selectAccountGroupRolePermissionByUsername(@Param("username") String username, @Param("lft") int lft, @Param("rgt") int rgt, @Param("type") String type);
280 public default List<Permission> selectByUsername(String username, String applicationCode, String type) {
281 List<Permission> permissions = new ArrayList<Permission>();
283 Permission applicationPermission = selectApplicationPermissionByCode(applicationCode);
284 if (applicationPermission == null) {
288 int lft = applicationPermission.getLft();
289 int rgt = applicationPermission.getRgt();
291 List<Permission> accountRolePermissions = selectAccountRolePermissionByUsername(username, lft, rgt, type);
292 permissions.addAll(accountRolePermissions);
294 List<Permission> accountGroupRolePermissions = selectAccountGroupRolePermissionByUsername(username, lft, rgt, type);
295 permissions.addAll(accountGroupRolePermissions);