2026-10-30 13:13:29 +08:00
|
|
|
|
package failedevent
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Repository 是 failed_events 的存储层。
|
|
|
|
|
|
type Repository struct {
|
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewRepository 构造仓储。
|
|
|
|
|
|
func NewRepository(db *gorm.DB) *Repository {
|
|
|
|
|
|
return &Repository{db: db}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// Save 写入一条停泊记录,返回是否是本次新插入的。
|
2026-10-30 13:13:29 +08:00
|
|
|
|
//
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 去重键是 (source_queue, event_id);EventID 为 NULL 时不参与唯一索引,可多条共存。
|
2026-10-30 13:13:29 +08:00
|
|
|
|
func (r *Repository) Save(ctx context.Context, e *FailedEvent) (bool, error) {
|
|
|
|
|
|
if r == nil || r.db == nil {
|
|
|
|
|
|
return false, errors.New("failedevent: repository is not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
if e == nil {
|
|
|
|
|
|
return false, errors.New("failedevent: entity is nil")
|
|
|
|
|
|
}
|
|
|
|
|
|
if e.EventID == nil {
|
|
|
|
|
|
return true, r.db.WithContext(ctx).Create(e).Error
|
|
|
|
|
|
}
|
|
|
|
|
|
res := r.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(e)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return false, res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
return res.RowsAffected > 0, nil
|
|
|
|
|
|
}
|