package com.zksy.api.service; import com.zksy.api.mapper.AutoWarningClosureMapper; import com.zksy.api.domain.EarlyWarning; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.UUID; /** 状态和解除轨迹同事务提交;只关闭未发布的自动草稿,不覆盖人工流程。 */ @Service public class AutoWarningClosureService { @Autowired private AutoWarningClosureMapper mapper; @Transactional(rollbackFor = Exception.class) public void close(String deviceCode, String marker, String reason) { EarlyWarning warning = mapper.lockDraft(deviceCode, marker); if (warning == null) return; LocalDateTime now = LocalDateTime.now(); if (mapper.closeDraft(warning.getWarningId(), now) != 1) throw new IllegalStateException("自动预警状态更新失败"); if (mapper.insertClosure(UUID.randomUUID().toString().replace("-", ""), warning.getWarningId(), reason == null || reason.trim().isEmpty() ? "监测值已恢复,系统自动解除" : reason, now) != 1) throw new IllegalStateException("自动解除处置记录保存失败"); } }