fix: 列表查询移除隐性写操作

This commit is contained in:
2026-07-26 11:50:53 +08:00
parent 06605bfd0d
commit c0df76566d
@@ -791,21 +791,24 @@ public class EpicScriptServiceImpl extends ServiceImpl<EpicScriptMapper, EpicScr
* 副作用:创建新 conversation,回填 script.conversationId,更新 script * 副作用:创建新 conversation,回填 script.conversationId,更新 script
*/ */
private void ensureConversationForScript(EpicScript script, String currentUserId) { private void ensureConversationForScript(EpicScript script, String currentUserId) {
// 修复:列表查询是读路径,禁止在读路径上触发数据库写操作(隐性写)
// 历史剧本兼容补建 conversation 的逻辑应放在独立的修复脚本/管理接口中,而不是每次列表查询都触发
// 这里只做检查和日志,不做任何写入
String conversationId = script.getConversationId(); String conversationId = script.getConversationId();
if (!org.springframework.util.StringUtils.hasText(conversationId)) { if (!org.springframework.util.StringUtils.hasText(conversationId)) {
// conversationId 字段本身为空,直接创建 log.debug("[EpicScript] 剧本缺少 conversationId: scriptId={}", script.getId());
createAndAttachConversation(script, currentUserId);
return; return;
} }
Conversation existing = conversationService.getById(conversationId); Conversation existing = conversationService.getById(conversationId);
if (existing != null) { if (existing == null) {
return; // 已存在,无需处理 log.warn("[EpicScript] 剧本 conversationId 指向不存在的 conversation: scriptId={}, conversationId={}",
script.getId(), conversationId);
} }
// 存在 conversationId 但 conversation 不存在(历史数据),创建新的
createAndAttachConversation(script, currentUserId);
} }
private void createAndAttachConversation(EpicScript script, String currentUserId) { private void createAndAttachConversation(EpicScript script, String currentUserId) {
// 修复:保留方法签名但不自动调用(避免读路径隐性写)
// 如需补建 conversation,请显式调用此方法(如:剧本详情页、单条修复脚本等)
Conversation conversation = new Conversation(); Conversation conversation = new Conversation();
conversation.setUserId(currentUserId); conversation.setUserId(currentUserId);
conversation.setScriptId(script.getId()); conversation.setScriptId(script.getId());