diff --git a/internal/service/welfare_activity/activity.go b/internal/service/welfare_activity/activity.go index c7f19be..b99449d 100644 --- a/internal/service/welfare_activity/activity.go +++ b/internal/service/welfare_activity/activity.go @@ -123,12 +123,16 @@ func (s *service) ListActivities(ctx context.Context, req ListActivitiesRequest) if req.PageSize > 100 { req.PageSize = 100 } + now := time.Now() db := s.repo.GetDbR().WithContext(ctx).Model(&Activity{}).Where("deleted_at IS NULL") if req.Type != "" { db = db.Where("type = ?", req.Type) } if req.Status != "" { db = db.Where("status = ?", req.Status) + if req.Status == StatusActive { + db = db.Where("start_time <= ? AND end_time >= ? AND draw_time >= ?", now, now, now) + } } if strings.TrimSpace(req.Title) != "" { db = db.Where("title LIKE ?", "%"+strings.TrimSpace(req.Title)+"%") @@ -157,6 +161,9 @@ func (s *service) GetActivity(ctx context.Context, id int64, userID int64) (*Act if err := s.repo.GetDbR().WithContext(ctx).Where("id = ? AND deleted_at IS NULL", id).First(&item).Error; err != nil { return nil, err } + if userID <= 0 && item.Status == StatusActive && time.Now().Before(item.StartTime) { + return nil, errors.New("活动未开始") + } detail := &ActivityDetail{Activity: item} s.repo.GetDbR().WithContext(ctx).Where("activity_id = ?", id).Order("sort ASC, id ASC").Find(&detail.Prizes) s.fillPrizeMeta(ctx, detail.Prizes) @@ -172,7 +179,7 @@ func (s *service) GetActivity(ctx context.Context, id int64, userID int64) (*Act if userID > 0 { start, end, period := periodRange(item.Type, time.Now()) detail.CurrentPaid, _ = s.sumPaidAmount(ctx, userID, start, end) - detail.CanJoin = detail.CurrentPaid >= item.ThresholdAmount && item.Status == StatusActive && time.Now().Before(item.DrawTime) && time.Now().Before(item.EndTime) + detail.CanJoin = isJoinWindowOpen(item, time.Now()) && detail.CurrentPaid >= item.ThresholdAmount var count int64 s.repo.GetDbR().WithContext(ctx).Model(&Participant{}).Where("activity_id = ? AND user_id = ? AND period_key = ?", id, userID, period).Count(&count) detail.Joined = count > 0 diff --git a/internal/service/welfare_activity/participant.go b/internal/service/welfare_activity/participant.go index 9a52ed6..00a339a 100644 --- a/internal/service/welfare_activity/participant.go +++ b/internal/service/welfare_activity/participant.go @@ -16,10 +16,10 @@ func (s *service) Join(ctx context.Context, activityID int64, userID int64) erro return err } now := time.Now() - if activity.Status != StatusActive { - return errors.New("活动未开放参与") - } - if now.Before(activity.StartTime) || now.After(activity.EndTime) || now.After(activity.DrawTime) { + if !isJoinWindowOpen(activity, now) { + if now.Before(activity.StartTime) { + return errors.New("活动未开始") + } return errors.New("当前不在活动参与时间内") } start, end, period := periodRange(activity.Type, now) diff --git a/internal/service/welfare_activity/welfare_activity.go b/internal/service/welfare_activity/welfare_activity.go index c64d493..30863fc 100644 --- a/internal/service/welfare_activity/welfare_activity.go +++ b/internal/service/welfare_activity/welfare_activity.go @@ -45,3 +45,16 @@ type service struct { func New(log logger.CustomLogger, repo mysql.Repo) Service { return &service{logger: log, repo: repo, userSvc: usersvc.New(log, repo)} } + +func isJoinWindowOpen(activity Activity, now time.Time) bool { + if activity.Status != StatusActive { + return false + } + if now.Before(activity.StartTime) { + return false + } + if now.After(activity.EndTime) || now.After(activity.DrawTime) { + return false + } + return true +}