feat(ems): 优化报警规则查询逻辑并添加图片文件删除功能

- 更新 EmsAlarmActionStepMapper.xml 中的 SQL 查询,支持基于 monitor_field 字段的匹配- 在 EmsAlarmActionStepServiceImpl 中添加 deleteImageFile 方法,实现图片文件的物理删除
- 修改删除规则步骤逻辑,确保相关图片文件也被正确删除
boardTest
zch 4 weeks ago
parent 3ced9d3f2a
commit fcee8e3347

@ -1,10 +1,15 @@
package com.os.ems.base.service.impl; package com.os.ems.base.service.impl;
import java.util.List; import java.util.List;
import java.io.File;
import java.net.URLDecoder;
import com.os.common.config.RuoYiConfig;
import com.os.common.utils.DateUtils; import com.os.common.utils.DateUtils;
import com.os.common.utils.StringUtils; import com.os.common.utils.StringUtils;
import com.os.common.utils.uuid.IdUtils; import com.os.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.os.ems.base.mapper.EmsAlarmActionStepMapper; import com.os.ems.base.mapper.EmsAlarmActionStepMapper;
@ -28,6 +33,12 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService
@Autowired @Autowired
private EmsAlarmActionStepImageMapper emsAlarmActionStepImageMapper; private EmsAlarmActionStepImageMapper emsAlarmActionStepImageMapper;
/**
*
*/
private final String uploadPath = RuoYiConfig.getUploadPath();
/** /**
* *
* *
@ -143,7 +154,7 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService
} }
try { try {
// 1. 先删除该规则下的所有现有步骤和图片 // 1. 先删除该规则下的所有现有步骤和图片(包括物理文件)
List<EmsAlarmActionStep> existingSteps = emsAlarmActionStepMapper.selectEmsAlarmActionStepByRuleObjId(ruleObjId); List<EmsAlarmActionStep> existingSteps = emsAlarmActionStepMapper.selectEmsAlarmActionStepByRuleObjId(ruleObjId);
for (EmsAlarmActionStep existingStep : existingSteps) { for (EmsAlarmActionStep existingStep : existingSteps) {
// 删除步骤关联的图片 // 删除步骤关联的图片
@ -151,7 +162,12 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService
imageQuery.setActionStepObjId(existingStep.getObjId()); imageQuery.setActionStepObjId(existingStep.getObjId());
List<EmsAlarmActionStepImage> existingImages = emsAlarmActionStepImageMapper.selectEmsAlarmActionStepImageList(imageQuery); List<EmsAlarmActionStepImage> existingImages = emsAlarmActionStepImageMapper.selectEmsAlarmActionStepImageList(imageQuery);
for (EmsAlarmActionStepImage image : existingImages) { for (EmsAlarmActionStepImage image : existingImages) {
try {
deleteImageFile(image.getImageUrl());
emsAlarmActionStepImageMapper.deleteEmsAlarmActionStepImageByObjId(image.getObjId()); emsAlarmActionStepImageMapper.deleteEmsAlarmActionStepImageByObjId(image.getObjId());
} catch (Exception e) {
throw new RuntimeException("文件删除失败,导致事务回滚: " + e.getMessage(), e);
}
} }
// 删除步骤 // 删除步骤
emsAlarmActionStepMapper.deleteEmsAlarmActionStepByObjId(existingStep.getObjId()); emsAlarmActionStepMapper.deleteEmsAlarmActionStepByObjId(existingStep.getObjId());
@ -188,4 +204,49 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService
throw new RuntimeException("批量保存措施步骤失败: " + e.getMessage(), e); throw new RuntimeException("批量保存措施步骤失败: " + e.getMessage(), e);
} }
} }
/**
*
*
* @param imageUrl URL
*/
private void deleteImageFile(String imageUrl) {
if (StringUtils.isEmpty(imageUrl) || StringUtils.isEmpty(uploadPath)) {
return;
}
try {
// 解析图片URL获取相对路径
// 例如:/profile/upload/2024/05/29/xxx.jpg -> upload/2024/05/29/xxx.jpg
String relativePath = null;
if (imageUrl.startsWith("/profile/")) {
relativePath = imageUrl.substring("/profile/".length());
} else if (imageUrl.contains("/upload/")) {
int uploadIndex = imageUrl.indexOf("/upload/");
relativePath = imageUrl.substring(uploadIndex + 1); // 去掉前面的"/"
}
if (StringUtils.isNotEmpty(relativePath)) {
// URL解码处理中文文件名
relativePath = URLDecoder.decode(relativePath, "UTF-8");
// 构建完整的文件路径
String fullPath = uploadPath + File.separator + relativePath.replace("/", File.separator);
File file = new File(fullPath);
if (file.exists() && file.isFile()) {
boolean deleted = file.delete();
if (deleted) {
System.out.println("成功删除图片文件: " + fullPath);
} else {
System.err.println("删除图片文件失败: " + fullPath);
}
} else {
System.out.println("图片文件不存在,无需删除: " + fullPath);
}
}
} catch (Exception e) {
System.err.println("删除图片文件时发生异常: " + imageUrl + ", 错误信息: " + e.getMessage());
}
}
} }

@ -97,7 +97,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
FROM ems_alarm_action_step step FROM ems_alarm_action_step step
LEFT JOIN ems_alarm_action_step_image img ON step.obj_id = img.action_step_obj_id LEFT JOIN ems_alarm_action_step_image img ON step.obj_id = img.action_step_obj_id
INNER JOIN ems_record_alarm_rule rule ON step.rule_obj_id = rule.obj_id INNER JOIN ems_record_alarm_rule rule ON step.rule_obj_id = rule.obj_id
WHERE rule.monitor_id = #{monitorId} AND rule.cause = #{cause} WHERE rule.monitor_id = #{monitorId}
AND (
-- 新版本规则基于monitor_field字段匹配
(rule.monitor_field IS NOT NULL AND
CASE rule.monitor_field
WHEN 0 THEN #{cause} = '温度'
WHEN 1 THEN #{cause} = '湿度'
WHEN 2 THEN #{cause} = '振动-速度(mm/s)'
WHEN 3 THEN #{cause} = '振动-位移(um)'
WHEN 4 THEN #{cause} = '振动-加速度(g)'
WHEN 5 THEN #{cause} = '振动-温度(℃)'
ELSE 0
END = 1
)
OR
-- 历史版本规则monitor_field为空匹配所有字段类型
(rule.monitor_field IS NULL)
)
ORDER BY step.step_sequence ASC, img.image_sequence ASC ORDER BY step.step_sequence ASC, img.image_sequence ASC
</select> </select>

Loading…
Cancel
Save