package service import ( "errors" "health-ai/internal/model" "health-ai/internal/repository/impl" ) type HealthService struct { healthRepo *impl.HealthRepository constitutionRepo *impl.ConstitutionRepository userRepo *impl.UserRepositoryImpl } func NewHealthService() *HealthService { return &HealthService{ healthRepo: impl.NewHealthRepository(), constitutionRepo: impl.NewConstitutionRepository(), userRepo: impl.NewUserRepository(), } } // ================= 响应结构体 ================= // HealthProfileResponse 健康档案响应 type HealthProfileResponse struct { Profile *model.HealthProfile `json:"profile"` Lifestyle *model.LifestyleInfo `json:"lifestyle"` MedicalHistory []model.MedicalHistory `json:"medical_history"` FamilyHistory []model.FamilyHistory `json:"family_history"` AllergyRecords []model.AllergyRecord `json:"allergy_records"` Constitution *ConstitutionScore `json:"constitution,omitempty"` } // ================= Service 方法 ================= // GetHealthProfile 获取完整健康档案 func (s *HealthService) GetHealthProfile(userID uint) (*HealthProfileResponse, error) { resp := &HealthProfileResponse{} // 获取基础档案 profile, err := s.healthRepo.GetProfileByUserID(userID) if err == nil && profile.ID > 0 { resp.Profile = profile } // 获取生活习惯 lifestyle, err := s.healthRepo.GetLifestyleByUserID(userID) if err == nil && lifestyle.ID > 0 { resp.Lifestyle = lifestyle } // 获取病史 if profile != nil && profile.ID > 0 { resp.MedicalHistory, _ = s.healthRepo.GetMedicalHistories(profile.ID) resp.FamilyHistory, _ = s.healthRepo.GetFamilyHistories(profile.ID) resp.AllergyRecords, _ = s.healthRepo.GetAllergyRecords(profile.ID) } // 获取最新体质信息 constitution, err := s.constitutionRepo.GetLatestAssessment(userID) if err == nil && constitution.ID > 0 { resp.Constitution = &ConstitutionScore{ Type: constitution.PrimaryConstitution, Name: model.ConstitutionNames[constitution.PrimaryConstitution], Description: model.ConstitutionDescriptions[constitution.PrimaryConstitution], } } return resp, nil } // GetBasicProfile 获取基础档案 func (s *HealthService) GetBasicProfile(userID uint) (*model.HealthProfile, error) { profile, err := s.healthRepo.GetProfileByUserID(userID) if err != nil { return nil, errors.New("健康档案不存在") } return profile, nil } // GetLifestyle 获取生活习惯 func (s *HealthService) GetLifestyle(userID uint) (*model.LifestyleInfo, error) { lifestyle, err := s.healthRepo.GetLifestyleByUserID(userID) if err != nil { return nil, errors.New("生活习惯信息不存在") } return lifestyle, nil } // GetMedicalHistory 获取病史列表 func (s *HealthService) GetMedicalHistory(userID uint) ([]model.MedicalHistory, error) { profile, err := s.healthRepo.GetProfileByUserID(userID) if err != nil { return nil, errors.New("请先填写基础信息") } return s.healthRepo.GetMedicalHistories(profile.ID) } // GetFamilyHistory 获取家族病史列表 func (s *HealthService) GetFamilyHistory(userID uint) ([]model.FamilyHistory, error) { profile, err := s.healthRepo.GetProfileByUserID(userID) if err != nil { return nil, errors.New("请先填写基础信息") } return s.healthRepo.GetFamilyHistories(profile.ID) } // GetAllergyRecords 获取过敏记录列表 func (s *HealthService) GetAllergyRecords(userID uint) ([]model.AllergyRecord, error) { profile, err := s.healthRepo.GetProfileByUserID(userID) if err != nil { return nil, errors.New("请先填写基础信息") } return s.healthRepo.GetAllergyRecords(profile.ID) } // DeleteMedicalHistory 删除病史记录 func (s *HealthService) DeleteMedicalHistory(userID, historyID uint) error { // TODO: 验证记录属于该用户 return s.healthRepo.DeleteMedicalHistory(historyID) } // DeleteFamilyHistory 删除家族病史记录 func (s *HealthService) DeleteFamilyHistory(userID, historyID uint) error { // TODO: 验证记录属于该用户 return s.healthRepo.DeleteFamilyHistory(historyID) } // DeleteAllergyRecord 删除过敏记录 func (s *HealthService) DeleteAllergyRecord(userID, recordID uint) error { // TODO: 验证记录属于该用户 return s.healthRepo.DeleteAllergyRecord(recordID) }