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.AccountCreateRequest;
28 import com.supwisdom.institute.backend.system.api.vo.request.AccountDeleteBatchRequest;
29 import com.supwisdom.institute.backend.system.api.vo.request.AccountQueryRequest;
30 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelateGroupsRequest;
31 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelateRolesRequest;
32 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelatedGroupsRequest;
33 import com.supwisdom.institute.backend.system.api.vo.request.AccountRelatedRolesRequest;
34 import com.supwisdom.institute.backend.system.api.vo.request.AccountUpdateRequest;
35 import com.supwisdom.institute.backend.system.api.vo.response.AccountCreateResponseData;
36 import com.supwisdom.institute.backend.system.api.vo.response.AccountDeleteBatchResponseData;
37 import com.supwisdom.institute.backend.system.api.vo.response.AccountLoadResponseData;
38 import com.supwisdom.institute.backend.system.api.vo.response.AccountQueryResponseData;
39 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelateGroupsResponseData;
40 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelateRolesResponseData;
41 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelatedGroupsResponseData;
42 import com.supwisdom.institute.backend.system.api.vo.response.AccountRelatedRolesResponseData;
43 import com.supwisdom.institute.backend.system.api.vo.response.AccountRemoveResponseData;
44 import com.supwisdom.institute.backend.system.api.vo.response.AccountUpdateResponseData;
45 import com.supwisdom.institute.backend.system.domain.entity.Account;
46 import com.supwisdom.institute.backend.system.domain.entity.AccountGroup;
47 import com.supwisdom.institute.backend.system.domain.entity.AccountRole;
48 import com.supwisdom.institute.backend.system.domain.service.AccountService;
50 @Api(value = "SystemAdminAccount", tags = { "SystemAdminAccount" }, description = "帐号的操作接口")
53 @RequestMapping("/v1/admin/accounts")
54 public class AdminAccountController {
57 private AccountService accountService;
61 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts'
62 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts?pageIndex=2&pageSize=50'
63 * 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'
64 * 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'
77 * "id":"ff80808164feb8990164feba0de50000",
80 * "addAccount":"account","addTime":"2018-08-03T07:39:23.000+0000",
81 * "editAccount":null,"editTime":null,
82 * "deleteAccount":null,"deleteTime":null,
83 * "accountname":"test001",
84 * "password":"test001",
86 * "accountNonExpired":true,
87 * "accountNonLocked":true,
88 * "credentialsNonExpired":true,
102 * "timestamp":"2018-08-03T08:48:25.777+0000",
104 * "error":"Http Status 401",
105 * "message":"Unauthorized",
106 * "path":"/api/v1/admin/accounts"
110 * @param pagerRequestModel
113 @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
114 @ResponseStatus(value = HttpStatus.OK)
116 public DefaultApiResponse<AccountQueryResponseData> query(AccountQueryRequest queryRequest) {
118 Page<Account> page = accountService.selectPageList(
119 queryRequest.isLoadAll(),
120 queryRequest.getPageIndex(),
121 queryRequest.getPageSize(),
122 queryRequest.getMapBean(),
123 queryRequest.getOrderBy());
125 AccountQueryResponseData data = AccountQueryResponseData.of(queryRequest).build(page);
127 return new DefaultApiResponse<AccountQueryResponseData>(data);
132 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
138 * "id":"ff80808164feb8990164feba0de50000",
141 * "addAccount":"account","addTime":"2018-08-03T07:39:23.000+0000",
142 * "editAccount":null,"editTime":null,
143 * "deleteAccount":null,"deleteTime":null,
144 * "username":"test001",
145 * "password":"test001",
147 * "accountNonExpired":true,
148 * "accountNonLocked":true,
149 * "credentialsNonExpired":true,
157 * response error 401:
161 * "timestamp":"2018-08-03T08:43:26.080+0000",
163 * "error":"Http Status 401",
164 * "message":"Unauthorized",
165 * "path":"/api/v1/admin/accounts/ff80808164fecf640164fed269480000"
169 * response error 500:
173 * "timestamp":"2018-08-03T07:44:07.963+0000",
175 * "error":"Internal Server Error",
176 * "exception":"java.lang.RuntimeException",
177 * "message":"exception.get.domain.not.exist",
178 * "path":"/api/v1/admin/accounts/1"
185 @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
186 @ResponseStatus(value = HttpStatus.OK)
188 public DefaultApiResponse<AccountLoadResponseData> load(@PathVariable("id") String id) {
190 if (id == null || id.length() == 0) {
191 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
194 Account account = accountService.selectById(id);
196 if (account == null) {
197 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
200 AccountLoadResponseData data = AccountLoadResponseData.of(account);
202 return new DefaultApiResponse<AccountLoadResponseData>(data);
207 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
208 * -d '{"accountname":"test001","password":"test001","enabled":true,"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"name":"测试001","status":"1"}'
214 * "success":"info.create.success"
218 * response error 401:
222 * "timestamp":"2018-08-03T08:48:25.777+0000",
224 * "error":"Http Status 401",
225 * "message":"Unauthorized",
226 * "path":"/api/v1/admin/accounts"
230 * response error: // FIXME: save error
234 * "timestamp":"2018-08-03T07:45:43.436+0000",
236 * "error":"Internal Server Error",
237 * "exception":"org.springframework.dao.DataIntegrityViolationException",
238 * "message":"could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
239 * "path":"/api/v1/admin/accounts"
246 @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
247 @ResponseStatus(value = HttpStatus.OK)
249 public DefaultApiResponse<AccountCreateResponseData> create(
250 @RequestBody AccountCreateRequest createRequest) {
254 Account account = createRequest.getEntity();
256 if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
257 //account.setPassword(passwordEncoder.encode(account.getPassword()));
260 Account ret = accountService.insert(account);
262 AccountCreateResponseData data = AccountCreateResponseData.build(ret);
264 return new DefaultApiResponse<AccountCreateResponseData>(data);
269 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
270 * -d '{"id":"1","status":"0"}'
276 * "success":"info.update.success"
280 * response error 401:
284 * "timestamp":"2018-08-03T08:48:25.777+0000",
286 * "error":"Http Status 401",
287 * "message":"Unauthorized",
288 * "path":"/api/v1/admin/accounts"
292 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
293 * -d '{"status":"0"}'
299 * "timestamp":"2018-08-03T07:50:52.327+0000",
301 * "error":"Internal Server Error",
302 * "exception":"java.lang.RuntimeException",
303 * "message":"exception.update.id.must.not.empty",
304 * "path":"/api/v1/admin/accounts"
308 * curl -i -s -X PUT -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts' \
309 * -d '{"id":"1","status":"0"}'
315 * "timestamp":"2018-08-03T07:48:24.774+0000",
317 * "error":"Internal Server Error",
318 * "exception":"java.lang.RuntimeException",
319 * "message":"exception.update.domain.not.exist",
320 * "path":"/api/v1/admin/accounts"
327 @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
328 @ResponseStatus(value = HttpStatus.OK)
330 public DefaultApiResponse<AccountUpdateResponseData> update(
331 @PathVariable("id") String id,
332 @RequestBody AccountUpdateRequest updateRequest) {
334 if (id == null || id.length() == 0) {
335 throw new RuntimeException("exception.update.id.must.not.empty");
338 Account tmp = accountService.selectById(id);
340 throw new RuntimeException("exception.update.domain.not.exist");
343 Account account = updateRequest.getEntity();
346 if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
347 //account.setPassword(passwordEncoder.encode(account.getPassword()));
350 account = EntityUtils.merge(tmp, account);
352 Account ret = accountService.update(account);
354 AccountUpdateResponseData data = AccountUpdateResponseData.build(ret);
356 return new DefaultApiResponse<AccountUpdateResponseData>(data);
362 * curl -i -s -X DELETE -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
368 * "success":"info.delete.success"
372 * response error 401:
376 * "timestamp":"2018-08-03T08:48:25.777+0000",
378 * "error":"Http Status 401",
379 * "message":"Unauthorized",
380 * "path":"/api/v1/admin/accounts/1"
384 * response error 500:
388 * "timestamp":"2018-08-03T08:03:16.364+0000",
390 * "error":"Internal Server Error",
391 * "exception":"java.lang.RuntimeException",
392 * "message":"exception.delete.domain.not.exist",
393 * "path":"/api/v1/admin/accounts/1"
400 @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
401 @ResponseStatus(value = HttpStatus.OK)
403 public DefaultApiResponse<AccountRemoveResponseData> delete(
404 @PathVariable("id") String id) {
406 if (id == null || id.length() == 0) {
407 throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
410 Account tmp = accountService.selectById(id);
412 throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
415 accountService.deleteById(id);
417 AccountRemoveResponseData data = AccountRemoveResponseData.build(tmp);
418 return new DefaultApiResponse<AccountRemoveResponseData>(data);
421 @DeleteMapping(path = "/batch", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
422 @ResponseStatus(value = HttpStatus.OK)
424 public DefaultApiResponse<AccountDeleteBatchResponseData> deleteBatch(
425 @RequestBody AccountDeleteBatchRequest deleteBatchRequest) {
427 System.out.println(deleteBatchRequest.getIds());
428 List<String> ids = deleteBatchRequest.getIds();
430 accountService.deleteBatch(ids);
432 AccountDeleteBatchResponseData data = AccountDeleteBatchResponseData.build(ids);
433 return new DefaultApiResponse<AccountDeleteBatchResponseData>(data);
438 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups'
439 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups?pageIndex=2&pageSize=50'
440 * 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'
445 * @param pagerRequestModel
448 @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
450 public DefaultApiResponse<AccountRelatedGroupsResponseData> accountGroups(
451 @PathVariable("id") String id,
452 AccountRelatedGroupsRequest request) {
454 if (id == null || id.length() == 0) {
455 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
458 Account account = accountService.selectById(id);
460 if (account == null) {
461 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
464 if (request.getMapBean() == null) {
465 request.setMapBean(new HashMap<String, Object>());
467 request.getMapBean().put("accountId", account.getId());
469 Page<AccountGroup> page = accountService.selectAccountGroups(request.getPageIndex(),
470 request.getPageSize(), request.getMapBean());
472 AccountRelatedGroupsResponseData data = AccountRelatedGroupsResponseData.of(request).build(page);
474 return new DefaultApiResponse<AccountRelatedGroupsResponseData>(data);
479 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/groups' \
480 * -d '{"groupAccounts":[{"groupId":"1"},{"groupId":"2"}]}'
484 * @param groupAccounts
487 @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
489 public DefaultApiResponse<AccountRelateGroupsResponseData> relateGroups(
490 @PathVariable("id") String id,
491 @RequestBody AccountRelateGroupsRequest accountGroups) {
493 if (id == null || id.length() == 0) {
494 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
497 Account tmp = accountService.selectById(id);
500 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
503 accountService.relateAccountGroups(tmp, accountGroups.getAccountGroups());
505 AccountRelateGroupsResponseData data = AccountRelateGroupsResponseData.of("info.relate.success");
507 return new DefaultApiResponse<AccountRelateGroupsResponseData>(data);
512 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles'
513 * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles?pageIndex=2&pageSize=50'
514 * 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'
519 * @param pagerRequestModel
522 @RequestMapping(method = RequestMethod.GET, path = "/{id}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
524 public DefaultApiResponse<AccountRelatedRolesResponseData> accountRoles(
525 @PathVariable("id") String id,
526 AccountRelatedRolesRequest request) {
528 if (id == null || id.length() == 0) {
529 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
532 Account account = accountService.selectById(id);
534 if (account == null) {
535 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
538 if (request.getMapBean() == null) {
539 request.setMapBean(new HashMap<String, Object>());
541 request.getMapBean().put("accountId", account.getId());
543 Page<AccountRole> page = accountService.selectAccountRoles(request.getPageIndex(),
544 request.getPageSize(), request.getMapBean());
546 AccountRelatedRolesResponseData data = AccountRelatedRolesResponseData.of(request).build(page);
548 return new DefaultApiResponse<AccountRelatedRolesResponseData>(data);
553 * curl -i -s -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1/roles' \
554 * -d '{"accountRoles":[{"roleId":"1"},{"roleId":"2"}]}'
558 * @param accountRoles
561 @RequestMapping(method = RequestMethod.POST, path = "/{id}/roles", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
563 public DefaultApiResponse<AccountRelateRolesResponseData> relateRoles(
564 @PathVariable("id") String id,
565 @RequestBody AccountRelateRolesRequest accountRoles) {
567 if (id == null || id.length() == 0) {
568 throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
571 Account account = accountService.selectById(id);
573 if (account == null) {
574 throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
577 accountService.relateAccountRoles(account, accountRoles.getAccountRoles());
579 AccountRelateRolesResponseData data = AccountRelateRolesResponseData.of("info.relate.success");
581 return new DefaultApiResponse<AccountRelateRolesResponseData>(data);