package provider import ( "context" "time" "go.uber.org/zap" "github.com/topfans/backend/pkg/logger" pb "github.com/topfans/backend/pkg/proto/statistic" eventPb "github.com/topfans/backend/pkg/proto/event" "github.com/topfans/backend/services/statisticService/model" "github.com/topfans/backend/services/statisticService/service" ) // StatisticInternalProvider 实现 Dubbo StatisticService 中的事件相关 RPC // (TrackEvent / BatchTrackEvent) // 业务侧通过 gRPC 调用此处 type StatisticInternalProvider struct { eventSvc *service.EventService } // NewStatisticInternalProvider 构造 func NewStatisticInternalProvider(eventSvc *service.EventService) *StatisticInternalProvider { return &StatisticInternalProvider{eventSvc: eventSvc} } // TrackEvent 接收单个事件 func (p *StatisticInternalProvider) TrackEvent(ctx context.Context, e *eventPb.Event) (*pb.TrackEventResponse, error) { logger.Logger.Debug("StatisticInternalProvider.TrackEvent", zap.String("event_id", e.EventId), zap.String("event_type", e.EventType)) return p.eventSvc.TrackEvent(ctx, toModel(e)) } // BatchTrackEvent 批量接收事件 func (p *StatisticInternalProvider) BatchTrackEvent(ctx context.Context, req *eventPb.BatchEventRequest) (*pb.TrackEventResponse, error) { events := make([]*model.Event, 0, len(req.Events)) for _, e := range req.Events { events = append(events, toModel(e)) } return p.eventSvc.BatchTrackEvent(ctx, events) } // toModel protobuf -> domain model func toModel(e *eventPb.Event) *model.Event { occurredAt := time.UnixMilli(e.OccurredAt) receivedAt := time.UnixMilli(e.ReceivedAt) if receivedAt.IsZero() { receivedAt = time.Now() } props := e.Properties if props == nil { props = map[string]string{} } return &model.Event{ EventID: e.EventId, UserID: e.UserId, StarID: e.StarId, EventType: e.EventType, OccurredAt: occurredAt, ReceivedAt: receivedAt, Properties: props, } }