healthapp
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.
 
 
 
 
 
 

73 lines
1.7 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 SubmitBasicInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSubmitBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitBasicInfoLogic {
return &SubmitBasicInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SubmitBasicInfoLogic) SubmitBasicInfo(req *types.SubmitBasicInfoReq) (resp *types.CommonResp, err error) {
userID, err := GetUserIDFromCtx(l.ctx)
if err != nil {
return nil, errorx.ErrUnauthorized
}
// 计算 BMI
heightM := req.Height / 100
bmi := req.Weight / (heightM * heightM)
profile := model.HealthProfile{
UserID: userID,
Name: req.Name,
Gender: req.Gender,
Height: req.Height,
Weight: req.Weight,
BMI: bmi,
BloodType: req.BloodType,
Occupation: req.Occupation,
MaritalStatus: req.MaritalStatus,
Region: req.Region,
}
if req.BirthDate != "" {
if birthDate, err := time.Parse("2006-01-02", req.BirthDate); err == nil {
profile.BirthDate = &birthDate
}
}
// 检查是否已存在
var existing model.HealthProfile
if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&existing).Error; err == nil {
profile.ID = existing.ID
profile.CreatedAt = existing.CreatedAt
if err := l.svcCtx.DB.Save(&profile).Error; err != nil {
return nil, errorx.ErrServerError
}
} else {
if err := l.svcCtx.DB.Create(&profile).Error; err != nil {
return nil, errorx.ErrServerError
}
}
return &types.CommonResp{Code: 0, Message: "提交成功"}, nil
}