bugfix:支付
This commit is contained in:
+53
-12
@@ -428,16 +428,35 @@ func NewQuotaGrantDao() *QuotaGrantDao {
|
||||
return &QuotaGrantDao{db: global.MysqlEngine}
|
||||
}
|
||||
|
||||
// QuotaGrantCreate 创建额度凭证(ref_id+quota_type 唯一索引兜底幂等,冲突返回 1062)。
|
||||
func (d *QuotaGrantDao) QuotaGrantCreate(ctx context.Context, grant *mysqlmodel.QuotaGrant) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantCreate")
|
||||
// QuotaGrantCreateWithFlow 事务内创建额度凭证并写发放流水(购买赠送/活动赠送)。
|
||||
// 返回值 created 表示本次是否新建了凭证:依赖 uk_grant_ref_type(ref_id, quota_type) 唯一索引实现幂等,
|
||||
// 凭证已存在时返回 (false, nil) 且不写流水,避免重复回调/补偿重试重复记账
|
||||
//(流水侧另有 (order_no, quota_type, change_type) 唯一索引兜底)。
|
||||
func (d *QuotaGrantDao) QuotaGrantCreateWithFlow(ctx context.Context, grant *mysqlmodel.QuotaGrant, flow *mysqlmodel.QuotaFlowLog) (bool, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantCreateWithFlow")
|
||||
defer span.End()
|
||||
|
||||
if err := d.db.WithContext(ctx).Create(grant).Error; err != nil {
|
||||
created := false
|
||||
err := d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
res := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "ref_id"}, {Name: "quota_type"}},
|
||||
DoNothing: true,
|
||||
}).Create(grant)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return nil // 凭证已存在:幂等跳过,不重复写流水。
|
||||
}
|
||||
created = true
|
||||
ensureFlowOrderNo(flow)
|
||||
return tx.Create(flow).Error
|
||||
})
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
return nil
|
||||
return created, nil
|
||||
}
|
||||
|
||||
// QuotaGrantListByUserIDAndType 查询用户某类型全部凭证(未删除)。
|
||||
@@ -474,16 +493,38 @@ func (d *QuotaGrantDao) QuotaGrantConsume(ctx context.Context, grantID int64, n
|
||||
return res.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
// QuotaGrantInvalidateByRefID 按关联ID软删除凭证(退款回收时使赠送额度失效)。
|
||||
func (d *QuotaGrantDao) QuotaGrantInvalidateByRefID(ctx context.Context, refID string) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantInvalidateByRefID")
|
||||
// QuotaGrantListByRefID 查询某关联ID(订单号/广告记录ID)下未删除的额度凭证。
|
||||
func (d *QuotaGrantDao) QuotaGrantListByRefID(ctx context.Context, refID string) ([]mysqlmodel.QuotaGrant, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantListByRefID")
|
||||
defer span.End()
|
||||
|
||||
if err := d.db.WithContext(ctx).Where("ref_id = ?", refID).Delete(&mysqlmodel.QuotaGrant{}).Error; err != nil {
|
||||
var list []mysqlmodel.QuotaGrant
|
||||
if err := d.db.WithContext(ctx).Where("ref_id = ?", refID).Find(&list).Error; err != nil {
|
||||
span.RecordError(err)
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// QuotaGrantInvalidateByRefIDWithFlows 事务内按关联ID软删除凭证,并写「退款回收」流水。
|
||||
// 凭证软删除后不再计入有效额度;已用部分不追回,故流水变更量由调用方按 (amount - used) 记负数。
|
||||
// 凭证失效与流水同事务,保证「额度被回收」与「额度明细」一致。
|
||||
func (d *QuotaGrantDao) QuotaGrantInvalidateByRefIDWithFlows(ctx context.Context, refID string, flows []*mysqlmodel.QuotaFlowLog) error {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantInvalidateByRefIDWithFlows")
|
||||
defer span.End()
|
||||
|
||||
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("ref_id = ?", refID).Delete(&mysqlmodel.QuotaGrant{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range flows {
|
||||
ensureFlowOrderNo(f)
|
||||
if err := tx.Create(f).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// QuotaGrantExistsByRefIDAndType 判断凭证是否已存在(广告激励防重复领取)。
|
||||
|
||||
Reference in New Issue
Block a user