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