package logic import ( "context" "healthapi/internal/model" "healthapi/internal/svc" "healthapi/internal/types" "healthapi/pkg/errorx" "github.com/zeromicro/go-zero/core/logx" ) type BatchSubmitAllergyLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewBatchSubmitAllergyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchSubmitAllergyLogic { return &BatchSubmitAllergyLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *BatchSubmitAllergyLogic) BatchSubmitAllergy(req *types.BatchAllergyReq) (resp *types.CommonResp, 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.CodeBadRequest, "请先填写基础信息") } // 清除旧数据 l.svcCtx.DB.Where("health_profile_id = ?", profile.ID).Delete(&model.AllergyRecord{}) // 创建新数据 if len(req.Items) > 0 { records := make([]model.AllergyRecord, len(req.Items)) for i, a := range req.Items { records[i] = model.AllergyRecord{ HealthProfileID: profile.ID, AllergyType: a.AllergyType, Allergen: a.Allergen, Severity: a.Severity, ReactionDesc: a.ReactionDesc, } } if err := l.svcCtx.DB.Create(&records).Error; err != nil { return nil, errorx.ErrServerError } } return &types.CommonResp{Code: 0, Message: "提交成功"}, nil }