소스 검색

更新!

TRX 1 년 전
부모
커밋
48da7f09a2

+ 41 - 56
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/operLogs/OperationLogsModel.java

@@ -1,9 +1,7 @@
 package com.zhongshu.iot.client.model.operLogs;
 
 import com.github.microservice.models.baseParam.SuperModel;
-import com.zhongshu.iot.client.type.MessageType;
-import com.zhongshu.iot.client.type.OperationLogType;
-import com.zhongshu.iot.client.type.ResponseCode;
+import com.zhongshu.iot.client.type.OperationLogBusType;
 import com.zhongshu.iot.client.type.type.LoginType;
 import com.zhongshu.iot.client.type.type.LogsLevel;
 import io.swagger.v3.oas.annotations.media.Schema;
@@ -16,6 +14,34 @@ import lombok.Data;
 @Data
 public class OperationLogsModel extends SuperModel {
 
+    @Schema(description = "日志管理的业务类型")
+    private OperationLogBusType operationLogBusType = OperationLogBusType.Common;
+
+    private String operationLogBusTypeStr;
+
+    public String getOperationLogBusTypeStr() {
+        if (operationLogBusType != null) {
+            return operationLogBusType.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "日志级别,重要程度")
+    private LogsLevel level;
+
+    private String levelStr;
+
+    public String getLevelStr() {
+        if (level != null) {
+            return level.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "日志操作端类型")
+    private LoginType loginType;
+
+    // ------------------用户信息 start --------------------
     @Schema(description = "用户ID")
     private String uid;
 
@@ -25,72 +51,31 @@ public class OperationLogsModel extends SuperModel {
     @Schema(description = "用户名称")
     private String userName;
 
-    @Schema(description = "类型")
-    private OperationLogType operationLogType;
-
-    private String operationLogTypeStr;
-
-    public String getOperationLogTypeStr() {
-        if (operationLogType != null) {
-            return operationLogType.getRemark();
-        }
-        return "";
-    }
+    @Schema(description = "")
+    private String phone;
 
+    //----------------------请求地址信息 start-------------------
     @Schema(description = "IP地址")
     private String ip;
 
     @Schema(description = "浏览器")
     private String ua;
 
-    @Schema(description = "日志操作端类型")
-    private LoginType loginType;
+    @Schema(description = "请求地址")
+    private String url;
 
-    private String loginTypeStr;
-
-    public String getLoginTypeStr() {
-        if (loginType != null) {
-            return loginType.getRemark();
-        }
-        return "";
-    }
+    //----------------------日志内容 start-------------------
 
     @Schema(description = "日志内容")
     private String content;
 
-    @Schema(description = "日志级别,重要程度")
-    private LogsLevel level;
-
-    private String levelStr;
-
-    public String getLevelStr() {
-        if (level != null) {
-            return level.getRemark();
-        }
-        return "";
-    }
-
-    @Schema(description = "消息类型")
-    private MessageType messageType;
+    @Schema(description = "时间")
+    private String time;
 
-    private String messageTypeStr;
+    @Schema(description = "管理数据ID")
+    private String dataId;
 
-    public String getMessageTypeStr() {
-        if (messageType != null) {
-            return messageType.getRemark();
-        }
-        return "";
-    }
-
-    @Schema(description = "响应姿态")
-    private ResponseCode responseCode;
-
-    private String responseCodeStr;
+    @Schema(description = "关联保存关联信息")
+    private Object backObj;
 
-    public String getResponseCodeStr() {
-        if (responseCode != null) {
-            return responseCode.getRemark();
-        }
-        return "";
-    }
 }

+ 21 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/type/OperationLogBusType.java

@@ -0,0 +1,21 @@
+package com.zhongshu.iot.client.type;
+
+import lombok.Getter;
+
+/**
+ * 操作日志关联的 业务
+ */
+public enum OperationLogBusType {
+    Device("设备管理"),
+    IotTemple("产品管理"),
+    IotThing("物模型相关"),
+    Common("通用"),
+    ;
+
+    @Getter
+    private String remark;
+
+    OperationLogBusType(String remark) {
+        this.remark = remark;
+    }
+}

+ 14 - 10
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/device/OperationLogs.java

@@ -1,13 +1,14 @@
 package com.zhongshu.iot.server.core.domain.iot.device;
 
-import com.zhongshu.iot.client.type.MessageType;
-import com.zhongshu.iot.client.type.OperationLogType;
-import com.zhongshu.iot.client.type.ResponseCode;
+import com.zhongshu.iot.client.type.OperationLogBusType;
 import com.zhongshu.iot.client.type.type.LoginType;
 import com.zhongshu.iot.client.type.type.LogsLevel;
 import com.zhongshu.iot.server.core.domain.base.SuperEntity;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
+import org.springframework.data.mongodb.core.index.Indexed;
+
+import java.util.Date;
 
 /**
  * @author TRX
@@ -16,18 +17,23 @@ import lombok.Data;
 @Data
 public class OperationLogs extends SuperEntity {
 
+    @Schema(description = "日志管理的业务类型")
+    private OperationLogBusType operationLogBusType = OperationLogBusType.Common;
+
+    // ------------------用户信息 start --------------------
     @Schema(description = "用户ID")
     private String uid;
 
-    @Schema(description = "类型")
-    private OperationLogType operationLogType = OperationLogType.Common;
-
     @Schema(description = "登录名")
     private String loginName;
 
     @Schema(description = "用户名称")
     private String userName;
 
+    @Schema(description = "")
+    private String phone;
+
+    //----------------------请求地址信息 start-------------------
     @Schema(description = "IP地址")
     private String ip;
 
@@ -55,9 +61,7 @@ public class OperationLogs extends SuperEntity {
     @Schema(description = "关联保存关联信息")
     private Object backObj;
 
-    @Schema(description = "消息类型")
-    private MessageType messageType;
+    @Indexed(expireAfterSeconds = 0)
+    private Date TTL;
 
-    @Schema(description = "响应姿态")
-    private ResponseCode responseCode;
 }

+ 4 - 4
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceInfoService.java

@@ -16,7 +16,7 @@ import com.zhongshu.iot.client.model.mqtt.DeviceInfoSearchParam;
 import com.zhongshu.iot.client.model.mqtt.DeviceInfoUpdateRemark;
 import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoModel;
 import com.zhongshu.iot.client.type.IotDataType;
-import com.zhongshu.iot.client.type.type.LogsLevel;
+import com.zhongshu.iot.client.type.OperationLogBusType;
 import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
 import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
 import com.zhongshu.iot.server.core.dao.mqtt.GateWay2DeviceDao;
@@ -148,7 +148,7 @@ public class DeviceInfoService {
             iotThingService.initAutoThingByDevice(finalDeviceInfo);
 
             // 日志
-            operationLogsService.addLogs(String.format("添加了设备;%s", finalDeviceInfo.getDeviceName()), LogsLevel.Middle, finalDeviceInfo);
+            operationLogsService.addLogs(String.format("添加了设备;%s", finalDeviceInfo.getDeviceName()), OperationLogBusType.Device, finalDeviceInfo);
 
             // 更新账号的绑定设备数量
             gateWayUserInfoService.updateBindNumber(finalDeviceInfo.getMqttUserName());
@@ -349,7 +349,7 @@ public class DeviceInfoService {
         deviceInfo.setIsDelete(Boolean.TRUE);
         deviceInfoDao.save(deviceInfo);
 
-        operationLogsService.addLogs(String.format("删除了设备;%s", deviceInfo.getDeviceName()), LogsLevel.High, deviceInfo);
+        operationLogsService.addLogs(String.format("删除了设备;%s", deviceInfo.getDeviceName()), OperationLogBusType.Device, deviceInfo);
 
         // 更新设备信息
         deviceSyncFullCardService.noticeSyncDevice(List.of(deviceInfo));
@@ -394,7 +394,7 @@ public class DeviceInfoService {
         // 更新设备权限
         jmxSyncService.syncDevicesSecurity(List.of(deviceInfo));
 
-        operationLogsService.addLogs(String.format("注销了设备;%s", deviceInfo.getDeviceName()), LogsLevel.High, deviceInfo);
+        operationLogsService.addLogs(String.format("注销了设备;%s", deviceInfo.getDeviceName()), OperationLogBusType.Device, deviceInfo);
         // 回收权限
 
         // 设备信息改变时,更新 对应的物模型数据

+ 34 - 55
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/user/OperationLogsService.java

@@ -1,9 +1,11 @@
 package com.zhongshu.iot.server.core.service.user;
 
+import com.github.microservice.dataConfig.IotTtlConfig;
+import com.github.microservice.net.ResultContent;
 import com.zhongshu.card.client.model.org.UserCountModel;
 import com.zhongshu.iot.client.model.operLogs.OperationLogsModel;
 import com.zhongshu.iot.client.model.operLogs.OperationLogsSearchParam;
-import com.zhongshu.iot.client.type.OperationLogType;
+import com.zhongshu.iot.client.type.OperationLogBusType;
 import com.zhongshu.iot.client.type.type.LogsLevel;
 import com.zhongshu.iot.server.core.dao.UserDao;
 import com.zhongshu.iot.server.core.dao.mqtt.OperationLogsDao;
@@ -14,7 +16,6 @@ import com.zhongshu.iot.server.core.util.DateUtils;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
 import com.zhongshu.iot.server.core.util.net.IPUtil;
 import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
-import com.github.microservice.net.ResultContent;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
@@ -23,6 +24,8 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
+
 /**
  * @author TRX
  * @date 2024/5/24
@@ -38,77 +41,53 @@ public class OperationLogsService extends SuperService {
     private UserDao userDao;
 
     @Autowired
-    HttpServletRequest request;
+    private HttpServletRequest request;
 
     public ResultContent addLogs(String content, LogsLevel level) {
-        OperationLogs logs = new OperationLogs();
-        logs.setContent(content);
-        if (level == null) {
-            level = LogsLevel.Low;
-        }
-        logs.setLevel(level);
-        UserCountModel user = getCrrentUser();
-        if (ObjectUtils.isNotEmpty(user)) {
-            logs.setUid(user.getId());
-            logs.setUserName(user.getName());
-            logs.setLoginName(user.getLoginName());
-        }
-        logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-        try {
-            logs.setUa(request.getHeader("User-Agent"));
-            logs.setIp(IPUtil.getRemoteIp(request));
-            logs.setUrl(request.getRequestURI());
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        operationLogsDao.save(logs);
-        return ResultContent.buildSuccess();
+        return addLogs(content, level, null, null);
     }
 
-    public ResultContent addLogs(String content, LogsLevel level, SuperEntity entity) {
-        OperationLogs logs = new OperationLogs();
-        logs.setContent(content);
-        if (level == null) {
-            level = LogsLevel.Low;
-        }
-        logs.setLevel(level);
-        UserCountModel user = getCrrentUser();
-        if (ObjectUtils.isNotEmpty(user)) {
-            logs.setUid(user.getId());
-            logs.setUserName(user.getName());
-            logs.setLoginName(user.getLoginName());
-        }
-        logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-        if (ObjectUtils.isNotEmpty(entity)) {
-            logs.setDataId(entity.getId());
-            logs.setBackObj(entity);
-        }
-        try {
-            logs.setUa(request.getHeader("User-Agent"));
-            logs.setIp(IPUtil.getRemoteIp(request));
-            logs.setUrl(request.getRequestURI());
-        } catch (Exception e) {
-            log.error("addLogs: {}", e.getMessage());
-        }
-        operationLogsDao.save(logs);
-        return ResultContent.buildSuccess();
+    public ResultContent addLogs(String content, OperationLogBusType operationLogBusType) {
+        return addLogs(content, null, operationLogBusType, null);
+    }
+
+    public ResultContent addLogs(String content, OperationLogBusType operationLogBusType, SuperEntity entity) {
+        return addLogs(content, null, operationLogBusType, entity);
     }
 
-    public ResultContent addLogs(String content, LogsLevel level, OperationLogType operationLogType, SuperEntity entity) {
+    /**
+     * 操作日志
+     * @param content 内容:如:删除了用户
+     * @param level   日志级别 如:高、中、低
+     * @param operationLogBusType 关联的业务类型
+     * @param entity  对应实体
+     * @return
+     */
+    public ResultContent addLogs(String content, LogsLevel level, OperationLogBusType operationLogBusType, SuperEntity entity) {
         OperationLogs logs = new OperationLogs();
+        logs.setTTL(new Date(System.currentTimeMillis() + IotTtlConfig.IOT_OPERATION_LOGS_TTL));
+
         logs.setContent(content);
-        logs.setOperationLogType(operationLogType);
         if (level == null) {
             level = LogsLevel.Low;
         }
         logs.setLevel(level);
+        if (operationLogBusType == null) {
+            operationLogBusType = OperationLogBusType.Common;
+        }
+        logs.setOperationLogBusType(operationLogBusType);
+
+        // 操作的用户信息
         UserCountModel user = getCrrentUser();
         if (ObjectUtils.isNotEmpty(user)) {
             logs.setUid(user.getId());
             logs.setUserName(user.getName());
             logs.setLoginName(user.getLoginName());
+            logs.setPhone(user.getPhone());
         }
         logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+
+        // 数据对象
         if (ObjectUtils.isNotEmpty(entity)) {
             logs.setDataId(entity.getId());
             logs.setBackObj(entity);
@@ -118,7 +97,7 @@ public class OperationLogsService extends SuperService {
             logs.setIp(IPUtil.getRemoteIp(request));
             logs.setUrl(request.getRequestURI());
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("addLogs: {}", e.getMessage());
         }
         operationLogsDao.save(logs);
         return ResultContent.buildSuccess();