From 0054466fd909d1f1100e39fd34d78143cccfba24 Mon Sep 17 00:00:00 2001 From: huangzj <826100833@qq.com> Date: Tue, 8 Aug 2023 09:51:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../njcn/common/pojo/dto/DeviceLogDTO.java | 43 +++++++++++++++++++ .../java/com/njcn/web/advice/DeviceLog.java | 23 ++++++++++ .../njcn/web/service/impl/LogServiceImpl.java | 10 +++++ .../system/controller/UserLogController.java | 12 +++++- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/DeviceLogDTO.java create mode 100644 pqs-common/common-web/src/main/java/com/njcn/web/advice/DeviceLog.java diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/DeviceLogDTO.java b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/DeviceLogDTO.java new file mode 100644 index 000000000..01be13e51 --- /dev/null +++ b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/DeviceLogDTO.java @@ -0,0 +1,43 @@ +package com.njcn.common.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Description: + * Date: 2023/8/7 19:09【需求编号】 + * + * @author clam + * @version V1.0.0 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class DeviceLogDTO { + + private String userName; + + /** + * 操作 + */ + private String operate; + + /** + * 结果(0:失败 1:成功) + */ + private Integer result; + + /** + * 失败原因 + */ + private String failReason; + + /** + * 登录名称 + */ + private String loginName; + + private String userIndex; + +} diff --git a/pqs-common/common-web/src/main/java/com/njcn/web/advice/DeviceLog.java b/pqs-common/common-web/src/main/java/com/njcn/web/advice/DeviceLog.java new file mode 100644 index 000000000..7e785ef14 --- /dev/null +++ b/pqs-common/common-web/src/main/java/com/njcn/web/advice/DeviceLog.java @@ -0,0 +1,23 @@ +package com.njcn.web.advice; + +import org.springframework.core.annotation.Order; + +import java.lang.annotation.*; + +/** + * Description: + * Date: 2023/8/7 18:47【需求编号】 + * + * @author clam + * @version V1.0.0 + */ + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +@Documented + +public @interface DeviceLog { + + + String operateType() default ""; +} diff --git a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java index f9ccfa4c4..35cffb9d4 100644 --- a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java +++ b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java @@ -2,12 +2,15 @@ package com.njcn.web.service.impl; import com.github.tocrhz.mqtt.publisher.MqttPublisher; import com.njcn.common.config.GeneralInfo; +import com.njcn.common.pojo.annotation.OperateInfo; import com.njcn.common.pojo.constant.LogInfo; +import com.njcn.common.pojo.dto.DeviceLogDTO; import com.njcn.common.pojo.dto.LogInfoDTO; import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.utils.PubUtils; import com.njcn.common.utils.ReflectCommonUtil; +import com.njcn.web.advice.DeviceLog; import com.njcn.web.service.ILogService; import com.njcn.web.utils.RequestUtil; import lombok.AllArgsConstructor; @@ -77,6 +80,13 @@ public class LogServiceImpl implements ILogService { String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod()); LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "", levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); + //如果存在设备日志注解,则记录设备日志 + if(Objects.nonNull((returnType.getMethod()))&& (returnType.getMethod()).isAnnotationPresent(DeviceLog.class)){ + String deviceOperate = returnType.getMethod().getAnnotation(DeviceLog.class).operateType(); + DeviceLogDTO deviceLogDTO = new DeviceLogDTO(userName,deviceOperate,result.equalsIgnoreCase("失败") ? 0 : 1,"",loginName,userIndex); + publisher.send("/deviceLog", PubUtils.obj2json(deviceLogDTO), 2, false); + + } } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java index a4e3b4c27..d80a2a3ba 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java @@ -3,6 +3,7 @@ package com.njcn.system.controller; import com.njcn.common.pojo.annotation.OperateInfo; import com.njcn.common.pojo.constant.OperateType; +import com.njcn.common.pojo.constant.ServerInfo; import com.njcn.common.pojo.dto.LogInfoDTO; import com.njcn.common.pojo.enums.common.LogEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum; @@ -24,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.stream.Stream; /** @@ -59,10 +61,16 @@ public class UserLogController extends BaseController { @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.QUERY) @PostMapping("/query") - @ApiOperation("查询审计日志") + @ApiOperation("查询审计日志(治理模块)") public HttpResult> queryUserLog() { String methodDescribe = getMethodDescribe("queryUserLog"); - List list = userLogService.lambdaQuery().eq(UserLog::getLoginName, RequestUtil.getUsername()).orderByDesc(UserLog::getCreateTime).last("limit 100").list(); + List list = userLogService.lambdaQuery().eq(UserLog::getLoginName, RequestUtil.getUsername()). + in(UserLog::getServiceName, Stream.of(ServerInfo.CS_DEVICE_BOOT, + ServerInfo.CS_HARMONIC_BOOT, + ServerInfo.CS_REPORT_BOOT, + ServerInfo.CS_EVENT_BOOT, + ServerInfo.CS_WARN_BOOT, + ServerInfo.CS_SYSTEM_BOOT)).orderByDesc(UserLog::getCreateTime).last("limit 100").list(); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe); } }