LogController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.genersoft.iot.vmp.vmanager.log;
  2. import com.genersoft.iot.vmp.conf.UserSetup;
  3. import com.genersoft.iot.vmp.media.zlm.ZLMRunner;
  4. import com.genersoft.iot.vmp.service.ILogService;
  5. import com.genersoft.iot.vmp.storager.dao.dto.LogDto;
  6. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  7. import com.github.pagehelper.PageInfo;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiImplicitParam;
  10. import io.swagger.annotations.ApiImplicitParams;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.http.HttpStatus;
  16. import org.springframework.http.ResponseEntity;
  17. import org.springframework.util.StringUtils;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. @Api(tags = "日志管理")
  22. @CrossOrigin
  23. @RestController
  24. @RequestMapping("/api/log")
  25. public class LogController {
  26. private final static Logger logger = LoggerFactory.getLogger(LogController.class);
  27. @Autowired
  28. private ILogService logService;
  29. @Autowired
  30. private UserSetup userSetup;
  31. private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  32. /**
  33. * 分页查询日志
  34. *
  35. * @param query 查询内容
  36. * @param page 当前页
  37. * @param count 每页查询数量
  38. * @param type 类型
  39. * @param startTime 开始时间
  40. * @param endTime 结束时间
  41. * @return
  42. */
  43. @ApiOperation("分页查询报警")
  44. @GetMapping("/all")
  45. @ApiImplicitParams({
  46. @ApiImplicitParam(name="query", value = "查询内容", dataTypeClass = String.class),
  47. @ApiImplicitParam(name="page", value = "当前页", required = true ,dataTypeClass = Integer.class),
  48. @ApiImplicitParam(name="count", value = "每页查询数量", required = true ,dataTypeClass = Integer.class),
  49. @ApiImplicitParam(name="type", value = "类型" ,dataTypeClass = String.class),
  50. @ApiImplicitParam(name="startTime", value = "查询内容" ,dataTypeClass = String.class),
  51. @ApiImplicitParam(name="endTime", value = "查询内容" ,dataTypeClass = String.class),
  52. })
  53. public ResponseEntity<PageInfo<LogDto>> getAll(
  54. @RequestParam int page,
  55. @RequestParam int count,
  56. @RequestParam(required = false) String query,
  57. @RequestParam(required = false) String type,
  58. @RequestParam(required = false) String startTime,
  59. @RequestParam(required = false) String endTime
  60. ) {
  61. if (StringUtils.isEmpty(query)) query = null;
  62. if (StringUtils.isEmpty(startTime)) startTime = null;
  63. if (StringUtils.isEmpty(endTime)) endTime = null;
  64. if (!userSetup.getLogInDatebase()) {
  65. logger.warn("自动记录日志功能已关闭,查询结果可能不完整。");
  66. }
  67. try {
  68. if (startTime != null) format.parse(startTime);
  69. if (endTime != null) format.parse(endTime);
  70. } catch (ParseException e) {
  71. return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
  72. }
  73. PageInfo<LogDto> allLog = logService.getAll(page, count, query, type, startTime, endTime);
  74. return new ResponseEntity<>(allLog, HttpStatus.OK);
  75. }
  76. /**
  77. * 清空日志
  78. *
  79. */
  80. @ApiOperation("清空日志")
  81. @DeleteMapping("/clear")
  82. @ApiImplicitParams({})
  83. public ResponseEntity<WVPResult<String>> clear() {
  84. int count = logService.clear();
  85. WVPResult wvpResult = new WVPResult();
  86. wvpResult.setCode(0);
  87. wvpResult.setMsg("success");
  88. wvpResult.setData(count);
  89. return new ResponseEntity<WVPResult<String>>(wvpResult, HttpStatus.OK);
  90. }
  91. }