a198eeb8decfdff648d71171585ab241ef5ca962
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.api.v1.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.common.framework.entity.EntityUtils;
26 import com.supwisdom.institute.backend.common.framework.vo.response.DefaultApiResponse;
27 import com.supwisdom.institute.backend.system.api.vo.request.RoleCreateRequest;
28 import com.supwisdom.institute.backend.system.api.vo.request.RoleDeleteBatchRequest;
29 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelateAccountsRequest;
30 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelateGroupsRequest;
31 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatePermissionsRequest;
32 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedAccountsRequest;
33 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedGroupsRequest;
34 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedPermissionsRequest;
35 import com.supwisdom.institute.backend.system.api.vo.request.RoleUpdateRequest;
36 import com.supwisdom.institute.backend.system.api.vo.request.RoleQueryRequest;
37 import com.supwisdom.institute.backend.system.api.vo.response.RoleCreateResponseData;
38 import com.supwisdom.institute.backend.system.api.vo.response.RoleDeleteBatchResponseData;
39 import com.supwisdom.institute.backend.system.api.vo.response.RoleLoadResponseData;
40 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateAccountsResponseData;
41 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateGroupsResponseData;
42 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatePermissionsResponseData;
43 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedAccountsResponseData;
44 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedGroupsResponseData;
45 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedPermissionsResponseData;
46 import com.supwisdom.institute.backend.system.api.vo.response.RoleRemoveResponseData;
47 import com.supwisdom.institute.backend.system.api.vo.response.RoleUpdateResponseData;
48 import com.supwisdom.institute.backend.system.api.vo.response.RoleQueryResponseData;
49 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
50 import com.supwisdom.institute.backend.system.domain.entity.GroupRole;
51 import com.supwisdom.institute.backend.system.domain.entity.Role;
52 import com.supwisdom.institute.backend.system.domain.entity.RolePermission;
53 import com.supwisdom.institute.backend.system.domain.service.RoleService;
54
55 @Api(value = "SystemAdminRole", tags = { "SystemAdminRole" }, 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     System.out.println(deleteBatchRequest.getIds());
174     List<String> ids = deleteBatchRequest.getIds();
175     
176     roleService.deleteBatch(ids);
177     
178     RoleDeleteBatchResponseData data = RoleDeleteBatchResponseData.build(ids);
179     return new DefaultApiResponse<RoleDeleteBatchResponseData>(data);
180   }
181
182
183   @RequestMapping(method = RequestMethod.GET, path = "/{id}/accounts", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
184   @ResponseBody
185   public DefaultApiResponse<RoleRelatedAccountsResponseData> roleAccounts(
186       @PathVariable("id") String id, 
187       RoleRelatedAccountsRequest request) {
188
189     if (id == null || id.length() == 0) {
190       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
191     }
192
193     Role role = roleService.selectById(id);
194
195     if (role == null) {
196       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
197     }
198
199     if (request.getMapBean() == null) {
200       request.setMapBean(new HashMap<String, Object>());
201     }
202     request.getMapBean().put("roleId", role.getId());
203
204     Page<AccountRole> page = roleService.selectRoleAccounts(
205         request.getPageIndex(),
206         request.getPageSize(), 
207         request.getMapBean());
208
209     RoleRelatedAccountsResponseData data = RoleRelatedAccountsResponseData.of(request).build(page);
210
211     return new DefaultApiResponse<RoleRelatedAccountsResponseData>(data);
212   }
213
214   @RequestMapping(method = RequestMethod.POST, path = "/{id}/accounts", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
215   @ResponseBody
216   public DefaultApiResponse<RoleRelateAccountsResponseData> relateAccounts(
217       @PathVariable("id") String id, 
218       @RequestBody RoleRelateAccountsRequest roleAccounts) {
219
220     if (id == null || id.length() == 0) {
221       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
222     }
223
224     Role role = roleService.selectById(id);
225
226     if (role == null) {
227       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
228     }
229
230     roleService.relateRoleAccounts(role, roleAccounts.getRoleAccounts());
231
232     RoleRelateAccountsResponseData data = RoleRelateAccountsResponseData.of("info.relate.success");
233
234     return new DefaultApiResponse<RoleRelateAccountsResponseData>(data);
235   }
236
237
238   @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
239   @ResponseBody
240   public DefaultApiResponse<RoleRelatedGroupsResponseData> roleGroups(
241       @PathVariable("id") String id, 
242       RoleRelatedGroupsRequest request) {
243
244     if (id == null || id.length() == 0) {
245       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
246     }
247
248     Role role = roleService.selectById(id);
249
250     if (role == null) {
251       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
252     }
253
254     if (request.getMapBean() == null) {
255       request.setMapBean(new HashMap<String, Object>());
256     }
257     request.getMapBean().put("roleId", role.getId());
258
259     Page<GroupRole> page = roleService.selectRoleGroups(
260         request.getPageIndex(),
261         request.getPageSize(), 
262         request.getMapBean());
263
264     RoleRelatedGroupsResponseData data = RoleRelatedGroupsResponseData.of(request).build(page);
265
266     return new DefaultApiResponse<RoleRelatedGroupsResponseData>(data);
267   }
268
269   @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
270   @ResponseBody
271   public DefaultApiResponse<RoleRelateGroupsResponseData> relateGroups(
272       @PathVariable("id") String id, 
273       @RequestBody RoleRelateGroupsRequest roleGroups) {
274
275     if (id == null || id.length() == 0) {
276       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
277     }
278
279     Role role = roleService.selectById(id);
280
281     if (role == null) {
282       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
283     }
284
285     roleService.relateRoleGroups(role, roleGroups.getRoleGroups());
286
287     RoleRelateGroupsResponseData data = RoleRelateGroupsResponseData.of("info.relate.success");
288
289     return new DefaultApiResponse<RoleRelateGroupsResponseData>(data);
290   }
291
292
293   @RequestMapping(method = RequestMethod.GET, path = "/{id}/permissions", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
294   @ResponseBody
295   public DefaultApiResponse<RoleRelatedPermissionsResponseData> rolePermissions(
296       @PathVariable("id") String id, 
297       RoleRelatedPermissionsRequest request) {
298
299     if (id == null || id.length() == 0) {
300       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
301     }
302
303     Role role = roleService.selectById(id);
304
305     if (role == null) {
306       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
307     }
308
309     if (request.getMapBean() == null) {
310       request.setMapBean(new HashMap<String, Object>());
311     }
312     request.getMapBean().put("roleId", role.getId());
313
314     Page<RolePermission> page = roleService.selectRolePermissions(
315         request.getPageIndex(),
316         request.getPageSize(), 
317         request.getMapBean());
318
319     RoleRelatedPermissionsResponseData data = RoleRelatedPermissionsResponseData.of(request).build(page);
320
321     return new DefaultApiResponse<RoleRelatedPermissionsResponseData>(data);
322   }
323
324   @RequestMapping(method = RequestMethod.POST, path = "/{id}/permissions", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
325   @ResponseBody
326   public DefaultApiResponse<RoleRelatePermissionsResponseData> relatePermissions(
327       @PathVariable("id") String id, 
328       @RequestBody RoleRelatePermissionsRequest rolePermissions) {
329
330     if (id == null || id.length() == 0) {
331       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
332     }
333
334     Role role = roleService.selectById(id);
335
336     if (role == null) {
337       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
338     }
339
340     roleService.relateRolePermissions(role, rolePermissions.getRolePermissions());
341
342     RoleRelatePermissionsResponseData data = RoleRelatePermissionsResponseData.of("info.relate.success");
343
344     return new DefaultApiResponse<RoleRelatePermissionsResponseData>(data);
345   }
346
347 }