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