2026-04-28 ebd79c8749eea0c4b3ca88b4eac0229cb11d1684
refactor(ai): 优化审批待办意图执行器的模式匹配逻辑

- 添加 RECENT_RANGE_PATTERN、HALF_RANGE_PATTERN 和 EXPLICIT_RANGE_PATTERN 静态常量用于时间范围解析
- 提取 isStatsIntent 方法,统一处理统计类意图识别逻辑
- 优化 extractValue 方法中的正则表达式,移除可选匹配符提高性能
- 使用预编译的静态常量替换动态创建的 Pattern 对象,提升时间范围检测效率
- 修改时间范围检测逻辑,使用统一的静态常量进行匹配判断
- 优化字段值提取中的正则表达式,确保数值匹配的准确性
已修改3个文件
34 ■■■■ 文件已修改
src/main/java/com/ruoyi/ai/assistant/ApproveTodoIntentExecutor.java 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application-new-pro.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/ai/assistant/ApproveTodoIntentExecutor.java
@@ -18,6 +18,9 @@
    private static final Pattern LIMIT_PATTERN = Pattern.compile("(前|最近)?(\\d{1,2})条");
    private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
    private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?)");
    private static final Pattern RECENT_RANGE_PATTERN = Pattern.compile("近\\d+(天|周|个月|月|年)");
    private static final Pattern HALF_RANGE_PATTERN = Pattern.compile("(最近|近)?半(个)?(月|年)");
    private static final Pattern EXPLICIT_RANGE_PATTERN = Pattern.compile(".*(到|至).*");
    private final ApproveTodoTools approveTodoTools;
@@ -33,7 +36,7 @@
        String text = message.trim();
        String approveId = extractApproveId(text);
        if (containsAny(text, "统计", "分析", "图表", "趋势", "占比")) {
        if (isStatsIntent(text)) {
            return approveTodoTools.getTodoStats(
                    memoryId,
                    extractStartDate(text),
@@ -99,6 +102,20 @@
                    extractLimit(text));
        }
        return null;
    }
    private boolean isStatsIntent(String text) {
        if (containsAny(text, "统计", "分析", "图表", "趋势", "占比", "汇总", "总量")) {
            return true;
        }
        boolean hasQueryWord = containsAny(text, "查询", "查看", "看下", "看看", "获取");
        boolean hasDataWord = containsAny(text, "数据", "报表", "情况", "数量", "金额");
        boolean hasTimeWord = containsAny(text, "今天", "昨日", "昨天", "本周", "上周", "本月", "上月", "本年", "今年", "去年")
                || DATE_PATTERN.matcher(text).find()
                || RECENT_RANGE_PATTERN.matcher(text).find()
                || HALF_RANGE_PATTERN.matcher(text).find()
                || EXPLICIT_RANGE_PATTERN.matcher(text).matches();
        return hasQueryWord && hasDataWord && hasTimeWord;
    }
    private boolean containsAny(String text, String... keywords) {
@@ -180,7 +197,7 @@
    }
    private String extractValue(String text, String fieldName) {
        Pattern pattern = Pattern.compile(fieldName + "(改为|修改为|是)?[::]?[\\s]*([^,,。;;\\s]+)");
        Pattern pattern = Pattern.compile(fieldName + "(改为|修改为|是)[::]?[\\s]*([^,,。;;\\s]+)");
        Matcher matcher = pattern.matcher(text);
        return matcher.find() ? matcher.group(2) : null;
    }
@@ -211,13 +228,13 @@
        if (containsAny(text, "今天", "昨日", "昨天", "本周", "上周", "本月", "上月", "本年", "今年", "去年")) {
            return text;
        }
        if (Pattern.compile("近\\d+(天|周|个月|月|年)").matcher(text).find()) {
        if (RECENT_RANGE_PATTERN.matcher(text).find()) {
            return text;
        }
        if (Pattern.compile("最近\\d+(天|周|个月|月|年)").matcher(text).find()) {
        if (HALF_RANGE_PATTERN.matcher(text).find()) {
            return text;
        }
        if (text.contains("到") || text.contains("至")) {
        if (EXPLICIT_RANGE_PATTERN.matcher(text).matches()) {
            return text;
        }
        return null;
@@ -227,7 +244,7 @@
        if (!text.contains(fieldName)) {
            return null;
        }
        Matcher matcher = Pattern.compile(fieldName + "(改为|修改为|是)?[::]?[\\s]*(\\d{1,2})").matcher(text);
        Matcher matcher = Pattern.compile(fieldName + "(改为|修改为|是)[::]?[\\s]*(\\d{1,2})").matcher(text);
        return matcher.find() ? Integer.parseInt(matcher.group(2)) : null;
    }
src/main/resources/application-new-pro.yml
@@ -202,7 +202,7 @@
  # 令牌自定义标识
  header: Authorization
  # 令牌密钥
  secret: abcdefghijklmnopqrstuvwxyz
  secret: xpAVjhCjQDaDB7mjPAzMDSbQWXNu2zYkTdDNUsPMS5Xx8QMmQVYN7n74eZrYJxDJ
  # 令牌有效期(默认30分钟)
  expireTime: 450
src/main/resources/application.yml
@@ -38,4 +38,5 @@
      model-name: "deepseek-r1:1.5b"
      log-requests: true
      log-responses: true
knowledge:
  one: D:\新疆大罗素企业产品体系说明文档.md