From 01320b9e04399357cdb18b415fa73a70ad456657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Fri, 21 Mar 2025 18:18:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.sum | 1 + internal/biz/cmb.go | 29 ++++++++++++++++++----------- internal/biz/order.go | 6 +----- internal/data/repoimpl/product.go | 16 ++++++---------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/go.sum b/go.sum index 22ebb29..26e6c8d 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,7 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= diff --git a/internal/biz/cmb.go b/internal/biz/cmb.go index 6b30804..eadc129 100644 --- a/internal/biz/cmb.go +++ b/internal/biz/cmb.go @@ -8,29 +8,36 @@ import ( v1 "voucher/api/v1" "voucher/internal/biz/bo" "voucher/internal/biz/vo" - "voucher/internal/pkg/helper" "voucher/internal/pkg/lock" ) -func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) { +func (v *VoucherBiz) GetByOutBizNo(ctx context.Context, req *bo.OrderCreateReqBo) (*bo.OrderBo, error) { - bizCtx := helper.CopyValueCtx(ctx) + order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo) + + if err != nil && !err2.IsDbNotFound(err) { + return nil, err + } + + return order, nil +} + +func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (orderNo string, err error) { c := vo.CmbOrderLockKey.BuildCache([]string{req.OutBizNo, req.Type.String()}) - err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(bizCtx, c.Key, func(bizCtx context.Context) error { + err = lock.NewMutex(v.rdb.Rdb, c.TTL).Lock(ctx, c.Key, func(ctx context.Context) error { - order, err3 := v.OrderRepo.GetByOutBizNo(bizCtx, vo.OrderTypeCmb, req.OutBizNo) - - if err3 != nil && !err2.IsDbNotFound(err3) { + order, err3 := v.GetByOutBizNo(ctx, req) + if err3 != nil { return err3 } if order != nil { - if order.Status.IsFail() { + if order.ProductNo != "001" && order.Status.IsFail() { - if err4 := v.orderRetry(bizCtx, order); err4 != nil { + if err4 := v.orderRetry(ctx, order); err4 != nil { return err4 } } @@ -39,12 +46,12 @@ func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (or return nil } - product, err3 := v.ProductRepo.GetByProductNo(bizCtx, req.ProductNo) + product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo) if err3 != nil { return err3 } - order, err3 = v.order(bizCtx, req, product) + order, err3 = v.order(ctx, req, product) if err3 != nil { return err3 } diff --git a/internal/biz/order.go b/internal/biz/order.go index d6b9f73..55967ff 100644 --- a/internal/biz/order.go +++ b/internal/biz/order.go @@ -37,11 +37,7 @@ func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, produc return nil, err } - if err = v.success(ctx, order, voucherNo); err != nil { - return nil, err - } - - return order, nil + return order, v.success(ctx, order, voucherNo) } func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error { diff --git a/internal/data/repoimpl/product.go b/internal/data/repoimpl/product.go index 62a5814..b5ca996 100644 --- a/internal/data/repoimpl/product.go +++ b/internal/data/repoimpl/product.go @@ -29,14 +29,8 @@ func NewProductRepoImpl(db *data.Db, rdb *data.Rdb) repo.ProductRepo { return &ProductRepoImpl{db: db, rdb: rdb} } -func (p *ProductRepoImpl) DB(ctx context.Context) *gorm.DB { - return p.db.DB(ctx).WithContext(ctx).Model(model.Product{}) -} - func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) { - var item *model.Product - c := vo.ProductQueryKey.BuildCache([]string{productNo}) cacheValue, err := r.rdb.Rdb.Get(ctx, c.Key).Result() @@ -45,6 +39,8 @@ func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string) return nil, fmt.Errorf(fmt.Sprintf("获取商品缓存异常,%s:%v", c.Key, err)) } + var item *model.Product + if len(cacheValue) > 0 { if err = json.Unmarshal([]byte(cacheValue), &item); err != nil { return nil, err @@ -68,9 +64,9 @@ func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string) item, err = r.getByProductNo(ctx, item, productNo) - b, err := json.Marshal(item) - if err != nil { - return err + b, err3 := json.Marshal(item) + if err3 != nil { + return err3 } return r.rdb.Rdb.Set(ctx, c.Key, string(b), c.TTL).Err() @@ -89,10 +85,10 @@ func (r *ProductRepoImpl) getByProductNo(ctx context.Context, item *model.Produc tx := db.Where(model.Product{ProductNo: productNo}).First(&item) if tx.Error != nil { + if errors.Is(tx.Error, gorm.ErrRecordNotFound) { return nil, err2.ErrorDbNotFound("商品数据不存在") } - sqlDB, _ := db.DB() log.Warnf("product当前打开连接数:%d,空闲连接数:%d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)