AlertDefinesController.java 4.0 KB

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