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.AccountCreateRequest;
27 import com.supwisdom.institute.backend.system.api.vo.request.AccountQueryRequest;
28 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelateGroupsRequest;
29 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelateRolesRequest;
30 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelatedGroupsRequest;
31 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelatedRolesRequest;
32 import com.supwisdom.institute.backend.system.api.vo.request.AccountUpdateRequest;
33 import com.supwisdom.institute.backend.system.api.vo.response.AccountCreateResponseData;
34 import com.supwisdom.institute.backend.system.api.vo.response.AccountLoadResponseData;
35 import com.supwisdom.institute.backend.system.api.vo.response.AccountQueryResponseData;
36 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelateGroupsResponseData;
37 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelateRolesResponseData;
38 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelatedGroupsResponseData;
39 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelatedRolesResponseData;
40 import com.supwisdom.institute.backend.system.api.vo.response.AccountRemoveResponseData;
41 import com.supwisdom.institute.backend.system.api.vo.response.AccountUpdateResponseData;
42 import com.supwisdom.institute.backend.system.domain.entity.Account;
43 import com.supwisdom.institute.backend.system.domain.entity.AccountGroup;
44 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
45 import com.supwisdom.institute.backend.system.domain.service.AccountService;
47 @Api(value = "SystemAdminAccount", tags = { "SystemAdminAccount" }, description = "帐号的操作接口")
50 @RequestMapping("/v1/admin/accounts")
51 public class AdminAccountController {
54 private AccountService accountService;
58 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts'
59 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts?pageIndex=2&pageSize=50'
60 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts?pageIndex=0&pageSize=20&mapBean[username]=username&mapBean[name]=name&mapBean[status]=1'
61 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts?pageIndex=0&pageSize=20&mapBean[username]=username&mapBean[name]=name&mapBean[status]=0'
74 * "id":"ff80808164feb8990164feba0de50000",
77 * "addAccount":"account","addTime":"2018-08-03T07:39:23.000+0000",
78 * "editAccount":null,"editTime":null,
79 * "deleteAccount":null,"deleteTime":null,
80 * "accountname":"test001",
81 * "password":"test001",
83 * "accountNonExpired":true,
84 * "accountNonLocked":true,
85 * "credentialsNonExpired":true,
99 * "timestamp":"2018-08-03T08:48:25.777+0000",
101 * "error":"Http Status 401",
102 * "message":"Unauthorized",
103 * "path":"/api/v1/admin/accounts"
107 * @param pagerRequestModel
110 @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
111 @ResponseStatus(value = HttpStatus.OK)
113 public DefaultApiResponse<AccountQueryResponseData> query(AccountQueryRequest queryRequest) {
115 Page<Account> page = accountService.selectPageList(
116 queryRequest.isLoadAll(),
117 queryRequest.getPageIndex(),
118 queryRequest.getPageSize(),
119 queryRequest.getMapBean(),
120 queryRequest.getOrderBy());
122 AccountQueryResponseData data = AccountQueryResponseData.of(queryRequest).build(page);
124 return new DefaultApiResponse<AccountQueryResponseData>(data);
129 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
135 * "id":"ff80808164feb8990164feba0de50000",
138 * "addAccount":"account","addTime":"2018-08-03T07:39:23.000+0000",
139 * "editAccount":null,"editTime":null,
140 * "deleteAccount":null,"deleteTime":null,
141 * "username":"test001",
142 * "password":"test001",
144 * "accountNonExpired":true,
145 * "accountNonLocked":true,
146 * "credentialsNonExpired":true,
154 * response error 401:
158 * "timestamp":"2018-08-03T08:43:26.080+0000",
160 * "error":"Http Status 401",
161 * "message":"Unauthorized",
162 * "path":"/api/v1/admin/accounts/ff80808164fecf640164fed269480000"
166 * response error 500:
170 * "timestamp":"2018-08-03T07:44:07.963+0000",
172 * "error":"Internal Server Error",
173 * "exception":"java.lang.RuntimeException",
174 * "message":"exception.get.domain.not.exist",
175 * "path":"/api/v1/admin/accounts/1"
182 @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
183 @ResponseStatus(value = HttpStatus.OK)
185 public DefaultApiResponse<AccountLoadResponseData> load(@PathVariable("id") String id) {
187 if (id == null || id.length() == 0) {
188 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
191 Account account = accountService.selectById(id);
193 if (account == null) {
194 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
197 AccountLoadResponseData data = AccountLoadResponseData.of(account);
199 return new DefaultApiResponse<AccountLoadResponseData>(data);
204 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
205 * -d '{"accountname":"test001","password":"test001","enabled":true,"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"name":"测试001","status":"1"}'
211 * "success":"info.create.success"
215 * response error 401:
219 * "timestamp":"2018-08-03T08:48:25.777+0000",
221 * "error":"Http Status 401",
222 * "message":"Unauthorized",
223 * "path":"/api/v1/admin/accounts"
227 * response error: // FIXME: save error
231 * "timestamp":"2018-08-03T07:45:43.436+0000",
233 * "error":"Internal Server Error",
234 * "exception":"org.springframework.dao.DataIntegrityViolationException",
235 * "message":"could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
236 * "path":"/api/v1/admin/accounts"
243 @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
244 @ResponseStatus(value = HttpStatus.OK)
246 public DefaultApiResponse<AccountCreateResponseData> create(
247 @RequestBody AccountCreateRequest createRequest) {
251 Account account = createRequest.getEntity();
253 if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
254 //account.setPassword(passwordEncoder.encode(account.getPassword()));
257 Account ret = accountService.insert(account);
259 AccountCreateResponseData data = AccountCreateResponseData.build(ret);
261 return new DefaultApiResponse<AccountCreateResponseData>(data);
266 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
267 * -d '{"id":"1","status":"0"}'
273 * "success":"info.update.success"
277 * response error 401:
281 * "timestamp":"2018-08-03T08:48:25.777+0000",
283 * "error":"Http Status 401",
284 * "message":"Unauthorized",
285 * "path":"/api/v1/admin/accounts"
289 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
290 * -d '{"status":"0"}'
296 * "timestamp":"2018-08-03T07:50:52.327+0000",
298 * "error":"Internal Server Error",
299 * "exception":"java.lang.RuntimeException",
300 * "message":"exception.update.id.must.not.empty",
301 * "path":"/api/v1/admin/accounts"
305 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
306 * -d '{"id":"1","status":"0"}'
312 * "timestamp":"2018-08-03T07:48:24.774+0000",
314 * "error":"Internal Server Error",
315 * "exception":"java.lang.RuntimeException",
316 * "message":"exception.update.domain.not.exist",
317 * "path":"/api/v1/admin/accounts"
324 @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
325 @ResponseStatus(value = HttpStatus.OK)
327 public DefaultApiResponse<AccountUpdateResponseData> update(
328 @PathVariable("id") String id,
329 @RequestBody AccountUpdateRequest updateRequest) {
331 if (id == null || id.length() == 0) {
332 throw new RuntimeException("exception.update.id.must.not.empty");
335 Account tmp = accountService.selectById(id);
337 throw new RuntimeException("exception.update.domain.not.exist");
340 Account account = updateRequest.getEntity();
343 if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
344 //account.setPassword(passwordEncoder.encode(account.getPassword()));
347 account = EntityUtils.merge(tmp, account);
349 Account ret = accountService.update(account);
351 AccountUpdateResponseData data = AccountUpdateResponseData.build(ret);
353 return new DefaultApiResponse<AccountUpdateResponseData>(data);
359 * curl -i -s -X DELETE -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
365 * "success":"info.delete.success"
369 * response error 401:
373 * "timestamp":"2018-08-03T08:48:25.777+0000",
375 * "error":"Http Status 401",
376 * "message":"Unauthorized",
377 * "path":"/api/v1/admin/accounts/1"
381 * response error 500:
385 * "timestamp":"2018-08-03T08:03:16.364+0000",
387 * "error":"Internal Server Error",
388 * "exception":"java.lang.RuntimeException",
389 * "message":"exception.delete.domain.not.exist",
390 * "path":"/api/v1/admin/accounts/1"
397 @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
398 @ResponseStatus(value = HttpStatus.OK)
400 public DefaultApiResponse<AccountRemoveResponseData> delete(
401 @PathVariable("id") String id) {
403 if (id == null || id.length() == 0) {
404 throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
407 Account tmp = accountService.selectById(id);
409 throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
412 accountService.deleteById(id);
414 AccountRemoveResponseData data = AccountRemoveResponseData.build(tmp);
415 return new DefaultApiResponse<AccountRemoveResponseData>(data);
420 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups'
421 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups?pageIndex=2&pageSize=50'
422 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups?pageIndex=0&pageSize=20&mapBean[groupCode]=groupCode&mapBean[groupName]=groupName'
427 * @param pagerRequestModel
430 @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
432 public DefaultApiResponse<AccountRelatedGroupsResponseData> accountGroups(
433 @PathVariable("id") String id,
434 AccountRelatedGroupsRequest request) {
436 if (id == null || id.length() == 0) {
437 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
440 Account account = accountService.selectById(id);
442 if (account == null) {
443 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
446 if (request.getMapBean() == null) {
447 request.setMapBean(new HashMap<String, Object>());
449 request.getMapBean().put("accountId", account.getId());
451 Page<AccountGroup> page = accountService.selectAccountGroups(request.getPageIndex(),
452 request.getPageSize(), request.getMapBean());
454 AccountRelatedGroupsResponseData data = AccountRelatedGroupsResponseData.of(request).build(page);
456 return new DefaultApiResponse<AccountRelatedGroupsResponseData>(data);
461 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups' \
462 * -d '{"groupAccounts":[{"groupId":"1"},{"groupId":"2"}]}'
466 * @param groupAccounts
469 @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
471 public DefaultApiResponse<AccountRelateGroupsResponseData> relateGroups(
472 @PathVariable("id") String id,
473 @RequestBody AccountRelateGroupsRequest accountGroups) {
475 if (id == null || id.length() == 0) {
476 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
479 Account tmp = accountService.selectById(id);
482 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
485 accountService.relateAccountGroups(tmp, accountGroups.getAccountGroups());
487 AccountRelateGroupsResponseData data = AccountRelateGroupsResponseData.of("info.relate.success");
489 return new DefaultApiResponse<AccountRelateGroupsResponseData>(data);
494 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles'
495 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles?pageIndex=2&pageSize=50'
496 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles?pageIndex=0&pageSize=20&mapBean[roleCode]=roleCode&mapBean[roleName]=roleName'
501 * @param pagerRequestModel
504 @RequestMapping(method = RequestMethod.GET, path = "/{id}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
506 public DefaultApiResponse<AccountRelatedRolesResponseData> accountRoles(
507 @PathVariable("id") String id,
508 AccountRelatedRolesRequest request) {
510 if (id == null || id.length() == 0) {
511 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
514 Account account = accountService.selectById(id);
516 if (account == null) {
517 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
520 if (request.getMapBean() == null) {
521 request.setMapBean(new HashMap<String, Object>());
523 request.getMapBean().put("accountId", account.getId());
525 Page<AccountRole> page = accountService.selectAccountRoles(request.getPageIndex(),
526 request.getPageSize(), request.getMapBean());
528 AccountRelatedRolesResponseData data = AccountRelatedRolesResponseData.of(request).build(page);
530 return new DefaultApiResponse<AccountRelatedRolesResponseData>(data);
535 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles' \
536 * -d '{"accountRoles":[{"roleId":"1"},{"roleId":"2"}]}'
540 * @param accountRoles
543 @RequestMapping(method = RequestMethod.POST, path = "/{id}/roles", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
545 public DefaultApiResponse<AccountRelateRolesResponseData> relateRoles(
546 @PathVariable("id") String id,
547 @RequestBody AccountRelateRolesRequest accountRoles) {
549 if (id == null || id.length() == 0) {
550 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
553 Account account = accountService.selectById(id);
555 if (account == null) {
556 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
559 accountService.relateAccountRoles(account, accountRoles.getAccountRoles());
561 AccountRelateRolesResponseData data = AccountRelateRolesResponseData.of("info.relate.success");
563 return new DefaultApiResponse<AccountRelateRolesResponseData>(data);