代码调整
This commit is contained in:
parent
a4390dbb8b
commit
01320b9e04
1
go.sum
1
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=
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue