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 Page<AccountGroup> selectAccountGroups(int pageIndex, int pageSize, Map<String, Object> mapBean) {
44 AccountGroup probe = new AccountGroup();
45 if (mapBean != null) {
46 probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
47 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
50 ExampleMatcher matcher = ExampleMatcher.matching()
51 .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
52 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
54 Example<AccountGroup> example = Example.of(probe, matcher);
56 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
58 Page<AccountGroup> page = accountGroupRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
63 public void relateAccountGroups(Account account, List<AccountGroup> accountGroups) {
65 List<AccountGroup> existAccountGroups = this.selectAccountGroupsByAccountId(account.getId());
67 Map<String, AccountGroup> existMapAccountGroups = new LinkedHashMap<String, AccountGroup>();
68 for (AccountGroup accountGroup : existAccountGroups) {
69 String k = String.format("%s", accountGroup.getGroupId());
70 existMapAccountGroups.put(k, accountGroup);
73 for (AccountGroup accountGroup : accountGroups) {
74 String k = String.format("%s", accountGroup.getGroupId());
76 if (existMapAccountGroups.containsKey(k)) {
77 existMapAccountGroups.remove(k);
79 accountGroup.setCompanyId(account.getCompanyId());
80 accountGroup.setAccountId(account.getId());
82 accountGroupRepository.insert(accountGroup);
86 for (AccountGroup accountGroup : existMapAccountGroups.values()) {
87 accountGroupRepository.deleteById(accountGroup.getId());
91 public List<AccountGroup> selectAccountGroupsByAccountId(String accountId) {
93 AccountGroup probe = new AccountGroup();
94 probe.setAccountId(accountId);
96 ExampleMatcher matcher = ExampleMatcher.matching()
97 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
99 Example<AccountGroup> example = Example.of(probe, matcher);
101 List<AccountGroup> accountGroups = accountGroupRepository.findAll(example);
103 return accountGroups;
108 public Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
110 AccountRole probe = new AccountRole();
111 if (mapBean != null) {
112 probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
113 probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
116 ExampleMatcher matcher = ExampleMatcher.matching()
117 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
118 .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
120 Example<AccountRole> example = Example.of(probe, matcher);
122 PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
124 Page<AccountRole> page = accountRoleRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
129 public void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
131 List<AccountRole> existAccountRoles = this.selectAccountRolesByAccountId(account.getId());
133 Map<String, AccountRole> existMapAccountRoles = new LinkedHashMap<String, AccountRole>();
134 for (AccountRole accountRole : existAccountRoles) {
135 String k = String.format("%s", accountRole.getRoleId());
136 existMapAccountRoles.put(k, accountRole);
139 for (AccountRole accountRole : accountRoles) {
140 String k = String.format("%s", accountRole.getRoleId());
142 if (existMapAccountRoles.containsKey(k)) {
143 existMapAccountRoles.remove(k);
145 accountRole.setCompanyId(account.getCompanyId());
146 accountRole.setAccountId(account.getId());
148 accountRoleRepository.insert(accountRole);
152 for (AccountRole accountRole : existMapAccountRoles.values()) {
153 accountRoleRepository.deleteById(accountRole.getId());
157 public List<AccountRole> selectAccountRolesByAccountId(String accountId) {
159 AccountRole probe = new AccountRole();
160 probe.setAccountId(accountId);
162 ExampleMatcher matcher = ExampleMatcher.matching()
163 .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
165 Example<AccountRole> example = Example.of(probe, matcher);
167 List<AccountRole> accountRoles = accountRoleRepository.findAll(example);