1 package com.supwisdom.institute.backend.system.domain.service;
3 import java.util.LinkedHashMap;
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;
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;
24 public class AccountService extends ABaseService<Account, AccountRepository> {
27 public AccountRepository getRepo() {
28 return accountRepository;
32 private AccountRepository accountRepository;
35 private AccountGroupRepository accountGroupRepository;
38 private AccountRoleRepository accountRoleRepository;
42 public Account insert(Account entity) {
44 entity.setEnabled(Account.STATUS_ENABLED.equals(entity.getStatus()));
45 entity.setAccountNonExpired(true);
46 entity.setAccountNonLocked(true);
47 entity.setCredentialsNonExpired(true);
49 return super.insert(entity);
53 public Account update(Account entity) {
55 entity.setEnabled(Account.STATUS_ENABLED.equals(entity.getStatus()));
57 return super.update(entity);
61 public void deleteBatch(List<String> ids) {
63 ids.stream().forEach(id -> {
69 public Page<AccountGroup> selectAccountGroups(int pageIndex, int pageSize, Map<String, Object> mapBean) {
71 AccountGroup probe = new AccountGroup();
72 if (mapBean != null) {
73 probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
74 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
77 ExampleMatcher matcher = ExampleMatcher.matching()
78 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
79 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
81 Example<AccountGroup> example = Example.of(probe, matcher);
83 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
85 Page<AccountGroup> page = accountGroupRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
90 public void relateAccountGroups(Account account, List<AccountGroup> accountGroups) {
92 List<AccountGroup> existAccountGroups = this.selectAccountGroupsByAccountId(account.getId());
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);
100 for (AccountGroup accountGroup : accountGroups) {
101 String k = String.format("%s", accountGroup.getGroupId());
103 if (existMapAccountGroups.containsKey(k)) {
104 existMapAccountGroups.remove(k);
106 accountGroup.setCompanyId(account.getCompanyId());
107 accountGroup.setAccountId(account.getId());
109 accountGroupRepository.insert(accountGroup);
113 for (AccountGroup accountGroup : existMapAccountGroups.values()) {
114 accountGroupRepository.deleteById(accountGroup.getId());
118 public List<AccountGroup> selectAccountGroupsByAccountId(String accountId) {
120 AccountGroup probe = new AccountGroup();
121 probe.setAccountId(accountId);
123 ExampleMatcher matcher = ExampleMatcher.matching()
124 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
126 Example<AccountGroup> example = Example.of(probe, matcher);
128 List<AccountGroup> accountGroups = accountGroupRepository.findAll(example);
130 return accountGroups;
135 public Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
137 AccountRole probe = new AccountRole();
138 if (mapBean != null) {
139 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
140 probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
143 ExampleMatcher matcher = ExampleMatcher.matching()
144 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
145 .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
147 Example<AccountRole> example = Example.of(probe, matcher);
149 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
151 Page<AccountRole> page = accountRoleRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
156 public void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
158 List<AccountRole> existAccountRoles = this.selectAccountRolesByAccountId(account.getId());
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);
166 for (AccountRole accountRole : accountRoles) {
167 String k = String.format("%s", accountRole.getRoleId());
169 if (existMapAccountRoles.containsKey(k)) {
170 existMapAccountRoles.remove(k);
172 accountRole.setCompanyId(account.getCompanyId());
173 accountRole.setAccountId(account.getId());
175 accountRoleRepository.insert(accountRole);
179 for (AccountRole accountRole : existMapAccountRoles.values()) {
180 accountRoleRepository.deleteById(accountRole.getId());
184 public List<AccountRole> selectAccountRolesByAccountId(String accountId) {
186 AccountRole probe = new AccountRole();
187 probe.setAccountId(accountId);
189 ExampleMatcher matcher = ExampleMatcher.matching()
190 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
192 Example<AccountRole> example = Example.of(probe, matcher);
194 List<AccountRole> accountRoles = accountRoleRepository.findAll(example);