Commit 72adaf97 authored by 赵灿灿's avatar 赵灿灿

保存对话

parent 11f6222f
Pipeline #21060 passed with stages
in 3 minutes and 27 seconds
package com.infoepoch.pms.dispatchassistant.domain.langchain.chat;
import com.infoepoch.pms.dispatchassistant.common.component.SnowFlake;
import com.infoepoch.pms.dispatchassistant.common.exception.ValidationException;
import com.infoepoch.pms.dispatchassistant.common.utils.JsonUtils;
import com.infoepoch.pms.dispatchassistant.common.utils.RestTemplateUtils;
import com.infoepoch.pms.dispatchassistant.controller.basic.Auth;
import com.infoepoch.pms.dispatchassistant.domain.basic.store.KeyValueStoreService;
import com.infoepoch.pms.dispatchassistant.domain.basic.user.User;
import com.infoepoch.pms.dispatchassistant.domain.langchain.ChatConstants;
import com.infoepoch.pms.dispatchassistant.domain.langchain.chat.chat.ChatRequest;
import com.infoepoch.pms.dispatchassistant.domain.langchain.chat.knowledgeBaseChat.KnowledgeBaseChatRequest;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.*;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
......@@ -23,6 +30,14 @@ public class ChatService {
@Autowired
private KeyValueStoreService keyValueStoreService;
@Autowired
private Auth auth;
@Autowired
private IConversationsRepository iConversationsRepository;
@Autowired
private IMessagesRepository iMessagesRepository;
/**
* 与LLM模型对话
*
......@@ -94,4 +109,74 @@ public class ChatService {
}
}
/**
* @param dialogId
* 新增会话
*/
private Conversations saveConversations(String dialogId,String question,String aiType) {
Conversations conversations=null;
try {
ConversationsCriteria conversationsCriteria=new ConversationsCriteria();
conversationsCriteria.setSessionId(dialogId);
List<Conversations> conversationsList=iConversationsRepository.selectByCriteria(conversationsCriteria);
if(conversationsList==null)
{
User user= auth.getUserReq();
conversations=new Conversations();
conversations.setId(SnowFlake.instant().nextId().toString());
conversations.setUserId(user.getId());
conversations.setUserName(user.getUsername());
conversations.setName(user.getFullname());
conversations.setFirstQuestion(question);
conversations.setAiType(aiType);
conversations.setSessionId(dialogId);
conversations.setRecordTime(new Date());
iConversationsRepository.insert(conversations);
}else
conversations=conversationsList.get(0);
} catch (Exception e) {
e.printStackTrace();
}
return conversations;
}
//新增问题信息
private Messages insertQuestionMessage(Conversations conversations,Messages questionMes) {
//查找当前对话的父对话
MessagesCriteria messagesCriteria=new MessagesCriteria();
messagesCriteria.setSessionId(conversations.getSessionId());
messagesCriteria.setCid(conversations.getId());
Messages messagesLast=iMessagesRepository.selectOneByCriteria(messagesCriteria);
try {
if(messagesLast!=null)
{
questionMes.setParentMsgId(messagesLast.getId());
}
String id=SnowFlake.instant().nextId().toString();
questionMes.setId(id);
questionMes.setCid(conversations.getId());
questionMes.setSessionId(conversations.getSessionId());
questionMes.setRecordTime(new Date());
iMessagesRepository.insert(questionMes);
} catch (Exception e) {
e.printStackTrace();
}
return questionMes;
}
//新增回答信息
private Messages insertMessage(Conversations conversations,Messages contMes) {
try {
String id=SnowFlake.instant().nextId().toString();
contMes.setId(id);
contMes.setCid(conversations.getId());
contMes.setSessionId(conversations.getSessionId());
contMes.setRecordTime(new Date());
iMessagesRepository.insert(contMes);
} catch (Exception e) {
e.printStackTrace();
}
return contMes;
}
}
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
/**
* generated by code-generator
*
*/
public class Conversations {
/**
* 主键
*/
private String id;
/**
* 用户ID
*/
private String userId;
/**
* 用户名
*/
private String userName;
/**
* 用户姓名
*/
private String name;
/**
* 会话id
*/
private String sessionId;
/**
* 第一次提的问题
*/
private String firstQuestion;
/**
* 状态(1:进行中, 0:已结束)
*/
private String status;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date recordTime;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
/**
* 智能体专业类型
*/
private String aiType;
/**
* 条件1
*/
private String condition1;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getFirstQuestion() {
return firstQuestion;
}
public void setFirstQuestion(String firstQuestion) {
this.firstQuestion = firstQuestion;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getRecordTime() {
return recordTime;
}
public void setRecordTime(Date recordTime) {
this.recordTime = recordTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.recordTime = updateTime;
}
public String getAiType() {
return aiType;
}
public void setAiType(String aiType) {
this.aiType = aiType;
}
public String getCondition1() {
return condition1;
}
public void setCondition1(String condition1) {
this.condition1 = condition1;
}
}
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import com.infoepoch.pms.dispatchassistant.common.component.AbstractCriteria;
/**
* generated by code-generator
* 查询条件类
*/
public class ConversationsCriteria extends AbstractCriteria {
//region 样例
//public boolean byId() {
// return this.andMap.containsKey("Id");
//}
//
//private String id;
//
//public String getId() {
// if (byId())
// return id;
// return null;
//}
//
//public void setId(String value) {
// this.id = value;
// this.andMap.put("Id", value);
//}
//endregion
//region 用户ID
public boolean byUserId() {
return this.andMap.containsKey("UserId");
}
private String userId;
public String getUserId() {
if (byUserId())
return userId;
return null;
}
public void setUserId(String value) {
this.userId = value;
this.andMap.put("UserId", value);
}
//endregion
//region 用户名
public boolean byUserName() {
return this.andMap.containsKey("UserName");
}
private String userName;
public String getUserName() {
if (byUserName())
return userName;
return null;
}
public void setUserName(String value) {
this.userName = value;
this.andMap.put("UserName", value);
}
//endregion
//region 用户姓名
public boolean byName() {
return this.andMap.containsKey("Name");
}
private String name;
public String getName() {
if (byName())
return name;
return null;
}
public void setName(String value) {
this.name = value;
this.andMap.put("Name", value);
}
//endregion
//region 会话id
public boolean bySessionId() {
return this.andMap.containsKey("SessionId");
}
private String sessionId;
public String getSessionId() {
if (bySessionId())
return sessionId;
return null;
}
public void setSessionId(String value) {
this.sessionId = value;
this.andMap.put("SessionId", value);
}
//endregion
//region 智能体专业类型
public boolean byAiType() {
return this.andMap.containsKey("AiType");
}
private String aiType;
public String getAiType() {
if (byAiType())
return aiType;
return null;
}
public void setAiType(String value) {
this.aiType = value;
this.andMap.put("AiType", value);
}
//endregion
//region 条件1
public boolean byCondition1() {
return this.andMap.containsKey("Condition1");
}
private String condition1;
public String getCondition1() {
if (byCondition1())
return condition1;
return null;
}
public void setCondition1(String value) {
this.condition1 = value;
this.andMap.put("Condition1", value);
}
//endregion
}
\ No newline at end of file
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import java.util.List;
/**
* generated by code-generator
* 仓储接口
*/
public interface IConversationsRepository {
/**
* 表序列id
*/
Long sequenceId();
/**
* 新增
* @param: [entity]
*/
boolean insert(Conversations entity);
/**
* 更新
* @param: [entity]
*/
boolean update(Conversations entity);
/**
* 批量新增
* @param: [entitys]
*/
int[] batchInsert(List<Conversations> entitys);
/**
* 批量更新
* @param: [entitys]
*/
int[] batchUpdate(List<Conversations> entitys);
/**
* 删除
* @param: [id]
*/
boolean delete(String id);
// region select
/**
* 根据Id查询
* @param: [id]
*/
Conversations selectById(String id);
/**
* 根据查询条件查询单个对象
* @param: [criteria]
*/
Conversations selectOneByCriteria(ConversationsCriteria criteria);
/**
* 根据查询条件查询对象集合
* @param: [criteria]
*/
List<Conversations> selectByCriteria(ConversationsCriteria criteria);
/**
* 根据查询条件分页查询对象结合
* @param: [criteria, pageNum, pageSize]
*/
List<Conversations> selectCriteriaByPage(ConversationsCriteria criteria, int pageNum, int pageSize);
/**
* 根据条件查询对象总记录数
* @param: [criteria]
*/
Integer selectCountByCriteria(ConversationsCriteria criteria);
// endregion
}
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import java.util.List;
/**
* generated by code-generator
* 仓储接口
*/
public interface IMessagesRepository {
/**
* 表序列id
*/
Long sequenceId();
/**
* 新增
* @param: [entity]
*/
boolean insert(Messages entity);
/**
* 更新
* @param: [entity]
*/
boolean update(Messages entity);
/**
* 批量新增
* @param: [entitys]
*/
int[] batchInsert(List<Messages> entitys);
/**
* 批量更新
* @param: [entitys]
*/
int[] batchUpdate(List<Messages> entitys);
/**
* 删除
* @param: [id]
*/
boolean delete(String id);
// region select
/**
* 根据Id查询
* @param: [id]
*/
Messages selectById(String id);
/**
* 根据查询条件查询单个对象
* @param: [criteria]
*/
Messages selectOneByCriteria(MessagesCriteria criteria);
/**
* 根据查询条件查询对象集合
* @param: [criteria]
*/
List<Messages> selectByCriteria(MessagesCriteria criteria);
/**
* 根据查询条件分页查询对象结合
* @param: [criteria, pageNum, pageSize]
*/
List<Messages> selectCriteriaByPage(MessagesCriteria criteria, int pageNum, int pageSize);
/**
* 根据条件查询对象总记录数
* @param: [criteria]
*/
Integer selectCountByCriteria(MessagesCriteria criteria);
// endregion
}
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.math.BigDecimal;
import java.util.Date;
/**
* generated by code-generator
*
*/
public class Messages {
/**
* 主键
*/
private String id;
/**
* 会话表主键
*/
private String cid;
/**
* 会话id
*/
private String sessionId;
/**
* 请求id
*/
private String requestId;
/**
* 角色'user','assistant'
*/
private String role;
/**
* 消息内容
*/
private String content;
/**
* 父消息id
*/
private String parentMsgId;
/**
* 关键词提取结果(用于快速检索上下文)
*/
private String keyWords;
/**
* 使用的AI模型
*/
private String modelName;
/**
* 记录时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date recordTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getParentMsgId() {
return parentMsgId;
}
public void setParentMsgId(String parentMsgId) {
this.parentMsgId = parentMsgId;
}
public String getKeyWords() {
return keyWords;
}
public void setKeyWords(String keyWords) {
this.keyWords = keyWords;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public Date getRecordTime() {
return recordTime;
}
public void setRecordTime(Date recordTime) {
this.recordTime = recordTime;
}
}
\ No newline at end of file
package com.infoepoch.pms.dispatchassistant.domain.langchain.history;
import com.infoepoch.pms.dispatchassistant.common.component.AbstractCriteria;
/**
* generated by code-generator
* 查询条件类
*/
public class MessagesCriteria extends AbstractCriteria {
//region 样例
//public boolean byId() {
// return this.andMap.containsKey("Id");
//}
//
//private String id;
//
//public String getId() {
// if (byId())
// return id;
// return null;
//}
//
//public void setId(String value) {
// this.id = value;
// this.andMap.put("Id", value);
//}
//endregion
//region 主键
public boolean byId() {
return this.andMap.containsKey("Id");
}
private String id;
public String getId() {
if (byId())
return id;
return null;
}
public void setId(String value) {
this.id = value;
this.andMap.put("Id", value);
}
//endregion
//region 会话表主键
public boolean byCid() {
return this.andMap.containsKey("Cid");
}
private String cid;
public String getCid() {
if (byCid())
return cid;
return null;
}
public void setCid(String value) {
this.cid = value;
this.andMap.put("Cid", value);
}
//endregion
//region 会话id
public boolean bySessionId() {
return this.andMap.containsKey("SessionId");
}
private String sessionId;
public String getSessionId() {
if (bySessionId())
return sessionId;
return null;
}
public void setSessionId(String value) {
this.sessionId = value;
this.andMap.put("SessionId", value);
}
//endregion
//region 请求id
public boolean byRequestId() {
return this.andMap.containsKey("RequestId");
}
private String requestId;
public String getRequestId() {
if (byRequestId())
return requestId;
return null;
}
public void setRequestId(String value) {
this.requestId = value;
this.andMap.put("RequestId", value);
}
//endregion
//region 角色'user','assistant'
public boolean byRole() {
return this.andMap.containsKey("Role");
}
private String role;
public String getRole() {
if (byRole())
return role;
return null;
}
public void setRole(String value) {
this.role = value;
this.andMap.put("Role", value);
}
//endregion
//region 父消息id
public boolean byParentMsgId() {
return this.andMap.containsKey("ParentMsgId");
}
private String parentMsgId;
public String getParentMsgId() {
if (byParentMsgId())
return parentMsgId;
return null;
}
public void setParentMsgId(String value) {
this.parentMsgId = value;
this.andMap.put("ParentMsgId", value);
}
//endregion
}
package com.infoepoch.pms.dispatchassistant.infractructure.langchain;
import com.infoepoch.pms.dispatchassistant.common.utils.OracleUtils;
import com.infoepoch.pms.dispatchassistant.controller.langchain.LangChainController;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.Conversations;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.ConversationsCriteria;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.IConversationsRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* generated by code-generator
* 仓储实现
*/
@Repository
public class ConversationsRepository implements IConversationsRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
private static final Logger LogHelper = LoggerFactory.getLogger(ConversationsRepository.class);
/**
* 查询表序列id
*/
@Override
public Long sequenceId() {
StringBuffer buffer = new StringBuffer("SELECT SEQ_T_CONVERSATIONS.NEXTVAL FROM DUAL ");
return jdbcTemplate.queryForObject(buffer.toString(), Long.class);
}
/**
* 新增
*/
@Override
public boolean insert(Conversations entity) {
String sql = "INSERT INTO T_CONVERSATIONS(C_ID, C_USER_ID, C_USER_NAME, C_NAME, C_SESSION_ID, C_FIRST_QUESTION, C_STATUS, C_RECORD_TIME, C_UPDATE_TIME, C_AI_TYPE, C_CONDITION1) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int result=0;
try {
result = jdbcTemplate.update(sql, entity.getId(), entity.getUserId(), entity.getUserName(), entity.getName(), entity.getSessionId(), entity.getFirstQuestion(), entity.getStatus(), entity.getRecordTime(), entity.getUpdateTime(), entity.getAiType(), entity.getCondition1());
} catch (Exception e) {
LogHelper.info(e.getMessage());
//throw new ServiceException("新增 数据 失败。");
}
return result > 0;
}
/**
* 更新
*/
@Override
public boolean update(Conversations entity) {
String sql = "UPDATE T_CONVERSATIONS SET C_USER_ID = ?, C_USER_NAME = ?, C_NAME = ?, C_SESSION_ID = ?, C_FIRST_QUESTION = ?, C_STATUS = ?, C_RECORD_TIME = ?, C_UPDATE_TIME = ?, C_AI_TYPE = ?, C_CONDITION1 = ? WHERE C_ID = ?";
int result=0;
try {
result = jdbcTemplate.update(sql, entity.getUserId(), entity.getUserName(), entity.getName(), entity.getSessionId(), entity.getFirstQuestion(), entity.getStatus(), entity.getRecordTime(), entity.getUpdateTime(), entity.getAiType(), entity.getCondition1(), entity.getId());
} catch (Exception e) {
LogHelper.info(e.getMessage());
// throw new ServiceException("更新 数据 失败。");
}
return result >= 0;
}
/**
* 批量新增
*/
public int[] batchInsert(List<Conversations> list) {
String sql = "INSERT INTO T_CONVERSATIONS(C_ID, C_USER_ID, C_USER_NAME, C_NAME, C_SESSION_ID, C_FIRST_QUESTION, C_STATUS, C_RECORD_TIME, C_UPDATE_TIME, C_AI_TYPE, C_CONDITION1) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int[] result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
int j = 0;
Conversations item = list.get(i);
ps.setString(++j, item.getId());
ps.setString(++j, item.getUserId());
ps.setString(++j, item.getUserName());
ps.setString(++j, item.getName());
ps.setString(++j, item.getSessionId());
ps.setString(++j, item.getFirstQuestion());
ps.setString(++j, item.getStatus());
ps.setTimestamp(++j, item.getRecordTime() != null ? new Timestamp(item.getRecordTime().getTime()) : null);
ps.setTimestamp(++j, item.getUpdateTime() != null ? new Timestamp(item.getUpdateTime().getTime()) : null);
ps.setString(++j, item.getAiType());
ps.setString(++j, item.getCondition1());
}
@Override
public int getBatchSize() {
return list.size();
}
});
return result;
}
/**
* 批量更新
*/
public int[] batchUpdate(List<Conversations> list) {
String sql = "UPDATE T_CONVERSATIONS SET C_USER_ID = ?, C_USER_NAME = ?, C_NAME = ?, C_SESSION_ID = ?, C_FIRST_QUESTION = ?, C_STATUS = ?, C_RECORD_TIME = ?, C_UPDATE_TIME = ?, C_AI_TYPE = ?, C_CONDITION1 = ? WHERE C_ID = ? ";
int[] result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
int j = 0;
Conversations item = list.get(i);
ps.setString(++j, item.getUserId());
ps.setString(++j, item.getUserName());
ps.setString(++j, item.getName());
ps.setString(++j, item.getSessionId());
ps.setString(++j, item.getFirstQuestion());
ps.setString(++j, item.getStatus());
ps.setTimestamp(++j, item.getRecordTime() != null ? new Timestamp(item.getRecordTime().getTime()) : null);
ps.setTimestamp(++j, item.getUpdateTime() != null ? new Timestamp(item.getUpdateTime().getTime()) : null);
ps.setString(++j, item.getAiType());
ps.setString(++j, item.getCondition1());
ps.setString(++j, item.getId());
}
@Override
public int getBatchSize() {
return list.size();
}
});
return result;
}
/**
* 删除
*/
@Override
public boolean delete(String id) {
String sql = "DELETE FROM T_CONVERSATIONS WHERE C_ID = ?";
int result=0;
try {
result = jdbcTemplate.update(sql, id);
} catch (Exception e) {
LogHelper.info(e.getMessage());
//throw new ServiceException("删除 数据 失败。");
}
return result > 0;
}
/**
* 根据Id查询
*/
@Override
public Conversations selectById(String id) {
String sql = "SELECT * FROM T_CONVERSATIONS WHERE C_ID = ? ";
try {
return jdbcTemplate.queryForObject(sql, new ConversationsRowMapper(), id);
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件查询单个对象
*/
@Override
public Conversations selectOneByCriteria(ConversationsCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_CONVERSATIONS ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria), 1, 1);
try {
return jdbcTemplate.queryForObject(buffer.toString(), list.toArray(), new ConversationsRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件查询对象集合
*/
@Override
public List<Conversations> selectByCriteria(ConversationsCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_CONVERSATIONS ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria));
try {
return jdbcTemplate.query(buffer.toString(), list.toArray(), new ConversationsRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件分页查询对象结合
*/
@Override
public List<Conversations> selectCriteriaByPage(ConversationsCriteria criteria, int pageNum, int pageSize) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_CONVERSATIONS ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria), pageNum, pageSize);
try {
return jdbcTemplate.query(buffer.toString(), list.toArray(), new ConversationsRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据条件查询对象总记录数
*/
@Override
public Integer selectCountByCriteria(ConversationsCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT COUNT(*) FROM T_CONVERSATIONS ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria));
int count = jdbcTemplate.queryForObject(buffer.toString(), list.toArray(), int.class);
return count;
}
/**
* RowMapper
*/
private class ConversationsRowMapper implements RowMapper<Conversations> {
@Override
public Conversations mapRow(ResultSet rs, int i) throws SQLException {
Conversations conversations=new Conversations();
conversations.setId( rs.getString("C_ID"));
conversations.setUserId(rs.getString("C_USER_ID"));
conversations.setUserName(rs.getString("C_USER_NAME"));
conversations.setName(rs.getString("C_NAME"));
conversations.setSessionId(rs.getString("C_SESSION_ID"));
conversations.setFirstQuestion( rs.getString("C_FIRST_QUESTION"));
conversations.setStatus( rs.getString("C_STATUS"));
conversations.setRecordTime(rs.getTimestamp("C_RECORD_TIME"));
conversations.setUpdateTime( rs.getTimestamp("C_UPDATE_TIME"));
conversations.setAiType( rs.getString("C_AI_TYPE"));
conversations.setCondition1( rs.getString("C_CONDITION1"));
return conversations;
}
}
/**
* 创建查询条件
*/
private Map<String, Object> createCriteriaSql(ConversationsCriteria criteria) {
Map<String, Object> andMap = new LinkedHashMap<>();
if (criteria == null)
return andMap;
//if (criteria.byId())
// andMap.put(criteria.getId() == null ? " T_Id IS NULL " : " T_Id = ? ", criteria.getId());
//用户ID
if (criteria.byUserId())
andMap.put(criteria.getUserId() == null ? " C_USER_ID IS NULL " : " C_USER_ID = ? ", criteria.getUserId());
//用户名
if (criteria.byUserName())
andMap.put(criteria.getUserName() == null ? " C_USER_NAME IS NULL " : " C_USER_NAME = ? ", criteria.getUserName());
//用户姓名
if (criteria.byName())
andMap.put(criteria.getName() == null ? " C_NAME IS NULL " : " C_NAME = ? ", criteria.getName());
//会话id
if (criteria.bySessionId())
andMap.put(criteria.getSessionId() == null ? " C_SESSION_ID IS NULL " : " C_SESSION_ID = ? ", criteria.getSessionId());
//智能体专业类型
if (criteria.byAiType())
andMap.put(criteria.getAiType() == null ? " C_AI_TYPE IS NULL " : " C_AI_TYPE = ? ", criteria.getAiType());
//条件1
if (criteria.byCondition1())
andMap.put(criteria.getCondition1() == null ? " C_CONDITION1 IS NULL " : " C_CONDITION1 = ? ", criteria.getCondition1());
return andMap;
}
}
package com.infoepoch.pms.dispatchassistant.infractructure.langchain;
import com.infoepoch.pms.dispatchassistant.common.utils.OracleUtils;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.IMessagesRepository;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.Messages;
import com.infoepoch.pms.dispatchassistant.domain.langchain.history.MessagesCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* generated by code-generator
* 仓储实现
*/
@Repository
public class MessagesRepository implements IMessagesRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
private static final Logger LogHelper = LoggerFactory.getLogger(ConversationsRepository.class);
/**
* 查询表序列id
*/
@Override
public Long sequenceId() {
StringBuffer buffer = new StringBuffer("SELECT SEQ_T_MESSAGES.NEXTVAL FROM DUAL ");
return jdbcTemplate.queryForObject(buffer.toString(), Long.class);
}
/**
* 新增
*/
@Override
public boolean insert(Messages entity) {
String sql = "INSERT INTO T_MESSAGES(M_ID, M_CID, M_SESSION_ID, M_REQUEST_ID, M_ROLE, M_CONTENT, M_PARENT_MSG_ID, M_KEY_WORDS, M_MODEL_NAME, M_RECORD_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int result=0;
try {
result = jdbcTemplate.update(sql, entity.getId(), entity.getCid(), entity.getSessionId(), entity.getRequestId(), entity.getRole(), entity.getContent(), entity.getParentMsgId(), entity.getKeyWords(), entity.getModelName(), entity.getRecordTime());
} catch (Exception e) {
LogHelper.info(e.getMessage());
// throw new ServiceException("新增 数据 失败。");
}
return result > 0;
}
/**
* 更新
*/
@Override
public boolean update(Messages entity) {
String sql = "UPDATE T_MESSAGES SET M_CID = ?, M_SESSION_ID = ?, M_REQUEST_ID = ?, M_ROLE = ?, M_CONTENT = ?, M_PARENT_MSG_ID = ?, M_KEY_WORDS = ?, M_MODEL_NAME = ?, M_RECORD_TIME = ? WHERE M_ID = ?";
int result=0;
try {
result = jdbcTemplate.update(sql, entity.getCid(), entity.getSessionId(), entity.getRequestId(), entity.getRole(), entity.getContent(), entity.getParentMsgId(), entity.getKeyWords(), entity.getModelName(), entity.getRecordTime(), entity.getId());
} catch (Exception e) {
LogHelper.info(e.getMessage());
// throw new ServiceException("更新 数据 失败。");
}
return result >= 0;
}
/**
* 批量新增
*/
public int[] batchInsert(List<Messages> list) {
String sql = "INSERT INTO T_MESSAGES(M_ID, M_CID, M_SESSION_ID, M_REQUEST_ID, M_ROLE, M_CONTENT, M_PARENT_MSG_ID, M_KEY_WORDS, M_MODEL_NAME, M_RECORD_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int[] result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
int j = 0;
Messages item = list.get(i);
ps.setString(++j, item.getId());
ps.setString(++j, item.getCid());
ps.setString(++j, item.getSessionId());
ps.setString(++j, item.getRequestId());
ps.setString(++j, item.getRole());
ps.setString(++j, item.getContent());
ps.setString(++j, item.getParentMsgId());
ps.setString(++j, item.getKeyWords());
ps.setString(++j, item.getModelName());
ps.setTimestamp(++j, item.getRecordTime() != null ? new Timestamp(item.getRecordTime().getTime()) : null);
}
@Override
public int getBatchSize() {
return list.size();
}
});
return result;
}
/**
* 批量更新
*/
public int[] batchUpdate(List<Messages> list) {
String sql = "UPDATE T_MESSAGES SET M_CID = ?, M_SESSION_ID = ?, M_REQUEST_ID = ?, M_ROLE = ?, M_CONTENT = ?, M_PARENT_MSG_ID = ?, M_KEY_WORDS = ?, M_MODEL_NAME = ?, M_RECORD_TIME = ? WHERE M_ID = ? ";
int[] result = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
int j = 0;
Messages item = list.get(i);
ps.setString(++j, item.getCid());
ps.setString(++j, item.getSessionId());
ps.setString(++j, item.getRequestId());
ps.setString(++j, item.getRole());
ps.setString(++j, item.getContent());
ps.setString(++j, item.getParentMsgId());
ps.setString(++j, item.getKeyWords());
ps.setString(++j, item.getModelName());
ps.setTimestamp(++j, item.getRecordTime() != null ? new Timestamp(item.getRecordTime().getTime()) : null);
ps.setString(++j, item.getId());
}
@Override
public int getBatchSize() {
return list.size();
}
});
return result;
}
/**
* 删除
*/
@Override
public boolean delete(String id) {
String sql = "DELETE FROM T_MESSAGES WHERE M_ID = ?";
int result=0;
try {
result = jdbcTemplate.update(sql, id);
} catch (Exception e) {
LogHelper.info(e.getMessage());
//throw new ServiceException("删除 数据 失败。");
}
return result > 0;
}
/**
* 根据Id查询
*/
@Override
public Messages selectById(String id) {
String sql = "SELECT * FROM T_MESSAGES WHERE M_ID = ? ";
try {
return jdbcTemplate.queryForObject(sql, new MessagesRowMapper(), id);
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件查询单个对象
*/
@Override
public Messages selectOneByCriteria(MessagesCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_MESSAGES ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria), 1, 1,"M_RECORD_TIME",true);
try {
return jdbcTemplate.queryForObject(buffer.toString(), list.toArray(), new MessagesRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件查询对象集合
*/
@Override
public List<Messages> selectByCriteria(MessagesCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_MESSAGES ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria));
try {
return jdbcTemplate.query(buffer.toString(), list.toArray(), new MessagesRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据查询条件分页查询对象结合
*/
@Override
public List<Messages> selectCriteriaByPage(MessagesCriteria criteria, int pageNum, int pageSize) {
StringBuffer buffer = new StringBuffer("SELECT * FROM T_MESSAGES ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria), pageNum, pageSize);
try {
return jdbcTemplate.query(buffer.toString(), list.toArray(), new MessagesRowMapper());
} catch (Exception e) {
LogHelper.info(e.getMessage());
return null;
}
}
/**
* 根据条件查询对象总记录数
*/
@Override
public Integer selectCountByCriteria(MessagesCriteria criteria) {
StringBuffer buffer = new StringBuffer("SELECT COUNT(*) FROM T_MESSAGES ");
List<Object> list = OracleUtils.combinationSql(buffer, createCriteriaSql(criteria));
int count = jdbcTemplate.queryForObject(buffer.toString(), list.toArray(), int.class);
return count;
}
/**
* RowMapper
*/
private class MessagesRowMapper implements RowMapper<Messages> {
@Override
public Messages mapRow(ResultSet rs, int i) throws SQLException {
Messages messages=new Messages();
messages.setId(rs.getString("M_ID"));
messages.setCid(rs.getString("M_CID"));
messages.setSessionId( rs.getString("M_SESSION_ID"));
messages.setRequestId(rs.getString("M_REQUEST_ID"));
messages.setRole(rs.getString("M_ROLE"));
messages.setContent(rs.getString("M_CONTENT"));
messages.setParentMsgId(rs.getString("M_PARENT_MSG_ID"));
messages.setKeyWords(rs.getString("M_KEY_WORDS"));
messages.setModelName( rs.getString("M_MODEL_NAME"));
messages.setRecordTime(rs.getTimestamp("M_RECORD_TIME"));
return messages;
}
}
/**
* 创建查询条件
*/
private Map<String, Object> createCriteriaSql(MessagesCriteria criteria) {
Map<String, Object> andMap = new LinkedHashMap<>();
if (criteria == null)
return andMap;
//if (criteria.byId())
// andMap.put(criteria.getId() == null ? " T_Id IS NULL " : " T_Id = ? ", criteria.getId());
//主键
if (criteria.byId())
andMap.put(criteria.getId() == null ? " M_ID IS NULL " : " M_ID = ? ", criteria.getId());
//会话表主键
if (criteria.byCid())
andMap.put(criteria.getCid() == null ? " M_CID IS NULL " : " M_CID = ? ", criteria.getCid());
//会话id
if (criteria.bySessionId())
andMap.put(criteria.getSessionId() == null ? " M_SESSION_ID IS NULL " : " M_SESSION_ID = ? ", criteria.getSessionId());
//请求id
if (criteria.byRequestId())
andMap.put(criteria.getRequestId() == null ? " M_REQUEST_ID IS NULL " : " M_REQUEST_ID = ? ", criteria.getRequestId());
//角色'user','assistant'
if (criteria.byRole())
andMap.put(criteria.getRole() == null ? " M_ROLE IS NULL " : " M_ROLE = ? ", criteria.getRole());
//父消息id
if (criteria.byParentMsgId())
andMap.put(criteria.getParentMsgId() == null ? " M_PARENT_MSG_ID IS NULL " : " M_PARENT_MSG_ID = ? ", criteria.getParentMsgId());
return andMap;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment