|
@@ -0,0 +1,88 @@
|
|
|
|
+package model
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
|
+ "github.com/gogf/gf/v2/os/gctx"
|
|
|
|
+ "github.com/gogf/gf/v2/os/glog"
|
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ Message = &cMessage{
|
|
|
|
+ Arr: make([]*SaveMessage, 0, 100),
|
|
|
|
+ }
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type cMessage struct {
|
|
|
|
+ Arr []*SaveMessage
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type ResHistory struct {
|
|
|
|
+ Id string `json:"id" dc:"信息加密id"`
|
|
|
|
+ Content string `json:"content" dc:"内容"`
|
|
|
|
+ Type int `json:"type" dc:"1:用户提问 2:智能助手回复"`
|
|
|
|
+ Useful int `json:"useful" dc:"1:有用 -1:无用 0:暂无评价"`
|
|
|
|
+ CreateTime time.Time `json:"create_time" dc:"聊天时间"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type SaveMessage struct {
|
|
|
|
+ Content string `json:"content" dc:"内容"`
|
|
|
|
+ Type int `json:"type" dc:"1:用户提问 2:智能助手回复"`
|
|
|
|
+ Useful int `json:"useful" dc:"1:有用 -1:无用 0:暂无评价"`
|
|
|
|
+ Item int `json:"item" dc:"1:常见问题 2:业务意图 3:chatGpt"`
|
|
|
|
+ Refer string `json:"refer" dc:"会话来源地址"`
|
|
|
|
+ PersonId int64 `json:"person_id" dc:"自然人id"`
|
|
|
|
+ CreateTime string `json:"create_time" dc:"时间"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// SaveMessage 保存聊天信息
|
|
|
|
+func (m *cMessage) SaveMessage(msg *SaveMessage) {
|
|
|
|
+ m.Arr = append(m.Arr, msg)
|
|
|
|
+ if len(m.Arr) > 0 {
|
|
|
|
+ tmp, ctx := m.Arr, gctx.New()
|
|
|
|
+ m.Arr = make([]*SaveMessage, 0, 5)
|
|
|
|
+
|
|
|
|
+ val := gconv.Maps(tmp)
|
|
|
|
+ r, err := g.Model("ai_message").Data(val).Insert()
|
|
|
|
+ if err != nil {
|
|
|
|
+ glog.Error(ctx, "插入聊天记录异常,error:%s\ndata:%v", err, val)
|
|
|
|
+ }
|
|
|
|
+ affect, _ := r.RowsAffected()
|
|
|
|
+ if len(tmp) == gconv.Int(affect) {
|
|
|
|
+ glog.Error(ctx, "插入聊天记录异常 共%d条 插入%d条,error:%s\ndata:%v", len(tmp), affect, err, val)
|
|
|
|
+ } else {
|
|
|
|
+ glog.Info(ctx, "插入%d条聊天记录成功", affect)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetMessage 查询聊天信息
|
|
|
|
+func (m *cMessage) GetMessage(userId int64, pageNum, pageSize int) (h []ResHistory, err error) {
|
|
|
|
+ err = g.Model("ai_message").Where("person_id = ?", userId).OrderDesc("create_time").OrderDesc("id").Limit(pageNum, (pageNum+1)*pageSize).Scan(&h)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Evaluate 评价
|
|
|
|
+func (m *cMessage) Evaluate(userId int64, megId string, value int) error {
|
|
|
|
+ res, err := g.Model("ai_message").One("id =? and person_id =? and type=2", megId, userId)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ if res == nil {
|
|
|
|
+ return fmt.Errorf("未找到记录")
|
|
|
|
+ }
|
|
|
|
+ if gconv.Int(res.Map()["useful"]) != 0 {
|
|
|
|
+ return fmt.Errorf("已评价")
|
|
|
|
+ }
|
|
|
|
+ r, _ := g.Model("ai_message").Data(g.Map{"useful": value}).Where("id =? and person_id =? and type=2", megId, userId).Update()
|
|
|
|
+ affect, _ := r.RowsAffected()
|
|
|
|
+ if affect != 1 {
|
|
|
|
+ return fmt.Errorf("评价异常")
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|