e59b9fd9553840c241966c9f22c22441a0eb5e82
[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
42   public Page<AccountGroup> selectAccountGroups(int pageIndex, int pageSize, Map<String, Object> mapBean) {
43
44     AccountGroup probe = new AccountGroup();
45     if (mapBean != null) {
46       probe.setGroupId(MapBeanUtils.getString(mapBean, "groupId"));
47       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
48     }
49
50     ExampleMatcher matcher = ExampleMatcher.matching()
51         .withMatcher("groupId", ExampleMatcher.GenericPropertyMatchers.exact())
52         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
53
54     Example<AccountGroup> example = Example.of(probe, matcher);
55
56     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
57
58     Page<AccountGroup> page = accountGroupRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
59
60     return page;
61   }
62
63   public void relateAccountGroups(Account account, List<AccountGroup> accountGroups) {
64
65     List<AccountGroup> existAccountGroups = this.selectAccountGroupsByAccountId(account.getId());
66
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);
71     }
72
73     for (AccountGroup accountGroup : accountGroups) {
74       String k = String.format("%s", accountGroup.getGroupId());
75
76       if (existMapAccountGroups.containsKey(k)) {
77         existMapAccountGroups.remove(k);
78       } else {
79         accountGroup.setCompanyId(account.getCompanyId());
80         accountGroup.setAccountId(account.getId());
81
82         accountGroupRepository.insert(accountGroup);
83       }
84     }
85
86     for (AccountGroup accountGroup : existMapAccountGroups.values()) {
87       this.deleteById(accountGroup.getId());
88     }
89   }
90
91   public List<AccountGroup> selectAccountGroupsByAccountId(String accountId) {
92
93     AccountGroup probe = new AccountGroup();
94     probe.setAccountId(accountId);
95
96     ExampleMatcher matcher = ExampleMatcher.matching()
97         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
98
99     Example<AccountGroup> example = Example.of(probe, matcher);
100
101     List<AccountGroup> accountGroups = accountGroupRepository.findAll(example);
102
103     return accountGroups;
104   }
105
106   
107
108   public Page<AccountRole> selectAccountRoles(int pageIndex, int pageSize, Map<String, Object> mapBean) {
109
110     AccountRole probe = new AccountRole();
111     if (mapBean != null) {
112       probe.setAccountId(MapBeanUtils.getString(mapBean, "accountId"));
113       probe.setRoleId(MapBeanUtils.getString(mapBean, "roleId"));
114     }
115
116     ExampleMatcher matcher = ExampleMatcher.matching()
117         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact())
118         .withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact());
119
120     Example<AccountRole> example = Example.of(probe, matcher);
121
122     PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
123
124     Page<AccountRole> page = accountRoleRepository.findAll(example, pageRequest); // FIXME: 多表关联查询
125
126     return page;
127   }
128
129   public void relateAccountRoles(Account account, List<AccountRole> accountRoles) {
130
131     List<AccountRole> existAccountRoles = this.selectAccountRolesByAccountId(account.getId());
132
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);
137     }
138
139     for (AccountRole accountRole : accountRoles) {
140       String k = String.format("%s", accountRole.getRoleId());
141
142       if (existMapAccountRoles.containsKey(k)) {
143         existMapAccountRoles.remove(k);
144       } else {
145         accountRole.setCompanyId(account.getCompanyId());
146         accountRole.setAccountId(account.getId());
147
148         accountRoleRepository.insert(accountRole);
149       }
150     }
151
152     for (AccountRole accountRole : existMapAccountRoles.values()) {
153       this.deleteById(accountRole.getId());
154     }
155   }
156
157   public List<AccountRole> selectAccountRolesByAccountId(String accountId) {
158
159     AccountRole probe = new AccountRole();
160     probe.setAccountId(accountId);
161
162     ExampleMatcher matcher = ExampleMatcher.matching()
163         .withMatcher("accountId", ExampleMatcher.GenericPropertyMatchers.exact());
164
165     Example<AccountRole> example = Example.of(probe, matcher);
166
167     List<AccountRole> accountRoles = accountRoleRepository.findAll(example);
168
169     return accountRoles;
170   }
171
172 }