预警10
This commit is contained in:
parent
d57225b9a9
commit
cb61887221
|
|
@ -17,7 +17,7 @@ func (this *VoucherBiz) WarningBudgetIncrKv(uid string) (string, time.Duration)
|
|||
return v.Key, v.TTL
|
||||
}
|
||||
|
||||
func (this *VoucherBiz) WarningBudgetIncr(ctx context.Context, key string, TTL time.Duration) (int64, error) {
|
||||
func (this *VoucherBiz) WarningBudgetIncr(ctx context.Context, key string, ttl time.Duration) (int64, error) {
|
||||
|
||||
// 增加发送计数
|
||||
count, err := this.rdb.Rdb.IncrBy(ctx, key, 1).Result()
|
||||
|
|
@ -27,7 +27,7 @@ func (this *VoucherBiz) WarningBudgetIncr(ctx context.Context, key string, TTL t
|
|||
|
||||
// 如果是第一次发送,设置 过期时间
|
||||
if count == 1 {
|
||||
if err = this.rdb.Rdb.Expire(ctx, key, TTL).Err(); err != nil {
|
||||
if err = this.rdb.Rdb.Expire(ctx, key, ttl).Err(); err != nil {
|
||||
return 0, fmt.Errorf("设置过期时间失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -42,16 +42,18 @@ func (this *VoucherBiz) WarningBudgetIncr(ctx context.Context, key string, TTL t
|
|||
|
||||
func (this *VoucherBiz) WarningBudgetIncrDel(ctx context.Context, key string) error {
|
||||
|
||||
//r, err := this.rdb.Rdb.Get(ctx, key).Result()
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
//
|
||||
//if len(r) == 0 {
|
||||
// return nil
|
||||
//}
|
||||
// 检查键是否存在
|
||||
exists, err := this.rdb.Rdb.Exists(ctx, key).Result()
|
||||
if err != nil {
|
||||
return fmt.Errorf("检查键存在性失败: %w", err)
|
||||
}
|
||||
|
||||
if _, err := this.rdb.Rdb.Del(ctx, key).Result(); err != nil {
|
||||
// 如果键不存在,直接返回成功
|
||||
if exists == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err = this.rdb.Rdb.Del(ctx, key).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -116,15 +118,21 @@ func (this *VoucherBiz) Calculate(ctx context.Context, product *bo.ProductBo, wx
|
|||
|
||||
w := this.WxResp(wxResp)
|
||||
|
||||
key, ttl := this.WarningBudgetIncrKv(product.BatchNo)
|
||||
|
||||
if w.AllBudget > product.AllBudget {
|
||||
if err := this.WarningBudgetIncrDel(ctx, key); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := this.ProductRepo.UpdateByWxResp(ctx, product.ID, w); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
k, t := this.WarningBudgetIncrKv(product.BatchNo)
|
||||
|
||||
if product.WarningBudget >= w.AvailableBudget {
|
||||
|
||||
count, err2 := this.WarningBudgetIncr(ctx, k, t)
|
||||
count, err2 := this.WarningBudgetIncr(ctx, key, ttl)
|
||||
if err2 != nil {
|
||||
return err2
|
||||
}
|
||||
|
|
@ -136,12 +144,6 @@ func (this *VoucherBiz) Calculate(ctx context.Context, product *bo.ProductBo, wx
|
|||
}
|
||||
}
|
||||
|
||||
if w.AllBudget > product.AllBudget {
|
||||
if err := this.WarningBudgetIncrDel(ctx, k); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue