34fb317ab84c02cf3b82a695427ea687a9e71206
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.api.v1.admin;
2
3 import java.util.HashMap;
4
5 import io.swagger.annotations.Api;
6 import lombok.extern.slf4j.Slf4j;
7
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.data.domain.Page;
10 import org.springframework.http.HttpStatus;
11 import org.springframework.util.MimeTypeUtils;
12 import org.springframework.web.bind.annotation.DeleteMapping;
13 import org.springframework.web.bind.annotation.GetMapping;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.PostMapping;
16 import org.springframework.web.bind.annotation.PutMapping;
17 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.bind.annotation.ResponseBody;
21 import org.springframework.web.bind.annotation.ResponseStatus;
22 import org.springframework.web.bind.annotation.RestController;
23
24 import com.supwisdom.institute.backend.common.framework.entity.EntityUtils;
25 import com.supwisdom.institute.backend.common.framework.vo.response.DefaultApiResponse;
26 import com.supwisdom.institute.backend.system.api.vo.request.RoleCreateRequest;
27 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelateAccountsRequest;
28 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelateGroupsRequest;
29 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedAccountsRequest;
30 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedGroupsRequest;
31 import com.supwisdom.institute.backend.system.api.vo.request.RoleUpdateRequest;
32 import com.supwisdom.institute.backend.system.api.vo.request.RoleQueryRequest;
33 import com.supwisdom.institute.backend.system.api.vo.response.RoleCreateResponseData;
34 import com.supwisdom.institute.backend.system.api.vo.response.RoleLoadResponseData;
35 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateAccountsResponseData;
36 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateGroupsResponseData;
37 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedAccountsResponseData;
38 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedGroupsResponseData;
39 import com.supwisdom.institute.backend.system.api.vo.response.RoleRemoveResponseData;
40 import com.supwisdom.institute.backend.system.api.vo.response.RoleUpdateResponseData;
41 import com.supwisdom.institute.backend.system.api.vo.response.RoleQueryResponseData;
42 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
43 import com.supwisdom.institute.backend.system.domain.entity.GroupRole;
44 import com.supwisdom.institute.backend.system.domain.entity.Role;
45 import com.supwisdom.institute.backend.system.domain.service.RoleService;
46
47 @Api(value = "SystemAdminRole", tags = { "SystemAdminRole" }, description = "角色的操作接口")
48 @Slf4j
49 @RestController
50 @RequestMapping("/v1/admin/roles")
51 public class AdminRoleController {
52
53   @Autowired
54   private RoleService roleService;
55
56   @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
57   @ResponseStatus(value = HttpStatus.OK)
58   @ResponseBody
59   public DefaultApiResponse<RoleQueryResponseData> query(RoleQueryRequest queryRequest) {
60
61     Page<Role> page = roleService.selectPageList(
62         queryRequest.isLoadAll(), 
63         queryRequest.getPageIndex(), 
64         queryRequest.getPageSize(),
65         queryRequest.getMapBean(),
66         queryRequest.getOrderBy());
67
68     RoleQueryResponseData data = RoleQueryResponseData.of(queryRequest).build(page);
69
70     return new DefaultApiResponse<RoleQueryResponseData>(data);
71   }
72   
73   @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
74   @ResponseStatus(value = HttpStatus.OK)
75   @ResponseBody
76   public DefaultApiResponse<RoleLoadResponseData> load(@PathVariable("id") String id) {
77
78     if (id == null || id.length() == 0) {
79       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
80     }
81
82     Role role = roleService.selectById(id);
83
84     if (role == null) {
85       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
86     }
87     
88     RoleLoadResponseData data = RoleLoadResponseData.of(role);
89
90     return new DefaultApiResponse<RoleLoadResponseData>(data);
91   }
92
93   @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
94   @ResponseStatus(value = HttpStatus.OK)
95   @ResponseBody
96   public DefaultApiResponse<RoleCreateResponseData> create(
97       @RequestBody RoleCreateRequest createRequest) {
98     
99     // FIXME: 验证数据有效性
100     
101     Role role = createRequest.getEntity();
102     
103     Role ret = roleService.insert(role);
104     
105     RoleCreateResponseData data = RoleCreateResponseData.build(ret);
106
107     return new DefaultApiResponse<RoleCreateResponseData>(data);
108   }
109   
110   @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
111   @ResponseStatus(value = HttpStatus.OK)
112   @ResponseBody
113   public DefaultApiResponse<RoleUpdateResponseData> update(
114       @PathVariable("id") String id, 
115       @RequestBody RoleUpdateRequest updateRequest) {
116
117     if (id == null || id.length() == 0) {
118       throw new RuntimeException("exception.update.id.must.not.empty");
119     }
120
121     Role tmp = roleService.selectById(id);
122     if (tmp == null) {
123       throw new RuntimeException("exception.update.domain.not.exist");
124     }
125
126     Role role = updateRequest.getEntity();
127     role.setId(id);
128
129     role = EntityUtils.merge(tmp, role);
130
131     Role ret = roleService.update(role);
132
133     RoleUpdateResponseData data = RoleUpdateResponseData.build(ret);
134     
135     return new DefaultApiResponse<RoleUpdateResponseData>(data);
136   }
137
138   @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
139   @ResponseStatus(value = HttpStatus.OK)
140   @ResponseBody
141   public DefaultApiResponse<RoleRemoveResponseData> delete(
142       @PathVariable("id") String id) {
143
144     if (id == null || id.length() == 0) {
145       throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
146     }
147
148     Role tmp = roleService.selectById(id);
149     if (tmp == null) {
150       throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
151     }
152
153     roleService.deleteById(id);
154
155     RoleRemoveResponseData data = RoleRemoveResponseData.build(tmp);
156     return new DefaultApiResponse<RoleRemoveResponseData>(data);
157   }
158
159
160   @RequestMapping(method = RequestMethod.GET, path = "/{id}/accounts", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
161   @ResponseBody
162   public DefaultApiResponse<RoleRelatedAccountsResponseData> roleAccounts(
163       @PathVariable("id") String id, 
164       RoleRelatedAccountsRequest request) {
165
166     if (id == null || id.length() == 0) {
167       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
168     }
169
170     Role role = roleService.selectById(id);
171
172     if (role == null) {
173       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
174     }
175
176     if (request.getMapBean() == null) {
177       request.setMapBean(new HashMap<String, Object>());
178     }
179     request.getMapBean().put("roleId", role.getId());
180
181     Page<AccountRole> page = roleService.selectRoleAccounts(
182         request.getPageIndex(),
183         request.getPageSize(), 
184         request.getMapBean());
185
186     RoleRelatedAccountsResponseData data = RoleRelatedAccountsResponseData.of(request).build(page);
187
188     return new DefaultApiResponse<RoleRelatedAccountsResponseData>(data);
189   }
190
191   @RequestMapping(method = RequestMethod.POST, path = "/{id}/accounts", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
192   @ResponseBody
193   public DefaultApiResponse<RoleRelateAccountsResponseData> relateAccounts(
194       @PathVariable("id") String id, 
195       @RequestBody RoleRelateAccountsRequest roleAccounts) {
196
197     if (id == null || id.length() == 0) {
198       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
199     }
200
201     Role role = roleService.selectById(id);
202
203     if (role == null) {
204       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
205     }
206
207     roleService.relateRoleAccounts(role, roleAccounts.getRoleAccounts());
208
209     RoleRelateAccountsResponseData data = RoleRelateAccountsResponseData.of("info.relate.success");
210
211     return new DefaultApiResponse<RoleRelateAccountsResponseData>(data);
212   }
213
214
215   @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
216   @ResponseBody
217   public DefaultApiResponse<RoleRelatedGroupsResponseData> roleGroups(
218       @PathVariable("id") String id, 
219       RoleRelatedGroupsRequest request) {
220
221     if (id == null || id.length() == 0) {
222       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
223     }
224
225     Role role = roleService.selectById(id);
226
227     if (role == null) {
228       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
229     }
230
231     if (request.getMapBean() == null) {
232       request.setMapBean(new HashMap<String, Object>());
233     }
234     request.getMapBean().put("roleId", role.getId());
235
236     Page<GroupRole> page = roleService.selectRoleGroups(
237         request.getPageIndex(),
238         request.getPageSize(), 
239         request.getMapBean());
240
241     RoleRelatedGroupsResponseData data = RoleRelatedGroupsResponseData.of(request).build(page);
242
243     return new DefaultApiResponse<RoleRelatedGroupsResponseData>(data);
244   }
245
246   @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
247   @ResponseBody
248   public DefaultApiResponse<RoleRelateGroupsResponseData> relateGroups(
249       @PathVariable("id") String id, 
250       @RequestBody RoleRelateGroupsRequest accountGroups) {
251
252     if (id == null || id.length() == 0) {
253       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
254     }
255
256     Role role = roleService.selectById(id);
257
258     if (role == null) {
259       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
260     }
261
262     roleService.relateRoleGroups(role, accountGroups.getRoleGroups());
263
264     RoleRelateGroupsResponseData data = RoleRelateGroupsResponseData.of("info.relate.success");
265
266     return new DefaultApiResponse<RoleRelateGroupsResponseData>(data);
267   }
268
269   
270 }