package service import ( "ai-agent/digital-human/consts" "ai-agent/digital-human/dao" "ai-agent/digital-human/model/dto" "context" "encoding/json" "errors" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" ) type digitalHuman struct{} // DigitalHuman 数字人形象服务 var DigitalHuman = new(digitalHuman) // Create 创建数字人形象 func (s *digitalHuman) Create(ctx context.Context, req *dto.CreateDigitalHumanReq) (res *dto.CreateDigitalHumanRes, err error) { count, err := dao.DigitalHuman.Count(ctx, req) if err != nil { return nil, err } if count > 0 { return nil, errors.New("数字人形象名称已存在") } // 插入数据库 ids, err := dao.DigitalHuman.Insert(ctx, req) if err != nil { return nil, err } // PostgreSQL 使用 int64 id := ids[0].(int64) res = &dto.CreateDigitalHumanRes{ Id: id, } return } // List 获取数字人形象列表 func (s *digitalHuman) List(ctx context.Context, req *dto.ListDigitalHumanReq) (res *dto.ListDigitalHumanRes, error error) { digitalHumanList, total, err := dao.DigitalHuman.List(ctx, req) if err != nil { return } res = &dto.ListDigitalHumanRes{ Total: total, } b, err := json.Marshal(digitalHumanList) if err != nil { return } err = json.Unmarshal(b, &res.List) return } // GetOne 获取单个数字人形象 func (s *digitalHuman) GetOne(ctx context.Context, id int64) (*dto.GetDigitalHumanRes, error) { digitalHumanOne, err := dao.DigitalHuman.GetOne(ctx, id) if err != nil { return nil, err } var createdAt, updatedAt *gtime.Time if digitalHumanOne.CreatedAt != nil { createdAt = digitalHumanOne.CreatedAt } if digitalHumanOne.UpdatedAt != nil { updatedAt = digitalHumanOne.UpdatedAt } return &dto.GetDigitalHumanRes{ ID: digitalHumanOne.Id, Name: digitalHumanOne.Name, Description: digitalHumanOne.Description, ImageURL: digitalHumanOne.AvatarURL, VideoURL: digitalHumanOne.VideoURL, Status: digitalHumanOne.Status, Tags: digitalHumanOne.Tags, Gender: digitalHumanOne.Gender, Age: digitalHumanOne.Age, Style: digitalHumanOne.Style, ExternalID: digitalHumanOne.ExternalID, Metadata: digitalHumanOne.Metadata, CreatedAt: createdAt, UpdatedAt: updatedAt, }, nil } // Update 更新数字人形象 func (s *digitalHuman) Update(ctx context.Context, req *dto.UpdateDigitalHumanReq) error { // 先获取原始数字人形象信息 digitalHumanOne, err := dao.DigitalHuman.GetOne(ctx, req.ID) if err != nil { return gerror.Wrap(err, "获取原始数字人形象信息失败") } // 修改字段 if !g.IsEmpty(req.Name) { digitalHumanOne.Name = req.Name } if !g.IsEmpty(req.Description) { digitalHumanOne.Description = req.Description } if !g.IsEmpty(req.ImageURL) { digitalHumanOne.AvatarURL = req.ImageURL } if !g.IsEmpty(req.VideoURL) { digitalHumanOne.VideoURL = req.VideoURL } digitalHumanOne.Status = req.Status if req.Tags != nil { digitalHumanOne.Tags = req.Tags } if !g.IsEmpty(req.Gender) { digitalHumanOne.Gender = req.Gender } if !g.IsEmpty(req.Age) { digitalHumanOne.Age = req.Age } if !g.IsEmpty(req.Style) { digitalHumanOne.Style = req.Style } if !g.IsEmpty(req.ExternalID) { digitalHumanOne.ExternalID = req.ExternalID } if req.Metadata != nil { digitalHumanOne.Metadata = req.Metadata } return dao.DigitalHuman.Update(ctx, req.ID, digitalHumanOne) } // UpdateStatus 更新数字人形象状态 func (s *digitalHuman) UpdateStatus(ctx context.Context, id int64, status consts.DigitalHumanStatus) error { _, err := dao.DigitalHuman.UpdateStatus(ctx, id, status) return err } // Delete 删除数字人形象 func (s *digitalHuman) Delete(ctx context.Context, id int64) error { return dao.DigitalHuman.Delete(ctx, id) } // GetStatusOptions 获取状态选项 func (s *digitalHuman) GetStatusOptions(ctx context.Context, req *dto.GetDigitalHumanStatusOptionsReq) (res *dto.GetDigitalHumanStatusOptionsRes, err error) { _ = ctx _ = req res = new(dto.GetDigitalHumanStatusOptionsRes) res.Options = consts.GetAllStatusKeyValue() return res, nil } // GetGenderOptions 获取性别选项 func (s *digitalHuman) GetGenderOptions(ctx context.Context, req *dto.GetGenderOptionsReq) (res *dto.GetGenderOptionsRes, err error) { _ = ctx _ = req res = new(dto.GetGenderOptionsRes) res.Options = consts.GetAllGenderKeyValue() return res, nil } // GetAgeOptions 获取年龄段选项 func (s *digitalHuman) GetAgeOptions(ctx context.Context, req *dto.GetAgeOptionsReq) (res *dto.GetAgeOptionsRes, err error) { _ = ctx _ = req res = new(dto.GetAgeOptionsRes) res.Options = consts.GetAllAgeKeyValue() return res, nil } // GetStyleOptions 获取风格选项 func (s *digitalHuman) GetStyleOptions(ctx context.Context, req *dto.GetStyleOptionsReq) (res *dto.GetStyleOptionsRes, err error) { _ = ctx _ = req res = new(dto.GetStyleOptionsRes) res.Options = consts.GetAllStyleKeyValue() return res, nil }