清除不必要的代码

This commit is contained in:
2026-03-24 19:57:03 +08:00
parent 37c5e9cbf8
commit fcddc064f6
2 changed files with 114 additions and 114 deletions

View File

@@ -1,112 +1,112 @@
package com.njcn.system.handler;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.github.tocrhz.mqtt.annotation.Payload;
import com.njcn.common.pojo.constant.LogInfo;
import com.njcn.common.pojo.constant.PatternRegex;
import com.njcn.common.pojo.dto.LogInfoDTO;
import com.njcn.common.utils.PubUtils;
import com.njcn.redis.pojo.enums.RedisKeyEnum;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.system.service.IUserLogService;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.po.User;
import com.njcn.web.utils.EmailUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author hongawen
* @version 1.0.0
* @date 2022年07月13日 13:35
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class MqttMessageHandler {
private final IUserLogService userLogService;
private final UserFeignClient userFeignClient;
private final EmailUtil emailUtil;
private final RedisUtil redisUtil;
private JSONObject jsonObject = null;
/**
* 订阅审计日志的记录,并进行入库操作
*/
// @MqttSubscribe(value = "/userLog")
public void subUserLog(String topic, MqttMessage message, @Payload String payload) {
if(Objects.isNull(jsonObject)){
jsonObject = (JSONObject) redisUtil.getObjectByKey(RedisKeyEnum.USER_ROLE_TYPE_KEY.getKey());
if(Objects.isNull(jsonObject)){
userFeignClient.userRoleList();
jsonObject = (JSONObject) redisUtil.getObjectByKey(RedisKeyEnum.USER_ROLE_TYPE_KEY.getKey());
}
}
LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
if(!logInfoDTO.getLoginName().equals(LogInfo.UNKNOWN_USER) && !logInfoDTO.getOperate().equals(LogInfo.UNKNOWN_OPERATE) && !logInfoDTO.getIp().equals(LogInfo.UNKNOWN_IP)){
if("注销".equals(logInfoDTO.getOperateType()) || "认证".equals(logInfoDTO.getOperateType())){
logInfoDTO.setType(1);
}else {
String loginName = logInfoDTO.getLoginName();
if(StrUtil.isNotBlank(loginName) && jsonObject.containsKey(loginName)){
if((Integer) jsonObject.get(loginName)==2){
logInfoDTO.setType(0);
}else {
logInfoDTO.setType(1);
};
}else {
logInfoDTO.setType(1);
}
}
userLogService.addUserLog(logInfoDTO);
}
}
/**
* 订阅审计日志的记录,并进行入库操作
*/
// @MqttSubscribe(value = "/userLogEmailPush")
public void subUserLogEmail(String topic, MqttMessage message, @Payload String payload) {
LogInfoDTO auditLogVO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
List<User> data = userFeignClient.getUserListByRoleCode("audit_manager").getData();
List<String> emails = data.stream()
.filter(x -> Pattern.matches(PatternRegex.EMAIL_REGEX,x.getEmail()))
.filter(x -> StrUtil.isNotBlank(x.getEmail()) && x.getEmailNotice() == 1)
.map(User::getEmail).collect(Collectors.toList());
if(CollUtil.isNotEmpty(emails)){
StringBuilder describe = new StringBuilder();
describe.append(auditLogVO.getType() == 0 ? "业务事件 -> " : "系统事件 -> ");
describe.append(auditLogVO.getUserName()).append("")
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(auditLogVO.getCreateTime()))
.append("")
.append(auditLogVO.getIp())
.append("执行了")
.append(auditLogVO.getOperate())
.append(",结果为");
if (auditLogVO.getResult() == 1) {
describe.append("成功");
}
if (auditLogVO.getResult() == 0) {
describe.append("失败").append(",失败原因为").append(auditLogVO.getFailReason());
}
emailUtil.sendMultiple(emails,"告警消息",describe.toString(),false);
}
}
}
//package com.njcn.system.handler;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONObject;
//import com.github.tocrhz.mqtt.annotation.Payload;
//import com.njcn.common.pojo.constant.LogInfo;
//import com.njcn.common.pojo.constant.PatternRegex;
//import com.njcn.common.pojo.dto.LogInfoDTO;
//import com.njcn.common.utils.PubUtils;
//import com.njcn.redis.pojo.enums.RedisKeyEnum;
//import com.njcn.redis.utils.RedisUtil;
//import com.njcn.system.service.IUserLogService;
//import com.njcn.user.api.UserFeignClient;
//import com.njcn.user.pojo.po.User;
//import com.njcn.web.utils.EmailUtil;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.eclipse.paho.client.mqttv3.MqttMessage;
//import org.springframework.stereotype.Component;
//
//import java.nio.charset.StandardCharsets;
//import java.time.format.DateTimeFormatter;
//import java.util.List;
//import java.util.Objects;
//import java.util.regex.Pattern;
//import java.util.stream.Collectors;
//
///**
// * @author hongawen
// * @version 1.0.0
// * @date 2022年07月13日 13:35
// */
//@Slf4j
//@Component
//@RequiredArgsConstructor
//public class MqttMessageHandler {
//
// private final IUserLogService userLogService;
// private final UserFeignClient userFeignClient;
// private final EmailUtil emailUtil;
//
// private final RedisUtil redisUtil;
//
//
//
//
// private JSONObject jsonObject = null;
// /**
// * 订阅审计日志的记录,并进行入库操作
// */
//// @MqttSubscribe(value = "/userLog")
// public void subUserLog(String topic, MqttMessage message, @Payload String payload) {
// if(Objects.isNull(jsonObject)){
// jsonObject = (JSONObject) redisUtil.getObjectByKey(RedisKeyEnum.USER_ROLE_TYPE_KEY.getKey());
// if(Objects.isNull(jsonObject)){
// userFeignClient.userRoleList();
// jsonObject = (JSONObject) redisUtil.getObjectByKey(RedisKeyEnum.USER_ROLE_TYPE_KEY.getKey());
// }
// }
//
// LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
// if(!logInfoDTO.getLoginName().equals(LogInfo.UNKNOWN_USER) && !logInfoDTO.getOperate().equals(LogInfo.UNKNOWN_OPERATE) && !logInfoDTO.getIp().equals(LogInfo.UNKNOWN_IP)){
// if("注销".equals(logInfoDTO.getOperateType()) || "认证".equals(logInfoDTO.getOperateType())){
// logInfoDTO.setType(1);
// }else {
// String loginName = logInfoDTO.getLoginName();
// if(StrUtil.isNotBlank(loginName) && jsonObject.containsKey(loginName)){
// if((Integer) jsonObject.get(loginName)==2){
// logInfoDTO.setType(0);
// }else {
// logInfoDTO.setType(1);
// };
// }else {
// logInfoDTO.setType(1);
// }
// }
// userLogService.addUserLog(logInfoDTO);
// }
// }
//
// /**
// * 订阅审计日志的记录,并进行入库操作
// */
//// @MqttSubscribe(value = "/userLogEmailPush")
// public void subUserLogEmail(String topic, MqttMessage message, @Payload String payload) {
// LogInfoDTO auditLogVO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
// List<User> data = userFeignClient.getUserListByRoleCode("audit_manager").getData();
// List<String> emails = data.stream()
// .filter(x -> Pattern.matches(PatternRegex.EMAIL_REGEX,x.getEmail()))
// .filter(x -> StrUtil.isNotBlank(x.getEmail()) && x.getEmailNotice() == 1)
// .map(User::getEmail).collect(Collectors.toList());
// if(CollUtil.isNotEmpty(emails)){
// StringBuilder describe = new StringBuilder();
// describe.append(auditLogVO.getType() == 0 ? "业务事件 -> " : "系统事件 -> ");
// describe.append(auditLogVO.getUserName()).append("在")
// .append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(auditLogVO.getCreateTime()))
// .append("在")
// .append(auditLogVO.getIp())
// .append("执行了")
// .append(auditLogVO.getOperate())
// .append(",结果为");
// if (auditLogVO.getResult() == 1) {
// describe.append("成功");
// }
// if (auditLogVO.getResult() == 0) {
// describe.append("失败").append(",失败原因为").append(auditLogVO.getFailReason());
// }
// emailUtil.sendMultiple(emails,"告警消息",describe.toString(),false);
// }
// }
//}