1 package com.supwisdom.institute.backend.system.api.v1.admin;
3 import java.util.HashMap;
6 import io.swagger.annotations.Api;
7 import lombok.extern.slf4j.Slf4j;
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;
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;
55 @Api(value = "SystemAdminRole", tags = { "SystemAdminRole" }, description = "角色的操作接口")
58 @RequestMapping("/v1/admin/roles")
59 public class AdminRoleController {
62 private RoleService roleService;
64 @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
65 @ResponseStatus(value = HttpStatus.OK)
67 public DefaultApiResponse<RoleQueryResponseData> query(RoleQueryRequest queryRequest) {
69 Page<Role> page = roleService.selectPageList(
70 queryRequest.isLoadAll(),
71 queryRequest.getPageIndex(),
72 queryRequest.getPageSize(),
73 queryRequest.getMapBean(),
74 queryRequest.getOrderBy());
76 RoleQueryResponseData data = RoleQueryResponseData.of(queryRequest).build(page);
78 return new DefaultApiResponse<RoleQueryResponseData>(data);
81 @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
82 @ResponseStatus(value = HttpStatus.OK)
84 public DefaultApiResponse<RoleLoadResponseData> load(@PathVariable("id") String id) {
86 if (id == null || id.length() == 0) {
87 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
90 Role role = roleService.selectById(id);
93 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
96 RoleLoadResponseData data = RoleLoadResponseData.of(role);
98 return new DefaultApiResponse<RoleLoadResponseData>(data);
101 @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
102 @ResponseStatus(value = HttpStatus.OK)
104 public DefaultApiResponse<RoleCreateResponseData> create(
105 @RequestBody RoleCreateRequest createRequest) {
109 Role role = createRequest.getEntity();
111 Role ret = roleService.insert(role);
113 RoleCreateResponseData data = RoleCreateResponseData.build(ret);
115 return new DefaultApiResponse<RoleCreateResponseData>(data);
118 @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
119 @ResponseStatus(value = HttpStatus.OK)
121 public DefaultApiResponse<RoleUpdateResponseData> update(
122 @PathVariable("id") String id,
123 @RequestBody RoleUpdateRequest updateRequest) {
125 if (id == null || id.length() == 0) {
126 throw new RuntimeException("exception.update.id.must.not.empty");
129 Role tmp = roleService.selectById(id);
131 throw new RuntimeException("exception.update.domain.not.exist");
134 Role role = updateRequest.getEntity();
137 role = EntityUtils.merge(tmp, role);
139 Role ret = roleService.update(role);
141 RoleUpdateResponseData data = RoleUpdateResponseData.build(ret);
143 return new DefaultApiResponse<RoleUpdateResponseData>(data);
146 @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
147 @ResponseStatus(value = HttpStatus.OK)
149 public DefaultApiResponse<RoleRemoveResponseData> delete(
150 @PathVariable("id") String id) {
152 if (id == null || id.length() == 0) {
153 throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
156 Role tmp = roleService.selectById(id);
158 throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
161 roleService.deleteById(id);
163 RoleRemoveResponseData data = RoleRemoveResponseData.build(tmp);
164 return new DefaultApiResponse<RoleRemoveResponseData>(data);
167 @DeleteMapping(path = "/batch", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
168 @ResponseStatus(value = HttpStatus.OK)
170 public DefaultApiResponse<RoleDeleteBatchResponseData> deleteBatch(
171 @RequestBody RoleDeleteBatchRequest deleteBatchRequest) {
173 System.out.println(deleteBatchRequest.getIds());
174 List<String> ids = deleteBatchRequest.getIds();
176 roleService.deleteBatch(ids);
178 RoleDeleteBatchResponseData data = RoleDeleteBatchResponseData.build(ids);
179 return new DefaultApiResponse<RoleDeleteBatchResponseData>(data);
183 @RequestMapping(method = RequestMethod.GET, path = "/{id}/accounts", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
185 public DefaultApiResponse<RoleRelatedAccountsResponseData> roleAccounts(
186 @PathVariable("id") String id,
187 RoleRelatedAccountsRequest request) {
189 if (id == null || id.length() == 0) {
190 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
193 Role role = roleService.selectById(id);
196 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
199 if (request.getMapBean() == null) {
200 request.setMapBean(new HashMap<String, Object>());
202 request.getMapBean().put("roleId", role.getId());
204 Page<AccountRole> page = roleService.selectRoleAccounts(
205 request.getPageIndex(),
206 request.getPageSize(),
207 request.getMapBean());
209 RoleRelatedAccountsResponseData data = RoleRelatedAccountsResponseData.of(request).build(page);
211 return new DefaultApiResponse<RoleRelatedAccountsResponseData>(data);
214 @RequestMapping(method = RequestMethod.POST, path = "/{id}/accounts", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
216 public DefaultApiResponse<RoleRelateAccountsResponseData> relateAccounts(
217 @PathVariable("id") String id,
218 @RequestBody RoleRelateAccountsRequest roleAccounts) {
220 if (id == null || id.length() == 0) {
221 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
224 Role role = roleService.selectById(id);
227 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
230 roleService.relateRoleAccounts(role, roleAccounts.getRoleAccounts());
232 RoleRelateAccountsResponseData data = RoleRelateAccountsResponseData.of("info.relate.success");
234 return new DefaultApiResponse<RoleRelateAccountsResponseData>(data);
238 @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
240 public DefaultApiResponse<RoleRelatedGroupsResponseData> roleGroups(
241 @PathVariable("id") String id,
242 RoleRelatedGroupsRequest request) {
244 if (id == null || id.length() == 0) {
245 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
248 Role role = roleService.selectById(id);
251 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
254 if (request.getMapBean() == null) {
255 request.setMapBean(new HashMap<String, Object>());
257 request.getMapBean().put("roleId", role.getId());
259 Page<GroupRole> page = roleService.selectRoleGroups(
260 request.getPageIndex(),
261 request.getPageSize(),
262 request.getMapBean());
264 RoleRelatedGroupsResponseData data = RoleRelatedGroupsResponseData.of(request).build(page);
266 return new DefaultApiResponse<RoleRelatedGroupsResponseData>(data);
269 @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
271 public DefaultApiResponse<RoleRelateGroupsResponseData> relateGroups(
272 @PathVariable("id") String id,
273 @RequestBody RoleRelateGroupsRequest roleGroups) {
275 if (id == null || id.length() == 0) {
276 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
279 Role role = roleService.selectById(id);
282 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
285 roleService.relateRoleGroups(role, roleGroups.getRoleGroups());
287 RoleRelateGroupsResponseData data = RoleRelateGroupsResponseData.of("info.relate.success");
289 return new DefaultApiResponse<RoleRelateGroupsResponseData>(data);
293 @RequestMapping(method = RequestMethod.GET, path = "/{id}/permissions", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
295 public DefaultApiResponse<RoleRelatedPermissionsResponseData> rolePermissions(
296 @PathVariable("id") String id,
297 RoleRelatedPermissionsRequest request) {
299 if (id == null || id.length() == 0) {
300 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
303 Role role = roleService.selectById(id);
306 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
309 if (request.getMapBean() == null) {
310 request.setMapBean(new HashMap<String, Object>());
312 request.getMapBean().put("roleId", role.getId());
314 Page<RolePermission> page = roleService.selectRolePermissions(
315 request.getPageIndex(),
316 request.getPageSize(),
317 request.getMapBean());
319 RoleRelatedPermissionsResponseData data = RoleRelatedPermissionsResponseData.of(request).build(page);
321 return new DefaultApiResponse<RoleRelatedPermissionsResponseData>(data);
324 @RequestMapping(method = RequestMethod.POST, path = "/{id}/permissions", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
326 public DefaultApiResponse<RoleRelatePermissionsResponseData> relatePermissions(
327 @PathVariable("id") String id,
328 @RequestBody RoleRelatePermissionsRequest rolePermissions) {
330 if (id == null || id.length() == 0) {
331 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
334 Role role = roleService.selectById(id);
337 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
340 roleService.relateRolePermissions(role, rolePermissions.getRolePermissions());
342 RoleRelatePermissionsResponseData data = RoleRelatePermissionsResponseData.of("info.relate.success");
344 return new DefaultApiResponse<RoleRelatePermissionsResponseData>(data);