dbb8dda35276be510c2ad3a883c2c306ac64a747
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.api.v1.admin;
2
3 import java.util.HashMap;
4 import java.util.List;
5
6 import io.swagger.annotations.Api;
7 import lombok.extern.slf4j.Slf4j;
8
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;
24
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;
49
50 @Api(value = "SystemAdminAccount", tags = { "SystemAdminAccount" }, description = "帐号的操作接口")
51 @Slf4j
52 @RestController
53 @RequestMapping("/v1/admin/accounts")
54 public class AdminAccountController {
55   
56   @Autowired
57   private AccountService accountService;
58
59   /**
60    * 
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'
65    * 
66    * response success:
67    * 
68    * <pre>
69    * {
70    *   "pageIndex":0,
71    *   "pageSize":20,
72    *   "mapBean":null,
73    *   "pageCount":1,
74    *   "recordCount":1,
75    *   "items":[
76    *     {
77    *       "id":"ff80808164feb8990164feba0de50000",
78    *       "companyId":"1",
79    *       "deleted":false,
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",
85    *       "enabled":true,
86    *       "accountNonExpired":true,
87    *       "accountNonLocked":true,
88    *       "credentialsNonExpired":true,
89    *       "name":"测试001",
90    *       "status":"1",
91    *       "mobile":null,
92    *       "email":null
93    *     }
94    *   ]
95    * }
96    * </pre>
97    * 
98    * response error 401:
99    * 
100    * <pre>
101    * {
102    *   "timestamp":"2018-08-03T08:48:25.777+0000",
103    *   "status":401,
104    *   "error":"Http Status 401",
105    *   "message":"Unauthorized",
106    *   "path":"/api/v1/admin/accounts"
107    * }
108    * </pre>
109    * 
110    * @param pagerRequestModel
111    * @return
112    */
113   @GetMapping(produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
114   @ResponseStatus(value = HttpStatus.OK)
115   @ResponseBody
116   public DefaultApiResponse<AccountQueryResponseData> query(AccountQueryRequest queryRequest) {
117
118     Page<Account> page = accountService.selectPageList(
119         queryRequest.isLoadAll(), 
120         queryRequest.getPageIndex(), 
121         queryRequest.getPageSize(),
122         queryRequest.getMapBean(),
123         queryRequest.getOrderBy());
124
125     AccountQueryResponseData data = AccountQueryResponseData.of(queryRequest).build(page);
126
127     return new DefaultApiResponse<AccountQueryResponseData>(data);
128   }
129
130   /**
131    * 
132    * curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
133    * 
134    * response success:
135    * 
136    * <pre>
137    * {
138    *   "id":"ff80808164feb8990164feba0de50000",
139    *   "companyId":"1",
140    *   "deleted":false,
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",
146    *   "enabled":true,
147    *   "accountNonExpired":true,
148    *   "accountNonLocked":true,
149    *   "credentialsNonExpired":true,
150    *   "name":"测试001",
151    *   "status":"1",
152    *   "mobile":null,
153    *   "email":null
154    * }
155    * </pre>
156    * 
157    * response error 401:
158    * 
159    * <pre>
160    * {
161    *   "timestamp":"2018-08-03T08:43:26.080+0000",
162    *   "status":401,
163    *   "error":"Http Status 401",
164    *   "message":"Unauthorized",
165    *   "path":"/api/v1/admin/accounts/ff80808164fecf640164fed269480000"
166    * }
167    * </pre>
168    * 
169    * response error 500:
170    * 
171    * <pre>
172    * {
173    *   "timestamp":"2018-08-03T07:44:07.963+0000",
174    *   "status":500,
175    *   "error":"Internal Server Error",
176    *   "exception":"java.lang.RuntimeException",
177    *   "message":"exception.get.domain.not.exist",
178    *   "path":"/api/v1/admin/accounts/1"
179    * }
180    * </pre>
181    * 
182    * @param id
183    * @return
184    */
185   @GetMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
186   @ResponseStatus(value = HttpStatus.OK)
187   @ResponseBody
188   public DefaultApiResponse<AccountLoadResponseData> load(@PathVariable("id") String id) {
189
190     if (id == null || id.length() == 0) {
191       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
192     }
193
194     Account account = accountService.selectById(id);
195
196     if (account == null) {
197       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
198     }
199     
200     AccountLoadResponseData data = AccountLoadResponseData.of(account);
201
202     return new DefaultApiResponse<AccountLoadResponseData>(data);
203   }
204
205   /**
206    * 
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"}'
209    * 
210    * response success:
211    * 
212    * <pre>
213    * {
214    *   "success":"info.create.success"
215    * }
216    * </pre>
217    * 
218    * response error 401:
219    * 
220    * <pre>
221    * {
222    *   "timestamp":"2018-08-03T08:48:25.777+0000",
223    *   "status":401,
224    *   "error":"Http Status 401",
225    *   "message":"Unauthorized",
226    *   "path":"/api/v1/admin/accounts"
227    * }
228    * </pre>
229    * 
230    * response error: // FIXME: save error
231    * 
232    * <pre>
233    * {
234    *   "timestamp":"2018-08-03T07:45:43.436+0000",
235    *   "status":500,
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"
240    * }
241    * </pre>
242    * 
243    * @param account
244    * @return
245    */
246   @PostMapping(consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
247   @ResponseStatus(value = HttpStatus.OK)
248   @ResponseBody
249   public DefaultApiResponse<AccountCreateResponseData> create(
250       @RequestBody AccountCreateRequest createRequest) {
251     
252     // FIXME: 验证数据有效性
253     
254     Account account = createRequest.getEntity();
255     
256     if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
257       //account.setPassword(passwordEncoder.encode(account.getPassword()));
258     }
259
260     Account ret = accountService.insert(account);
261     
262     AccountCreateResponseData data = AccountCreateResponseData.build(ret);
263
264     return new DefaultApiResponse<AccountCreateResponseData>(data);
265   }
266
267   /**
268    * 
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"}'
271    * 
272    * response success:
273    * 
274    * <pre>
275    * {
276    *   "success":"info.update.success"
277    * }
278    * </pre>
279    * 
280    * response error 401:
281    * 
282    * <pre>
283    * {
284    *   "timestamp":"2018-08-03T08:48:25.777+0000",
285    *   "status":401,
286    *   "error":"Http Status 401",
287    *   "message":"Unauthorized",
288    *   "path":"/api/v1/admin/accounts"
289    * }
290    * </pre>
291    * 
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"}'
294    * 
295    * response error:
296    * 
297    * <pre>
298    * {
299    *   "timestamp":"2018-08-03T07:50:52.327+0000",
300    *   "status":500,
301    *   "error":"Internal Server Error",
302    *   "exception":"java.lang.RuntimeException",
303    *   "message":"exception.update.id.must.not.empty",
304    *   "path":"/api/v1/admin/accounts"
305    * }
306    * </pre>
307    * 
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"}'
310    * 
311    * response error:
312    * 
313    * <pre>
314    * {
315    *   "timestamp":"2018-08-03T07:48:24.774+0000",
316    *   "status":500,
317    *   "error":"Internal Server Error",
318    *   "exception":"java.lang.RuntimeException",
319    *   "message":"exception.update.domain.not.exist",
320    *   "path":"/api/v1/admin/accounts"
321    * }
322    * </pre>
323    * 
324    * @param account
325    * @return
326    */
327   @PutMapping(path = "/{id}", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
328   @ResponseStatus(value = HttpStatus.OK)
329   @ResponseBody
330   public DefaultApiResponse<AccountUpdateResponseData> update(
331       @PathVariable("id") String id, 
332       @RequestBody AccountUpdateRequest updateRequest) {
333
334     if (id == null || id.length() == 0) {
335       throw new RuntimeException("exception.update.id.must.not.empty");
336     }
337
338     Account tmp = accountService.selectById(id);
339     if (tmp == null) {
340       throw new RuntimeException("exception.update.domain.not.exist");
341     }
342
343     Account account = updateRequest.getEntity();
344     account.setId(id);
345
346     if (account.getPassword() !=null && account.getPassword().length() > 0 && !account.getPassword().startsWith("{")) {
347       //account.setPassword(passwordEncoder.encode(account.getPassword()));
348     }
349
350     account = EntityUtils.merge(tmp, account);
351
352     Account ret = accountService.update(account);
353
354     AccountUpdateResponseData data = AccountUpdateResponseData.build(ret);
355     
356     return new DefaultApiResponse<AccountUpdateResponseData>(data);
357   }
358   
359
360   /**
361    * 
362    * curl -i -s -X DELETE -H 'Accept:application/json' 'http://localhost:8081/api/v1/admin/accounts/1'
363    * 
364    * response success:
365    * 
366    * <pre>
367    * {
368    *   "success":"info.delete.success"
369    * }
370    * </pre>
371    * 
372    * response error 401:
373    * 
374    * <pre>
375    * {
376    *   "timestamp":"2018-08-03T08:48:25.777+0000",
377    *   "status":401,
378    *   "error":"Http Status 401",
379    *   "message":"Unauthorized",
380    *   "path":"/api/v1/admin/accounts/1"
381    * }
382    * </pre>
383    * 
384    * response error 500:
385    * 
386    * <pre>
387    * {
388    *   "timestamp":"2018-08-03T08:03:16.364+0000",
389    *   "status":500,
390    *   "error":"Internal Server Error",
391    *   "exception":"java.lang.RuntimeException",
392    *   "message":"exception.delete.domain.not.exist",
393    *   "path":"/api/v1/admin/accounts/1"
394    * }
395    * </pre>
396    * 
397    * @param id
398    * @return
399    */
400   @DeleteMapping(path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
401   @ResponseStatus(value = HttpStatus.OK)
402   @ResponseBody
403   public DefaultApiResponse<AccountRemoveResponseData> delete(
404       @PathVariable("id") String id) {
405
406     if (id == null || id.length() == 0) {
407       throw new RuntimeException("exception.delete.id.must.not.empty"); // FIXME: RestException
408     }
409
410     Account tmp = accountService.selectById(id);
411     if (tmp == null) {
412       throw new RuntimeException("exception.delete.domain.not.exist"); // FIXME: RestException
413     }
414
415     accountService.deleteById(id);
416
417     AccountRemoveResponseData data = AccountRemoveResponseData.build(tmp);
418     return new DefaultApiResponse<AccountRemoveResponseData>(data);
419   }
420   
421   @DeleteMapping(path = "/batch", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
422   @ResponseStatus(value = HttpStatus.OK)
423   @ResponseBody
424   public DefaultApiResponse<AccountDeleteBatchResponseData> deleteBatch(
425       @RequestBody AccountDeleteBatchRequest deleteBatchRequest) {
426     
427     System.out.println(deleteBatchRequest.getIds());
428     List<String> ids = deleteBatchRequest.getIds();
429     
430     accountService.deleteBatch(ids);
431     
432     AccountDeleteBatchResponseData data = AccountDeleteBatchResponseData.build(ids);
433     return new DefaultApiResponse<AccountDeleteBatchResponseData>(data);
434   }
435
436   /**
437    * 
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'
441    * 
442    * 
443    * 
444    * @param id
445    * @param pagerRequestModel
446    * @return
447    */
448   @RequestMapping(method = RequestMethod.GET, path = "/{id}/groups", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
449   @ResponseBody
450   public DefaultApiResponse<AccountRelatedGroupsResponseData> accountGroups(
451       @PathVariable("id") String id, 
452       AccountRelatedGroupsRequest request) {
453
454     if (id == null || id.length() == 0) {
455       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
456     }
457
458     Account account = accountService.selectById(id);
459
460     if (account == null) {
461       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
462     }
463
464     if (request.getMapBean() == null) {
465       request.setMapBean(new HashMap<String, Object>());
466     }
467     request.getMapBean().put("accountId", account.getId());
468
469     Page<AccountGroup> page = accountService.selectAccountGroups(request.getPageIndex(),
470         request.getPageSize(), request.getMapBean());
471
472     AccountRelatedGroupsResponseData data = AccountRelatedGroupsResponseData.of(request).build(page);
473
474     return new DefaultApiResponse<AccountRelatedGroupsResponseData>(data);
475   }
476
477   /**
478    * 
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"}]}'
481    * 
482    * 
483    * @param id
484    * @param groupAccounts
485    * @return
486    */
487   @RequestMapping(method = RequestMethod.POST, path = "/{id}/groups", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
488   @ResponseBody
489   public DefaultApiResponse<AccountRelateGroupsResponseData> relateGroups(
490       @PathVariable("id") String id, 
491       @RequestBody AccountRelateGroupsRequest accountGroups) {
492
493     if (id == null || id.length() == 0) {
494       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
495     }
496
497     Account tmp = accountService.selectById(id);
498
499     if (tmp == null) {
500       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
501     }
502
503     accountService.relateAccountGroups(tmp, accountGroups.getAccountGroups());
504
505     AccountRelateGroupsResponseData data = AccountRelateGroupsResponseData.of("info.relate.success");
506
507     return new DefaultApiResponse<AccountRelateGroupsResponseData>(data);
508   }
509
510   /**
511    * 
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'
515    * 
516    * 
517    * 
518    * @param id
519    * @param pagerRequestModel
520    * @return
521    */
522   @RequestMapping(method = RequestMethod.GET, path = "/{id}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
523   @ResponseBody
524   public DefaultApiResponse<AccountRelatedRolesResponseData> accountRoles(
525       @PathVariable("id") String id, 
526       AccountRelatedRolesRequest request) {
527
528     if (id == null || id.length() == 0) {
529       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
530     }
531
532     Account account = accountService.selectById(id);
533
534     if (account == null) {
535       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
536     }
537
538     if (request.getMapBean() == null) {
539       request.setMapBean(new HashMap<String, Object>());
540     }
541     request.getMapBean().put("accountId", account.getId());
542
543     Page<AccountRole> page = accountService.selectAccountRoles(request.getPageIndex(),
544         request.getPageSize(), request.getMapBean());
545
546     AccountRelatedRolesResponseData data = AccountRelatedRolesResponseData.of(request).build(page);
547
548     return new DefaultApiResponse<AccountRelatedRolesResponseData>(data);
549   }
550
551   /**
552    * 
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"}]}'
555    * 
556    * 
557    * @param id
558    * @param accountRoles
559    * @return
560    */
561   @RequestMapping(method = RequestMethod.POST, path = "/{id}/roles", consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
562   @ResponseBody
563   public DefaultApiResponse<AccountRelateRolesResponseData> relateRoles(
564       @PathVariable("id") String id, 
565       @RequestBody AccountRelateRolesRequest accountRoles) {
566
567     if (id == null || id.length() == 0) {
568       throw new RuntimeException("exception.get.id.must.not.empty"); // FIXME: RestException
569     }
570
571     Account account = accountService.selectById(id);
572
573     if (account == null) {
574       throw new RuntimeException("exception.get.domain.not.exist"); // FIXME: RestException
575     }
576
577     accountService.relateAccountRoles(account, accountRoles.getAccountRoles());
578
579     AccountRelateRolesResponseData data = AccountRelateRolesResponseData.of("info.relate.success");
580
581     return new DefaultApiResponse<AccountRelateRolesResponseData>(data);
582   }
583
584 }