diff --git a/internal/biz/repo/product.go b/internal/biz/repo/product.go index 1194a81..fbf75f2 100644 --- a/internal/biz/repo/product.go +++ b/internal/biz/repo/product.go @@ -10,6 +10,7 @@ type ProductRepo interface { GetById(ctx context.Context, id int32) (*bo.ProductBo, error) FindWarningBudget(ctx context.Context, fun func(ctx context.Context, rows []*bo.ProductBo) error) error GetByBatchNo(ctx context.Context, batchNo string) (*bo.ProductBo, error) + GetByMchStockId(ctx context.Context, mchId, stockId string) (*bo.ProductBo, error) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) UpdateByWxResp(ctx context.Context, id int32, req *do.WxResp) error } diff --git a/internal/biz/wechat_notify.go b/internal/biz/wechat_notify.go index 89d33e8..874207e 100644 --- a/internal/biz/wechat_notify.go +++ b/internal/biz/wechat_notify.go @@ -11,6 +11,12 @@ import ( func (this *VoucherBiz) WechatNotifyConsumer(ctx context.Context, ip string, req *bo.WechatVoucherNotifyBo) error { + // 商品数据量较少,先查询商品是否存在,过滤多余的通知信息 + _, err := this.ProductRepo.GetByMchStockId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID) + if err != nil { + return fmt.Errorf("商品查询错误 error: %w", err) + } + c := vo.WechatNotifyConsumeLockKey.BuildCache([]string{req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID}) return lock.NewMutex(this.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { diff --git a/internal/data/repoimpl/product.go b/internal/data/repoimpl/product.go index bad4723..d1510b4 100644 --- a/internal/data/repoimpl/product.go +++ b/internal/data/repoimpl/product.go @@ -104,14 +104,28 @@ func (r *ProductRepoImpl) GetByBatchNo(ctx context.Context, batchNo string) (*bo tx := db.Where(model.Product{BatchNo: batchNo}).First(&item) if tx.Error != nil { - if errors.Is(tx.Error, gorm.ErrRecordNotFound) { - return nil, err2.ErrorDbNotFound("商品数据不存在") - } - return nil, fmt.Errorf("product db fail %w", tx.Error) + return nil, tx.Error } if tx.RowsAffected == 0 { - return nil, err2.ErrorDbNotFound("商品数据不存在") + return nil, gorm.ErrRecordNotFound + } + + return r.ToBo(item), nil +} + +func (r *ProductRepoImpl) GetByMchStockId(ctx context.Context, mchId, stockId string) (*bo.ProductBo, error) { + + var item *model.Product + + tx := r.db.DB(ctx).Where(model.Product{MchId: mchId, BatchNo: stockId}).First(&item) + + if tx.Error != nil { + return nil, tx.Error + } + + if tx.RowsAffected == 0 { + return nil, gorm.ErrRecordNotFound } return r.ToBo(item), nil