diff --git a/os-ems/src/main/java/com/os/ems/base/service/impl/EmsAlarmActionStepServiceImpl.java b/os-ems/src/main/java/com/os/ems/base/service/impl/EmsAlarmActionStepServiceImpl.java index 5319dba..982c7ff 100644 --- a/os-ems/src/main/java/com/os/ems/base/service/impl/EmsAlarmActionStepServiceImpl.java +++ b/os-ems/src/main/java/com/os/ems/base/service/impl/EmsAlarmActionStepServiceImpl.java @@ -1,10 +1,15 @@ package com.os.ems.base.service.impl; 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.StringUtils; import com.os.common.utils.uuid.IdUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.os.ems.base.mapper.EmsAlarmActionStepMapper; @@ -28,6 +33,12 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService @Autowired private EmsAlarmActionStepImageMapper emsAlarmActionStepImageMapper; + /** + * 文件上传路径配置 + */ + private final String uploadPath = RuoYiConfig.getUploadPath(); + + /** * 查询报警规则具体措施步骤 * @@ -143,7 +154,7 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService } try { - // 1. 先删除该规则下的所有现有步骤和图片 + // 1. 先删除该规则下的所有现有步骤和图片(包括物理文件) List existingSteps = emsAlarmActionStepMapper.selectEmsAlarmActionStepByRuleObjId(ruleObjId); for (EmsAlarmActionStep existingStep : existingSteps) { // 删除步骤关联的图片 @@ -151,7 +162,12 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService imageQuery.setActionStepObjId(existingStep.getObjId()); List existingImages = emsAlarmActionStepImageMapper.selectEmsAlarmActionStepImageList(imageQuery); for (EmsAlarmActionStepImage image : existingImages) { - emsAlarmActionStepImageMapper.deleteEmsAlarmActionStepImageByObjId(image.getObjId()); + try { + deleteImageFile(image.getImageUrl()); + emsAlarmActionStepImageMapper.deleteEmsAlarmActionStepImageByObjId(image.getObjId()); + } catch (Exception e) { + throw new RuntimeException("文件删除失败,导致事务回滚: " + e.getMessage(), e); + } } // 删除步骤 emsAlarmActionStepMapper.deleteEmsAlarmActionStepByObjId(existingStep.getObjId()); @@ -188,4 +204,49 @@ public class EmsAlarmActionStepServiceImpl implements IEmsAlarmActionStepService 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()); + } + } } diff --git a/os-ems/src/main/resources/mapper/ems/base/EmsAlarmActionStepMapper.xml b/os-ems/src/main/resources/mapper/ems/base/EmsAlarmActionStepMapper.xml index 7537747..45dcb1a 100644 --- a/os-ems/src/main/resources/mapper/ems/base/EmsAlarmActionStepMapper.xml +++ b/os-ems/src/main/resources/mapper/ems/base/EmsAlarmActionStepMapper.xml @@ -97,7 +97,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" FROM ems_alarm_action_step step 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 - 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