|
@@ -1,6 +1,7 @@
|
|
package model
|
|
package model
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "context"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
@@ -11,12 +12,12 @@ import (
|
|
|
|
|
|
var (
|
|
var (
|
|
ChatHistroy = &cChatHistroy{
|
|
ChatHistroy = &cChatHistroy{
|
|
- Arr: make([]*SaveMessage, 0, 100),
|
|
|
|
|
|
+ Arr: make([]*ChatRecord, 0, 100),
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
|
|
type cChatHistroy struct {
|
|
type cChatHistroy struct {
|
|
- Arr []*SaveMessage
|
|
|
|
|
|
+ Arr []*ChatRecord
|
|
}
|
|
}
|
|
|
|
|
|
type ResHistory struct {
|
|
type ResHistory struct {
|
|
@@ -27,7 +28,7 @@ type ResHistory struct {
|
|
CreateTime time.Time `json:"create_time" dc:"聊天时间"`
|
|
CreateTime time.Time `json:"create_time" dc:"聊天时间"`
|
|
}
|
|
}
|
|
|
|
|
|
-type SaveMessage struct {
|
|
|
|
|
|
+type ChatRecord struct {
|
|
Content string `json:"content" dc:"内容"`
|
|
Content string `json:"content" dc:"内容"`
|
|
Type int `json:"type" dc:"1:用户提问 2:智能助手回复"`
|
|
Type int `json:"type" dc:"1:用户提问 2:智能助手回复"`
|
|
Useful int `json:"useful" dc:"1:有用 -1:无用 0:暂无评价"`
|
|
Useful int `json:"useful" dc:"1:有用 -1:无用 0:暂无评价"`
|
|
@@ -37,12 +38,12 @@ type SaveMessage struct {
|
|
CreateTime string `json:"create_time" dc:"时间"`
|
|
CreateTime string `json:"create_time" dc:"时间"`
|
|
}
|
|
}
|
|
|
|
|
|
-// SaveMessage 保存聊天信息
|
|
|
|
-func (m *cChatHistroy) SaveMessage(msg *SaveMessage) {
|
|
|
|
|
|
+// CacheSave 保存聊天信息
|
|
|
|
+func (m *cChatHistroy) CacheSave(msg *ChatRecord) {
|
|
m.Arr = append(m.Arr, msg)
|
|
m.Arr = append(m.Arr, msg)
|
|
- if len(m.Arr) > 0 {
|
|
|
|
|
|
+ if len(m.Arr) > 100 {
|
|
tmp, ctx := m.Arr, gctx.New()
|
|
tmp, ctx := m.Arr, gctx.New()
|
|
- m.Arr = make([]*SaveMessage, 0, 5)
|
|
|
|
|
|
+ m.Arr = make([]*ChatRecord, 0, 5)
|
|
|
|
|
|
val := gconv.Maps(tmp)
|
|
val := gconv.Maps(tmp)
|
|
r, err := g.Model("ai_message_history").Data(val).Insert()
|
|
r, err := g.Model("ai_message_history").Data(val).Insert()
|
|
@@ -60,6 +61,22 @@ func (m *cChatHistroy) SaveMessage(msg *SaveMessage) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Save 保存聊天记录 返回最后一条记录id
|
|
|
|
+func (m *cChatHistroy) Save(ctx context.Context, msgs ...*ChatRecord) (id int64) {
|
|
|
|
+ val := gconv.Maps(msgs)
|
|
|
|
+ r, err := g.Model("ai_message_history").Data(val).Insert()
|
|
|
|
+ if err != nil {
|
|
|
|
+ glog.Error(ctx, "插入聊天记录异常,error:%s\ndata:%v", err, val)
|
|
|
|
+ g.Dump(val)
|
|
|
|
+ } else {
|
|
|
|
+ if id, err = r.LastInsertId(); err != nil {
|
|
|
|
+ glog.Error(ctx, "插入聊天记录异常")
|
|
|
|
+ g.Dump(val)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
// GetMessage 查询聊天信息
|
|
// GetMessage 查询聊天信息
|
|
func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int) (h []ResHistory, err error) {
|
|
func (m *cChatHistroy) GetMessage(userId int64, pageNum, pageSize int) (h []ResHistory, err error) {
|
|
err = g.Model("ai_message_history").Where("person_id = ?", userId).OrderDesc("create_time").OrderDesc("id").Limit(pageNum, (pageNum+1)*pageSize).Scan(&h)
|
|
err = g.Model("ai_message_history").Where("person_id = ?", userId).OrderDesc("create_time").OrderDesc("id").Limit(pageNum, (pageNum+1)*pageSize).Scan(&h)
|