1 package com.supwisdom.institute.backend.base.api.v1.controller.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.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;
55 @Api(value = "BaseAdminRole", tags = { "BaseAdminRole" }, 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 List<String> ids = deleteBatchRequest.getIds();
175 roleService.deleteBatch(ids);
177 RoleDeleteBatchResponseData data = RoleDeleteBatchResponseData.build(ids);
178 return new DefaultApiResponse<RoleDeleteBatchResponseData>(data);
182 @RequestMapping(method = RequestMethod.GET, path = "/{id}/accounts", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
184 public DefaultApiResponse<RoleRelatedAccountsResponseData> roleAccounts(
185 @PathVariable("id") String id,
186 RoleRelatedAccountsRequest request) {
188 if (id == null || id.length() == 0) {
189 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
192 Role role = roleService.selectById(id);
195 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
198 if (request.getMapBean() == null) {
199 request.setMapBean(new HashMap<String, Object>());
201 request.getMapBean().put("roleId", role.getId());
203 Page<AccountRole> page = roleService.selectRoleAccounts(
204 request.getPageIndex(),
205 request.getPageSize(),
206 request.getMapBean());
208 RoleRelatedAccountsResponseData data = RoleRelatedAccountsResponseData.of(request).build(page);
210 return new DefaultApiResponse<RoleRelatedAccountsResponseData>(data);
213 @RequestMapping(method = RequestMethod.POST, path = "/{id}/accounts", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
215 public DefaultApiResponse<RoleRelateAccountsResponseData> relateAccounts(
216 @PathVariable("id") String id,
217 @RequestBody RoleRelateAccountsRequest roleAccounts) {
219 if (id == null || id.length() == 0) {
220 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
223 Role role = roleService.selectById(id);
226 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
229 roleService.relateRoleAccounts(role, roleAccounts.getRoleAccounts());
231 RoleRelateAccountsResponseData data = RoleRelateAccountsResponseData.of("info.relate.success");
233 return new DefaultApiResponse<RoleRelateAccountsResponseData>(data);
237 @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
239 public DefaultApiResponse<RoleRelatedGroupsResponseData> roleGroups(
240 @PathVariable("id") String id,
241 RoleRelatedGroupsRequest request) {
243 if (id == null || id.length() == 0) {
244 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
247 Role role = roleService.selectById(id);
250 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
253 if (request.getMapBean() == null) {
254 request.setMapBean(new HashMap<String, Object>());
256 request.getMapBean().put("roleId", role.getId());
258 Page<GroupRole> page = roleService.selectRoleGroups(
259 request.getPageIndex(),
260 request.getPageSize(),
261 request.getMapBean());
263 RoleRelatedGroupsResponseData data = RoleRelatedGroupsResponseData.of(request).build(page);
265 return new DefaultApiResponse<RoleRelatedGroupsResponseData>(data);
268 @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
270 public DefaultApiResponse<RoleRelateGroupsResponseData> relateGroups(
271 @PathVariable("id") String id,
272 @RequestBody RoleRelateGroupsRequest roleGroups) {
274 if (id == null || id.length() == 0) {
275 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
278 Role role = roleService.selectById(id);
281 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
284 roleService.relateRoleGroups(role, roleGroups.getRoleGroups());
286 RoleRelateGroupsResponseData data = RoleRelateGroupsResponseData.of("info.relate.success");
288 return new DefaultApiResponse<RoleRelateGroupsResponseData>(data);
292 @RequestMapping(method = RequestMethod.GET, path = "/{id}/permissions", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
294 public DefaultApiResponse<RoleRelatedPermissionsResponseData> rolePermissions(
295 @PathVariable("id") String id,
296 RoleRelatedPermissionsRequest request) {
298 if (id == null || id.length() == 0) {
299 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
302 Role role = roleService.selectById(id);
305 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
308 if (request.getMapBean() == null) {
309 request.setMapBean(new HashMap<String, Object>());
311 request.getMapBean().put("roleId", role.getId());
313 Page<RolePermission> page = roleService.selectRolePermissions(
314 request.getPageIndex(),
315 request.getPageSize(),
316 request.getMapBean());
318 RoleRelatedPermissionsResponseData data = RoleRelatedPermissionsResponseData.of(request).build(page);
320 return new DefaultApiResponse<RoleRelatedPermissionsResponseData>(data);
323 @RequestMapping(method = RequestMethod.POST, path = "/{id}/permissions", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
325 public DefaultApiResponse<RoleRelatePermissionsResponseData> relatePermissions(
326 @PathVariable("id") String id,
327 @RequestBody RoleRelatePermissionsRequest rolePermissions) {
329 if (id == null || id.length() == 0) {
330 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
333 Role role = roleService.selectById(id);
336 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
339 roleService.relateRolePermissions(role, rolePermissions.getRolePermissions());
341 RoleRelatePermissionsResponseData data = RoleRelatePermissionsResponseData.of("info.relate.success");
343 return new DefaultApiResponse<RoleRelatePermissionsResponseData>(data);