You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.7 KiB
61 lines
1.7 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetAssessmentResultLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetAssessmentResultLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAssessmentResultLogic {
|
|
return &GetAssessmentResultLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetAssessmentResultLogic) GetAssessmentResult() (resp *types.AssessmentResult, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var assessment model.ConstitutionAssessment
|
|
if err := l.svcCtx.DB.Where("user_id = ?", userID).Order("assessed_at DESC").First(&assessment).Error; err != nil {
|
|
return nil, errorx.NewCodeError(errorx.CodeNotFound, "暂无体质测评记录")
|
|
}
|
|
|
|
return parseAssessment(&assessment), nil
|
|
}
|
|
|
|
func parseAssessment(assessment *model.ConstitutionAssessment) *types.AssessmentResult {
|
|
var scores map[string]float64
|
|
var secondaryTypes []string
|
|
var recommendations map[string]string
|
|
|
|
json.Unmarshal([]byte(assessment.Scores), &scores)
|
|
json.Unmarshal([]byte(assessment.SecondaryConstitutions), &secondaryTypes)
|
|
json.Unmarshal([]byte(assessment.Recommendations), &recommendations)
|
|
|
|
return &types.AssessmentResult{
|
|
ID: uint(assessment.ID),
|
|
AssessedAt: assessment.AssessedAt.Format(time.RFC3339),
|
|
Scores: scores,
|
|
PrimaryConstitution: assessment.PrimaryConstitution,
|
|
SecondaryConstitutions: secondaryTypes,
|
|
Recommendations: recommendations,
|
|
}
|
|
}
|
|
|