package logic import ( "context" "healthapi/internal/model" "healthapi/internal/svc" "healthapi/internal/types" "healthapi/pkg/errorx" "github.com/zeromicro/go-zero/core/logx" ) type GetMedicalHistoryLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetMedicalHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMedicalHistoryLogic { return &GetMedicalHistoryLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetMedicalHistoryLogic) GetMedicalHistory() (resp []types.MedicalHistory, err error) { userID, err := GetUserIDFromCtx(l.ctx) if err != nil { return nil, errorx.ErrUnauthorized } var profile model.HealthProfile if err := l.svcCtx.DB.Where("user_id = ?", userID).First(&profile).Error; err != nil { return nil, errorx.NewCodeError(errorx.CodeNotFound, "请先填写基础信息") } var histories []model.MedicalHistory l.svcCtx.DB.Where("health_profile_id = ?", profile.ID).Find(&histories) resp = make([]types.MedicalHistory, 0, len(histories)) for _, h := range histories { resp = append(resp, types.MedicalHistory{ ID: uint(h.ID), HealthProfileID: h.HealthProfileID, DiseaseName: h.DiseaseName, DiseaseType: h.DiseaseType, DiagnosedDate: h.DiagnosedDate, Status: h.Status, Notes: h.Notes, }) } return resp, nil }