97 lines
2.8 KiB
Go
97 lines
2.8 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/frame/g"
|
||
|
|
"github.com/gogf/gf/v2/util/gconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
var Keyword = new(keywordDao)
|
||
|
|
|
||
|
|
type keywordDao struct{}
|
||
|
|
|
||
|
|
func (d *keywordDao) Insert(ctx context.Context, req *dto.CreateKeywordReq) (id int64, err error) {
|
||
|
|
var res *entity.Keyword
|
||
|
|
if err = gconv.Struct(req, &res); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).Data(&res).Insert()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.LastInsertId()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) BatchSaveOrUpdate(ctx context.Context, req []*dto.CreateKeywordReq) (rows int64, err error) {
|
||
|
|
var res []*entity.Keyword
|
||
|
|
if err = gconv.Structs(req, &res); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).Data(&res).OnConflict(
|
||
|
|
entity.KeywordCol.TenantId,
|
||
|
|
entity.KeywordCol.DatasetId,
|
||
|
|
entity.KeywordCol.DocumentId,
|
||
|
|
entity.KeywordCol.Word).Save()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) Update(ctx context.Context, req *dto.UpdateKeywordReq) (rows int64, err error) {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword)
|
||
|
|
r, err := model.Data(&req).Where(entity.KeywordCol.Id, req.Id).Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) Delete(ctx context.Context, req *dto.DeleteKeywordReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).Where(entity.KeywordCol.Id, req.Id).Delete()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) Count(ctx context.Context, req *dto.ListKeywordReq) (count int, err error) {
|
||
|
|
count, err = gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).OmitEmpty().
|
||
|
|
Where(entity.KeywordCol.DatasetId, req.DatasetId).
|
||
|
|
Where(entity.KeywordCol.DocumentId, req.DocumentId).
|
||
|
|
Where(entity.KeywordCol.Word, req.Word).Count()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) GetByID(ctx context.Context, req *dto.GetKeywordReq, fields ...string) (res *entity.Document, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).Where(entity.KeywordCol.Id, req.Id).Fields(fields).One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
func (d *keywordDao) List(ctx context.Context, req *dto.ListKeywordReq, fields ...string) (res []*entity.Keyword, total int, err error) {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameKeyword).Fields(fields).OmitEmpty()
|
||
|
|
if !g.IsEmpty(req.Keyword) {
|
||
|
|
model.WhereLike(entity.KeywordCol.Word, "%"+req.Keyword+"%")
|
||
|
|
}
|
||
|
|
model.OrderDesc(entity.KeywordCol.Weight)
|
||
|
|
model.OrderDesc(entity.KeywordCol.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
|
||
|
|
}
|