package logic import ( "context" "healthapi/internal/model" "healthapi/internal/svc" "healthapi/internal/types" "healthapi/pkg/errorx" "github.com/zeromicro/go-zero/core/logx" ) type GetHealthProfileLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetHealthProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetHealthProfileLogic { return &GetHealthProfileLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetHealthProfileLogic) GetHealthProfile() (resp *types.FullHealthProfileResp, err error) { userID, err := GetUserIDFromCtx(l.ctx) if err != nil { return nil, errorx.ErrUnauthorized } resp = &types.FullHealthProfileResp{} // 获取基础档案 var profile model.HealthProfile if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&profile).Error; err == nil { resp.Profile = types.HealthProfile{ ID: uint(profile.ID), UserID: profile.UserID, Name: profile.Name, Gender: profile.Gender, Height: profile.Height, Weight: profile.Weight, BMI: profile.BMI, BloodType: profile.BloodType, Occupation: profile.Occupation, MaritalStatus: profile.MaritalStatus, Region: profile.Region, } if profile.BirthDate != nil { resp.Profile.BirthDate = profile.BirthDate.Format("2006-01-02") } // 获取病史 var medicalHistories []model.MedicalHistory l.svcCtx.DB.Where("health_profile_id = ?", profile.ID).Find(&medicalHistories) for _, mh := range medicalHistories { resp.MedicalHistory = append(resp.MedicalHistory, types.MedicalHistory{ ID: uint(mh.ID), HealthProfileID: mh.HealthProfileID, DiseaseName: mh.DiseaseName, DiseaseType: mh.DiseaseType, DiagnosedDate: mh.DiagnosedDate, Status: mh.Status, Notes: mh.Notes, }) } // 获取家族病史 var familyHistories []model.FamilyHistory l.svcCtx.DB.Where("health_profile_id = ?", profile.ID).Find(&familyHistories) for _, fh := range familyHistories { resp.FamilyHistory = append(resp.FamilyHistory, types.FamilyHistory{ ID: uint(fh.ID), HealthProfileID: fh.HealthProfileID, Relation: fh.Relation, DiseaseName: fh.DiseaseName, Notes: fh.Notes, }) } // 获取过敏记录 var allergyRecords []model.AllergyRecord l.svcCtx.DB.Where("health_profile_id = ?", profile.ID).Find(&allergyRecords) for _, ar := range allergyRecords { resp.AllergyRecords = append(resp.AllergyRecords, types.AllergyRecord{ ID: uint(ar.ID), HealthProfileID: ar.HealthProfileID, AllergyType: ar.AllergyType, Allergen: ar.Allergen, Severity: ar.Severity, ReactionDesc: ar.ReactionDesc, }) } } // 获取生活习惯 var lifestyle model.LifestyleInfo if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&lifestyle).Error; err == nil { resp.Lifestyle = 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, } } return resp, nil }