Commit 0d550166 authored by shiyu's avatar shiyu

ai会话保存消息记录

parent 62940cc7
......@@ -81,4 +81,42 @@ public class IMEnum {
return des;
}
}
/**
* 消息状态
*/
public enum MsgStateEnum {
UNREAD(1, "未读"),
READED(2, "已读"),
REVOKE(3, "撤回"),
FAILED(10, "发送失败"),
;
private int code;
private String des;
MsgStateEnum(int code, String des) {
this.code = code;
this.des = des;
}
public static String getNameByCode(int code) {
for (IMEnum.MsgStateEnum msgStateEnum : IMEnum.MsgStateEnum.values()) {
if (code == msgStateEnum.getCode()) {
return msgStateEnum.getDes();
}
}
return null;
}
public int getCode() {
return code;
}
public String getDes() {
return des;
}
}
}
package com.wwdz.ch.db.dao;
import com.wwdz.ch.db.domain.ImRecord;
public interface ImRecordDao {
boolean create(ImRecord imRecord);
}
This diff is collapsed.
package com.wwdz.ch.db.impl;
import com.wwdz.ch.db.dao.ImRecordDao;
import com.wwdz.ch.db.domain.ImRecord;
import com.wwdz.ch.db.mapper.ImRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ImRecordDaoImpl implements ImRecordDao {
@Autowired
ImRecordMapper imRecordMapper;
@Override
public boolean create(ImRecord imRecord) {
return imRecordMapper.insert(imRecord) > 0;
}
}
package com.wwdz.ch.db.mapper;
import com.wwdz.ch.db.domain.ImRecord;
import com.wwdz.ch.db.domain.ImRecordExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface ImRecordMapper {
long countByExample(ImRecordExample example);
int deleteByExample(ImRecordExample example);
int deleteByPrimaryKey(Long id);
int insert(ImRecord record);
int insertSelective(ImRecord record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table im_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
ImRecord selectOneByExample(ImRecordExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table im_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
ImRecord selectOneByExampleSelective(@Param("example") ImRecordExample example, @Param("selective") ImRecord.Column ... selective);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table im_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
ImRecord selectOneByExampleWithBLOBs(ImRecordExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table im_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
List<ImRecord> selectByExampleSelective(@Param("example") ImRecordExample example, @Param("selective") ImRecord.Column ... selective);
List<ImRecord> selectByExampleWithBLOBs(ImRecordExample example);
List<ImRecord> selectByExample(ImRecordExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table im_record
*
* @mbg.generated
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
ImRecord selectByPrimaryKeySelective(@Param("id") Long id, @Param("selective") ImRecord.Column ... selective);
ImRecord selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") ImRecord record, @Param("example") ImRecordExample example);
int updateByExampleWithBLOBs(@Param("record") ImRecord record, @Param("example") ImRecordExample example);
int updateByExample(@Param("record") ImRecord record, @Param("example") ImRecordExample example);
int updateByPrimaryKeySelective(ImRecord record);
int updateByPrimaryKeyWithBLOBs(ImRecord record);
int updateByPrimaryKey(ImRecord record);
}
\ No newline at end of file
......@@ -72,7 +72,7 @@
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wwdz.ch.db.mapper"
targetProject="ch-dao/src/main/java"/>
<table tableName="ai_assistant" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<table tableName="im_record" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
......
package com.wwdz.ch.wx.api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/imCallback")
public class IMCallbackController {
}
......@@ -40,6 +40,7 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
.excludePathPatterns("/*.html", "/**/*.html", "/**/*.css", "/**/*.js"
, "/wx/user/loginByWx", "/wx/user/loginByMobile", "/wx/user/loginRegCaptcha", "/wx/user/updateRegCaptcha", "/wx/user/login", "/wx/user/fillCode",
"/officialAccountCallback/**",
"/imCallback/**",
"/wx/officialAccount/**",
"/wx/item/**",
"/wx/aiAssistant/**",
......
package com.wwdz.ch.wx.impl;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.db.dao.ImRecordDao;
import com.wwdz.ch.db.domain.ImRecord;
import com.wwdz.ch.wx.service.ImRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ImRecordServiceImpl implements ImRecordService {
private static final Logger logger = LoggerFactory.getLogger(ImRecordServiceImpl.class);
@Autowired
ImRecordDao imRecordDao;
@Override
public Result create(ImRecord imRecord) {
try {
imRecordDao.create(imRecord);
return Result.success();
} catch (Exception e) {
logger.info("保存聊天消息error : {}", e);
}
return Result.failed();
}
}
......@@ -9,14 +9,19 @@ import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.IMUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.db.dao.AiAssistantDao;
import com.wwdz.ch.db.dao.ImRecordDao;
import com.wwdz.ch.db.domain.AiAssistant;
import com.wwdz.ch.db.domain.ImRecord;
import com.wwdz.ch.wx.api.ChatApi;
import com.wwdz.ch.wx.service.IMService;
import com.wwdz.ch.wx.service.ImRecordService;
import com.xxdxxs.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
......@@ -25,9 +30,9 @@ import java.util.concurrent.TimeUnit;
* 会话
*/
@Service
public class IMServiceImpl implements IMService {
public class ImServiceImpl implements IMService {
private static final Logger logger = LoggerFactory.getLogger(IMServiceImpl.class);
private static final Logger logger = LoggerFactory.getLogger(ImServiceImpl.class);
@Autowired
ConversationApi conversationApi;
......@@ -47,6 +52,9 @@ public class IMServiceImpl implements IMService {
@Autowired
RedisUtils redisUtils;
@Autowired
ImRecordService imRecordService;
/**
* 新增ai会话,发送默认打招呼的消息
* @param dto
......@@ -112,6 +120,25 @@ public class IMServiceImpl implements IMService {
if (!askResult.getSuccess()) {
return askResult;
}
//保存提问的消息记录
ImRecord imRecord = new ImRecord();
imRecord.setFromAccount(dto.getFromAccount());
imRecord.setToAccount(dto.getToAccount());
imRecord.setMsgType(dto.getMsgType());
//0:成功
imRecord.setSendMsgResult(0);
if (IMEnum.MsgTypeEnum.TIMTextElem.getCode() == dto.getMsgType()) {
imRecord.setMsgContent(dto.getMsgContent().toString());
}
Date now = new Date();
imRecord.setMsgTime(now);
imRecord.setOnlineOnlyFlag(0);
imRecord.setCreateTime(now);
imRecord.setUpdateTime(now);
imRecord.setState(IMEnum.MsgStateEnum.READED.getCode());
imRecordService.create(imRecord);
logger.info("=========== userid:{}, 提问ai的消息发送成功 ==========", dto.getFromAccount());
if (dto.getMsgType() == IMEnum.MsgTypeEnum.TIMTextElem.getCode()) {
String question = (String) dto.getMsgContent();
......@@ -134,6 +161,21 @@ public class IMServiceImpl implements IMService {
logger.info("=============== 发送问题的ai答复消息失败 =============");
return answerResult;
}
//保存ai回答的消息记录
ImRecord answerImRecord = new ImRecord();
answerImRecord.setFromAccount(dto.getToAccount());
answerImRecord.setToAccount(dto.getFromAccount());
answerImRecord.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
answerImRecord.setMsgContent(answer);
answerImRecord.setOnlineOnlyFlag(0);
Date answerTime = new Date();
answerImRecord.setSendMsgResult(0);
answerImRecord.setMsgTime(answerTime);
answerImRecord.setCreateTime(answerTime);
answerImRecord.setUpdateTime(answerTime);
answerImRecord.setState(IMEnum.MsgStateEnum.READED.getCode());
imRecordService.create(answerImRecord);
} else {
return Result.failed("目前尚不支持除文本外的消息发送");
}
......
package com.wwdz.ch.wx.service;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.db.domain.ImRecord;
public interface ImRecordService {
Result create(ImRecord imRecord);
}
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