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