Files
prompts-core/dao/prompt_dao.go
2026-05-12 13:59:15 +08:00

98 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package dao
import (
"context"
"prompts-core/consts/public"
"prompts-core/model/entity"
"gitea.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/util/gconv"
)
var Prompt = &promptDao{}
type promptDao struct{}
func (d *promptDao) Insert(ctx context.Context, m *entity.PromptConfig) (id int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).Data(m).Insert()
if err != nil {
return 0, err
}
return r.LastInsertId()
}
func (d *promptDao) UpdateByID(ctx context.Context, id int64, data map[string]any) (rows int64, err error) {
// 触发 gfdb 的 updateHook 自动填充 updater需要显式带 updater 字段
data[entity.PromptConfigCol.Updater] = ""
r, err := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).
Where(entity.PromptConfigCol.Id, id).
Data(data).
Update()
if err != nil {
return 0, err
}
return r.RowsAffected()
}
func (d *promptDao) DeleteByID(ctx context.Context, id int64) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).
Where(entity.PromptConfigCol.Id, id).
Delete()
if err != nil {
return 0, err
}
return r.RowsAffected()
}
func (d *promptDao) GetByID(ctx context.Context, id int64) (m *entity.PromptConfig, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).
Where(entity.PromptConfigCol.Id, id).
One()
if err != nil {
return nil, err
}
if r.IsEmpty() {
return nil, nil
}
err = r.Struct(&m)
return
}
func (d *promptDao) GetLatestEnabledByModelTypeID(ctx context.Context, modelTypeID int) (m *entity.PromptConfig, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).
Where("deleted_at IS NULL").
Where(entity.PromptConfigCol.ModelTypeId, modelTypeID).
Where(entity.PromptConfigCol.Enabled, 1).
OrderDesc(entity.PromptConfigCol.CreatedAt).
One()
if err != nil {
return nil, err
}
if r.IsEmpty() {
return nil, nil
}
err = r.Struct(&m)
return
}
func (d *promptDao) List(ctx context.Context, pageNum, pageSize int, modelTypeID *int, modelTypeLike string) (list []*entity.PromptConfig, total int64, err error) {
model := gfdb.DB(ctx).Model(ctx, public.TableNamePromptConfig).Where("deleted_at IS NULL").OrderDesc(entity.PromptConfigCol.CreatedAt)
if modelTypeID != nil && *modelTypeID > 0 {
model = model.Where(entity.PromptConfigCol.ModelTypeId, *modelTypeID)
}
if modelTypeLike != "" {
model = model.WhereLike(entity.PromptConfigCol.ModelType, "%"+modelTypeLike+"%")
}
if pageNum > 0 && pageSize > 0 {
model = model.Page(pageNum, pageSize)
}
r, totalInt, err := model.AllAndCount(false)
if err != nil {
return nil, 0, err
}
total = gconv.Int64(totalInt)
err = r.Structs(&list)
return
}