traffic-audit-server/src/main/java/com/trafficaudit/rulemanage/controller/RuleManageController.java
@@ -8,6 +8,8 @@
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.List;
/** 规则管理:按报表类型查看/启停/调整阈值 */
@@ -26,7 +28,9 @@
            qw.eq(AuditRule::getReportType, reportType.trim());
        }
        qw.orderByAsc(AuditRule::getSortOrder);
        return Result.ok(ruleMapper.selectList(qw));
        List<AuditRule> list = ruleMapper.selectList(qw);
        for (AuditRule r : list) syncThresholdText(r); // 名称/说明随阈值动态展示(如 ±10% → ±20%)
        return Result.ok(list);
    }
    /** 启停规则 */
@@ -47,11 +51,33 @@
        if (body.getId() == null) return Result.error("缺少规则ID");
        AuditRule rule = ruleMapper.selectById(body.getId());
        if (rule == null) return Result.error("规则不存在");
        String oldThreshold = rule.getThreshold();
        if (body.getThreshold() != null) rule.setThreshold(body.getThreshold());
        if (body.getAlertLevel() != null) rule.setAlertLevel(body.getAlertLevel());
        if (body.getDescription() != null) rule.setDescription(body.getDescription());
        // 阈值变化时同步规则名称与说明(名称/说明里的 ±N% 按新阈值改写),并随保存持久化
        if (body.getThreshold() != null && !body.getThreshold().equals(oldThreshold)) {
            syncThresholdText(rule);
        }
        rule.setUpdatedAt(LocalDateTime.now());
        ruleMapper.updateById(rule);
        return Result.ok("已保存");
    }
}
    /** ±N% 字样匹配(名称/说明中随阈值动态变化的片段) */
    private static final Pattern PCT_TOKEN = Pattern.compile("±\\s*\\d+(?:\\.\\d+)?\\s*%");
    /** 把文本中的 ±N% 替换为 ±threshold%(含已带 ± 的前缀,避免双 ±) */
    private String decorateThresholdText(String text, String threshold) {
        if (text == null || text.trim().isEmpty() || threshold == null || threshold.trim().isEmpty()) return text;
        String token = "±" + threshold.trim() + "%";
        return PCT_TOKEN.matcher(text).replaceAll(Matcher.quoteReplacement(token));
    }
    /** 名称/说明随阈值同步(读列表时也动态校正,DB 中旧文本不阻塞显示) */
    private void syncThresholdText(AuditRule rule) {
        if (rule == null || rule.getThreshold() == null) return;
        rule.setRuleName(decorateThresholdText(rule.getRuleName(), rule.getThreshold()));
        rule.setDescription(decorateThresholdText(rule.getDescription(), rule.getThreshold()));
    }
}