9e29e3198be58d53c79d452304237f2ab3fcf450
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.domain.service;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.springframework.beans.factory.annotation.Autowired;
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.Service;
13
14 import com.supwisdom.institute.backend.common.framework.service.ABaseService;
15 import com.supwisdom.institute.backend.common.util.MapBeanUtils;
16 import com.supwisdom.institute.backend.system.domain.entity.Account;
17 import com.supwisdom.institute.backend.system.domain.entity.AccountGroup;
18 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
19 import com.supwisdom.institute.backend.system.domain.repo.AccountGroupRepository;
20 import com.supwisdom.institute.backend.system.domain.repo.AccountRepository;
21 import com.supwisdom.institute.backend.system.domain.repo.AccountRoleRepository;
22
23 @Service
24 public class AccountService extends ABaseService<Account, AccountRepository> {
25   
26   @Override
27   public AccountRepository getRepo() {
28     return accountRepository;
29   }
30
31   @Autowired
32   private AccountRepository accountRepository;
33
34   @Autowired
35   private AccountGroupRepository accountGroupRepository;
36
37   @Autowired
38   private AccountRoleRepository accountRoleRepository;
39   
40   
41   @Override
42   public Account insert(Account entity) {
43
44     entity.setEnabled(Account.STATUS_ENABLED.equals(entity.getStatus()));
45     entity.setAccountNonExpired(true);
46     entity.setAccountNonLocked(true);
47     entity.setCredentialsNonExpired(true);
48     
49     return super.insert(entity);
50   }
51   
52   @Override
53   public Account update(Account entity) {
54     
55     entity.setEnabled(Account.STATUS_ENABLED.equals(entity.getStatus()));
56
57     return super.update(entity);
58   }
59   
60
61   public void deleteBatch(List<String> ids) {
62     
63     ids.stream().forEach(id -> {
64       this.deleteById(id);
65     });
66   }
67
68
69   public Page<AccountGroup> selectAccountGroups(int pageIndex, int pageSize, Map<String, Object> mapBean) {
70
71     AccountGroup probe = new AccountGroup();
72     if (mapBean != null) {
73       probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
74       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
75     }
76
77     ExampleMatcher matcher = ExampleMatcher.matching()
78         .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
79         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
80
81     Example<AccountGroup> example = Example.of(probe, matcher);
82
83     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
84
85     Page<AccountGroup> page = accountGroupRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
86
87     return page;
88   }
89
90   public void relateAccountGroups(Account account, List<AccountGroup> accountGroups) {
91
92     List<AccountGroup> existAccountGroups = this.selectAccountGroupsByAccountId(account.getId());
93
94     Map<String, AccountGroup> existMapAccountGroups = new LinkedHashMap<String, AccountGroup>();
95     for (AccountGroup accountGroup : existAccountGroups) {
96       String k = String.format("%s", accountGroup.getGroupId());
97       existMapAccountGroups.put(k, accountGroup);
98     }
99
100     for (AccountGroup accountGroup : accountGroups) {
101       String k = String.format("%s", accountGroup.getGroupId());
102
103       if (existMapAccountGroups.containsKey(k)) {
104         existMapAccountGroups.remove(k);
105       } else {
106         accountGroup.setCompanyId(account.getCompanyId());
107         accountGroup.setAccountId(account.getId());
108
109         accountGroupRepository.insert(accountGroup);
110       }
111     }
112
113     for (AccountGroup accountGroup : existMapAccountGroups.values()) {
114       accountGroupRepository.deleteById(accountGroup.getId());
115     }
116   }
117
118   public List<AccountGroup> selectAccountGroupsByAccountId(String accountId) {
119
120     AccountGroup probe = new AccountGroup();
121     probe.setAccountId(accountId);
122
123     ExampleMatcher matcher = ExampleMatcher.matching()
124         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
125
126     Example<AccountGroup> example = Example.of(probe, matcher);
127
128     List<AccountGroup> accountGroups = accountGroupRepository.findAll(example);
129
130     return accountGroups;
131   }
132
133   
134
135   public Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
136
137     AccountRole probe = new AccountRole();
138     if (mapBean != null) {
139       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
140       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
141     }
142
143     ExampleMatcher matcher = ExampleMatcher.matching()
144         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
145         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
146
147     Example<AccountRole> example = Example.of(probe, matcher);
148
149     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
150
151     Page<AccountRole> page = accountRoleRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
152
153     return page;
154   }
155
156   public void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
157
158     List<AccountRole> existAccountRoles = this.selectAccountRolesByAccountId(account.getId());
159
160     Map<String, AccountRole> existMapAccountRoles = new LinkedHashMap<String, AccountRole>();
161     for (AccountRole accountRole : existAccountRoles) {
162       String k = String.format("%s", accountRole.getRoleId());
163       existMapAccountRoles.put(k, accountRole);
164     }
165
166     for (AccountRole accountRole : accountRoles) {
167       String k = String.format("%s", accountRole.getRoleId());
168
169       if (existMapAccountRoles.containsKey(k)) {
170         existMapAccountRoles.remove(k);
171       } else {
172         accountRole.setCompanyId(account.getCompanyId());
173         accountRole.setAccountId(account.getId());
174
175         accountRoleRepository.insert(accountRole);
176       }
177     }
178
179     for (AccountRole accountRole : existMapAccountRoles.values()) {
180       accountRoleRepository.deleteById(accountRole.getId());
181     }
182   }
183
184   public List<AccountRole> selectAccountRolesByAccountId(String accountId) {
185
186     AccountRole probe = new AccountRole();
187     probe.setAccountId(accountId);
188
189     ExampleMatcher matcher = ExampleMatcher.matching()
190         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
191
192     Example<AccountRole> example = Example.of(probe, matcher);
193
194     List<AccountRole> accountRoles = accountRoleRepository.findAll(example);
195
196     return accountRoles;
197   }
198
199 }