1 package com.supwisdom.institute.backend.system.domain.repo;
3 import java.util.LinkedHashMap;
6 import java.util.Optional;
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;
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;
21 public interface AccountRoleRepository extends BaseJpaRepository<AccountRole> {
23 public default Page<AccountRole> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
25 AccountRole probe = new AccountRole();
26 if (mapBean != null) {
27 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
28 probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
31 ExampleMatcher matcher = ExampleMatcher.matching()
32 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
33 .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
35 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
36 Example<AccountRole> example = Example.of(probe, matcher);
38 Page<AccountRole> page = this.findAll(example, pageRequest);
43 public default Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
45 AccountRole probe = new AccountRole();
46 if (mapBean != null) {
47 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
48 probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
51 ExampleMatcher matcher = ExampleMatcher.matching()
52 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
53 .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
55 Example<AccountRole> example = Example.of(probe, matcher);
57 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
59 Page<AccountRole> page = this.findAll(example, pageRequest); // FIXME: 多表关联查询
64 public default void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
66 List<AccountRole> existAccountRoles = this.selectListByAccountId(account.getId());
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);
74 for (AccountRole accountRole : accountRoles) {
75 String k = String.format("%s", accountRole.getRoleId());
77 if (existMapAccountRoles.containsKey(k)) {
78 existMapAccountRoles.remove(k);
80 accountRole.setCompanyId(account.getCompanyId());
81 accountRole.setAccountId(account.getId());
83 this.insert(accountRole);
87 for (AccountRole accountRole : existMapAccountRoles.values()) {
88 this.deleteById(accountRole.getId());
92 public default List<AccountRole> selectListByAccountId(String accountId) {
94 AccountRole probe = new AccountRole();
95 probe.setAccountId(accountId);
97 ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("accountId",
98 ExampleMatcher.GenericPropertyMatchers.exact());
100 Example<AccountRole> example = Example.of(probe, matcher);
102 List<AccountRole> accountRoles = this.findAll(example);
107 public default void relateRoleAccounts(Role role, List<AccountRole> accountRoles) {
109 List<AccountRole> existRoleAccounts = this.selectListByRoleId(role.getId());
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);
117 for (AccountRole accountRole : accountRoles) {
118 String k = String.format("%s", accountRole.getAccountId());
120 if (existMapRoleAccounts.containsKey(k)) {
121 existMapRoleAccounts.remove(k);
123 accountRole.setCompanyId(role.getCompanyId());
124 accountRole.setRoleId(role.getId());
126 this.insert(accountRole);
130 for (AccountRole accountRole : existMapRoleAccounts.values()) {
131 this.deleteById(accountRole.getId());
135 public default List<AccountRole> selectListByRoleId(String roleId) {
137 AccountRole probe = new AccountRole();
138 probe.setRoleId(roleId);
140 ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("roleId",
141 ExampleMatcher.GenericPropertyMatchers.exact());
143 Example<AccountRole> example = Example.of(probe, matcher);
145 List<AccountRole> accountRoles = this.findAll(example);
150 public default AccountRole selectOneByAccountRole(String accountId, String roleId) {
152 AccountRole probe = new AccountRole();
153 probe.setAccountId(accountId);
154 probe.setRoleId(roleId);
156 ExampleMatcher matcher = ExampleMatcher.matching()
157 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
158 .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact())
161 Example<AccountRole> example = Example.of(probe, matcher);
163 Optional<AccountRole> o = this.findOne(example);
165 return o.isPresent() ? o.get() : null;
168 public default void addAccountRole(String accountId, String roleId) {
170 AccountRole accountRole = this.selectOneByAccountRole(accountId, roleId);
172 if (accountRole == null) {
173 accountRole = new AccountRole();
174 //accountRole.setCompanyId(companyId);
175 accountRole.setAccountId(accountId);
176 accountRole.setRoleId(roleId);
178 this.insert(accountRole);
182 public default void removeAccountRole(String accountId, String roleId) {
184 AccountRole accountRole = this.selectOneByAccountRole(accountId, roleId);
186 if (accountRole != null) {
187 this.deleteById(accountRole.getId());