42b70c8d521c45fa4bcc0e1f3a9c4614e17c9c04
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.repo;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Optional;
7
8 import javax.transaction.Transactional;
9
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;
18
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;
22
23 @Repository
24 @Transactional
25 public interface PermissionRepository extends BaseJpaRepository<Permission> {
26
27   @Override
28   public default Page<Permission> selectPageList(boolean loadAll, int pageIndex, int pageSize, Map<String, Object> mapBean, Map<String, String> orderBy) {
29     if (loadAll) {
30       pageIndex = 0;
31       pageSize = Integer.MAX_VALUE;
32     }
33     
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"));
42     }
43     
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());
51     
52     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
53     Example<Permission> example = Example.of(probe, matcher);
54     
55     Page<Permission> page = this.findAll(example, pageRequest);
56     
57     return page;
58   }
59   
60   
61   
62   @Query(value = "select max(p.rgt) from Permission p")
63   public int selectMaxRgt();
64   
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);
67   
68   @Modifying
69   @Query(value = "update TB_U_PERMISSION "
70       + "set "
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);
75
76   @Modifying
77   @Query(value = "update TB_U_PERMISSION "
78       + "set "
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);
83   
84   
85   @Override
86   public default Permission insert(Permission entity) {
87
88     if (entity.getParentId() == null) {
89       entity.setParentId("0");
90     }
91
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);
96       
97       entity.setLevel(1);
98     } else {
99       Permission parentEntity = this.selectById(entity.getParentId());
100       if (parentEntity == null) {
101         throw new RuntimeException(String.format("父级对象不存在!"));
102       } else {
103         // 将 lft或rgt 大于等于父级对象 rgt 的记录的 lft、rgt +offset
104         int rgt = parentEntity.getRgt();
105         int offset = 2;
106         updateLftRgtWhenInsert(rgt, offset);
107         
108         entity.setLft(rgt);
109         entity.setRgt(rgt + 1);
110         
111         entity.setLevel(parentEntity.getLevel() + 1);
112       }
113     }
114     
115     return BaseJpaRepository.super.insert(entity);
116   }
117   
118   @Override
119   public default Permission update(Permission entity) {
120
121     Permission originEntity = this.selectById(entity.getId());
122     if (originEntity == null) {
123       return null;
124     }
125     
126     //if (!this.checkFieldExists("code", entity.getCode(), entity.getId())) {
127     //  throw new RuntimeException(String.format("代码重复!"));
128     //}
129     
130     if (originEntity.getParentId() == null) {
131       originEntity.setParentId("0");
132     }
133     
134     if (entity.getParentId() == null) {
135       entity.setParentId("0");
136     }
137     
138     if (!originEntity.getParentId().equals(entity.getParentId()) ) {
139
140       int lft = originEntity.getLft();
141       int rgt = originEntity.getRgt();
142       int level = originEntity.getLevel();
143       int offset = rgt - lft +1;
144       
145       List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
146
147       if (entity.getParentId() == null || entity.getParentId().isEmpty() || "0".equals(entity.getParentId())) {
148         // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
149         updateLftRgtWhenDelete(rgt, offset);
150   
151         int maxRgt = selectMaxRgt();
152         entity.setLft((maxRgt+1));
153         entity.setRgt((maxRgt+1) + 1 +offset-2);
154         
155         entity.setLevel(1);
156       } else {
157         // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
158         updateLftRgtWhenDelete(rgt, offset);
159
160         Permission parentEntity = this.selectById(entity.getParentId());
161         if (parentEntity == null) {
162           throw new RuntimeException(String.format("父级对象不存在!"));
163         }
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("不能设置自身或自身的子节点作为父级!"));
167         }
168         
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);
174         
175         entity.setLft(pRgt);
176         entity.setRgt(pRgt + 1 + offset-2);
177         
178         entity.setLevel(parentEntity.getLevel() + 1);
179       }
180       
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());
188         
189         int cLft = childEntity.getLft();
190         int cRgt = childEntity.getRgt();
191         int cLevel = childEntity.getLevel();
192         
193         childEntity.setLft(cLft + (newLft - lft));
194         childEntity.setRgt(cRgt + (newRgt - rgt));
195         
196         childEntity.setLevel(cLevel + (newLevel - level));
197         
198         BaseJpaRepository.super.update(childEntity);
199       }
200
201     }
202     
203     return BaseJpaRepository.super.update(entity);
204   }
205
206   @Override
207   public default void delete(String id) {
208     
209     Permission originEntity = this.selectById(id);
210     if (originEntity == null) {
211       return;
212     }
213
214     int lft = originEntity.getLft();
215     int rgt = originEntity.getRgt();
216     int offset = rgt - lft +1;
217
218     // FIXME: 判断是否有子节点
219     //if (lft + 1 != rgt) {
220     //  return;
221     //}
222
223     List<Permission> childEntities = this.selectBetweenLftRgt(lft, rgt);
224     for (Permission childEntity : childEntities) {
225       BaseJpaRepository.super.delete(childEntity.getId());
226     }
227     
228     // 将 lft或rgt 大于等于该对象 rgt 的记录的 lft、rgt -offset
229     updateLftRgtWhenDelete(rgt, offset);
230     
231     BaseJpaRepository.super.delete(id);
232   }
233
234   public default Permission selectApplicationPermissionByCode(String code) {
235     Permission probe = new Permission();
236     probe.setCode(code);
237     probe.setType("1");
238     
239     ExampleMatcher matcher = ExampleMatcher.matching()
240         .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact())
241         .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
242     
243     Example<Permission> example = Example.of(probe, matcher);
244     
245     Optional<Permission> o = this.findOne(example);
246     
247     if (o.isPresent()) {
248       return o.get();
249     }
250     
251     return null;
252   }
253   
254  
255
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);
266   
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);
279
280   public default List<Permission> selectByUsername(String username, String applicationCode, String type) {
281     List<Permission> permissions = new ArrayList<Permission>();
282     
283     Permission applicationPermission = selectApplicationPermissionByCode(applicationCode);
284     if (applicationPermission == null) {
285       return permissions;
286     }
287     
288     int lft = applicationPermission.getLft();
289     int rgt = applicationPermission.getRgt();
290     
291     List<Permission> accountRolePermissions = selectAccountRolePermissionByUsername(username, lft, rgt, type);
292     permissions.addAll(accountRolePermissions);
293     
294     List<Permission> accountGroupRolePermissions = selectAccountGroupRolePermissionByUsername(username, lft, rgt, type);
295     permissions.addAll(accountGroupRolePermissions);
296     
297     return permissions;
298   }
299
300 }