Files
digital-psychology/apps/api/internal/service/scale/service.go
T
jackyu66gitandCursor bd22d9dddd feat: P1 合盘/星座/问答与测测完整设计包及模拟器取证工具
落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 11:37:53 +08:00

101 lines
3.8 KiB
Go

package scale
import (
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
sc "github.com/yuxingu/digital-psychology/apps/api/internal/scale"
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
)
// Service serves 探索测试.
type Service struct {
Repo *repository.ScaleRepo
Profiles *repository.ProfileRepo
}
// List returns published scales.
func (s *Service) List(ctx context.Context) ([]repository.ScaleListItem, error) {
return s.Repo.ListPublished(ctx)
}
// Get returns scale detail.
func (s *Service) Get(ctx context.Context, slug string) (*repository.ScaleDetail, error) {
d, err := s.Repo.GetBySlug(ctx, slug)
if err != nil {
return nil, errors.New("scale not found")
}
return d, nil
}
// SubmitInput answers map question_id -> option key.
type SubmitInput struct {
ProfileID uuid.UUID
Answers map[string]string
}
// SubmitResult is exploration result.
type SubmitResult struct {
ID uuid.UUID `json:"id"`
Result map[string]interface{} `json:"result"`
}
// Submit scores a simple majority style.
func (s *Service) Submit(ctx context.Context, userID uuid.UUID, slug string, in SubmitInput) (*SubmitResult, error) {
if _, err := s.Profiles.GetForUser(ctx, userID, in.ProfileID); err != nil {
return nil, errors.New("profile not found")
}
scaleID, err := s.Repo.ScaleIDBySlug(ctx, slug)
if err != nil {
return nil, errors.New("scale not found")
}
labels, _, _ := labelsForSlug(slug)
key, label := sc.ScoreMajority(in.Answers, labels, "平衡探索型")
result := sc.BuildResult(slug, key, label)
ansJSON, _ := json.Marshal(in.Answers)
resJSON, _ := json.Marshal(result)
id, err := s.Repo.SaveResult(ctx, userID, scaleID, in.ProfileID, ansJSON, resJSON)
if err != nil {
return nil, err
}
return &SubmitResult{ID: id, Result: result}, nil
}
func labelsForSlug(slug string) (labels map[string]string, sharePrefix, summary string) {
switch slug {
case "emotion-pattern":
return sc.EmotionLabels(),
"我的情感模式:",
"这是你当前情绪调节偏好的探索结果,可用于自我了解与日常调整,不是固定标签。"
case "mbti-lite":
return map[string]string{"E": "外向充能型", "I": "内向充能型", "T": "理性决策型", "F": "感受决策型", "J": "结构安排型", "P": "弹性探索型"},
"我的人格偏好:", "轻量人格偏好探索,不是固定分类。"
case "enneagram-lite":
return map[string]string{"A": "尽责驱动型", "B": "联结驱动型", "C": "独立驱动型"},
"我的动机模式:", "内在动机轻量探索,用于自我觉察。"
case "bigfive-lite":
return map[string]string{"O": "开放探索型", "C": "条理稳健型", "E": "主动外展型", "A": "温和协作型", "N": "细腻敏感型"},
"我的性格五维:", "稳定特质速览,可随情境变化。"
case "love-style":
return map[string]string{"A": "稳定陪伴型", "B": "深度共鸣型", "C": "空间尊重型"},
"我的亲密互动:", "亲密关系偏好探索,不是关系定论。"
case "eq-lite":
return map[string]string{"A": "觉察表达型", "B": "行动调节型", "C": "缓慢识别型"},
"我的情绪觉察:", "情绪习惯探索,可用于日常调节。"
case "stress-index":
return map[string]string{"A": "节奏稳定型", "B": "波动调节型", "C": "高负荷需休息型"},
"我的压力负荷:", "近期压力与恢复偏好,不是诊断。"
case "career-interest":
return map[string]string{"A": "创造表达型", "B": "助人支持型", "C": "分析解决型"},
"我的职业兴趣:", "工作动力偏好探索,可作方向参考。"
default:
return sc.CommunicationLabels(),
"我的沟通方式:",
"这是你当前沟通偏好的探索结果,可用于自我了解与关系理解,不是固定标签。"
}
}