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