Files
pay/internal/store/orphan.go
T

27 lines
655 B
Go

package store
import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/wangjia/pay/internal/model"
)
type OrphanStore struct{ db *gorm.DB }
func NewOrphanStore(db *gorm.DB) *OrphanStore { return &OrphanStore{db: db} }
// Record 幂等落一条孤儿(tx_id 冲突 no-op)。返回是否新记(供告警只喊一次)。
func (s *OrphanStore) Record(o *model.OrphanPayment) (bool, error) {
res := s.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "tx_id"}},
DoNothing: true,
}).Create(o)
if res.Error != nil {
return false, fmt.Errorf("store.OrphanStore.Record: %w", res.Error)
}
return res.RowsAffected > 0, nil
}