|
|
|
@ -6,12 +6,14 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; |
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
|
|
|
import cn.iocoder.yudao.module.alert.controller.admin.instant.vo.InstantSaveReqVO; |
|
|
|
import cn.iocoder.yudao.module.alert.controller.admin.warn.vo.WarnPageReqVO; |
|
|
|
import cn.iocoder.yudao.module.alert.controller.admin.warn.vo.WarnRespVO; |
|
|
|
import cn.iocoder.yudao.module.alert.controller.admin.warn.vo.WarnSaveReqVO; |
|
|
|
import cn.iocoder.yudao.module.alert.dal.dataobject.instant.InstantDO; |
|
|
|
import cn.iocoder.yudao.module.alert.dal.dataobject.warn.WarnDO; |
|
|
|
import cn.iocoder.yudao.module.alert.dal.dataobject.warn.WarnTableDO; |
|
|
|
import cn.iocoder.yudao.module.alert.dal.mysql.warn.WarnMapper; |
|
|
|
import cn.iocoder.yudao.module.alert.dal.mysql.warn.WarnTableMapper; |
|
|
|
import cn.iocoder.yudao.module.alert.utils.EXAUtils; |
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqVO; |
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; |
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
|
|
|
@ -22,13 +24,23 @@ import com.mzt.logapi.service.impl.DiffParseFunction; |
|
|
|
import com.mzt.logapi.starter.annotation.LogRecord; |
|
|
|
import jakarta.annotation.Resource; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.cache.annotation.CacheEvict; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
import java.util.List; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WARN_NOT_EXISTS; |
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
|
|
|
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*; |
|
|
|
|
|
|
|
@Service |
|
|
|
@ -38,18 +50,127 @@ public class WarnServiceImpl implements WarnService { |
|
|
|
private WarnMapper warnMapper; |
|
|
|
@Resource |
|
|
|
private WarnTableMapper warnTableMapper; |
|
|
|
|
|
|
|
@Value("${EXA.ip}") |
|
|
|
private String EXA_IP; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageResult<WarnDO> getWarnPage(WarnPageReqVO reqVO) { |
|
|
|
return warnMapper.selectPage(reqVO); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageResult<WarnRespVO> getWarnPageReal(WarnPageReqVO reqVO) { |
|
|
|
PageResult<WarnDO> warnPage = warnMapper.selectPageReal(reqVO); |
|
|
|
EXAUtils exaUtils = new EXAUtils(); |
|
|
|
// 2. 循环处理每个预警,读取实时值并转换为WarnRespVO
|
|
|
|
List<WarnRespVO> respVOList = warnPage.getList().stream().map(warnDO -> { |
|
|
|
// 2.1 基础属性拷贝
|
|
|
|
WarnRespVO respVO = BeanUtils.toBean(warnDO, WarnRespVO.class); |
|
|
|
|
|
|
|
// 2.2 读取pointId和outpointPoint的实时值(假设实时服务方法为getRealTimeValue)
|
|
|
|
List<String> pointValue = null; |
|
|
|
List<String> outpointValue = null; |
|
|
|
try { |
|
|
|
|
|
|
|
pointValue = exaUtils.getNowData(EXA_IP, warnDO.getPointId()); |
|
|
|
outpointValue = exaUtils.getNowData(EXA_IP, warnDO.getPointId()); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
throw exception(EXA_REAL_FAILED, "EXA实时值读取失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
// 2.3 实时值到value字段
|
|
|
|
respVO.setPointValue(Double.parseDouble(pointValue.get(0))); |
|
|
|
respVO.setOutputPointValue(Double.parseDouble(outpointValue.get(0))); |
|
|
|
//偏差值
|
|
|
|
//偏差值=输入值-重构值
|
|
|
|
double point = Double.parseDouble(pointValue.get(0)); |
|
|
|
double outpoint = Double.parseDouble(outpointValue.get(0)); |
|
|
|
|
|
|
|
respVO.setBiasValue(point - outpoint); |
|
|
|
//2.4处理uplimit、lowlimit
|
|
|
|
if (warnDO.getUplimit() != null) { |
|
|
|
String originalUplimit = warnDO.getUplimit(); |
|
|
|
// 检查是否包含[]格式的点号
|
|
|
|
if (originalUplimit.contains("[") && originalUplimit.contains("]")) { |
|
|
|
// 使用正则提取[]内的点号(例如:[point123] -> point123)
|
|
|
|
Pattern pattern = Pattern.compile("\\[(.*?)\\]"); |
|
|
|
Matcher matcher = pattern.matcher(originalUplimit); |
|
|
|
if (matcher.find()) { |
|
|
|
String innerPoint = matcher.group(1); // 获取括号内的点号
|
|
|
|
// 读取内点的实时值
|
|
|
|
List<String> innerPointValue = null; |
|
|
|
try { |
|
|
|
innerPointValue = exaUtils.getNowData(EXA_IP, innerPoint); |
|
|
|
} catch (Exception e) { |
|
|
|
throw exception(EXA_REAL_FAILED, "EXA实时值读取失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
String realValue = innerPointValue.get(0); // 取第一个值
|
|
|
|
// 替换[]内的点号为实时值(例如:"温度>[temp_point]" -> "温度>25.5")
|
|
|
|
String processedUplimit = originalUplimit.replace("[" + innerPoint + "]", realValue); |
|
|
|
respVO.setUplimit(processedUplimit); |
|
|
|
} else { |
|
|
|
// 未匹配到有效格式,保留原始值
|
|
|
|
respVO.setUplimit(originalUplimit); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 不包含[],直接使用原始值
|
|
|
|
respVO.setUplimit(originalUplimit); |
|
|
|
} |
|
|
|
} |
|
|
|
if (warnDO.getLowlimit() != null) { |
|
|
|
String originalLowlimit = warnDO.getLowlimit(); |
|
|
|
// 检查是否包含[]格式的点号
|
|
|
|
if (originalLowlimit.contains("[") && originalLowlimit.contains("]")) { |
|
|
|
// 使用正则提取[]内的点号(例如:[point123] -> point123)
|
|
|
|
Pattern pattern = Pattern.compile("\\[(.*?)\\]"); |
|
|
|
Matcher matcher = pattern.matcher(originalLowlimit); |
|
|
|
if (matcher.find()) { |
|
|
|
String innerPoint = matcher.group(1); // 获取括号内的点号
|
|
|
|
// 读取内点的实时值
|
|
|
|
List<String> innerPointValue = null; |
|
|
|
try { |
|
|
|
innerPointValue = exaUtils.getNowData(EXA_IP, innerPoint); |
|
|
|
} catch (Exception e) { |
|
|
|
throw exception(EXA_REAL_FAILED, "EXA实时值读取失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
String realValue = innerPointValue.get(0); // 取第一个值
|
|
|
|
// 替换[]内的点号为实时值(例如:"温度>[temp_point]" -> "温度>25.5")
|
|
|
|
String processedLowlimit = originalLowlimit.replace("[" + innerPoint + "]", realValue); |
|
|
|
respVO.setLowlimit(processedLowlimit); |
|
|
|
} else { |
|
|
|
// 未匹配到有效格式,保留原始值
|
|
|
|
respVO.setLowlimit(originalLowlimit); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 不包含[],直接使用原始值
|
|
|
|
respVO.setLowlimit(originalLowlimit); |
|
|
|
} |
|
|
|
} |
|
|
|
//计算当前时间与create_time的时间差(单位:秒)
|
|
|
|
long totalSeconds = ChronoUnit.SECONDS.between(warnDO.getCreateTime(), LocalDateTime.now()); |
|
|
|
respVO.setTimeDiff(totalSeconds); |
|
|
|
// 转换为天、时、分、秒
|
|
|
|
long days = totalSeconds / 86400; // 1天 = 86400秒
|
|
|
|
long remainingSeconds = totalSeconds % 86400; |
|
|
|
long hours = remainingSeconds / 3600; // 1小时 = 3600秒
|
|
|
|
remainingSeconds %= 3600; |
|
|
|
long minutes = remainingSeconds / 60; // 1分钟 = 60秒
|
|
|
|
long seconds = remainingSeconds % 60; |
|
|
|
// 格式化时间差字符串(例如:1天2小时3分4秒)
|
|
|
|
String timeDiffStr = String.format("%d天%d小时%d分%d秒", days, hours, minutes, seconds); |
|
|
|
respVO.setTimeDiffStr(timeDiffStr); |
|
|
|
return respVO; |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
// 3. 构建并返回包含实时值的分页结果
|
|
|
|
return new PageResult<>(respVOList, warnPage.getTotal()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@LogRecord(type = SYSTEM_WARN_TYPE, subType = SYSTEM_WARN_CREATE_SUB_TYPE, bizNo = "{{#warn.id}}", |
|
|
|
success = SYSTEM_WARN_CREATE_SUCCESS) |
|
|
|
@LogRecord(type = SYSTEM_WARN_TYPE, subType = SYSTEM_WARN_CREATE_SUB_TYPE, bizNo = "{{#warn.id}}", success = SYSTEM_WARN_CREATE_SUCCESS) |
|
|
|
public Long createWarn(WarnSaveReqVO createReqVO) { |
|
|
|
// 1.1 校验账户配合
|
|
|
|
// tenantService.handleTenantInfo(tenant -> {
|
|
|
|
@ -72,16 +193,14 @@ public class WarnServiceImpl implements WarnService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public WarnDO getWarn(Long id) { |
|
|
|
return warnMapper.selectById(id); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@CacheEvict(value = RedisKeyConstants.WARN, key = "#updateReqVO.warnId") |
|
|
|
@LogRecord(type = SYSTEM_WARN_TYPE, subType = SYSTEM_WARN_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}", |
|
|
|
success = SYSTEM_WARN_UPDATE_SUCCESS) |
|
|
|
@LogRecord(type = SYSTEM_WARN_TYPE, subType = SYSTEM_WARN_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}", success = SYSTEM_WARN_UPDATE_SUCCESS) |
|
|
|
public void updateWarn(WarnSaveReqVO updateReqVO) { |
|
|
|
//为简单--省去检验步骤,之后根据情况增加
|
|
|
|
// 1.1 校验是否可以更新
|
|
|
|
|