Files
customer-server/dao/scripted_speech_dao.go

93 lines
3.0 KiB
Go

package dao
import (
"context"
"customer-server/consts/public"
"customer-server/model/dto"
"customer-server/model/entity"
"gitea.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/util/gconv"
)
var ScriptedSpeech = new(scriptedSpeech)
type scriptedSpeech struct{}
func (d *scriptedSpeech) Insert(ctx context.Context, req *dto.AddScriptedSpeechReq) (id int64, err error) {
var e *entity.ScriptedSpeech
if err = gconv.Struct(req, &e); err != nil {
return
}
result, err := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Insert(e)
if err != nil {
return
}
return result.LastInsertId()
}
func (d *scriptedSpeech) Update(ctx context.Context, req *dto.UpdateScriptedSpeechReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Data(&req).Where(entity.ScriptedSpeechCol.Id, req.Id).OmitEmpty().Update()
if err != nil {
return
}
return r.RowsAffected()
}
func (d *scriptedSpeech) Delete(ctx context.Context, req *dto.DeleteScriptedSpeechReq) (rows int64, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Where(entity.ScriptedSpeechCol.Id, req.Id).Delete()
if err != nil {
return
}
return r.RowsAffected()
}
func (d *scriptedSpeech) Count(ctx context.Context, req *dto.ListScriptedSpeechReq) (count int, err error) {
count, err = gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).OmitEmpty().
Where(entity.ScriptedSpeechCol.DatasetId, req.DatasetId).
Where(entity.ScriptedSpeechCol.SceneType, req.SceneType).
Count()
return
}
// GetById 根据ID查询预制话术
func (d *scriptedSpeech) GetById(ctx context.Context, req *dto.GetScriptedSpeechReq, fields ...string) (res *entity.ScriptedSpeech, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Where(entity.ScriptedSpeechCol.Id, req.Id).Fields(fields).One()
if err != nil {
return
}
err = r.Struct(&res)
return
}
// GetByDatasetIdAndSceneType 根据数据集ID和场景类型查询预制话术
func (d *scriptedSpeech) GetByDatasetIdAndSceneType(ctx context.Context, req *dto.ListScriptedSpeechReq, fields ...string) (res *entity.ScriptedSpeech, err error) {
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Fields(fields).
Where(entity.ScriptedSpeechCol.DatasetId, req.DatasetId).
Where(entity.ScriptedSpeechCol.SceneType, req.SceneType).
One()
if err != nil {
return
}
err = r.Struct(&res)
return
}
// List 获取预制话术列表
func (d *scriptedSpeech) List(ctx context.Context, req *dto.ListScriptedSpeechReq, fields ...string) (res []*entity.ScriptedSpeech, total int, err error) {
model := gfdb.DB(ctx).Model(ctx, public.TableNameScriptedSpeech).Fields(fields).OmitEmpty()
model.Where(entity.ScriptedSpeechCol.DatasetId, req.DatasetId)
model.Where(entity.ScriptedSpeechCol.SceneType, req.SceneType)
model.OrderDesc(entity.ScriptedSpeechCol.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
}