Files
slogan/styleagent/service/avatar_model_service.go
T
admin 775995f3e8 refactor: 五层对齐 22 表 + 工具并入 agent/ + 支付适配器内联(路由零变更)
- service/dto/controller 各拆 18 文件,一表一文件;跨表编排留在 outfit_generation_task_service
- controller 共享 struct(outfit/member)保 /outfit/*、/member/* 前缀,路由零漂移(28 路径 diff 为空)
- 修复 4 个缺失 g.Meta handler:/user/profile、/avatar/get、/body-measurement/get、/hairstyle/list 从 ALL 收敛为 GET
- 工具包并入 agent/ 单包(weather/imagegen/avatar/scoring),NewCache 泛化 NewTTLCache
- 虎皮椒支付适配器内联 service/payment_order_service.go,member 服务拆 4 表文件
- 冒烟 21 路径全绿 + 客户端 dart analyze 零 error
2026-07-31 15:15:04 +08:00

98 lines
2.8 KiB
Go

package service
import (
"context"
"encoding/json"
"errors"
"slogan-agent/styleagent/agent"
"slogan-agent/styleagent/consts"
"slogan-agent/styleagent/dao"
"slogan-agent/styleagent/model/entity"
)
type avatarService struct{}
var AvatarService = new(avatarService)
// Build 构建化身(v1 同步简化:模板匹配 + 记录,异步管线后续接入)
func (s *avatarService) Build(ctx context.Context, userId int64) (*entity.AvatarModel, error) {
photos, err := dao.UserPhoto.ListByUser(ctx, userId, 0)
if err != nil {
return nil, err
}
var hasHead, hasFull bool
for _, p := range photos {
if p.Type == consts.PhotoTypeHeadshot {
hasHead = true
}
if p.Type >= consts.PhotoTypeFullFront {
hasFull = true
}
}
if !hasHead {
return nil, errors.New("请先上传大头照")
}
if !hasFull {
return nil, errors.New("请先上传全身照")
}
bm, err := dao.BodyMeasurement.GetByUser(ctx, userId)
if err != nil {
return nil, err
}
feature := &agent.FaceFeature{SkinTone: 3, HeightCm: 170, WeightKg: 60}
if bm != nil {
feature = &agent.FaceFeature{SkinTone: bm.SkinTone, HeightCm: bm.Height, WeightKg: bm.Weight}
}
faceId, bodyId, skinIdx := agent.MatchTemplates(feature)
// 已有化身则重建(更新模板索引),否则新建
existing, _ := dao.AvatarModel.GetByUser(ctx, userId)
if existing != nil {
err := dao.AvatarModel.Update(ctx, existing.Id, map[string]any{
"face_template_id": faceId, "body_template_id": bodyId,
"skin_tone_index": skinIdx, "glb_url": agent.PackGlbUrl(faceId, bodyId, skinIdx),
"build_status": consts.AvatarBuildDone, "error": "",
"params_snapshot": mustJSON(map[string]any{
"height_cm": feature.HeightCm, "weight_kg": feature.WeightKg, "skin_tone": skinIdx,
}),
"updated_at": "datetime('now','localtime')",
})
if err != nil {
return nil, err
}
existing.FaceTemplateId = faceId
existing.BodyTemplateId = bodyId
existing.SkinToneIndex = skinIdx
existing.GlbUrl = agent.PackGlbUrl(faceId, bodyId, skinIdx)
existing.BuildStatus = consts.AvatarBuildDone
return existing, nil
}
_, err = dao.AvatarModel.Insert(ctx, &entity.AvatarModel{
UserId: userId,
FaceTemplateId: faceId,
BodyTemplateId: bodyId,
SkinToneIndex: skinIdx,
GlbUrl: agent.PackGlbUrl(faceId, bodyId, skinIdx),
BuildStatus: consts.AvatarBuildDone,
ParamsSnapshot: mustJSON(map[string]any{
"height_cm": feature.HeightCm, "weight_kg": feature.WeightKg, "skin_tone": skinIdx,
}),
})
if err != nil {
return nil, err
}
return dao.AvatarModel.GetByUser(ctx, userId)
}
func (s *avatarService) Get(ctx context.Context, userId int64) (*entity.AvatarModel, error) {
return dao.AvatarModel.GetByUser(ctx, userId)
}
func mustJSON(v any) string {
b, _ := json.Marshal(v)
return string(b)
}