diff --git a/internal/biz/cmb/consume.go b/internal/biz/cmb/consume.go index 2b3a680..9488626 100644 --- a/internal/biz/cmb/consume.go +++ b/internal/biz/cmb/consume.go @@ -54,6 +54,7 @@ func (v *Cmb) OrderConsume(ctx context.Context, order *bo.OrderBo) (outRequestNo func (v *Cmb) registerNotifyTag(ctx context.Context, stockCreatorMchID, stockID string) error { c := vo.WechatNotifyRegisterTagCacheKey.BuildRegisterCache(v.bc.WechatNotifyMQ.Tag, stockCreatorMchID, stockID) + _, err := v.rdb.Rdb.Get(ctx, c.Key).Result() if err == nil { @@ -177,7 +178,7 @@ func (v *Cmb) orderWechat(ctx context.Context, order *bo.OrderBo, orderOutReques orderWechat, err := v.OrderWechatRepo.GetByOutRequestNo(ctx, orderOutRequestNo) if err != nil { - return nil, err + return nil, fmt.Errorf("根据订单号%s获取微信订单失败:%s", orderWechat.OrderNo, err.Error()) } if !orderWechat.Status.CanNotify() { diff --git a/internal/biz/consume.go b/internal/biz/consume.go index 0145ebb..1ca4593 100644 --- a/internal/biz/consume.go +++ b/internal/biz/consume.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "go.opentelemetry.io/otel/trace" - "time" + "voucher/internal/biz/vo" "voucher/internal/pkg/lock" "voucher/internal/pkg/mq" ) @@ -26,7 +26,9 @@ func (v *VoucherBiz) PushOrderMQ(ctx context.Context, orderNo string) error { func (v *VoucherBiz) OrderConsume(ctx context.Context, orderNo string) (err error) { - err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("order_consume_%s", orderNo), func(ctx context.Context) error { + c := vo.OrderConsume.BuildCache([]string{orderNo}) + + err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo) if err != nil { @@ -67,7 +69,9 @@ func (v *VoucherBiz) PushNotifyMQ(ctx context.Context, orderNo, outRequestNo str func (v *VoucherBiz) NotifyConsume(ctx context.Context, orderNo, orderOutRequestNo string) (err error) { - err = lock.NewMutex(v.rdb.Rdb, time.Second*30).Lock(ctx, fmt.Sprintf("notify_consume_%s", orderNo), func(ctx context.Context) error { + c := vo.NotifyConsume.BuildCache([]string{orderNo}) + + err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { order, err := v.OrderRepo.GetByOrderNo(ctx, orderNo) if err != nil { diff --git a/internal/biz/vo/cache.go b/internal/biz/vo/cache.go index 0fc89e6..ecb5f52 100644 --- a/internal/biz/vo/cache.go +++ b/internal/biz/vo/cache.go @@ -12,6 +12,9 @@ const ( CmbQueryLockKey CacheKey = "cmb_query" CmbProductQueryLockKey CacheKey = "cmb_product_query" + OrderConsume CacheKey = "order_consume" + NotifyConsume CacheKey = "notify_consume" + WechatNotifyRegisterTagCacheKey CacheKey = "wechat_notify_register_tag" WechatNotifyRegisterTagCacheLockKey CacheKey = "wechat_notify_register_tag_lock" @@ -22,6 +25,8 @@ var CacheKeyMap = map[CacheKey]time.Duration{ CmbOrderLockKey: 30 * time.Second, CmbQueryLockKey: 30 * time.Second, CmbProductQueryLockKey: 30 * time.Second, + OrderConsume: 30 * time.Second, + NotifyConsume: 30 * time.Second, WechatNotifyRegisterTagCacheKey: 86400 * time.Second, WechatNotifyRegisterTagCacheLockKey: 30 * time.Second, WechatNotifyConsumeLockKey: 30 * time.Second, diff --git a/internal/biz/wechat_notify_consume.go b/internal/biz/wechat_notify_consume.go index 7b13cd5..ce6d4b1 100644 --- a/internal/biz/wechat_notify_consume.go +++ b/internal/biz/wechat_notify_consume.go @@ -21,9 +21,9 @@ func (j *VoucherBiz) WechatNotifyConsumer(ctx context.Context, tag string, req * return nil } - //req.PlainText.StockCreatorMchid = "1676203838" - //req.PlainText.StockID = "20215869" - //req.PlainText.CouponID = "96059179220" + req.PlainText.StockCreatorMchid = "1676203838" + req.PlainText.StockID = "20215869" + req.PlainText.CouponID = "96059179220" orderWechat, err := j.OrderWechatRepo.GetByMSCId(ctx, req.PlainText.StockCreatorMchid, req.PlainText.StockID, req.PlainText.CouponID) if err != nil { @@ -55,7 +55,7 @@ func (v *VoucherBiz) wechatVoucherUsed(ctx context.Context, orderWechat *bo.Orde order, err := v.OrderRepo.GetByOrderNo(ctx, orderWechat.OrderNo) if err != nil { - return err + return fmt.Errorf("根据订单号%s获取订单失败:%s", orderWechat.OrderNo, err.Error()) } if err = v.OrderWechatRepo.Used(ctx, orderWechat.ID); err != nil { diff --git a/internal/pkg/mq_http/mq_http.go b/internal/pkg/mq_http/mq_http.go index a411d36..f71ff88 100644 --- a/internal/pkg/mq_http/mq_http.go +++ b/internal/pkg/mq_http/mq_http.go @@ -6,7 +6,7 @@ import ( "strconv" ) -func wechatNotifyProducer(bodyStr string) error { +func wechatNotifyProducer(tag, bodyStr string) error { // 设置HTTP协议客户端接入点,进入消息队列RocketMQ版控制台实例详情页面的接入点区域查看。 endpoint := "http://1389288909295870.mqrest.cn-hangzhou.aliyuncs.com" // 请确保环境变量ALIBABA_CLOUD_ACCESS_KEY_ID、ALIBABA_CLOUD_ACCESS_KEY_SECRET已设置。 @@ -19,8 +19,6 @@ func wechatNotifyProducer(bodyStr string) error { // Topic所属的实例ID,在消息队列RocketMQ版控制台创建。 // 若实例有命名空间,则实例ID必须传入;若实例无命名空间,则实例ID传入null空值或字符串空值。实例的命名空间可以在消息队列RocketMQ版控制台的实例详情页面查看。 instanceId := "MQ_INST_1389288909295870_BYSoMttI" - - tag := "voucher_notify_dev" //tag := "voucher_notify_pro" client := mq_http_sdk.NewAliyunMQClient(endpoint, accessKey, secretKey, "") diff --git a/internal/pkg/mq_http/mq_http_test.go b/internal/pkg/mq_http/mq_http_test.go index c3690b9..d2d2c13 100644 --- a/internal/pkg/mq_http/mq_http_test.go +++ b/internal/pkg/mq_http/mq_http_test.go @@ -8,9 +8,11 @@ import ( func Test_WechatNotifyProducer(t *testing.T) { + tag := "voucher_notify_dev" + bodyStr := `{"id":"5465699d-de6a-5414-a8df-283167b577ca","create_time":"2025-03-07T15:57:24+08:00","resource_type":"encrypt-resource","event_type":"COUPON.USE","summary":"代金券核销通知","original_type":"coupon","associated_data":"coupon","plain_text":{"stock_creator_mchid":"1605446142","stock_id":"19990623","coupon_id":"95952277058","coupon_name":"萧山农商新客激活礼","description":"","status":"USED","create_time":"2025-03-07T15:49:31+08:00","coupon_type":"NORMAL","no_cash":false,"singleitem":false,"consume_information":{"consume_time":"2025-03-07T15:57:24+08:00","consume_mchid":"1800002761","transaction_id":"4200002544202503077103159055"}}}` - err := wechatNotifyProducer(bodyStr) - if err != nil { + + if err := wechatNotifyProducer(tag, bodyStr); err != nil { t.Errorf("入队失败 error = %v", err) return }