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.
106 lines
2.5 KiB
106 lines
2.5 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateHealthProfileLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewUpdateHealthProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHealthProfileLogic {
|
|
return &UpdateHealthProfileLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateHealthProfileLogic) UpdateHealthProfile(req *types.UpdateHealthProfileReq) (resp *types.HealthProfile, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
// 获取或创建健康档案
|
|
var profile model.HealthProfile
|
|
if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&profile).Error; err != nil {
|
|
profile = model.HealthProfile{UserID: userID}
|
|
}
|
|
|
|
// 更新字段
|
|
if req.Name != "" {
|
|
profile.Name = req.Name
|
|
}
|
|
if req.BirthDate != "" {
|
|
if parsedDate, err := time.Parse("2006-01-02", req.BirthDate); err == nil {
|
|
profile.BirthDate = &parsedDate
|
|
}
|
|
}
|
|
if req.Gender != "" {
|
|
profile.Gender = req.Gender
|
|
}
|
|
if req.Height > 0 {
|
|
profile.Height = req.Height
|
|
}
|
|
if req.Weight > 0 {
|
|
profile.Weight = req.Weight
|
|
if profile.Height > 0 {
|
|
heightM := profile.Height / 100
|
|
profile.BMI = profile.Weight / (heightM * heightM)
|
|
}
|
|
}
|
|
if req.BloodType != "" {
|
|
profile.BloodType = req.BloodType
|
|
}
|
|
if req.Occupation != "" {
|
|
profile.Occupation = req.Occupation
|
|
}
|
|
if req.MaritalStatus != "" {
|
|
profile.MaritalStatus = req.MaritalStatus
|
|
}
|
|
if req.Region != "" {
|
|
profile.Region = req.Region
|
|
}
|
|
|
|
// 保存
|
|
if profile.ID == 0 {
|
|
if err := l.svcCtx.DB.Create(&profile).Error; err != nil {
|
|
return nil, errorx.ErrServerError
|
|
}
|
|
} else {
|
|
if err := l.svcCtx.DB.Save(&profile).Error; err != nil {
|
|
return nil, errorx.ErrServerError
|
|
}
|
|
}
|
|
|
|
birthDate := ""
|
|
if profile.BirthDate != nil {
|
|
birthDate = profile.BirthDate.Format("2006-01-02")
|
|
}
|
|
|
|
return &types.HealthProfile{
|
|
ID: uint(profile.ID),
|
|
UserID: profile.UserID,
|
|
Name: profile.Name,
|
|
BirthDate: birthDate,
|
|
Gender: profile.Gender,
|
|
Height: profile.Height,
|
|
Weight: profile.Weight,
|
|
BMI: profile.BMI,
|
|
BloodType: profile.BloodType,
|
|
Occupation: profile.Occupation,
|
|
MaritalStatus: profile.MaritalStatus,
|
|
Region: profile.Region,
|
|
}, nil
|
|
}
|
|
|