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.
127 lines
4.0 KiB
127 lines
4.0 KiB
package impl
|
|
|
|
import (
|
|
"health-ai/internal/database"
|
|
"health-ai/internal/model"
|
|
)
|
|
|
|
type HealthRepository struct{}
|
|
|
|
func NewHealthRepository() *HealthRepository {
|
|
return &HealthRepository{}
|
|
}
|
|
|
|
// ================= HealthProfile =================
|
|
|
|
func (r *HealthRepository) CreateProfile(profile *model.HealthProfile) error {
|
|
return database.DB.Create(profile).Error
|
|
}
|
|
|
|
func (r *HealthRepository) GetProfileByUserID(userID uint) (*model.HealthProfile, error) {
|
|
var profile model.HealthProfile
|
|
err := database.DB.Where("user_id = ?", userID).First(&profile).Error
|
|
return &profile, err
|
|
}
|
|
|
|
func (r *HealthRepository) UpdateProfile(profile *model.HealthProfile) error {
|
|
return database.DB.Save(profile).Error
|
|
}
|
|
|
|
// ================= LifestyleInfo =================
|
|
|
|
func (r *HealthRepository) CreateLifestyle(lifestyle *model.LifestyleInfo) error {
|
|
return database.DB.Create(lifestyle).Error
|
|
}
|
|
|
|
func (r *HealthRepository) GetLifestyleByUserID(userID uint) (*model.LifestyleInfo, error) {
|
|
var lifestyle model.LifestyleInfo
|
|
err := database.DB.Where("user_id = ?", userID).First(&lifestyle).Error
|
|
return &lifestyle, err
|
|
}
|
|
|
|
func (r *HealthRepository) UpdateLifestyle(lifestyle *model.LifestyleInfo) error {
|
|
return database.DB.Save(lifestyle).Error
|
|
}
|
|
|
|
// ================= MedicalHistory =================
|
|
|
|
func (r *HealthRepository) CreateMedicalHistory(history *model.MedicalHistory) error {
|
|
return database.DB.Create(history).Error
|
|
}
|
|
|
|
func (r *HealthRepository) GetMedicalHistories(profileID uint) ([]model.MedicalHistory, error) {
|
|
var histories []model.MedicalHistory
|
|
err := database.DB.Where("health_profile_id = ?", profileID).Find(&histories).Error
|
|
return histories, err
|
|
}
|
|
|
|
func (r *HealthRepository) DeleteMedicalHistory(id uint) error {
|
|
return database.DB.Delete(&model.MedicalHistory{}, id).Error
|
|
}
|
|
|
|
func (r *HealthRepository) BatchCreateMedicalHistories(histories []model.MedicalHistory) error {
|
|
if len(histories) == 0 {
|
|
return nil
|
|
}
|
|
return database.DB.Create(&histories).Error
|
|
}
|
|
|
|
// ================= FamilyHistory =================
|
|
|
|
func (r *HealthRepository) CreateFamilyHistory(history *model.FamilyHistory) error {
|
|
return database.DB.Create(history).Error
|
|
}
|
|
|
|
func (r *HealthRepository) GetFamilyHistories(profileID uint) ([]model.FamilyHistory, error) {
|
|
var histories []model.FamilyHistory
|
|
err := database.DB.Where("health_profile_id = ?", profileID).Find(&histories).Error
|
|
return histories, err
|
|
}
|
|
|
|
func (r *HealthRepository) DeleteFamilyHistory(id uint) error {
|
|
return database.DB.Delete(&model.FamilyHistory{}, id).Error
|
|
}
|
|
|
|
func (r *HealthRepository) BatchCreateFamilyHistories(histories []model.FamilyHistory) error {
|
|
if len(histories) == 0 {
|
|
return nil
|
|
}
|
|
return database.DB.Create(&histories).Error
|
|
}
|
|
|
|
// ================= AllergyRecord =================
|
|
|
|
func (r *HealthRepository) CreateAllergyRecord(record *model.AllergyRecord) error {
|
|
return database.DB.Create(record).Error
|
|
}
|
|
|
|
func (r *HealthRepository) GetAllergyRecords(profileID uint) ([]model.AllergyRecord, error) {
|
|
var records []model.AllergyRecord
|
|
err := database.DB.Where("health_profile_id = ?", profileID).Find(&records).Error
|
|
return records, err
|
|
}
|
|
|
|
func (r *HealthRepository) DeleteAllergyRecord(id uint) error {
|
|
return database.DB.Delete(&model.AllergyRecord{}, id).Error
|
|
}
|
|
|
|
func (r *HealthRepository) BatchCreateAllergyRecords(records []model.AllergyRecord) error {
|
|
if len(records) == 0 {
|
|
return nil
|
|
}
|
|
return database.DB.Create(&records).Error
|
|
}
|
|
|
|
// ================= 清除旧数据方法 =================
|
|
|
|
func (r *HealthRepository) ClearMedicalHistories(profileID uint) error {
|
|
return database.DB.Where("health_profile_id = ?", profileID).Delete(&model.MedicalHistory{}).Error
|
|
}
|
|
|
|
func (r *HealthRepository) ClearFamilyHistories(profileID uint) error {
|
|
return database.DB.Where("health_profile_id = ?", profileID).Delete(&model.FamilyHistory{}).Error
|
|
}
|
|
|
|
func (r *HealthRepository) ClearAllergyRecords(profileID uint) error {
|
|
return database.DB.Where("health_profile_id = ?", profileID).Delete(&model.AllergyRecord{}).Error
|
|
}
|
|
|