277 lines
7.0 KiB
Go
277 lines
7.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"order/model/entity"
|
|
)
|
|
|
|
// OrderDao 订单数据访问对象
|
|
// 支持按状态拆分的订单表操作
|
|
|
|
type OrderDao struct {
|
|
collections map[entity.OrderStatus]*mongo.Collection
|
|
}
|
|
|
|
// NewOrderDao 创建订单DAO实例
|
|
func NewOrderDao(collections map[entity.OrderStatus]*mongo.Collection) *OrderDao {
|
|
return &OrderDao{
|
|
collections: collections,
|
|
}
|
|
}
|
|
|
|
// getCollection 根据订单状态获取对应的集合
|
|
func (d *OrderDao) getCollection(status entity.OrderStatus) (*mongo.Collection, error) {
|
|
collection, exists := d.collections[status]
|
|
if !exists {
|
|
return nil, fmt.Errorf("collection for status %s not found", status)
|
|
}
|
|
return collection, nil
|
|
}
|
|
|
|
// CreatePendingOrder 创建待支付订单
|
|
func (d *OrderDao) CreatePendingOrder(ctx context.Context, order *entity.OrderPending) error {
|
|
collection, err := d.getCollection(entity.OrderStatusPending)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
order.ID = primitive.NewObjectID()
|
|
order.CreatedAt = time.Now()
|
|
order.UpdatedAt = time.Now()
|
|
|
|
_, err = collection.InsertOne(ctx, order)
|
|
return err
|
|
}
|
|
|
|
// GetOrderByNo 根据订单号查询订单(自动识别状态)
|
|
func (d *OrderDao) GetOrderByNo(ctx context.Context, tenantID, orderNo string) (interface{}, entity.OrderStatus, error) {
|
|
// 按状态优先级搜索(从最新状态开始)
|
|
statuses := []entity.OrderStatus{
|
|
entity.OrderStatusCompleted,
|
|
entity.OrderStatusShipped,
|
|
entity.OrderStatusPaid,
|
|
entity.OrderStatusPending,
|
|
}
|
|
|
|
for _, status := range statuses {
|
|
collection, err := d.getCollection(status)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
switch status {
|
|
case entity.OrderStatusPending:
|
|
var order entity.OrderPending
|
|
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
|
return &order, status, nil
|
|
}
|
|
case entity.OrderStatusPaid:
|
|
var order entity.OrderPaid
|
|
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
|
return &order, status, nil
|
|
}
|
|
case entity.OrderStatusShipped:
|
|
var order entity.OrderShipped
|
|
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
|
return &order, status, nil
|
|
}
|
|
case entity.OrderStatusCompleted:
|
|
var order entity.OrderCompleted
|
|
if err := d.findOrderByNo(collection, ctx, tenantID, orderNo, &order); err == nil {
|
|
return &order, status, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, "", errors.New("order not found")
|
|
}
|
|
|
|
// findOrderByNo 通用订单查询方法
|
|
func (d *OrderDao) findOrderByNo(collection *mongo.Collection, ctx context.Context, tenantID, orderNo string, result interface{}) error {
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"order_no": orderNo,
|
|
}
|
|
|
|
err := collection.FindOne(ctx, filter).Decode(result)
|
|
if err == mongo.ErrNoDocuments {
|
|
return errors.New("order not found")
|
|
}
|
|
return err
|
|
}
|
|
|
|
// MoveOrderToStatus 将订单从一个状态移动到另一个状态
|
|
func (d *OrderDao) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus entity.OrderStatus, tenantID, orderNo string, updateData bson.M) error {
|
|
// 获取源集合
|
|
srcCollection, err := d.getCollection(fromStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 获取目标集合
|
|
destCollection, err := d.getCollection(toStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 查找源订单
|
|
var orderData bson.M
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"order_no": orderNo,
|
|
}
|
|
|
|
err = srcCollection.FindOne(ctx, filter).Decode(&orderData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 更新数据
|
|
orderData["updated_at"] = time.Now()
|
|
for key, value := range updateData {
|
|
orderData[key] = value
|
|
}
|
|
|
|
// 插入到目标集合
|
|
_, err = destCollection.InsertOne(ctx, orderData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 从源集合删除
|
|
_, err = srcCollection.DeleteOne(ctx, filter)
|
|
return err
|
|
}
|
|
|
|
// UpdatePendingOrder 更新待支付订单
|
|
func (d *OrderDao) UpdatePendingOrder(ctx context.Context, tenantID, orderNo string, update bson.M) error {
|
|
collection, err := d.getCollection(entity.OrderStatusPending)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
update["updated_at"] = time.Now()
|
|
|
|
_, err = collection.UpdateOne(ctx, bson.M{
|
|
"tenant_id": tenantID,
|
|
"order_no": orderNo,
|
|
}, bson.M{"$set": update})
|
|
|
|
return err
|
|
}
|
|
|
|
// ListOrdersByStatus 根据状态查询订单列表
|
|
func (d *OrderDao) ListOrdersByStatus(ctx context.Context, status entity.OrderStatus, tenantID, userID string, page, pageSize int) (interface{}, int64, error) {
|
|
collection, err := d.getCollection(status)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 构建查询条件
|
|
filter := bson.M{"tenant_id": tenantID}
|
|
if userID != "" {
|
|
filter["user_id"] = userID
|
|
}
|
|
|
|
// 计算总数
|
|
total, err := collection.CountDocuments(ctx, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// 分页查询
|
|
skip := int64((page - 1) * pageSize)
|
|
opt := options.Find().
|
|
SetSort(bson.D{{Key: "created_at", Value: -1}}).
|
|
SetSkip(skip).
|
|
SetLimit(int64(pageSize))
|
|
|
|
cursor, err := collection.Find(ctx, filter, opt)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
// 根据状态返回对应的订单类型
|
|
switch status {
|
|
case entity.OrderStatusPending:
|
|
var orders []entity.OrderPending
|
|
if err := cursor.All(ctx, &orders); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return orders, total, nil
|
|
case entity.OrderStatusPaid:
|
|
var orders []entity.OrderPaid
|
|
if err := cursor.All(ctx, &orders); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return orders, total, nil
|
|
case entity.OrderStatusShipped:
|
|
var orders []entity.OrderShipped
|
|
if err := cursor.All(ctx, &orders); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return orders, total, nil
|
|
case entity.OrderStatusCompleted:
|
|
var orders []entity.OrderCompleted
|
|
if err := cursor.All(ctx, &orders); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return orders, total, nil
|
|
default:
|
|
return nil, 0, errors.New("unsupported order status")
|
|
}
|
|
}
|
|
|
|
// GetExpiredPendingOrders 获取过期的待支付订单
|
|
func (d *OrderDao) GetExpiredPendingOrders(ctx context.Context, tenantID string) ([]entity.OrderPending, error) {
|
|
collection, err := d.getCollection(entity.OrderStatusPending)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"expired_at": bson.M{"$lte": time.Now()},
|
|
}
|
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
var orders []entity.OrderPending
|
|
if err := cursor.All(ctx, &orders); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return orders, nil
|
|
}
|
|
|
|
// UpdatePayInfo 更新支付信息(待支付订单)
|
|
func (d *OrderDao) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
|
collection, err := d.getCollection(entity.OrderStatusPending)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = collection.UpdateOne(ctx, bson.M{
|
|
"tenant_id": tenantID,
|
|
"order_no": orderNo,
|
|
}, bson.M{"$set": bson.M{
|
|
"pay_info": payInfo,
|
|
"updated_at": time.Now(),
|
|
}})
|
|
|
|
return err
|
|
}
|