126 lines
3.7 KiB
Go
126 lines
3.7 KiB
Go
package dao
|
||
|
||
import (
|
||
"ai-agent/digital-human/consts"
|
||
"ai-agent/digital-human/consts/public"
|
||
"ai-agent/digital-human/model/dto"
|
||
"ai-agent/digital-human/model/entity"
|
||
"context"
|
||
|
||
"gitea.redpowerfuture.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"
|
||
)
|
||
|
||
// DigitalHuman 数字人形象数据
|
||
var DigitalHuman = &digitalHuman{}
|
||
|
||
type digitalHuman struct{}
|
||
|
||
// Insert 插入数字人形象
|
||
func (d *digitalHuman) Insert(ctx context.Context, req *dto.CreateDigitalHumanReq) (ids []any, err error) {
|
||
var result entity.DigitalHuman
|
||
if err = gconv.Struct(req, &result); err != nil {
|
||
return
|
||
}
|
||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Data(&result).Insert()
|
||
if err != nil {
|
||
return
|
||
}
|
||
lastInsertId, err := r.LastInsertId()
|
||
if err != nil {
|
||
return
|
||
}
|
||
ids = []any{lastInsertId}
|
||
return
|
||
}
|
||
|
||
// Update 更新数字人形象
|
||
func (d *digitalHuman) Update(ctx context.Context, id int64, updateData *entity.DigitalHuman) (err error) {
|
||
_, err = gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Data(updateData).Where("id = ?", id).Update()
|
||
return
|
||
}
|
||
|
||
// UpdateStatus 更新数字人形象状态
|
||
func (d *digitalHuman) UpdateStatus(ctx context.Context, id int64, status consts.DigitalHumanStatus) (rows int64, err error) {
|
||
model := gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Where("id = ?", id)
|
||
r, err := model.Data(g.Map{"status": status}).Update()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
// Delete 删除数字人形象
|
||
func (d *digitalHuman) Delete(ctx context.Context, id int64) (err error) {
|
||
_, err = gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Where("id = ?", id).Delete()
|
||
return
|
||
}
|
||
|
||
// GetOne 获取单个数字人形象
|
||
func (d *digitalHuman) GetOne(ctx context.Context, id int64) (digitalHuman *entity.DigitalHuman, err error) {
|
||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Where("id = ?", id).One()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Struct(&digitalHuman)
|
||
return
|
||
}
|
||
|
||
// List 获取数字人形象列表
|
||
func (d *digitalHuman) List(ctx context.Context, req *dto.ListDigitalHumanReq) (res []entity.DigitalHuman, total int64, err error) {
|
||
model := d.buildListFilter(ctx, req)
|
||
|
||
var totalCount int
|
||
totalCount, err = model.Count()
|
||
if err != nil {
|
||
return
|
||
}
|
||
total = gconv.Int64(totalCount)
|
||
|
||
if req.Page != nil {
|
||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||
}
|
||
|
||
r, err := model.OrderDesc("id").All()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Structs(&res)
|
||
return
|
||
}
|
||
|
||
// buildListFilter 构建列表查询的过滤条件
|
||
func (d *digitalHuman) buildListFilter(ctx context.Context, req *dto.ListDigitalHumanReq) *gdb.Model {
|
||
model := gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).OmitEmpty()
|
||
// 状态字段允许查询所有状态值,包括0(停用),所以需要特别处理
|
||
if req.Status != consts.DigitalHumanStatusInactive && req.Status != consts.DigitalHumanStatusActive {
|
||
// 如果状态不是有效值之一,则不添加状态过滤条件
|
||
} else {
|
||
model = model.Where("status = ?", req.Status)
|
||
}
|
||
if !g.IsEmpty(req.Gender) {
|
||
model = model.Where("gender = ?", req.Gender)
|
||
}
|
||
if !g.IsEmpty(req.Style) {
|
||
model = model.Where("style = ?", req.Style)
|
||
}
|
||
if !g.IsEmpty(req.Keyword) {
|
||
model = model.Where("name LIKE ? OR description LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||
}
|
||
|
||
return model
|
||
}
|
||
|
||
// Count 计数
|
||
func (d *digitalHuman) Count(ctx context.Context, req *dto.CreateDigitalHumanReq) (count int64, err error) {
|
||
var totalCount int
|
||
totalCount, err = gfdb.DB(ctx).Model(ctx, public.TableNameDigitalHuman).Where("name = ?", req.Name).Count()
|
||
if err != nil {
|
||
return
|
||
}
|
||
count = gconv.Int64(totalCount)
|
||
return
|
||
}
|