From 31c2da9027498a335c5884c11ecfcc5b31a9fa8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Thu, 20 Mar 2025 19:06:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/data/gorm_test.go | 2 +- internal/data/repoimpl/order.go | 20 +++++++++---------- internal/data/repoimpl/order_notify.go | 2 +- internal/data/repoimpl/product.go | 2 +- .../repoimpl/wechat_notify_register_tag.go | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/data/gorm_test.go b/internal/data/gorm_test.go index fe6bf04..ecb0a20 100644 --- a/internal/data/gorm_test.go +++ b/internal/data/gorm_test.go @@ -36,7 +36,7 @@ func Test_db(t *testing.T) { defer wg.Done() var m model.Product - tx := gormDb.Model(model.Product{}).Where(model.Product{ProductNo: "001"}).Find(&m) + tx := gormDb.Model(model.Product{}).Where(model.Product{ProductNo: "000"}).First(&m) if tx.Error != nil { if errors.Is(tx.Error, gorm.ErrRecordNotFound) { t.Errorf("未找到记录") diff --git a/internal/data/repoimpl/order.go b/internal/data/repoimpl/order.go index c0ab343..f272544 100644 --- a/internal/data/repoimpl/order.go +++ b/internal/data/repoimpl/order.go @@ -2,6 +2,7 @@ package repoimpl import ( "context" + "errors" "fmt" "github.com/go-kratos/kratos/v2/log" "gorm.io/gorm" @@ -83,18 +84,17 @@ func (p *OrderRepoImpl) Create(ctx context.Context, req *bo.OrderBo) (*bo.OrderB } func (p *OrderRepoImpl) GetByOutBizNo(ctx context.Context, t vo.OrderType, outBizNo string) (*bo.OrderBo, error) { - - ctx, cancel := context.WithTimeout(ctx, 20*time.Second) // 设置查询超时 - defer cancel() - info := &model.Order{} db := p.DB(ctx) - tx := db.Where(model.Order{Type: t.GetValue(), OutBizNo: outBizNo}).Find(&info) + tx := db.Where(model.Order{Type: t.GetValue(), OutBizNo: outBizNo}).First(&info) if tx.Error != nil { - sqlDB, _ := db.DB() - log.Warnf("order当前打开连接数:%d,空闲连接数:%d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle) + + if errors.Is(tx.Error, context.DeadlineExceeded) { + sqlDB, _ := db.DB() + log.Warnf("order当前打开连接数:%d,空闲连接数:%d", sqlDB.Stats().OpenConnections, sqlDB.Stats().Idle) + } return nil, fmt.Errorf("order db fail %w", tx.Error) } @@ -109,7 +109,7 @@ func (p *OrderRepoImpl) GetByOutBizNo(ctx context.Context, t vo.OrderType, outBi func (p *OrderRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.OrderBo, error) { info := &model.Order{} - tx := p.DB(ctx).Where(model.Order{ID: id}).Find(&info) + tx := p.DB(ctx).Where(model.Order{ID: id}).First(&info) if tx.Error != nil { return nil, fmt.Errorf("order db fail %w", tx.Error) @@ -125,7 +125,7 @@ func (p *OrderRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.OrderBo, er func (p *OrderRepoImpl) GetByOrderNo(ctx context.Context, orderNo string) (*bo.OrderBo, error) { info := &model.Order{} - tx := p.DB(ctx).Where(model.Order{OrderNo: orderNo}).Find(&info) + tx := p.DB(ctx).Where(model.Order{OrderNo: orderNo}).First(&info) if tx.Error != nil { return nil, fmt.Errorf("order db fail %w", tx.Error) @@ -141,7 +141,7 @@ func (p *OrderRepoImpl) GetByOrderNo(ctx context.Context, orderNo string) (*bo.O func (p *OrderRepoImpl) GetByMBV(ctx context.Context, merchantNo, batchNo, voucherNo string) (*bo.OrderBo, error) { info := &model.Order{} - tx := p.DB(ctx).Where(model.Order{MerchantNo: merchantNo, BatchNo: batchNo, VoucherNo: voucherNo}).Find(&info) + tx := p.DB(ctx).Where(model.Order{MerchantNo: merchantNo, BatchNo: batchNo, VoucherNo: voucherNo}).First(&info) if tx.Error != nil { return nil, fmt.Errorf("db fail %w", tx.Error) diff --git a/internal/data/repoimpl/order_notify.go b/internal/data/repoimpl/order_notify.go index a31bb38..7877434 100644 --- a/internal/data/repoimpl/order_notify.go +++ b/internal/data/repoimpl/order_notify.go @@ -31,7 +31,7 @@ func (p *OrderNotifyRepoImpl) DB(ctx context.Context) *gorm.DB { func (p *OrderNotifyRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.OrderNotifyBo, error) { info := &model.OrderNotify{} - tx := p.DB(ctx).Where(model.OrderNotify{ID: id}).Find(&info) + tx := p.DB(ctx).Where(model.OrderNotify{ID: id}).First(&info) if tx.Error != nil { return nil, tx.Error diff --git a/internal/data/repoimpl/product.go b/internal/data/repoimpl/product.go index a546f03..cdfddce 100644 --- a/internal/data/repoimpl/product.go +++ b/internal/data/repoimpl/product.go @@ -85,7 +85,7 @@ func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.Product func (r *ProductRepoImpl) getByPNO(ctx context.Context, item *model.Product, PNO string) (*model.Product, error) { db := r.DB(ctx) - tx := db.Where(model.Product{ProductNo: PNO}).Find(&item) + tx := db.Where(model.Product{ProductNo: PNO}).First(&item) if tx.Error != nil { sqlDB, _ := db.DB() diff --git a/internal/data/repoimpl/wechat_notify_register_tag.go b/internal/data/repoimpl/wechat_notify_register_tag.go index 26d208d..b6a5018 100644 --- a/internal/data/repoimpl/wechat_notify_register_tag.go +++ b/internal/data/repoimpl/wechat_notify_register_tag.go @@ -31,7 +31,7 @@ func (p *WechatNotifyRegisterTagRepoImpl) DB(ctx context.Context) *gorm.DB { func (p *WechatNotifyRegisterTagRepoImpl) GetByStockIdAndMchId(ctx context.Context, stockCreatorMchID, stockId string) (*bo.WechatNotifyRegisterTagBo, error) { info := &model.WechatNotifyRegisterTag{} - tx := p.DB(ctx).Where(model.WechatNotifyRegisterTag{StockCreatorMchID: stockCreatorMchID, StockID: stockId}).Find(&info) + tx := p.DB(ctx).Where(model.WechatNotifyRegisterTag{StockCreatorMchID: stockCreatorMchID, StockID: stockId}).First(&info) if tx.Error != nil { return nil, tx.Error