common版本更新v0.2.6;数据库查询接口增加是否从缓存中查询数据开关

This commit is contained in:
2025-12-29 15:58:48 +08:00
parent ca7b1cd989
commit c150f38a87
9 changed files with 129 additions and 56 deletions

View File

@@ -16,10 +16,18 @@ import (
"gitee.com/red-future---jilin-g/common/mongo"
)
type order struct{}
type order struct {
NoCache bool
}
// Order 订单数据访问对象
var Order = &order{}
var Order = &order{
NoCache: false,
}
func (d *order) SetNoCache() {
Order.NoCache = true
}
// getCollection 根据订单状态获取对应的集合
func (d *order) getCollection(status consts.OrderStatus) (string, error) {
@@ -101,7 +109,7 @@ func (d *order) findOrderByNo(collection string, ctx context.Context, orderNo st
"order_no": orderNo,
}
err := mongo.FindOne(ctx, filter, result, collection)
err := mongo.FindOne(ctx, d.NoCache, filter, result, collection)
if err != nil {
return errors.New("order not found")
}
@@ -128,7 +136,7 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons
"order_no": orderNo,
}
err = mongo.FindOne(ctx, filter, &orderData, srcCollection)
err = mongo.FindOne(ctx, d.NoCache, filter, &orderData, srcCollection)
if err != nil {
return err
}
@@ -181,7 +189,7 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
}
// 计算总数
total, err := mongo.Count(ctx, filter, collection)
total, err := mongo.Count(ctx, d.NoCache, filter, collection)
if err != nil {
return nil, 0, err
}
@@ -212,25 +220,25 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
switch status {
case consts.OrderStatusPending:
var orders []entity.OrderPending
if err := mongo.Find(ctx, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
return nil, 0, err
}
return orders, total, nil
case consts.OrderStatusPaid:
var orders []entity.OrderPaid
if err := mongo.Find(ctx, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
return nil, 0, err
}
return orders, total, nil
case consts.OrderStatusShipped:
var orders []entity.OrderShipped
if err := mongo.Find(ctx, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
return nil, 0, err
}
return orders, total, nil
case consts.OrderStatusCompleted:
var orders []entity.OrderCompleted
if err := mongo.Find(ctx, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
return nil, 0, err
}
return orders, total, nil
@@ -250,7 +258,7 @@ func (d *order) GetPendingOrder(ctx context.Context, orderNo string) (*entity.Or
filter := bson.M{"order_no": orderNo}
var order entity.OrderPending
if err := mongo.FindOne(ctx, filter, &order, collection); err != nil {
if err := mongo.FindOne(ctx, d.NoCache, filter, &order, collection); err != nil {
if err.Error() == "mongo: no documents in result" {
return nil, nil
}
@@ -271,7 +279,7 @@ func (d *order) GetExpiredPendingOrders(ctx context.Context) ([]entity.OrderPend
}
var orders []entity.OrderPending
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection); err != nil {
return nil, err
}