TRX 1 yıl önce
ebeveyn
işleme
9c67f15c29

+ 48 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/operLogs/OperationLogsAddParam.java

@@ -1,12 +1,60 @@
 package com.zhongshu.card.client.model.operLogs;
 
+import com.github.microservice.models.type.LogServiceType;
+import com.zhongshu.card.client.type.LoginType;
+import com.zhongshu.card.client.type.LogsLevel;
+import com.zhongshu.card.client.type.MessageType;
+import com.zhongshu.card.client.type.ResponseCode;
+import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
+import java.util.Date;
+
 /**
+ * 操作日志
+ *
  * @author TRX
  * @date 2024/5/29
  */
 @Data
 public class OperationLogsAddParam {
+    @Schema(description = "用户ID")
+    private String uid;
+
+    @Schema(description = "IP地址")
+    private String ip;
+
+    @Schema(description = "浏览器")
+    private String ua;
+
+    @Schema(description = "api地址")
+    private String url;
+
+    @Schema(description = "日志来自那个服务")
+    private LogServiceType logServiceType;
+
+    @Schema(description = "日志操作端类型")
+    private LoginType loginType = LoginType.Web;
+
+    @Schema(description = "标题")
+    private String title;
+
+    @Schema(description = "日志内容")
+    private String content;
+
+    @Schema(description = "日志级别,重要程度")
+    private LogsLevel level;
+
+    @Schema(description = "管理数据ID")
+    private String dataId;
+
+    @Schema(description = "关联保存关联信息")
+    private Object backObj;
+
+    @Schema(description = "消息类型")
+    private MessageType messageType = MessageType.Info;
+
+    @Schema(description = "响应姿态")
+    private ResponseCode responseCode;
 
 }

+ 19 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/service/feign/OperationLogsFeignService.java

@@ -0,0 +1,19 @@
+package com.zhongshu.card.client.service.feign;
+
+import com.github.microservice.net.ResultContent;
+import com.zhongshu.card.client.model.operLogs.OperationLogsAddParam;
+import io.swagger.v3.oas.annotations.Operation;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+
+@FeignClient(name = "fullcardserver/operationLogs/manager")
+public interface OperationLogsFeignService {
+
+    @Operation(summary = "添加操作日志", description = "添加操作日志")
+    @RequestMapping(value = "addLog", method = {RequestMethod.POST})
+    ResultContent addLog(@RequestBody OperationLogsAddParam param);
+
+}

+ 11 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/user/OperationLogsController.java

@@ -2,9 +2,12 @@ package com.zhongshu.card.server.core.controller.user;
 
 import com.github.microservice.auth.security.annotations.ResourceAuth;
 import com.github.microservice.auth.security.type.AuthType;
+import com.zhongshu.card.client.model.operLogs.OperationLogsAddParam;
 import com.zhongshu.card.client.model.operLogs.OperationLogsModel;
 import com.zhongshu.card.client.model.operLogs.OperationLogsSearchParam;
 import com.github.microservice.net.ResultContent;
+import com.zhongshu.card.client.model.org.OrgUserDetailParam;
+import com.zhongshu.card.client.model.org.OrganizationUserModel;
 import com.zhongshu.card.server.core.service.user.OperationLogsService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
@@ -14,6 +17,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
@@ -40,4 +44,11 @@ public class OperationLogsController {
             @Parameter(required = false) OperationLogsSearchParam param) {
         return operationLogsService.pageLogs(pageable, param);
     }
+
+    @Operation(summary = "添加日志", description = "添加日志")
+    @RequestMapping(value = "manager/addLog", method = {RequestMethod.POST})
+    public ResultContent<OrganizationUserModel> addLog(@RequestBody OperationLogsAddParam param) {
+        return operationLogsService.addLog(param);
+    }
+
 }

+ 17 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/operlog/OperationLogs.java

@@ -1,5 +1,6 @@
 package com.zhongshu.card.server.core.domain.operlog;
 
+import com.github.microservice.models.type.LogServiceType;
 import com.zhongshu.card.client.type.LoginType;
 import com.zhongshu.card.client.type.LogsLevel;
 import com.zhongshu.card.client.type.MessageType;
@@ -7,6 +8,9 @@ import com.zhongshu.card.client.type.ResponseCode;
 import com.zhongshu.card.server.core.domain.base.SuperMain;
 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
@@ -27,12 +31,21 @@ public class OperationLogs extends SuperMain {
     @Schema(description = "IP地址")
     private String ip;
 
+    @Schema(description = "日志来自那个服务")
+    private LogServiceType logServiceType;
+
+    @Schema(description = "api地址")
+    private String url;
+
     @Schema(description = "浏览器")
     private String ua;
 
     @Schema(description = "日志操作端类型")
     private LoginType loginType;
 
+    @Schema(description = "标题")
+    private String title;
+
     @Schema(description = "日志内容")
     private String content;
 
@@ -53,4 +66,8 @@ public class OperationLogs extends SuperMain {
 
     @Schema(description = "响应姿态")
     private ResponseCode responseCode;
+
+    @Schema(description = "过期时间")
+    @Indexed(expireAfterSeconds = 0)
+    private Date ttl;
 }

+ 28 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/user/OperationLogsService.java

@@ -2,6 +2,7 @@ package com.zhongshu.card.server.core.service.user;
 
 import com.github.microservice.components.data.base.util.PageEntityUtil;
 import com.github.microservice.components.data.mongo.mongo.domain.SuperEntity;
+import com.zhongshu.card.client.model.operLogs.OperationLogsAddParam;
 import com.zhongshu.card.client.model.operLogs.OperationLogsModel;
 import com.zhongshu.card.client.model.operLogs.OperationLogsSearchParam;
 import com.github.microservice.net.ResultContent;
@@ -9,16 +10,21 @@ import com.zhongshu.card.client.type.LogsLevel;
 import com.zhongshu.card.server.core.dao.org.UserCountDao;
 import com.zhongshu.card.server.core.dao.user.OperationLogsDao;
 import com.zhongshu.card.server.core.domain.operlog.OperationLogs;
+import com.zhongshu.card.server.core.domain.org.UserAccount;
 import com.zhongshu.card.server.core.service.base.SuperService;
 import com.zhongshu.card.server.core.util.BeanUtils;
 import com.zhongshu.card.client.utils.DateUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
+
 /**
  * @author TRX
  * @date 2024/5/24
@@ -32,6 +38,26 @@ public class OperationLogsService extends SuperService {
     @Autowired
     UserCountDao userDao;
 
+    // 保存60天
+    private static Long ttlTime = 60 * 24 * 60 * 60 * 1000L;
+
+    @Async
+    public ResultContent addLog(OperationLogsAddParam param) {
+        OperationLogs logs = new OperationLogs();
+        BeanUtils.copyProperties(param, logs);
+        logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+        logs.setTtl(new Date(System.currentTimeMillis() + ttlTime));
+        if (StringUtils.isNotEmpty(param.getUid())) {
+            UserAccount userAccount = userDao.findTopByUserId(param.getUid());
+            if (ObjectUtils.isNotEmpty(userAccount)) {
+                logs.setUserName(userAccount.getName());
+                logs.setLoginName(userAccount.getLoginName());
+            }
+        }
+        operationLogsDao.save(logs);
+        return ResultContent.buildSuccess();
+    }
+
     public ResultContent addLogs(String content, LogsLevel level) {
         OperationLogs logs = new OperationLogs();
         initEntityNoCheckOid(logs);
@@ -41,6 +67,7 @@ public class OperationLogsService extends SuperService {
         }
         logs.setLevel(level);
         logs.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+        logs.setTtl(new Date(System.currentTimeMillis() + ttlTime));
         operationLogsDao.save(logs);
         return ResultContent.buildSuccess();
     }
@@ -58,6 +85,7 @@ public class OperationLogsService extends SuperService {
             logs.setDataId(entity.getId());
             logs.setBackObj(entity);
         }
+        logs.setTtl(new Date(System.currentTimeMillis() + ttlTime));
         operationLogsDao.save(logs);
         return ResultContent.buildSuccess();
     }