From adbdc0e4bd803ac49a320c03898681f19232ac1a Mon Sep 17 00:00:00 2001 From: ziming Date: Thu, 19 Jun 2025 10:21:35 +0800 Subject: [PATCH] register tag --- internal/biz/cmb/notify.go | 6 +++++- internal/biz/cron_notice.go | 23 +++++++++++------------ internal/biz/register_tag.go | 12 +++++++++--- internal/biz/repo/product.go | 1 + internal/data/repoimpl/product.go | 21 +++++++++++++++++++++ internal/service/cmb.go | 11 +++++------ 6 files changed, 52 insertions(+), 22 deletions(-) diff --git a/internal/biz/cmb/notify.go b/internal/biz/cmb/notify.go index 5c6f259..4c7d80e 100644 --- a/internal/biz/cmb/notify.go +++ b/internal/biz/cmb/notify.go @@ -72,7 +72,11 @@ func (v *Cmb) bizContent(_ context.Context, order *bo.OrderBo, orderNotify *bo.O } if cmbStatus == vo.CmbStatusUse { - req.TransDate = order.LastUseTime.Format("2006-01-02 15:04:05.000") + if order.LastUseTime == nil || order.LastUseTime.IsZero() { + req.TransDate = time.Now().Format("2006-01-02 15:04:05.000") + } else { + req.TransDate = order.LastUseTime.Format("2006-01-02 15:04:05.000") + } } else { req.TransDate = time.Now().Format("2006-01-02 15:04:05.000") } diff --git a/internal/biz/cron_notice.go b/internal/biz/cron_notice.go index 9d2d8e4..6197c74 100644 --- a/internal/biz/cron_notice.go +++ b/internal/biz/cron_notice.go @@ -158,17 +158,6 @@ func (v *VoucherBiz) ExecuteNotice(ctx context.Context, req *bo.FindInBatchesUse func (v *VoucherBiz) notice(ctx context.Context, order *bo.OrderBo, useNum, sucNum *int) (respErr error) { // 批量通知不做数据存储,量会很大 - if order == nil { - return fmt.Errorf("order is nil") - } - - defer func() { - if err := recover(); err != nil { - _, file, line, _ := runtime.Caller(1) // 1 表示获取当前调用者的调用信息 - respErr = fmt.Errorf("notice panic:%v, orderNo:%s, file:%s, line:%d", err, order.OrderNo, file, line) - } - }() - status, err := v.WechatCpnRepo.Query(ctx, order) if err != nil { return err @@ -178,6 +167,8 @@ func (v *VoucherBiz) notice(ctx context.Context, order *bo.OrderBo, useNum, sucN return nil // 券状态未改变,忽略不处理 } + order.Status = status + event, err := status.GetOrderNotifyEvent() if err != nil { return err @@ -212,11 +203,19 @@ func (v *VoucherBiz) request(ctx context.Context, order *bo.OrderBo, notify *bo. defer func() { if err := recover(); err != nil { + // 打印堆栈信息 + stackBuf := make([]byte, 1024) + stackSize := runtime.Stack(stackBuf, false) + // 获取调用栈信息 _, file, line, _ := runtime.Caller(1) // 1 表示获取当前调用者的调用信息 - respErr = fmt.Errorf("request panic:%v, orderNo:%s, file:%s, line:%d", err, order.OrderNo, file, line) + respErr = fmt.Errorf("request panic:%v, orderNo:%s, file:%s, line:%d, stack: %s", err, order.OrderNo, file, line, stackBuf[:stackSize]) } }() + if order == nil { + return fmt.Errorf("request order is nil") + } + if notify == nil { return fmt.Errorf("notify is nil") } diff --git a/internal/biz/register_tag.go b/internal/biz/register_tag.go index 4c48e42..90eab42 100644 --- a/internal/biz/register_tag.go +++ b/internal/biz/register_tag.go @@ -10,14 +10,20 @@ import ( ) // RegisterTag 注册通知标签 stock.MchId 批次创建商户, stock.BatchNo 商品批次号 -func (this *VoucherBiz) RegisterTag(ctx context.Context, productNo string) error { +func (this *VoucherBiz) RegisterTag(ctx context.Context, batchNo string) error { - stock, err := this.ProductRepo.GetByProductNo(ctx, productNo) + stock, err := this.ProductRepo.GetByBatchNo(ctx, batchNo) if err != nil { return err } - return this.registerNotifyTag(ctx, stock.MchId, stock.BatchNo) + if err = this.registerNotifyTag(ctx, stock.MchId, stock.BatchNo); err != nil { + return err + } + + _, err = this.ProductRepo.GetByProductNo(ctx, stock.ProductNo) + + return err } func (v *VoucherBiz) registerNotifyTag(ctx context.Context, stockCreatorMchID, stockID string) error { diff --git a/internal/biz/repo/product.go b/internal/biz/repo/product.go index a06caf6..8de43f7 100644 --- a/internal/biz/repo/product.go +++ b/internal/biz/repo/product.go @@ -6,5 +6,6 @@ import ( ) type ProductRepo interface { + GetByBatchNo(ctx context.Context, batchNo string) (*bo.ProductBo, error) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) } diff --git a/internal/data/repoimpl/product.go b/internal/data/repoimpl/product.go index 613ebac..79cb4ab 100644 --- a/internal/data/repoimpl/product.go +++ b/internal/data/repoimpl/product.go @@ -29,6 +29,27 @@ func NewProductRepoImpl(db *data.Db, rdb *data.Rdb) repo.ProductRepo { return &ProductRepoImpl{db: db, rdb: rdb} } +func (r *ProductRepoImpl) GetByBatchNo(ctx context.Context, batchNo string) (*bo.ProductBo, error) { + + var item *model.Product + + db := r.db.DB(ctx).Model(model.Product{}) + 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) + } + + if tx.RowsAffected == 0 { + return nil, err2.ErrorDbNotFound("商品数据不存在") + } + + return r.ToBo(item), nil +} + func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) { c := vo.ProductQueryKey.BuildCache([]string{productNo}) diff --git a/internal/service/cmb.go b/internal/service/cmb.go index cb5d52f..a1cf2a6 100644 --- a/internal/service/cmb.go +++ b/internal/service/cmb.go @@ -70,17 +70,16 @@ func (c *CmbService) OrderRetry(ctx context.Context, request *v1.OrderRetryReque func (this *CmbService) RegisterTag(ctx http.Context) error { - productNo := ctx.Vars().Get("product_no") - if productNo == "" { - return fmt.Errorf("product_no is empty") + batchNo := ctx.Vars().Get("batch_no") + if batchNo == "" { + return fmt.Errorf("batch_no is empty") } - err := this.VoucherBiz.RegisterTag(ctx, productNo) - if err != nil { + if err := this.VoucherBiz.RegisterTag(ctx, batchNo); err != nil { return err } return ctx.JSON(http2.StatusOK, map[string]interface{}{ - "data": productNo, + "data": batchNo, }) }