处理数据库 复杂查询问题
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package org.jiayunet.pojo.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 导师模拟对话表(bg_mentor_mock_chat)
|
||||
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
@TableName(value = "bg_mentor_mock_chat", autoResultMap = true)
|
||||
@TableName(value = "bg_mentor_mock_chat")
|
||||
public class MentorMockChat {
|
||||
|
||||
@TableId(type = IdType.ASSIGN_ID)
|
||||
@@ -23,21 +23,9 @@ public class MentorMockChat {
|
||||
/** 导师ID */
|
||||
private Long mentorId;
|
||||
|
||||
/** 对话消息列表(JSON数组) */
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private List<ChatMessage> messages;
|
||||
/** 对话消息列表(JSON字符串) */
|
||||
private String messages;
|
||||
|
||||
/** 创建时间 */
|
||||
private Instant createTime;
|
||||
|
||||
/**
|
||||
* 对话消息项
|
||||
*/
|
||||
@Data
|
||||
public static class ChatMessage {
|
||||
/** 角色:user=用户,mentor=导师 */
|
||||
private String role;
|
||||
/** 消息内容 */
|
||||
private String content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class MentorMockChatService {
|
||||
|
||||
/**
|
||||
* 获取导师模拟对话
|
||||
* <p>1. 查库,有缓存直接返回 2. 无缓存则获取导师信息+随机话题 3. AI生成 4. 入库 5. 返回</p>
|
||||
* <p>1. 查库,有缓存直接返回 2. 无缓存则获取导师信息+话题 3. AI生成 4. 入库 5. 返回</p>
|
||||
*
|
||||
* @param mentorId 导师ID
|
||||
* @return 模拟对话
|
||||
@@ -49,12 +49,8 @@ public class MentorMockChatService {
|
||||
public MentorMockChatDto generate(Long mentorId) {
|
||||
// 1. 查缓存
|
||||
MentorMockChat existing = mentorMockChatMapper.selectOne(new LambdaQueryWrapper<MentorMockChat>().eq(MentorMockChat::getMentorId, mentorId));
|
||||
if (existing != null) {
|
||||
MentorMockChatDto dto = new MentorMockChatDto();
|
||||
dto.setMessages(existing.getMessages().stream()
|
||||
.map(m -> new MentorMockChatDto.Message(m.getRole(), m.getContent()))
|
||||
.toList());
|
||||
return dto;
|
||||
if (existing != null && existing.getMessages() != null) {
|
||||
return parseDto(existing.getMessages());
|
||||
}
|
||||
|
||||
// 2. 获取导师信息
|
||||
@@ -70,34 +66,36 @@ public class MentorMockChatService {
|
||||
String aiResponse = aiChatAbility.chat(systemPrompt, userMessage);
|
||||
String json = AiResponseCleanTool.clean(aiResponse);
|
||||
|
||||
// 5. 解析
|
||||
List<MentorMockChat.ChatMessage> chatMessages;
|
||||
try {
|
||||
chatMessages = HttpTool.objectMapper.readValue(json, new TypeReference<>() {});
|
||||
} catch (Exception e) {
|
||||
log.warn("模拟对话AI返回解析失败: {}", json, e);
|
||||
throw new RuntimeException("模拟对话生成失败", e);
|
||||
}
|
||||
|
||||
// 6. 入库(存在则更新,不存在则插入)
|
||||
// 5. 入库(存在则更新,不存在则插入)
|
||||
MentorMockChat existRecord = mentorMockChatMapper.selectOne(new LambdaQueryWrapper<MentorMockChat>().eq(MentorMockChat::getMentorId, mentorId));
|
||||
if (existRecord != null) {
|
||||
existRecord.setMessages(chatMessages);
|
||||
existRecord.setMessages(json);
|
||||
mentorMockChatMapper.updateById(existRecord);
|
||||
} else {
|
||||
MentorMockChat record = new MentorMockChat();
|
||||
record.setMentorId(mentorId);
|
||||
record.setMessages(chatMessages);
|
||||
record.setMessages(json);
|
||||
record.setCreateTime(Instant.now());
|
||||
mentorMockChatMapper.insert(record);
|
||||
}
|
||||
|
||||
// 7. 返回
|
||||
// 6. 返回
|
||||
return parseDto(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析JSON字符串为DTO
|
||||
*/
|
||||
private MentorMockChatDto parseDto(String json) {
|
||||
try {
|
||||
List<MentorMockChatDto.Message> messages = HttpTool.objectMapper.readValue(json, new TypeReference<>() {});
|
||||
MentorMockChatDto dto = new MentorMockChatDto();
|
||||
dto.setMessages(chatMessages.stream()
|
||||
.map(m -> new MentorMockChatDto.Message(m.getRole(), m.getContent()))
|
||||
.toList());
|
||||
dto.setMessages(messages);
|
||||
return dto;
|
||||
} catch (Exception e) {
|
||||
log.warn("模拟对话JSON解析失败: {}", json, e);
|
||||
throw new RuntimeException("模拟对话解析失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,18 +105,19 @@ public class MentorMockChatService {
|
||||
return """
|
||||
你是一个对话生成器。根据提供的导师资料和话题信息,生成一段求职者与导师之间的模拟对话。
|
||||
|
||||
要求:
|
||||
1. 对话轮数:2~3轮(随机),每轮由一条用户消息+一条导师消息组成
|
||||
2. 用户消息:简短口语化,10~30字,带有真实困惑或追问
|
||||
3. 导师消息:字数浮动,有长有短,短的10~30字,长的40~80字,体现专业性
|
||||
4. 对话内容必须与导师的真实职业背景和话题方向强相关
|
||||
5. 语气自然,像真实的即时通讯对话,不要书面化
|
||||
6. 用户是求职者/应届生身份,导师根据自己的行业经验给出建议
|
||||
严格要求:
|
||||
1. 必须生成4~6条消息(即2~3轮对话),最少4条,最多6条,每轮=一条用户消息+一条导师消息,交替排列
|
||||
2. 第一条必须是用户消息,第二条必须是导师消息,以此交替
|
||||
3. 用户消息:简短口语化,10~30字,带有真实困惑或追问
|
||||
4. 导师消息:字数浮动有长有短,短的10~30字,长的40~80字,体现专业性和行业经验
|
||||
5. 对话内容必须与导师的真实职业背景和话题方向强相关
|
||||
6. 语气自然,像微信聊天,不要书面化,不要用"您好"开头
|
||||
7. 用户是求职者/应届生身份
|
||||
|
||||
返回JSON数组格式:
|
||||
[{"role":"user","content":"..."},{"role":"mentor","content":"..."},...]
|
||||
返回JSON数组,数组长度必须>=4:
|
||||
[{"role":"user","content":"..."},{"role":"mentor","content":"..."},{"role":"user","content":"..."},{"role":"mentor","content":"..."}]
|
||||
|
||||
只返回JSON,不要任何额外说明。""";
|
||||
只返回JSON数组,不要任何其他文字。""";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user