package logic import ( "context" "healthapi/internal/model" "healthapi/internal/svc" "healthapi/internal/types" "healthapi/pkg/errorx" "github.com/zeromicro/go-zero/core/logx" ) type GetLifestyleLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetLifestyleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLifestyleLogic { return &GetLifestyleLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetLifestyleLogic) GetLifestyle() (resp *types.LifestyleInfo, err error) { userID, err := GetUserIDFromCtx(l.ctx) if err != nil { return nil, errorx.ErrUnauthorized } var lifestyle model.LifestyleInfo if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&lifestyle).Error; err != nil { return nil, errorx.NewCodeError(errorx.CodeNotFound, "生活习惯信息不存在") } return &types.LifestyleInfo{ ID: uint(lifestyle.ID), UserID: lifestyle.UserID, SleepTime: lifestyle.SleepTime, WakeTime: lifestyle.WakeTime, SleepQuality: lifestyle.SleepQuality, MealRegularity: lifestyle.MealRegularity, DietPreference: lifestyle.DietPreference, DailyWaterML: lifestyle.DailyWaterML, ExerciseFrequency: lifestyle.ExerciseFrequency, ExerciseType: lifestyle.ExerciseType, ExerciseDurationMin: lifestyle.ExerciseDurationMin, IsSmoker: lifestyle.IsSmoker, AlcoholFrequency: lifestyle.AlcoholFrequency, }, nil }