mongo.go重构
This commit is contained in:
@@ -17,17 +17,10 @@ import (
|
||||
)
|
||||
|
||||
type order struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
// Order 订单数据访问对象
|
||||
var Order = &order{
|
||||
NoCache: false,
|
||||
}
|
||||
|
||||
func (d *order) SetNoCache() {
|
||||
Order.NoCache = true
|
||||
}
|
||||
var Order = &order{}
|
||||
|
||||
// getCollection 根据订单状态获取对应的集合
|
||||
func (d *order) getCollection(status consts.OrderStatus) (string, error) {
|
||||
@@ -56,7 +49,7 @@ func (d *order) CreatePendingOrder(ctx context.Context, order *entity.OrderPendi
|
||||
order.CreatedAt = time.Now()
|
||||
order.UpdatedAt = time.Now()
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{order}, collection)
|
||||
_, err = mongo.DB().Insert(ctx, []interface{}{order}, collection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -109,7 +102,7 @@ func (d *order) findOrderByNo(collection string, ctx context.Context, orderNo st
|
||||
"order_no": orderNo,
|
||||
}
|
||||
|
||||
err := mongo.FindOne(ctx, d.NoCache, filter, result, collection)
|
||||
err := mongo.DB().FindOne(ctx, filter, result, collection)
|
||||
if err != nil {
|
||||
return errors.New("order not found")
|
||||
}
|
||||
@@ -136,7 +129,7 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons
|
||||
"order_no": orderNo,
|
||||
}
|
||||
|
||||
err = mongo.FindOne(ctx, d.NoCache, filter, &orderData, srcCollection)
|
||||
err = mongo.DB().FindOne(ctx, filter, &orderData, srcCollection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -148,13 +141,13 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons
|
||||
}
|
||||
|
||||
// 插入到目标集合
|
||||
_, err = mongo.Insert(ctx, []interface{}{orderData}, destCollection)
|
||||
_, err = mongo.DB().Insert(ctx, []interface{}{orderData}, destCollection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 从源集合删除
|
||||
_, err = mongo.Delete(ctx, filter, srcCollection)
|
||||
_, err = mongo.DB().Delete(ctx, filter, srcCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -170,7 +163,7 @@ func (d *order) UpdatePendingOrder(ctx context.Context, orderNo string, update b
|
||||
filter := bson.M{
|
||||
"order_no": orderNo,
|
||||
}
|
||||
_, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
_, err = mongo.DB().Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -189,7 +182,7 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
total, err := mongo.Count(ctx, d.NoCache, filter, collection)
|
||||
total, err := mongo.DB().Count(ctx, filter, collection)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -220,25 +213,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, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
|
||||
if err := mongo.DB().Find(ctx, 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, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
|
||||
if err := mongo.DB().Find(ctx, 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, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
|
||||
if err := mongo.DB().Find(ctx, 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, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
|
||||
if err := mongo.DB().Find(ctx, filter, &orders, collection, findOptions...); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
@@ -258,7 +251,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, d.NoCache, filter, &order, collection); err != nil {
|
||||
if err := mongo.DB().FindOne(ctx, filter, &order, collection); err != nil {
|
||||
if err.Error() == "mongo: no documents in result" {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -279,7 +272,7 @@ func (d *order) GetExpiredPendingOrders(ctx context.Context) ([]entity.OrderPend
|
||||
}
|
||||
|
||||
var orders []entity.OrderPending
|
||||
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection); err != nil {
|
||||
if err := mongo.DB().Find(ctx, filter, &orders, collection); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -300,7 +293,7 @@ func (d *order) UpdatePayInfo(ctx context.Context, orderNo string, payInfo entit
|
||||
"pay_info": payInfo,
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
_, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
_, err = mongo.DB().Update(ctx, filter, bson.M{"$set": update}, collection)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user