822bd9ff6ffc876b626a393d2c43b71770a5530d
[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.Role;
17 import com.supwisdom.institute.backend.system.domain.entity.Account;
18 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
19
20 @Repository
21 public interface AccountRoleRepository extends BaseJpaRepository<AccountRole> {
22
23   public default Page<AccountRole> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
24
25     AccountRole probe = new AccountRole();
26     if (mapBean != null) {
27       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
28       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
29     }
30
31     ExampleMatcher matcher = ExampleMatcher.matching()
32         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
33         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
34
35     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
36     Example<AccountRole> example = Example.of(probe, matcher);
37
38     Page<AccountRole> page = this.findAll(example, pageRequest);
39
40     return page;
41   }
42
43   public default Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
44
45     AccountRole probe = new AccountRole();
46     if (mapBean != null) {
47       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
48       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
49     }
50
51     ExampleMatcher matcher = ExampleMatcher.matching()
52         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
53         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
54
55     Example<AccountRole> example = Example.of(probe, matcher);
56
57     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
58
59     Page<AccountRole> page = this.findAll(example, pageRequest); // FIXME: 多表关联查询
60
61     return page;
62   }
63
64   public default void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
65
66     List<AccountRole> existAccountRoles = this.selectListByAccountId(account.getId());
67
68     Map<String, AccountRole> existMapAccountRoles = new LinkedHashMap<String, AccountRole>();
69     for (AccountRole accountRole : existAccountRoles) {
70       String k = String.format("%s", accountRole.getRoleId());
71       existMapAccountRoles.put(k, accountRole);
72     }
73
74     for (AccountRole accountRole : accountRoles) {
75       String k = String.format("%s", accountRole.getRoleId());
76
77       if (existMapAccountRoles.containsKey(k)) {
78         existMapAccountRoles.remove(k);
79       } else {
80         accountRole.setCompanyId(account.getCompanyId());
81         accountRole.setAccountId(account.getId());
82
83         this.insert(accountRole);
84       }
85     }
86
87     for (AccountRole accountRole : existMapAccountRoles.values()) {
88       this.deleteById(accountRole.getId());
89     }
90   }
91
92   public default List<AccountRole> selectListByAccountId(String accountId) {
93
94     AccountRole probe = new AccountRole();
95     probe.setAccountId(accountId);
96
97     ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("accountId",
98         ExampleMatcher.GenericPropertyMatchers.exact());
99
100     Example<AccountRole> example = Example.of(probe, matcher);
101
102     List<AccountRole> accountRoles = this.findAll(example);
103
104     return accountRoles;
105   }
106
107   public default void relateRoleAccounts(Role role, List<AccountRole> accountRoles) {
108
109     List<AccountRole> existRoleAccounts = this.selectListByRoleId(role.getId());
110
111     Map<String, AccountRole> existMapRoleAccounts = new LinkedHashMap<String, AccountRole>();
112     for (AccountRole accountRole : existRoleAccounts) {
113       String k = String.format("%s", accountRole.getAccountId());
114       existMapRoleAccounts.put(k, accountRole);
115     }
116
117     for (AccountRole accountRole : accountRoles) {
118       String k = String.format("%s", accountRole.getAccountId());
119
120       if (existMapRoleAccounts.containsKey(k)) {
121         existMapRoleAccounts.remove(k);
122       } else {
123         accountRole.setCompanyId(role.getCompanyId());
124         accountRole.setRoleId(role.getId());
125
126         this.insert(accountRole);
127       }
128     }
129
130     for (AccountRole accountRole : existMapRoleAccounts.values()) {
131       this.deleteById(accountRole.getId());
132     }
133   }
134
135   public default List<AccountRole> selectListByRoleId(String roleId) {
136
137     AccountRole probe = new AccountRole();
138     probe.setRoleId(roleId);
139
140     ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("roleId",
141         ExampleMatcher.GenericPropertyMatchers.exact());
142
143     Example<AccountRole> example = Example.of(probe, matcher);
144
145     List<AccountRole> accountRoles = this.findAll(example);
146
147     return accountRoles;
148   }
149   
150   public default AccountRole selectOneByAccountRole(String accountId, String roleId) {
151
152     AccountRole probe = new AccountRole();
153     probe.setAccountId(accountId);
154     probe.setRoleId(roleId);
155
156     ExampleMatcher matcher = ExampleMatcher.matching()
157         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
158         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact())
159         ;
160
161     Example<AccountRole> example = Example.of(probe, matcher);
162     
163     Optional<AccountRole> o = this.findOne(example);
164     
165     return o.isPresent() ? o.get() : null;
166   }
167
168   public default void addAccountRole(String accountId, String roleId) {
169
170     AccountRole accountRole = this.selectOneByAccountRole(accountId, roleId);
171     
172     if (accountRole == null) {
173       accountRole = new AccountRole();
174       //accountRole.setCompanyId(companyId);
175       accountRole.setAccountId(accountId);
176       accountRole.setRoleId(roleId);
177       
178       this.insert(accountRole);
179     }
180   }
181
182   public default void removeAccountRole(String accountId, String roleId) {
183
184     AccountRole accountRole = this.selectOneByAccountRole(accountId, roleId);
185     
186     if (accountRole != null) {
187       this.deleteById(accountRole.getId());
188     }
189   }
190
191 }