1 package com.supwisdom.institute.backend.system.api.v1.admin;
3 import java.util.HashMap;
5 import io.swagger.annotations.Api;
6 import lombok.extern.slf4j.Slf4j;
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;
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.RoleRelatedAccountsRequest;
30 import com.supwisdom.institute.backend.system.api.vo.request.RoleRelatedGroupsRequest;
31 import com.supwisdom.institute.backend.system.api.vo.request.RoleUpdateRequest;
32 import com.supwisdom.institute.backend.system.api.vo.request.RoleQueryRequest;
33 import com.supwisdom.institute.backend.system.api.vo.response.RoleCreateResponseData;
34 import com.supwisdom.institute.backend.system.api.vo.response.RoleLoadResponseData;
35 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateAccountsResponseData;
36 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelateGroupsResponseData;
37 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedAccountsResponseData;
38 import com.supwisdom.institute.backend.system.api.vo.response.RoleRelatedGroupsResponseData;
39 import com.supwisdom.institute.backend.system.api.vo.response.RoleRemoveResponseData;
40 import com.supwisdom.institute.backend.system.api.vo.response.RoleUpdateResponseData;
41 import com.supwisdom.institute.backend.system.api.vo.response.RoleQueryResponseData;
42 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
43 import com.supwisdom.institute.backend.system.domain.entity.GroupRole;
44 import com.supwisdom.institute.backend.system.domain.entity.Role;
45 import com.supwisdom.institute.backend.system.domain.service.RoleService;
47 @Api(value = "SystemAdminRole", tags = { "SystemAdminRole" }, description = "角色的操作接口")
50 @RequestMapping("/v1/admin/roles")
51 public class AdminRoleController {
54 private RoleService roleService;
56 @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
57 @ResponseStatus(value = HttpStatus.OK)
59 public DefaultApiResponse<RoleQueryResponseData> query(RoleQueryRequest queryRequest) {
61 Page<Role> page = roleService.selectPageList(
62 queryRequest.isLoadAll(),
63 queryRequest.getPageIndex(),
64 queryRequest.getPageSize(),
65 queryRequest.getMapBean(),
66 queryRequest.getOrderBy());
68 RoleQueryResponseData data = RoleQueryResponseData.of(queryRequest).build(page);
70 return new DefaultApiResponse<RoleQueryResponseData>(data);
73 @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
74 @ResponseStatus(value = HttpStatus.OK)
76 public DefaultApiResponse<RoleLoadResponseData> load(@PathVariable("id") String id) {
78 if (id == null || id.length() == 0) {
79 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
82 Role role = roleService.selectById(id);
85 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
88 RoleLoadResponseData data = RoleLoadResponseData.of(role);
90 return new DefaultApiResponse<RoleLoadResponseData>(data);
93 @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
94 @ResponseStatus(value = HttpStatus.OK)
96 public DefaultApiResponse<RoleCreateResponseData> create(
97 @RequestBody RoleCreateRequest createRequest) {
101 Role role = createRequest.getEntity();
103 Role ret = roleService.insert(role);
105 RoleCreateResponseData data = RoleCreateResponseData.build(ret);
107 return new DefaultApiResponse<RoleCreateResponseData>(data);
110 @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
111 @ResponseStatus(value = HttpStatus.OK)
113 public DefaultApiResponse<RoleUpdateResponseData> update(
114 @PathVariable("id") String id,
115 @RequestBody RoleUpdateRequest updateRequest) {
117 if (id == null || id.length() == 0) {
118 throw new RuntimeException("exception.update.id.must.not.empty");
121 Role tmp = roleService.selectById(id);
123 throw new RuntimeException("exception.update.domain.not.exist");
126 Role role = updateRequest.getEntity();
129 role = EntityUtils.merge(tmp, role);
131 Role ret = roleService.update(role);
133 RoleUpdateResponseData data = RoleUpdateResponseData.build(ret);
135 return new DefaultApiResponse<RoleUpdateResponseData>(data);
138 @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
139 @ResponseStatus(value = HttpStatus.OK)
141 public DefaultApiResponse<RoleRemoveResponseData> delete(
142 @PathVariable("id") String id) {
144 if (id == null || id.length() == 0) {
145 throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
148 Role tmp = roleService.selectById(id);
150 throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
153 roleService.deleteById(id);
155 RoleRemoveResponseData data = RoleRemoveResponseData.build(tmp);
156 return new DefaultApiResponse<RoleRemoveResponseData>(data);
160 @RequestMapping(method = RequestMethod.GET, path = "/{id}/accounts", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
162 public DefaultApiResponse<RoleRelatedAccountsResponseData> roleAccounts(
163 @PathVariable("id") String id,
164 RoleRelatedAccountsRequest request) {
166 if (id == null || id.length() == 0) {
167 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
170 Role role = roleService.selectById(id);
173 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
176 if (request.getMapBean() == null) {
177 request.setMapBean(new HashMap<String, Object>());
179 request.getMapBean().put("roleId", role.getId());
181 Page<AccountRole> page = roleService.selectRoleAccounts(
182 request.getPageIndex(),
183 request.getPageSize(),
184 request.getMapBean());
186 RoleRelatedAccountsResponseData data = RoleRelatedAccountsResponseData.of(request).build(page);
188 return new DefaultApiResponse<RoleRelatedAccountsResponseData>(data);
191 @RequestMapping(method = RequestMethod.POST, path = "/{id}/accounts", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
193 public DefaultApiResponse<RoleRelateAccountsResponseData> relateAccounts(
194 @PathVariable("id") String id,
195 @RequestBody RoleRelateAccountsRequest roleAccounts) {
197 if (id == null || id.length() == 0) {
198 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
201 Role role = roleService.selectById(id);
204 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
207 roleService.relateRoleAccounts(role, roleAccounts.getRoleAccounts());
209 RoleRelateAccountsResponseData data = RoleRelateAccountsResponseData.of("info.relate.success");
211 return new DefaultApiResponse<RoleRelateAccountsResponseData>(data);
215 @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
217 public DefaultApiResponse<RoleRelatedGroupsResponseData> roleGroups(
218 @PathVariable("id") String id,
219 RoleRelatedGroupsRequest request) {
221 if (id == null || id.length() == 0) {
222 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
225 Role role = roleService.selectById(id);
228 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
231 if (request.getMapBean() == null) {
232 request.setMapBean(new HashMap<String, Object>());
234 request.getMapBean().put("roleId", role.getId());
236 Page<GroupRole> page = roleService.selectRoleGroups(
237 request.getPageIndex(),
238 request.getPageSize(),
239 request.getMapBean());
241 RoleRelatedGroupsResponseData data = RoleRelatedGroupsResponseData.of(request).build(page);
243 return new DefaultApiResponse<RoleRelatedGroupsResponseData>(data);
246 @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
248 public DefaultApiResponse<RoleRelateGroupsResponseData> relateGroups(
249 @PathVariable("id") String id,
250 @RequestBody RoleRelateGroupsRequest accountGroups) {
252 if (id == null || id.length() == 0) {
253 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
256 Role role = roleService.selectById(id);
259 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
262 roleService.relateRoleGroups(role, accountGroups.getRoleGroups());
264 RoleRelateGroupsResponseData data = RoleRelateGroupsResponseData.of("info.relate.success");
266 return new DefaultApiResponse<RoleRelateGroupsResponseData>(data);