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.
 
 
 
 
 
 

63 lines
1.7 KiB

package logic
import (
"context"
"healthapi/internal/model"
"healthapi/internal/svc"
"healthapi/internal/types"
"healthapi/pkg/errorx"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitLifestyleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSubmitLifestyleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitLifestyleLogic {
return &SubmitLifestyleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SubmitLifestyleLogic) SubmitLifestyle(req *types.SubmitLifestyleReq) (resp *types.CommonResp, err error) {
userID, err := GetUserIDFromCtx(l.ctx)
if err != nil {
return nil, errorx.ErrUnauthorized
}
lifestyle := model.LifestyleInfo{
UserID: userID,
SleepTime: req.SleepTime,
WakeTime: req.WakeTime,
SleepQuality: req.SleepQuality,
MealRegularity: req.MealRegularity,
DietPreference: req.DietPreference,
DailyWaterML: req.DailyWaterML,
ExerciseFrequency: req.ExerciseFrequency,
ExerciseType: req.ExerciseType,
ExerciseDurationMin: req.ExerciseDurationMin,
IsSmoker: req.IsSmoker,
AlcoholFrequency: req.AlcoholFrequency,
}
var existing model.LifestyleInfo
if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&existing).Error; err == nil {
lifestyle.ID = existing.ID
lifestyle.CreatedAt = existing.CreatedAt
if err := l.svcCtx.DB.Save(&lifestyle).Error; err != nil {
return nil, errorx.ErrServerError
}
} else {
if err := l.svcCtx.DB.Create(&lifestyle).Error; err != nil {
return nil, errorx.ErrServerError
}
}
return &types.CommonResp{Code: 0, Message: "提交成功"}, nil
}