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