From 261a7d2645877369a8928d573e0fa0d978577ef3 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期三, 27 五月 2026 10:59:57 +0800
Subject: [PATCH] fix(ai): 优化财务AI助手的工作规则和数据分析功能

---
 src/main/java/com/ruoyi/ai/assistant/FinancialIntentExecutor.java |   76 +++++++++++++++++++++++--------------
 1 files changed, 47 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/ruoyi/ai/assistant/FinancialIntentExecutor.java b/src/main/java/com/ruoyi/ai/assistant/FinancialIntentExecutor.java
index 9afadff..f402fc2 100644
--- a/src/main/java/com/ruoyi/ai/assistant/FinancialIntentExecutor.java
+++ b/src/main/java/com/ruoyi/ai/assistant/FinancialIntentExecutor.java
@@ -14,9 +14,10 @@
 public class FinancialIntentExecutor {
 
     private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-    private static final Pattern LIMIT_PATTERN = Pattern.compile("(鍓峾鏈�杩�)?\\s*(\\d{1,2})\\s*鏉�");
+    private static final Pattern LIMIT_PATTERN = Pattern.compile("(?:鍓峾鏈�杩憒灞曠ず|杩斿洖)?\\s*(\\d{1,2})\\s*(?:鏉涓獆鍚�)");
     private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
-    private static final Pattern RELATIVE_DAY_PATTERN = Pattern.compile("(杩憒鏈�杩�)?\\s*(\\d{1,3})\\s*澶�");
+    private static final Pattern RELATIVE_DAY_PATTERN = Pattern.compile("(?:杩憒鏈�杩�)\\s*(\\d{1,3})\\s*澶�");
+    private static final Pattern FUTURE_MONTH_PATTERN = Pattern.compile("(?:鏈潵|鍚庣画|鎺ヤ笅鏉�)\\s*(\\d{1,2})\\s*(?:涓�)?鏈�");
 
     private final FinancialAgentTools financialAgentTools;
 
@@ -42,18 +43,18 @@
         String endDate = dateRange.endDate();
         String timeRange = dateRange.label();
 
-        if (containsAny(text, "鎴愭湰鏍哥畻", "浜у搧鎴愭湰", "宸ュ簭鎴愭湰", "浜哄伐鎴愭湰", "鎶樻棫", "鏉愭枡鎹熻��")) {
+        if (containsAny(text, "鎴愭湰鏍哥畻", "浜у搧鎴愭湰", "宸ュ簭鎴愭湰", "浜哄伐鎴愭湰", "鎶樻棫", "鏉愭枡鎹熻��", "鎴愭湰鏈�楂�")) {
             return financialAgentTools.calculateIntelligentCost(memoryId, startDate, endDate, timeRange, keyword, limit);
         }
-        if (containsAny(text, "鍒╂鼎鍒嗘瀽", "璁㈠崟鍒╂鼎", "浜忔崯璁㈠崟", "浣庡埄娑�",
-                "鏈�璧氶挶瀹㈡埛", "鍝釜瀹㈡埛鏈�璧氶挶", "瀹㈡埛鏈�璧氶挶", "鍒╂鼎鏈�楂樺鎴�", "鍒╂鼎璐$尞鏈�楂樺鎴�", "鍒╂鼎涓嬮檷")) {
+        if (containsAny(text, "鍒╂鼎鍒嗘瀽", "璁㈠崟鍒╂鼎", "浜忔崯璁㈠崟", "浣庡埄娑�", "鏈�璧氶挶瀹㈡埛", "鍝釜瀹㈡埛鏈�璧氶挶",
+                "瀹㈡埛鏈�璧氶挶", "鍒╂鼎鏈�楂樺鎴�", "鍒╂鼎璐$尞鏈�楂�", "鍒╂鼎涓嬮檷")) {
             return financialAgentTools.analyzeOrderProfit(memoryId, startDate, endDate, timeRange, keyword, limit);
         }
         if (containsAny(text, "搴撳瓨璧勯噾", "搴撳瓨绉帇", "鍛嗘粸搴撳瓨", "璧勯噾鍗犵敤", "鍛ㄨ浆鐜�", "搴撳瓨鍛ㄨ浆")) {
             return financialAgentTools.analyzeInventoryCapital(memoryId, startDate, endDate, timeRange, keyword, limit);
         }
         if (containsAny(text, "鐜伴噾娴�", "鍥炴椋庨櫓", "浠樻鍘嬪姏", "璧勯噾缂哄彛", "搴旀敹", "搴斾粯", "鍥炴棰勬祴")) {
-            return financialAgentTools.forecastCashFlow(memoryId, startDate, endDate, timeRange, limit);
+            return financialAgentTools.forecastCashFlow(memoryId, startDate, endDate, timeRange, extractForecastMonths(text));
         }
         if (containsAny(text, "寮傚父棰勮", "缁忚惀寮傚父", "椋庨櫓棰勮", "鎴愭湰寮傚父", "鍒╂鼎寮傚父", "鍥炴寮傚父", "璁㈠崟椋庨櫓")) {
             return financialAgentTools.detectBusinessAnomalies(memoryId, startDate, endDate, timeRange, limit);
@@ -73,30 +74,35 @@
 
     private String tryExecuteQuickPrompt(String memoryId, String text) {
         String normalized = normalizeForMatch(text);
+        if ("鐢熸垚鏈懆缁忚惀鍛ㄦ姤鍒╂鼎涓庣幇閲戞祦".equals(normalized) || "鐢熸垚鏈懆缁忚惀鍛ㄦ姤".equals(normalized) || "鐢熸垚鍛ㄦ姤".equals(normalized)) {
+            DateRange range = weekRange();
+            return financialAgentTools.generateOperationReport(memoryId, range.startDate(), range.endDate(), range.label(), "weekly");
+        }
+        if ("鍒嗘瀽鏈湀鍒╂鼎涓嬮檷鍘熷洜".equals(normalized)) {
+            DateRange range = monthRange();
+            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, null);
+        }
+        if ("杩�30澶╁摢涓鎴峰埄娑﹁础鐚渶楂�".equals(normalized)) {
+            DateRange range = recentDaysRange(30);
+            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, null);
+        }
         if ("鏌ョ湅鏈湀缁忚惀椹鹃┒鑸�".equals(normalized) || "鏌ョ湅缁忚惀椹鹃┒鑸�".equals(normalized)) {
             DateRange range = monthRange();
             return financialAgentTools.getBusinessCockpit(memoryId, range.startDate(), range.endDate(), range.label());
         }
-        if ("鏌ヨ杩�30澶╀簭鎹熻鍗�".equals(normalized) || "鍝釜璁㈠崟浜忔崯".equals(normalized)) {
+        if ("鏌ヨ杩�30澶╀簭鎹熻鍗�".equals(normalized)) {
             DateRange range = recentDaysRange(30);
-            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, 20);
+            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, null);
         }
-        if ("鐢熸垚鏈懆缁忚惀鍛ㄦ姤".equals(normalized) || "鐢熸垚鍛ㄦ姤".equals(normalized)) {
-            DateRange range = weekRange();
-            return financialAgentTools.generateOperationReport(memoryId, range.startDate(), range.endDate(), range.label(), "weekly");
+        if ("鍒嗘瀽杩�30澶╁簱瀛樿祫閲戝崰鐢�".equals(normalized)) {
+            DateRange range = recentDaysRange(30);
+            return financialAgentTools.analyzeInventoryCapital(memoryId, range.startDate(), range.endDate(), range.label(), null, null);
         }
-        if ("涓轰粈涔堝埄娑︿笅闄�".equals(normalized)) {
-            DateRange range = monthRange();
-            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, 20);
+        if ("棰勬祴鏈潵3涓湀鐜伴噾娴�".equals(normalized)) {
+            return financialAgentTools.forecastCashFlow(memoryId, null, null, null, 3);
         }
-        if ("鍝釜瀹㈡埛鏈�璧氶挶".equals(normalized)
-                || "鏈�杩戝摢涓鎴锋渶璧氶挶".equals(normalized)
-                || "鏈湀鍝釜瀹㈡埛鏈�璧氶挶".equals(normalized)
-                || "杩�30澶╁摢涓鎴锋渶璧氶挶".equals(normalized)
-                || "鍝釜瀹㈡埛鍒╂鼎鏈�楂�".equals(normalized)
-                || "鍝釜瀹㈡埛鍒╂鼎璐$尞鏈�楂�".equals(normalized)) {
-            DateRange range = extractDateRange(text);
-            return financialAgentTools.analyzeOrderProfit(memoryId, range.startDate(), range.endDate(), range.label(), null, 20);
+        if ("鍝釜宸ュ簭鎴愭湰鏈�楂�".equals(normalized)) {
+            return financialAgentTools.calculateIntelligentCost(memoryId, null, null, null, null, null);
         }
         return null;
     }
@@ -112,7 +118,12 @@
 
     private Integer extractLimit(String text) {
         Matcher matcher = LIMIT_PATTERN.matcher(text);
-        return matcher.find() ? Integer.parseInt(matcher.group(2)) : 10;
+        return matcher.find() ? Integer.parseInt(matcher.group(1)) : null;
+    }
+
+    private Integer extractForecastMonths(String text) {
+        Matcher matcher = FUTURE_MONTH_PATTERN.matcher(text);
+        return matcher.find() ? Integer.parseInt(matcher.group(1)) : null;
     }
 
     private DateRange extractDateRange(String text) {
@@ -128,7 +139,7 @@
         if (text.contains("涓婃湀")) {
             return lastMonthRange();
         }
-        if (text.contains("鏈勾") || text.contains("浠婂勾")) {
+        if (text.contains("浠婂勾") || text.contains("鏈勾")) {
             return yearRange();
         }
         if (text.contains("鏈懆")) {
@@ -136,17 +147,17 @@
         }
         Matcher relativeDayMatcher = RELATIVE_DAY_PATTERN.matcher(text);
         if (relativeDayMatcher.find()) {
-            int days = Integer.parseInt(relativeDayMatcher.group(2));
+            int days = Integer.parseInt(relativeDayMatcher.group(1));
             return recentDaysRange(days);
         }
-        return new DateRange(null, null, "杩�30澶�");
+        return new DateRange(null, null, null);
     }
 
     private DateRange buildDateRange(String start, String end, String label) {
         LocalDate startDate = parseDate(start);
         LocalDate endDate = parseDate(end);
         if (startDate == null || endDate == null) {
-            return new DateRange(null, null, "杩�30澶�");
+            return new DateRange(null, null, null);
         }
         if (startDate.isAfter(endDate)) {
             LocalDate temp = startDate;
@@ -200,7 +211,11 @@
         if (!StringUtils.hasText(text)) {
             return "";
         }
-        return text.replace("锛�", "")
+        return text.replace("锛�", "")
+                .replace("锛�", "")
+                .replace("(", "")
+                .replace(")", "")
+                .replace("锛�", "")
                 .replace(",", "")
                 .replace("銆�", "")
                 .replace(".", "")
@@ -218,13 +233,16 @@
 
     private String extractKeyword(String text) {
         String cleaned = text
+                .replaceAll("\\d{4}-\\d{2}-\\d{2}", "")
+                .replaceAll("(?:杩憒鏈�杩�)\\s*\\d{1,3}\\s*澶�", "")
+                .replaceAll("(?:鍓峾鏈�杩憒灞曠ず|杩斿洖)?\\s*\\d{1,2}\\s*(?:鏉涓獆鍚�)", "")
                 .replace("鏌ヨ", "")
                 .replace("鏌ョ湅", "")
                 .replace("鐪嬩笅", "")
                 .replace("鐪嬬湅", "")
                 .replace("甯垜", "")
                 .replace("璇�", "")
-                .replace("涓�涓�", "")
+                .replace("涓�涓�", "")
                 .replace("涓轰粈涔�", "")
                 .replace("鍝釜瀹㈡埛鏈�璧氶挶", "")
                 .replace("鏈�杩戝摢涓鎴锋渶璧氶挶", "")

--
Gitblit v1.9.3