fix: 剧本查询时自动为历史剧本补建 conversation(兼容前端临时 ID)
This commit is contained in:
@@ -147,7 +147,12 @@ public class EpicScriptServiceImpl extends ServiceImpl<EpicScriptMapper, EpicScr
|
||||
.eq(EpicScript::getIsDeleted, 0)
|
||||
.orderByDesc(EpicScript::getCreateTime);
|
||||
|
||||
return this.list(wrapper).stream()
|
||||
List<EpicScript> scripts = this.list(wrapper);
|
||||
for (EpicScript script : scripts) {
|
||||
ensureConversationForScript(script, currentUserId);
|
||||
}
|
||||
|
||||
return scripts.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
@@ -165,6 +170,9 @@ public class EpicScriptServiceImpl extends ServiceImpl<EpicScriptMapper, EpicScr
|
||||
return null;
|
||||
}
|
||||
|
||||
// 历史剧本兼容:conversation 不存在时自动创建
|
||||
ensureConversationForScript(script, currentUserId);
|
||||
|
||||
return convertToResponse(script);
|
||||
}
|
||||
|
||||
@@ -775,4 +783,38 @@ public class EpicScriptServiceImpl extends ServiceImpl<EpicScriptMapper, EpicScr
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为剧本补建 conversation(历史剧本兼容)
|
||||
*
|
||||
* 触发条件:script.conversationId 指向的 conversation 不存在(DB 中无对应记录)
|
||||
* 副作用:创建新 conversation,回填 script.conversationId,更新 script
|
||||
*/
|
||||
private void ensureConversationForScript(EpicScript script, String currentUserId) {
|
||||
String conversationId = script.getConversationId();
|
||||
if (!org.springframework.util.StringUtils.hasText(conversationId)) {
|
||||
// conversationId 字段本身为空,直接创建
|
||||
createAndAttachConversation(script, currentUserId);
|
||||
return;
|
||||
}
|
||||
Conversation existing = conversationService.getById(conversationId);
|
||||
if (existing != null) {
|
||||
return; // 已存在,无需处理
|
||||
}
|
||||
// 存在 conversationId 但 conversation 不存在(历史数据),创建新的
|
||||
createAndAttachConversation(script, currentUserId);
|
||||
}
|
||||
|
||||
private void createAndAttachConversation(EpicScript script, String currentUserId) {
|
||||
Conversation conversation = new Conversation();
|
||||
conversation.setUserId(currentUserId);
|
||||
conversation.setScriptId(script.getId());
|
||||
conversation.setTitle(script.getTitle() != null ? script.getTitle() : "未命名剧本");
|
||||
conversation.setType("script");
|
||||
conversation.setConversationStatus("active");
|
||||
conversationService.save(conversation);
|
||||
|
||||
script.setConversationId(conversation.getId());
|
||||
this.updateById(script);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user