package dao import ( "context" "digital-human/digitalhuman/consts" "digital-human/digitalhuman/consts/public" "digital-human/digitalhuman/model/dto" "digital-human/digitalhuman/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" ) // Video 视频数据 var Video = &video{} type video struct{} // Insert 插入视频 func (d *video) Insert(ctx context.Context, req *dto.CreateVideoReq) (ids []any, err error) { var result entity.Video if err = gconv.Struct(req, &result); err != nil { return } r, err := gfdb.DB(ctx).Model(ctx, public.TableNameVideo).Data(&result).Insert() if err != nil { return } lastInsertId, err := r.LastInsertId() if err != nil { return } ids = []any{lastInsertId} return } // Update 更新视频 func (d *video) Update(ctx context.Context, id int64, updateData *entity.Video) (err error) { _, err = gfdb.DB(ctx).Model(ctx, public.TableNameVideo).Data(updateData).Where("id = ?", id).Update() return } // Delete 删除视频 func (d *video) Delete(ctx context.Context, id int64) (err error) { _, err = gfdb.DB(ctx).Model(ctx, public.TableNameVideo).Where("id = ?", id).Delete() return } // GetOne 获取单个视频 func (d *video) GetOne(ctx context.Context, id int64) (video *entity.Video, err error) { r, err := gfdb.DB(ctx).Model(ctx, public.TableNameVideo).Where("id = ?", id).One() if err != nil { return } err = r.Struct(&video) return } // List 获取视频列表 func (d *video) List(ctx context.Context, req *dto.ListVideoReq) (res []entity.Video, 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 *video) buildListFilter(ctx context.Context, req *dto.ListVideoReq) *gdb.Model { model := gfdb.DB(ctx).Model(ctx, public.TableNameVideo).OmitEmpty() // 状态字段允许查询所有状态值,包括0(生成中),所以需要特别处理 if req.Status != consts.VideoStatusGenerating && req.Status != consts.VideoStatusSuccess && req.Status != consts.VideoStatusFailed { // 如果状态不是有效值之一,则不添加状态过滤条件 } else { model = model.Where("status = ?", req.Status) } if !g.IsEmpty(req.DigitalHumanID) { model = model.Where("digitalHumanId = ?", req.DigitalHumanID) } if !g.IsEmpty(req.Keyword) { model = model.Where("name LIKE ? OR description LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%") } return model } // UpdateStatus 更新视频状态 func (d *video) UpdateStatus(ctx context.Context, id int64, status consts.VideoStatus, errorMsg string, videoURL string, duration int, thumbnailURL string, externalTaskID string) (rows int64, err error) { model := gfdb.DB(ctx).Model(ctx, public.TableNameVideo).Where("id = ?", id) updateData := gdb.Map{ "status": status, } if errorMsg != "" { updateData["errorMsg"] = errorMsg } if videoURL != "" { updateData["videoUrl"] = videoURL } if duration > 0 { updateData["duration"] = duration } if thumbnailURL != "" { updateData["thumbnailUrl"] = thumbnailURL } if externalTaskID != "" { updateData["externalTaskId"] = externalTaskID } r, err := model.Data(updateData).Update() if err != nil { return } return r.RowsAffected() }