Browse Source

聊天接口支持外部账号自动创建并修复会话按账号隔离

master
pyx 13 hours ago
parent
commit
3e128ff5ff
  1. 13
      src/main/java/com/wok/supportbot/controller/ConversationController.java
  2. 31
      src/main/java/com/wok/supportbot/service/ConversationService.java

13
src/main/java/com/wok/supportbot/controller/ConversationController.java

@ -37,7 +37,7 @@ public class ConversationController {
@RequestParam(required = false) String accountId, @RequestParam(required = false) String accountId,
@RequestParam(required = false) Long roleId) { @RequestParam(required = false) Long roleId) {
try { try {
Map<String, Object> result = conversationService.listConversations(page, size, keyword, toLong(accountId), roleId);
Map<String, Object> result = conversationService.listConversations(page, size, keyword, accountId, roleId);
Map<String, Object> data = new java.util.HashMap<>(); Map<String, Object> data = new java.util.HashMap<>();
data.put("success", true); data.put("success", true);
data.put("data", result.get("records")); 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;
}
}
} }

31
src/main/java/com/wok/supportbot/service/ConversationService.java

@ -38,7 +38,7 @@ public class ConversationService {
return listConversations(page, size, keyword, null, null); return listConversations(page, size, keyword, null, null);
} }
public Map<String, Object> listConversations(int page, int size, String keyword, Long accountId, Long roleId) {
public Map<String, Object> listConversations(int page, int size, String keyword, String accountId, Long roleId) {
// 构建基础 SQL 条件 // 构建基础 SQL 条件
StringBuilder whereClause = new StringBuilder("WHERE cm1.is_delete = false "); StringBuilder whereClause = new StringBuilder("WHERE cm1.is_delete = false ");
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
@ -58,9 +58,17 @@ public class ConversationService {
params.add(like); params.add(like);
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) { if (roleId != null && roleId > 0) {
whereClause.append(" AND cs.role_id = ? "); whereClause.append(" AND cs.role_id = ? ");
@ -147,8 +155,8 @@ public class ConversationService {
INSERT INTO conversation_session (conversation_id, account_id, role_id) INSERT INTO conversation_session (conversation_id, account_id, role_id)
VALUES (?, ?, ?) VALUES (?, ?, ?)
ON CONFLICT (conversation_id) 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 update_time = CURRENT_TIMESTAMP
""", conversationId, normalizedAccountId, normalizedRoleId); """, conversationId, normalizedAccountId, normalizedRoleId);
} }
@ -389,4 +397,15 @@ public class ConversationService {
return stats; 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;
}
}
} }
Loading…
Cancel
Save