8f0386e5bc74495e92bbb4b4b3241ac4177bb05c
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.repo;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Optional;
7
8 import org.springframework.data.domain.Example;
9 import org.springframework.data.domain.ExampleMatcher;
10 import org.springframework.data.domain.Page;
11 import org.springframework.data.domain.PageRequest;
12 import org.springframework.stereotype.Repository;
13
14 import com.supwisdom.institute.backend.common.framework.repo.BaseJpaRepository;
15 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
16 import com.supwisdom.institute.backend.system.domain.entity.Group;
17 import com.supwisdom.institute.backend.system.domain.entity.GroupRole;
18 import com.supwisdom.institute.backend.system.domain.entity.Role;
19
20 @Repository
21 public interface GroupRoleRepository extends BaseJpaRepository<GroupRole> {
22
23   public default Page<GroupRole> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
24     GroupRole probe = new GroupRole();
25     if (mapBean != null) {
26       probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
27       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
28     }
29
30     ExampleMatcher matcher = ExampleMatcher.matching()
31         .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
32         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
33
34     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
35     Example<GroupRole> example = Example.of(probe, matcher);
36
37     Page<GroupRole> page = this.findAll(example, pageRequest);
38
39     return page;
40   }
41
42   public default Page<GroupRole> selectGroupRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
43
44     GroupRole probe = new GroupRole();
45     if (mapBean != null) {
46       probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
47       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
48     }
49
50     ExampleMatcher matcher = ExampleMatcher.matching()
51         .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
52         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
53
54     Example<GroupRole> example = Example.of(probe, matcher);
55
56     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
57
58     Page<GroupRole> page = this.findAll(example, pageRequest);  // FIXME: 多表关联查询
59
60     return page;
61   }
62
63   public default void relateGroupRoles(Group group, List<GroupRole> groupRoles) {
64
65     List<GroupRole> existGroupRoles = this.selectListByGroupId(group.getId());
66
67     Map<String, GroupRole> existMapGroupRoles = new LinkedHashMap<String, GroupRole>();
68     for (GroupRole groupRole : existGroupRoles) {
69       String k = String.format("%s", groupRole.getRoleId());
70       existMapGroupRoles.put(k, groupRole);
71     }
72
73     for (GroupRole groupRole : groupRoles) {
74       String k = String.format("%s", groupRole.getRoleId());
75
76       if (existMapGroupRoles.containsKey(k)) {
77         existMapGroupRoles.remove(k);
78       } else {
79         groupRole.setCompanyId(group.getCompanyId());
80         groupRole.setGroupId(group.getId());
81
82         this.insert(groupRole);
83       }
84     }
85
86     for (GroupRole groupRole : existMapGroupRoles.values()) {
87       this.deleteById(groupRole.getId());
88     }
89   }
90
91   public default List<GroupRole> selectListByGroupId(String groupId) {
92
93     GroupRole probe = new GroupRole();
94     probe.setGroupId(groupId);
95
96     ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("groupId",
97         ExampleMatcher.GenericPropertyMatchers.exact());
98
99     Example<GroupRole> example = Example.of(probe, matcher);
100
101     List<GroupRole> groupRoles = this.findAll(example);
102
103     return groupRoles;
104   }
105
106
107   public default void relateRoleGroups(Role role, List<GroupRole> groupRoles) {
108
109     List<GroupRole> existRoleGroups = this.selectListByRoleId(role.getCode());
110
111     Map<String, GroupRole> existMapRoleGroups = new LinkedHashMap<String, GroupRole>();
112     for (GroupRole groupRole : existRoleGroups) {
113       String k = String.format("%s", groupRole.getGroupId());
114       existMapRoleGroups.put(k, groupRole);
115     }
116
117     for (GroupRole groupRole : groupRoles) {
118       String k = String.format("%s", groupRole.getGroupId());
119
120       if (existMapRoleGroups.containsKey(k)) {
121         existMapRoleGroups.remove(k);
122       } else {
123         groupRole.setCompanyId(role.getCompanyId());
124         groupRole.setRoleId(role.getId());
125
126         this.insert(groupRole);
127       }
128     }
129
130     for (GroupRole groupRole : existMapRoleGroups.values()) {
131       this.deleteById(groupRole.getId());
132     }
133   }
134
135   public default List<GroupRole> selectListByRoleId(String roleId) {
136
137     GroupRole probe = new GroupRole();
138     probe.setRoleId(roleId);
139
140     ExampleMatcher matcher = ExampleMatcher.matching()
141         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
142
143     Example<GroupRole> example = Example.of(probe, matcher);
144
145     List<GroupRole> groupRoles = this.findAll(example);
146
147     return groupRoles;
148   }
149
150   public default GroupRole selectOneByGroupRole(String groupId, String roleId) {
151
152     GroupRole probe = new GroupRole();
153     probe.setGroupId(groupId);
154     probe.setRoleId(roleId);
155
156     ExampleMatcher matcher = ExampleMatcher.matching()
157         .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
158         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact())
159         ;
160
161     Example<GroupRole> example = Example.of(probe, matcher);
162     
163     Optional<GroupRole> o = this.findOne(example);
164     
165     return o.isPresent() ? o.get() : null;
166   }
167
168   public default void addGroupRole(String groupId, String roleId) {
169
170     GroupRole groupRole = this.selectOneByGroupRole(groupId, roleId);
171     
172     if (groupRole == null) {
173       groupRole = new GroupRole();
174       //groupRole.setCompanyId(companyId);
175       groupRole.setGroupId(groupId);
176       groupRole.setRoleId(roleId);
177       
178       this.insert(groupRole);
179     }
180   }
181
182   public default void removeGroupRole(String groupId, String roleId) {
183
184     GroupRole groupRole = this.selectOneByGroupRole(groupId, roleId);
185     
186     if (groupRole != null) {
187       this.deleteById(groupRole.getId());
188     }
189   }
190
191 }