88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
|
|
package dao
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"rag/consts/public"
|
||
|
|
"rag/model/dto"
|
||
|
|
"rag/model/entity"
|
||
|
|
|
||
|
|
"gitea.com/red-future/common/db/gfdb"
|
||
|
|
"github.com/gogf/gf/v2/database/gdb"
|
||
|
|
"github.com/gogf/gf/v2/frame/g"
|
||
|
|
"github.com/gogf/gf/v2/util/gconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
var Document = new(documentDao)
|
||
|
|
|
||
|
|
type documentDao struct{}
|
||
|
|
|
||
|
|
// Insert 插入文件
|
||
|
|
func (d *documentDao) Insert(ctx context.Context, req *dto.CreateDocumentReq) (id int64, err error) {
|
||
|
|
var res *entity.Document
|
||
|
|
if err = gconv.Struct(req, &res); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameDocument).Data(&res).Insert()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.LastInsertId()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update 更新文件
|
||
|
|
func (d *documentDao) Update(ctx context.Context, req *dto.UpdateDocumentReq) (rows int64, err error) {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameDocument).OmitEmpty()
|
||
|
|
if !g.IsEmpty(req.ChunkCount) {
|
||
|
|
model.Data(entity.DocumentCol.ChunkCount, &gdb.Counter{
|
||
|
|
Field: entity.DocumentCol.ChunkCount,
|
||
|
|
Value: gconv.Float64(req.ChunkCount),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
r, err := model.Data(&req).Where(entity.DocumentCol.Id, req.Id).Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete 删除文件
|
||
|
|
func (d *documentDao) Delete(ctx context.Context, req *dto.DeleteDocumentReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameDocument).Where(entity.DocumentCol.Id, req.Id).Delete()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetByID 根据ID获取文件
|
||
|
|
func (d *documentDao) GetByID(ctx context.Context, req *dto.GetDocumentReq, fields ...string) (res *entity.Document, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameDocument).Where(entity.DocumentCol.Id, req.Id).Fields(fields).One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// List 获取文件列表
|
||
|
|
func (d *documentDao) List(ctx context.Context, req *dto.ListDocumentReq, fields ...string) (res []*entity.Document, total int, err error) {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameDocument).OmitEmpty()
|
||
|
|
if !g.IsEmpty(req.Keyword) {
|
||
|
|
model.WhereLike(entity.DocumentCol.Title, "%"+req.Keyword+"%")
|
||
|
|
}
|
||
|
|
model.Where(entity.DocumentCol.DatasetId, req.DatasetId)
|
||
|
|
model.Where(entity.DocumentCol.Status, req.Status)
|
||
|
|
model.Fields(fields)
|
||
|
|
model.OrderDesc(entity.DocumentCol.CreatedAt)
|
||
|
|
if req.Page != nil {
|
||
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||
|
|
}
|
||
|
|
r, total, err := model.AllAndCount(false)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Structs(&res)
|
||
|
|
return
|
||
|
|
}
|