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.Group;
17 import com.supwisdom.institute.backend.system.domain.entity.Account;
18 import com.supwisdom.institute.backend.system.domain.entity.AccountGroup;
21 public interface AccountGroupRepository extends BaseJpaRepository<AccountGroup> {
23 public default Page<AccountGroup> selectPageList(int pageIndex, int pageSize, Map<String, Object> mapBean) {
24 AccountGroup probe = new AccountGroup();
25 if (mapBean != null) {
26 probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
27 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
30 ExampleMatcher matcher = ExampleMatcher.matching()
31 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
32 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
34 Example<AccountGroup> example = Example.of(probe, matcher);
36 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
38 Page<AccountGroup> page = this.findAll(example, pageRequest);
43 public default Page<AccountGroup> selectAccountGroups(int pageIndex, int pageSize, Map<String, Object> mapBean) {
45 AccountGroup probe = new AccountGroup();
46 if (mapBean != null) {
47 probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
48 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
51 ExampleMatcher matcher = ExampleMatcher.matching()
52 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
53 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
55 Example<AccountGroup> example = Example.of(probe, matcher);
57 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
59 Page<AccountGroup> page = this.findAll(example, pageRequest); // FIXME: 多表关联查询
64 public default void relateAccountGroups(Account account, List<AccountGroup> accountGroups) {
66 List<AccountGroup> existAccountGroups = this.selectListByAccountId(account.getId());
68 Map<String, AccountGroup> existMapAccountGroups = new LinkedHashMap<String, AccountGroup>();
69 for (AccountGroup accountGroup : existAccountGroups) {
70 String k = String.format("%s", accountGroup.getGroupId());
71 existMapAccountGroups.put(k, accountGroup);
74 for (AccountGroup accountGroup : accountGroups) {
75 String k = String.format("%s", accountGroup.getGroupId());
77 if (existMapAccountGroups.containsKey(k)) {
78 existMapAccountGroups.remove(k);
80 accountGroup.setCompanyId(account.getCompanyId());
81 accountGroup.setAccountId(account.getId());
83 this.insert(accountGroup);
87 for (AccountGroup accountGroup : existMapAccountGroups.values()) {
88 this.deleteById(accountGroup.getId());
92 public default List<AccountGroup> selectListByAccountId(String accountId) {
94 AccountGroup probe = new AccountGroup();
95 probe.setAccountId(accountId);
97 ExampleMatcher matcher = ExampleMatcher.matching()
98 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
100 Example<AccountGroup> example = Example.of(probe, matcher);
102 List<AccountGroup> accountGroups = this.findAll(example);
104 return accountGroups;
107 public default void relateGroupAccounts(Group group, List<AccountGroup> accountGroups) {
109 List<AccountGroup> existGroupAccounts = this.selectListByGroupId(group.getId());
111 Map<String, AccountGroup> existMapGroupAccounts = new LinkedHashMap<String, AccountGroup>();
112 for (AccountGroup accountGroup : existGroupAccounts) {
113 String k = String.format("%s", accountGroup.getAccountId());
114 existMapGroupAccounts.put(k, accountGroup);
117 for (AccountGroup accountGroup : accountGroups) {
118 String k = String.format("%s", accountGroup.getAccountId());
120 if (existMapGroupAccounts.containsKey(k)) {
121 existMapGroupAccounts.remove(k);
123 accountGroup.setCompanyId(group.getCompanyId());
124 accountGroup.setGroupId(group.getId());
126 this.insert(accountGroup);
130 for (AccountGroup accountGroup : existMapGroupAccounts.values()) {
131 this.deleteById(accountGroup.getId());
135 public default List<AccountGroup> selectListByGroupId(String groupId) {
137 AccountGroup probe = new AccountGroup();
138 probe.setGroupId(groupId);
140 ExampleMatcher matcher = ExampleMatcher.matching()
141 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact());
143 Example<AccountGroup> example = Example.of(probe, matcher);
145 List<AccountGroup> accountGroups = this.findAll(example);
147 return accountGroups;
150 public default AccountGroup selectOneByAccountGroup(String accountId, String groupId) {
152 AccountGroup probe = new AccountGroup();
153 probe.setAccountId(accountId);
154 probe.setGroupId(groupId);
156 ExampleMatcher matcher = ExampleMatcher.matching()
157 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
158 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
161 Example<AccountGroup> example = Example.of(probe, matcher);
163 Optional<AccountGroup> o = this.findOne(example);
165 return o.isPresent() ? o.get() : null;
168 public default void addAccountGroup(String accountId, String groupId) {
170 AccountGroup accountGroup = this.selectOneByAccountGroup(accountId, groupId);
172 if (accountGroup == null) {
173 accountGroup = new AccountGroup();
174 //accountGroup.setCompanyId(companyId);
175 accountGroup.setAccountId(accountId);
176 accountGroup.setGroupId(groupId);
178 this.insert(accountGroup);
182 public default void removeAccountGroup(String accountId, String groupId) {
184 AccountGroup accountGroup = this.selectOneByAccountGroup(accountId, groupId);
186 if (accountGroup != null) {
187 this.deleteById(accountGroup.getId());