diff --git a/src/main/java/com/wok/supportbot/controller/ConversationController.java b/src/main/java/com/wok/supportbot/controller/ConversationController.java index 75254d0..df23bc6 100644 --- a/src/main/java/com/wok/supportbot/controller/ConversationController.java +++ b/src/main/java/com/wok/supportbot/controller/ConversationController.java @@ -37,7 +37,7 @@ public class ConversationController { @RequestParam(required = false) String accountId, @RequestParam(required = false) Long roleId) { try { - Map result = conversationService.listConversations(page, size, keyword, toLong(accountId), roleId); + Map result = conversationService.listConversations(page, size, keyword, accountId, roleId); Map data = new java.util.HashMap<>(); data.put("success", true); data.put("data", result.get("records")); @@ -202,15 +202,4 @@ public class ConversationController { } } - /** 将字符串 accountId 安全转为 Long,非数字字符串返回 null */ - private static Long toLong(String value) { - if (value == null || value.isBlank()) { - return null; - } - try { - return Long.valueOf(value.trim()); - } catch (NumberFormatException e) { - return null; - } - } } diff --git a/src/main/java/com/wok/supportbot/service/ConversationService.java b/src/main/java/com/wok/supportbot/service/ConversationService.java index 356d0fd..cfee7da 100644 --- a/src/main/java/com/wok/supportbot/service/ConversationService.java +++ b/src/main/java/com/wok/supportbot/service/ConversationService.java @@ -38,7 +38,7 @@ public class ConversationService { return listConversations(page, size, keyword, null, null); } - public Map listConversations(int page, int size, String keyword, Long accountId, Long roleId) { + public Map listConversations(int page, int size, String keyword, String accountId, Long roleId) { // 构建基础 SQL 条件 StringBuilder whereClause = new StringBuilder("WHERE cm1.is_delete = false "); List params = new ArrayList<>(); @@ -58,9 +58,17 @@ public class ConversationService { params.add(like); params.add(like); } - if (accountId != null && accountId > 0) { - whereClause.append(" AND cs.account_id = ? "); - params.add(accountId); + if (accountId != null && !accountId.isBlank()) { + String normalizedAccountId = accountId.trim(); + Long numericAccountId = toLong(normalizedAccountId); + if (numericAccountId != null && numericAccountId > 0) { + whereClause.append(" AND (cs.account_id = ? OR ca.account_key = ?) "); + params.add(numericAccountId); + params.add(normalizedAccountId); + } else { + whereClause.append(" AND ca.account_key = ? "); + params.add(normalizedAccountId); + } } if (roleId != null && roleId > 0) { whereClause.append(" AND cs.role_id = ? "); @@ -147,8 +155,8 @@ public class ConversationService { INSERT INTO conversation_session (conversation_id, account_id, role_id) VALUES (?, ?, ?) ON CONFLICT (conversation_id) - DO UPDATE SET account_id = EXCLUDED.account_id, - role_id = EXCLUDED.role_id, + DO UPDATE SET account_id = COALESCE(EXCLUDED.account_id, conversation_session.account_id), + role_id = COALESCE(EXCLUDED.role_id, conversation_session.role_id), update_time = CURRENT_TIMESTAMP """, conversationId, normalizedAccountId, normalizedRoleId); } @@ -389,4 +397,15 @@ public class ConversationService { return stats; } + + private static Long toLong(String value) { + if (value == null || value.isBlank()) { + return null; + } + try { + return Long.valueOf(value.trim()); + } catch (NumberFormatException e) { + return null; + } + } }