95 lines
2.7 KiB
Go
95 lines
2.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"rag-local/kb/consts"
|
|
"rag-local/kb/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
var Conversation = &conversationDao{}
|
|
|
|
type conversationDao struct{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := g.DB(consts.DbGroupChat).Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameConversation+` (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
dataset_id INTEGER NOT NULL DEFAULT 0,
|
|
title TEXT NOT NULL DEFAULT '',
|
|
created_at DATETIME DEFAULT (datetime('now','localtime')),
|
|
updated_at DATETIME DEFAULT (datetime('now','localtime'))
|
|
)`)
|
|
if err != nil {
|
|
g.Log().Warningf(ctx, "create chat_conversation table failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func (d *conversationDao) GetOne(ctx context.Context, id int64) (*entity.Conversation, error) {
|
|
var m entity.Conversation
|
|
err := g.DB(consts.DbGroupChat).Model(consts.TableNameConversation).Ctx(ctx).Where("id", id).Scan(&m)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
func (d *conversationDao) List(ctx context.Context) ([]*entity.Conversation, error) {
|
|
var list []*entity.Conversation
|
|
err := g.DB(consts.DbGroupChat).Model(consts.TableNameConversation).Ctx(ctx).OrderDesc("id").Scan(&list)
|
|
return list, err
|
|
}
|
|
|
|
func (d *conversationDao) Insert(ctx context.Context, datasetId int64, title string) (int64, error) {
|
|
now := gtime.Now().Format("Y-m-d H:i:s")
|
|
r, err := g.DB(consts.DbGroupChat).Model(consts.TableNameConversation).Ctx(ctx).Data(g.Map{
|
|
"dataset_id": datasetId,
|
|
"title": title,
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
}).Insert()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
func (d *conversationDao) UpdateTitle(ctx context.Context, id int64, title string) error {
|
|
_, err := g.DB(consts.DbGroupChat).Model(consts.TableNameConversation).Ctx(ctx).Data(g.Map{
|
|
"title": title,
|
|
"updated_at": gtime.Now().Format("Y-m-d H:i:s"),
|
|
}).Where("id", id).Update()
|
|
return err
|
|
}
|
|
|
|
func (d *conversationDao) UpdateDataset(ctx context.Context, id, datasetId int64) error {
|
|
_, err := g.DB(consts.DbGroupChat).Model(consts.TableNameConversation).Ctx(ctx).Data(g.Map{
|
|
"dataset_id": datasetId,
|
|
"updated_at": gtime.Now().Format("Y-m-d H:i:s"),
|
|
}).Where("id", id).Update()
|
|
return err
|
|
}
|
|
|
|
func (d *conversationDao) Delete(ctx context.Context, id int64) error {
|
|
tx, err := g.DB(consts.DbGroupChat).Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
if _, err := tx.Model(consts.TableNameMessage).Ctx(ctx).Where("conversation_id", id).Delete(); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Model(consts.TableNameConversation).Ctx(ctx).Where("id", id).Delete(); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|