From e8406f731e7d8dc14af6b49ac8e5c4ddceb539a9 Mon Sep 17 00:00:00 2001 From: Asai Neko Date: Thu, 5 Feb 2026 14:33:39 +0800 Subject: [PATCH] Add checkin count in attendance and event api Signed-off-by: Asai Neko --- data/attendance.go | 23 ++++++++++++++++ data/event.go | 15 ++++++----- service/service_event/get_event_info.go | 36 ++++++++++++++++++++----- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/data/attendance.go b/data/attendance.go index dd128df..b4207d4 100644 --- a/data/attendance.go +++ b/data/attendance.go @@ -149,6 +149,29 @@ func (self *Attendance) CountUsersByEventID(ctx context.Context, eventID uuid.UU Where("event_id = ?", eventID). Count(&count).Error + if err == gorm.ErrRecordNotFound { + return 0, nil + } + + if err != nil { + return 0, err + } + + return count, nil +} + +func (self *Attendance) CountCheckedInUsersByEventID(ctx context.Context, eventID uuid.UUID) (int64, error) { + var count int64 + + err := Database.WithContext(ctx). + Model(&Attendance{}). + Where("event_id = ? AND checkin_at IS NOT NULL AND checkin_at > ?", eventID, time.Time{}). // 过滤未签到用户 + Count(&count).Error + + if err == gorm.ErrRecordNotFound { + return 0, nil + } + if err != nil { return 0, err } diff --git a/data/event.go b/data/event.go index ea326a5..60fc612 100644 --- a/data/event.go +++ b/data/event.go @@ -25,13 +25,14 @@ type Event struct { } type EventIndexDoc struct { - EventId string `json:"event_id"` - Name string `json:"name"` - Type string `json:"type"` - Description string `json:"description"` - StartTime time.Time `json:"start_time"` - EndTime time.Time `json:"end_time"` - Thumbnail string `json:"thumbnail"` + EventId string `json:"event_id"` + Name string `json:"name"` + Type string `json:"type"` + Description string `json:"description"` + StartTime time.Time `json:"start_time"` + EndTime time.Time `json:"end_time"` + Thumbnail string `json:"thumbnail"` + CheckinCount int64 `json:"checkin_count"` } func (self *Event) GetEventById(ctx context.Context, eventId uuid.UUID) (*Event, error) { diff --git a/service/service_event/get_event_info.go b/service/service_event/get_event_info.go index fbd71b8..1cd196d 100644 --- a/service/service_event/get_event_info.go +++ b/service/service_event/get_event_info.go @@ -45,6 +45,27 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E return } + checkinCount, err := new(data.Attendance).CountCheckedInUsersByEventID(payload.Context, payload.Data.EventId) + if err != nil { + exception := new(exception.Builder). + SetStatus(exception.StatusUser). + SetService(exception.ServiceEvent). + SetEndpoint(exception.EndpointEventServiceInfo). + SetType(exception.TypeCommon). + SetOriginal(exception.CommonErrorDatabase). + SetError(err). + Throw(payload.Context) + + result = &EventInfoResult{ + Common: shared.CommonResult{ + HttpCode: 500, + Exception: exception, + }, + } + + return + } + result = &EventInfoResult{ Common: shared.CommonResult{ HttpCode: 200, @@ -57,13 +78,14 @@ func (self *EventServiceImpl) GetEventInfo(payload *EventInfoPayload) (result *E Throw(payload.Context), }, Data: &data.EventIndexDoc{ - EventId: event.EventId.String(), - Name: event.Name, - Type: event.Type, - Description: event.Description, - StartTime: event.StartTime, - EndTime: event.EndTime, - Thumbnail: event.Thumbnail, + EventId: event.EventId.String(), + Name: event.Name, + Type: event.Type, + Description: event.Description, + StartTime: event.StartTime, + EndTime: event.EndTime, + Thumbnail: event.Thumbnail, + CheckinCount: checkinCount, }, }