gomod引用
This commit is contained in:
@@ -4,18 +4,17 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
|
||||
"order/consts"
|
||||
"order/model/entity"
|
||||
)
|
||||
|
||||
// Init 初始化DAO
|
||||
func Init() error {
|
||||
return nil
|
||||
}
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
)
|
||||
|
||||
type order struct{}
|
||||
|
||||
@@ -169,7 +168,7 @@ func (d *order) UpdatePendingOrder(ctx context.Context, orderNo string, update b
|
||||
}
|
||||
|
||||
// ListOrdersByStatus 根据状态查询订单列表
|
||||
func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatus, userID string, page, pageSize int) (interface{}, int64, error) {
|
||||
func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatus, userID string, page int, pageSize int, sortFields ...string) (interface{}, int64, error) {
|
||||
collection, err := d.getCollection(status)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -187,32 +186,51 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 分页查询(暂时忽略排序和分页,因为 mongo.Find 不支持这些参数)
|
||||
// TODO: 需要在 common/mongo 中添加支持排序和分页的 Find 方法
|
||||
// 设置分页参数
|
||||
var findOptions []options.Lister[options.FindOptions]
|
||||
if page > 0 && pageSize > 0 {
|
||||
findOptions = append(findOptions, options.Find().SetSkip(int64((page-1)*pageSize)).SetLimit(int64(pageSize)))
|
||||
}
|
||||
|
||||
// 设置排序参数
|
||||
if len(sortFields) > 0 {
|
||||
sort := bson.D{}
|
||||
for _, field := range sortFields {
|
||||
var order int
|
||||
if strings.HasPrefix(field, "-") {
|
||||
order = -1
|
||||
field = strings.TrimPrefix(field, "-")
|
||||
} else {
|
||||
order = 1
|
||||
}
|
||||
sort = append(sort, bson.E{Key: field, Value: order})
|
||||
}
|
||||
findOptions = append(findOptions, options.Find().SetSort(sort))
|
||||
}
|
||||
|
||||
// 根据状态返回对应的订单类型
|
||||
switch status {
|
||||
case consts.OrderStatusPending:
|
||||
var orders []entity.OrderPending
|
||||
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
|
||||
if err := mongo.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, filter, &orders, collection); err != nil {
|
||||
if err := mongo.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, filter, &orders, collection); err != nil {
|
||||
if err := mongo.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, filter, &orders, collection); err != nil {
|
||||
if err := mongo.Find(ctx, filter, &orders, collection, findOptions...); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return orders, total, nil
|
||||
@@ -222,6 +240,26 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
|
||||
}
|
||||
|
||||
// GetExpiredPendingOrders 获取过期的待支付订单
|
||||
// GetPendingOrder gets pending order by order number
|
||||
func (d *order) GetPendingOrder(ctx context.Context, orderNo string) (*entity.OrderPending, error) {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
filter := bson.M{"order_no": orderNo}
|
||||
|
||||
var order entity.OrderPending
|
||||
if err := mongo.FindOne(ctx, filter, &order, collection); err != nil {
|
||||
if err.Error() == "mongo: no documents in result" {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &order, nil
|
||||
}
|
||||
|
||||
func (d *order) GetExpiredPendingOrders(ctx context.Context) ([]entity.OrderPending, error) {
|
||||
collection, err := d.getCollection(consts.OrderStatusPending)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user