补齐相关日志日志
This commit is contained in:
@@ -644,3 +644,54 @@ func (d *QuotaGrantDao) QuotaGrantExistsByRefIDAndType(ctx context.Context, refI
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// quotaFlowChangeTypeExpire 与 service.QuotaChangeTypeExpire 对应。
|
||||
// DAO 层不反向依赖 service,故此处用字面量并在此说明。
|
||||
const quotaFlowChangeTypeExpire int8 = 7
|
||||
|
||||
// QuotaGrantListExpiredFixed 查询「固定有效期」且已过期、但尚未补记到期失效流水的凭证。
|
||||
//
|
||||
// 只处理 expire_time > 0 的凭证:expire_time=0 表示跟随 VIP,用户续费后凭证会重新生效,
|
||||
// 不应记为「失效」(否则续费后额度凭空多一笔负数流水,反而污染明细)。
|
||||
//
|
||||
// 幂等依据:(ref_id, quota_type, change_type=7) 的流水是否已存在;写入失败下轮会重新捞取。
|
||||
func (d *QuotaGrantDao) QuotaGrantListExpiredFixed(ctx context.Context, now int64, limit int) ([]mysqlmodel.QuotaGrant, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantListExpiredFixed")
|
||||
defer span.End()
|
||||
|
||||
var list []mysqlmodel.QuotaGrant
|
||||
err := d.db.WithContext(ctx).Model(&mysqlmodel.QuotaGrant{}).
|
||||
Where("expire_time > 0 AND expire_time <= ?", now).
|
||||
Where("NOT EXISTS (?)", d.db.Model(&mysqlmodel.QuotaFlowLog{}).
|
||||
Select("1").
|
||||
Where("quota_flow_logs.order_no = quota_grants.ref_id").
|
||||
Where("quota_flow_logs.quota_type = quota_grants.quota_type").
|
||||
Where("quota_flow_logs.change_type = ?", quotaFlowChangeTypeExpire)).
|
||||
Order("id ASC").Limit(limit).Find(&list).Error
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// QuotaFlowLogCreateIgnoreDup 写入额度流水,命中唯一索引 uk_quota_flow_idem 冲突时静默忽略。
|
||||
// 返回 created 表示本次是否真正插入,供「到期失效」流水的幂等补记使用。
|
||||
func (d *QuotaGrantDao) QuotaFlowLogCreateIgnoreDup(ctx context.Context, flow *mysqlmodel.QuotaFlowLog) (bool, error) {
|
||||
ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaFlowLogCreateIgnoreDup")
|
||||
defer span.End()
|
||||
|
||||
res := d.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
{Name: "order_no"},
|
||||
{Name: "quota_type"},
|
||||
{Name: "change_type"},
|
||||
},
|
||||
DoNothing: true,
|
||||
}).Create(flow)
|
||||
if res.Error != nil {
|
||||
span.RecordError(res.Error)
|
||||
return false, res.Error
|
||||
}
|
||||
return res.RowsAffected > 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user