OperationLogsService.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.zswl.dataservice.service.mqtt;
  2. import com.zswl.dataservice.dao.UserDao;
  3. import com.zswl.dataservice.dao.mqtt.OperationLogsDao;
  4. import com.zswl.dataservice.domain.base.SuperEntity;
  5. import com.zswl.dataservice.domain.user.User;
  6. import com.zswl.dataservice.domain.mqtt.OperationLogs;
  7. import com.zswl.dataservice.model.operLogs.OperationLogsModel;
  8. import com.zswl.dataservice.model.operLogs.OperationLogsSearchParam;
  9. import com.zswl.dataservice.service.base.SuperService;
  10. import com.zswl.dataservice.utils.DateUtils;
  11. import com.zswl.dataservice.utils.bean.BeanUtils;
  12. import com.zswl.dataservice.utils.mqtt.type.LogsLevel;
  13. import com.zswl.dataservice.utils.page.PageEntityUtil;
  14. import com.zswl.dataservice.utils.result.ResultContent;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.ObjectUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.data.domain.Page;
  19. import org.springframework.data.domain.Pageable;
  20. import org.springframework.stereotype.Service;
  21. /**
  22. * @author TRX
  23. * @date 2024/5/24
  24. */
  25. @Slf4j
  26. @Service
  27. public class OperationLogsService extends SuperService {
  28. @Autowired
  29. OperationLogsDao operationLogsDao;
  30. @Autowired
  31. UserDao userDao;
  32. public ResultContent addLogs(String content, LogsLevel level) {
  33. OperationLogs logs = new OperationLogs();
  34. logs.setContent(content);
  35. if(level == null) {
  36. level = LogsLevel.Low;
  37. }
  38. logs.setLevel(level);
  39. User user = getCrrentUser();
  40. if (ObjectUtils.isNotEmpty(user)) {
  41. logs.setUid(user.getId());
  42. logs.setUserName(user.getUserName());
  43. logs.setLoginName(user.getLoginName());
  44. }
  45. logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
  46. operationLogsDao.save(logs);
  47. return ResultContent.buildSuccess();
  48. }
  49. public ResultContent addLogs(String content, LogsLevel level, SuperEntity entity) {
  50. OperationLogs logs = new OperationLogs();
  51. logs.setContent(content);
  52. if(level == null) {
  53. level = LogsLevel.Low;
  54. }
  55. logs.setLevel(level);
  56. User user = getCrrentUser();
  57. if (ObjectUtils.isNotEmpty(user)) {
  58. logs.setUid(user.getId());
  59. logs.setUserName(user.getUserName());
  60. logs.setLoginName(user.getLoginName());
  61. }
  62. logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
  63. if (ObjectUtils.isNotEmpty(entity)) {
  64. logs.setDataId(entity.getId());
  65. logs.setBackObj(entity);
  66. }
  67. operationLogsDao.save(logs);
  68. return ResultContent.buildSuccess();
  69. }
  70. /**
  71. * 日志列表
  72. *
  73. * @param pageable
  74. * @param param
  75. * @return
  76. */
  77. public ResultContent<Page<OperationLogsModel>> pageLogs(Pageable pageable, OperationLogsSearchParam param) {
  78. Page<OperationLogs> page = operationLogsDao.page(pageable, param);
  79. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  80. }
  81. public OperationLogsModel toModel(OperationLogs entity) {
  82. OperationLogsModel model = new OperationLogsModel();
  83. if (ObjectUtils.isNotEmpty(entity)) {
  84. BeanUtils.copyProperties(entity, model);
  85. }
  86. return model;
  87. }
  88. }