代码调整

This commit is contained in:
李子铭 2025-03-21 18:18:36 +08:00
parent a4390dbb8b
commit 01320b9e04
4 changed files with 26 additions and 26 deletions

1
go.sum
View File

@ -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/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/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= 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/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.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

View File

@ -8,29 +8,36 @@ import (
v1 "voucher/api/v1" v1 "voucher/api/v1"
"voucher/internal/biz/bo" "voucher/internal/biz/bo"
"voucher/internal/biz/vo" "voucher/internal/biz/vo"
"voucher/internal/pkg/helper"
"voucher/internal/pkg/lock" "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()}) 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) order, err3 := v.GetByOutBizNo(ctx, req)
if err3 != nil {
if err3 != nil && !err2.IsDbNotFound(err3) {
return err3 return err3
} }
if order != nil { 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 return err4
} }
} }
@ -39,12 +46,12 @@ func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (or
return nil return nil
} }
product, err3 := v.ProductRepo.GetByProductNo(bizCtx, req.ProductNo) product, err3 := v.ProductRepo.GetByProductNo(ctx, req.ProductNo)
if err3 != nil { if err3 != nil {
return err3 return err3
} }
order, err3 = v.order(bizCtx, req, product) order, err3 = v.order(ctx, req, product)
if err3 != nil { if err3 != nil {
return err3 return err3
} }

View File

@ -37,11 +37,7 @@ func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, produc
return nil, err return nil, err
} }
if err = v.success(ctx, order, voucherNo); err != nil { return order, v.success(ctx, order, voucherNo)
return nil, err
}
return order, nil
} }
func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error { func (v *VoucherBiz) orderRetry(ctx context.Context, order *bo.OrderBo) error {

View File

@ -29,14 +29,8 @@ func NewProductRepoImpl(db *data.Db, rdb *data.Rdb) repo.ProductRepo {
return &ProductRepoImpl{db: db, rdb: rdb} 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) { func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string) (*bo.ProductBo, error) {
var item *model.Product
c := vo.ProductQueryKey.BuildCache([]string{productNo}) c := vo.ProductQueryKey.BuildCache([]string{productNo})
cacheValue, err := r.rdb.Rdb.Get(ctx, c.Key).Result() 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)) return nil, fmt.Errorf(fmt.Sprintf("获取商品缓存异常,%s:%v", c.Key, err))
} }
var item *model.Product
if len(cacheValue) > 0 { if len(cacheValue) > 0 {
if err = json.Unmarshal([]byte(cacheValue), &item); err != nil { if err = json.Unmarshal([]byte(cacheValue), &item); err != nil {
return nil, err return nil, err
@ -68,9 +64,9 @@ func (r *ProductRepoImpl) GetByProductNo(ctx context.Context, productNo string)
item, err = r.getByProductNo(ctx, item, productNo) item, err = r.getByProductNo(ctx, item, productNo)
b, err := json.Marshal(item) b, err3 := json.Marshal(item)
if err != nil { if err3 != nil {
return err return err3
} }
return r.rdb.Rdb.Set(ctx, c.Key, string(b), c.TTL).Err() 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) tx := db.Where(model.Product{ProductNo: productNo}).First(&item)
if tx.Error != nil { if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) { if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, err2.ErrorDbNotFound("商品数据不存在") return nil, err2.ErrorDbNotFound("商品数据不存在")
} }
sqlDB, _ := db.DB() sqlDB, _ := db.DB()
log.Warnf("product当前打开连接数:%d,空闲连接数:%d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle) log.Warnf("product当前打开连接数:%d,空闲连接数:%d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle)