LogController.java 3.7 KB

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