AlertDefinesController.java 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.usthe.alert.controller;
  2. import com.usthe.common.entity.alerter.AlertDefine;
  3. import com.usthe.alert.service.AlertDefineService;
  4. import com.usthe.common.entity.dto.Message;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import io.swagger.annotations.ApiParam;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.data.domain.PageRequest;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.data.jpa.domain.Specification;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import javax.persistence.criteria.CriteriaBuilder;
  20. import javax.persistence.criteria.Predicate;
  21. import java.util.ArrayList;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
  25. /**
  26. * 告警定义批量API
  27. * @author tom
  28. * @date 2021/12/9 10:32
  29. */
  30. @Api(tags = "Alert Define Batch API | 告警定义管理API")
  31. @RestController
  32. @RequestMapping(path = "/alert/defines", produces = {APPLICATION_JSON_VALUE})
  33. public class AlertDefinesController {
  34. @Autowired
  35. private AlertDefineService alertDefineService;
  36. @GetMapping
  37. @ApiOperation(value = "查询告警定义列表", notes = "根据查询过滤项获取告警定义信息列表")
  38. public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefines(
  39. @ApiParam(value = "告警定义ID", example = "6565463543") @RequestParam(required = false) List<Long> ids,
  40. @ApiParam(value = "告警定义级别", example = "6565463543") @RequestParam(required = false) Byte priority,
  41. @ApiParam(value = "排序字段,默认id", example = "id") @RequestParam(defaultValue = "id") String sort,
  42. @ApiParam(value = "排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") String order,
  43. @ApiParam(value = "列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
  44. @ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
  45. Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
  46. List<Predicate> andList = new ArrayList<>();
  47. if (ids != null && !ids.isEmpty()) {
  48. CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
  49. for (long id : ids) {
  50. inPredicate.value(id);
  51. }
  52. andList.add(inPredicate);
  53. }
  54. if (priority != null) {
  55. Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
  56. andList.add(predicate);
  57. }
  58. Predicate[] predicates = new Predicate[andList.size()];
  59. return criteriaBuilder.and(andList.toArray(predicates));
  60. };
  61. // 分页是必须的
  62. Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
  63. PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
  64. Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification,pageRequest);
  65. Message<Page<AlertDefine>> message = new Message<>(alertDefinePage);
  66. return ResponseEntity.ok(message);
  67. }
  68. @DeleteMapping
  69. @ApiOperation(value = "批量删除告警定义", notes = "根据告警定义ID列表批量删除告警定义")
  70. public ResponseEntity<Message<Void>> deleteAlertDefines(
  71. @ApiParam(value = "告警定义IDs", example = "6565463543") @RequestParam(required = false) List<Long> ids
  72. ) {
  73. if (ids != null && !ids.isEmpty()) {
  74. alertDefineService.deleteAlertDefines(new HashSet<>(ids));
  75. }
  76. Message<Void> message = new Message<>();
  77. return ResponseEntity.ok(message);
  78. }
  79. }