gomod引用
This commit is contained in:
@@ -7,12 +7,13 @@ import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"order/consts"
|
||||
"order/dao"
|
||||
"order/model/dto"
|
||||
"order/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type order struct{}
|
||||
@@ -113,7 +114,7 @@ func convertEntityOrderItemsToDTO(items []entity.OrderItem) []dto.OrderItem {
|
||||
// CreateOrder 创建订单
|
||||
func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.CreateOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.UserID == "" || req.Subject == "" {
|
||||
if req.UserID == 0 || req.Subject == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
}
|
||||
|
||||
@@ -141,7 +142,7 @@ func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.
|
||||
}
|
||||
|
||||
// 3. 生成订单号
|
||||
orderNo := s.generateOrderNo(req.TenantID)
|
||||
orderNo := s.generateOrderNo(gconv.String(req.TenantID))
|
||||
|
||||
// 4. 设置订单过期时间(30分钟后)
|
||||
expiredAt := time.Now().Add(30 * time.Minute)
|
||||
@@ -149,7 +150,6 @@ func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.
|
||||
// 5. 创建待支付订单
|
||||
order := &entity.OrderPending{
|
||||
OrderBase: entity.OrderBase{
|
||||
TenantID: req.TenantID,
|
||||
OrderNo: orderNo,
|
||||
UserID: req.UserID,
|
||||
TotalAmount: totalAmount,
|
||||
@@ -190,12 +190,12 @@ func (s *order) generateOrderNo(tenantID string) string {
|
||||
// QueryOrder 查询订单详情
|
||||
func (s *order) QueryOrder(ctx context.Context, req *dto.QueryOrderReq) (*dto.QueryOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.OrderNo == "" {
|
||||
if req.OrderNo == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -271,7 +271,7 @@ func (s *order) convertPendingOrderToDetail(order *entity.OrderPending) dto.Orde
|
||||
// convertPaidOrderToDetail 转换已支付订单为详情
|
||||
func (s *order) convertPaidOrderToDetail(order *entity.OrderPaid) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
ID: order.Id.Hex(),
|
||||
TenantID: order.TenantId,
|
||||
OrderNo: order.OrderNo,
|
||||
UserID: order.UserID,
|
||||
@@ -306,8 +306,8 @@ func (s *order) convertPaidOrderToDetail(order *entity.OrderPaid) dto.OrderDetai
|
||||
// convertShippedOrderToDetail 转换已发货订单为详情
|
||||
func (s *order) convertShippedOrderToDetail(order *entity.OrderShipped) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
ID: order.Id.Hex(),
|
||||
TenantID: order.TenantId,
|
||||
OrderNo: order.OrderNo,
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
@@ -337,8 +337,8 @@ func (s *order) convertShippedOrderToDetail(order *entity.OrderShipped) dto.Orde
|
||||
// convertCompletedOrderToDetail 转换已完成订单为详情
|
||||
func (s *order) convertCompletedOrderToDetail(order *entity.OrderCompleted) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
ID: order.Id.Hex(),
|
||||
TenantID: order.TenantId,
|
||||
OrderNo: order.OrderNo,
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
@@ -395,12 +395,12 @@ func (s *order) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
||||
// CancelOrder 取消订单
|
||||
func (s *order) CancelOrder(ctx context.Context, req *dto.CancelOrderReq) (*dto.CancelOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.OrderNo == "" {
|
||||
if req.OrderNo == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -419,7 +419,7 @@ func (s *order) CancelOrder(ctx context.Context, req *dto.CancelOrderReq) (*dto.
|
||||
"cancel_reason": req.Reason,
|
||||
}
|
||||
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, req.TenantID, req.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, req.OrderNo, updateData); err != nil {
|
||||
return nil, fmt.Errorf("取消订单失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -435,10 +435,6 @@ func (s *order) CancelOrder(ctx context.Context, req *dto.CancelOrderReq) (*dto.
|
||||
// ListOrders 查询订单列表
|
||||
func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.ListOrdersResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" {
|
||||
return nil, errors.New("租户ID不能为空")
|
||||
}
|
||||
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
@@ -455,7 +451,7 @@ func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.Li
|
||||
status = consts.OrderStatusPending
|
||||
}
|
||||
|
||||
orders, total, err := dao.Order.ListOrdersByStatus(ctx, status, req.TenantID, req.UserID, req.Page, req.PageSize)
|
||||
orders, total, err := dao.Order.ListOrdersByStatus(ctx, status, req.UserID, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询订单列表失败: %w", err)
|
||||
}
|
||||
@@ -468,7 +464,7 @@ func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.Li
|
||||
if pendingOrders, ok := orders.([]entity.OrderPending); ok {
|
||||
for _, order := range pendingOrders {
|
||||
orderSummaries = append(orderSummaries, dto.OrderSummary{
|
||||
ID: order.ID.Hex(),
|
||||
ID: order.Id.Hex(),
|
||||
OrderNo: order.OrderNo,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
@@ -482,7 +478,7 @@ func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.Li
|
||||
if paidOrders, ok := orders.([]entity.OrderPaid); ok {
|
||||
for _, order := range paidOrders {
|
||||
orderSummaries = append(orderSummaries, dto.OrderSummary{
|
||||
ID: order.ID.Hex(),
|
||||
ID: order.Id.Hex(),
|
||||
OrderNo: order.OrderNo,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
@@ -508,9 +504,9 @@ func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.Li
|
||||
}
|
||||
|
||||
// ProcessExpiredOrders 处理过期订单
|
||||
func (s *order) ProcessExpiredOrders(ctx context.Context, tenantID string) error {
|
||||
func (s *order) ProcessExpiredOrders(ctx context.Context) error {
|
||||
// 获取过期的待支付订单
|
||||
expiredOrders, err := dao.Order.GetExpiredPendingOrders(ctx, tenantID)
|
||||
expiredOrders, err := dao.Order.GetExpiredPendingOrders(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取过期订单失败: %w", err)
|
||||
}
|
||||
@@ -521,7 +517,7 @@ func (s *order) ProcessExpiredOrders(ctx context.Context, tenantID string) error
|
||||
"cancel_reason": "订单超时自动取消",
|
||||
}
|
||||
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, tenantID, order.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, order.OrderNo, updateData); err != nil {
|
||||
// 记录错误但继续处理其他订单
|
||||
fmt.Printf("取消过期订单失败: %s, 错误: %v\n", order.OrderNo, err)
|
||||
}
|
||||
@@ -531,6 +527,6 @@ func (s *order) ProcessExpiredOrders(ctx context.Context, tenantID string) error
|
||||
}
|
||||
|
||||
// UpdatePayInfo 更新支付信息
|
||||
func (s *order) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
||||
return dao.Order.UpdatePayInfo(ctx, tenantID, orderNo, payInfo)
|
||||
func (s *order) UpdatePayInfo(ctx context.Context, orderNo string, payInfo entity.PayInfo) error {
|
||||
return dao.Order.UpdatePayInfo(ctx, orderNo, payInfo)
|
||||
}
|
||||
|
||||
@@ -7,11 +7,12 @@ import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"order/consts"
|
||||
"order/dao"
|
||||
"order/model/dto"
|
||||
"order/model/entity"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type payment struct{}
|
||||
@@ -27,7 +28,7 @@ func (s *payment) PayOrder(ctx context.Context, req *dto.PayOrderReq) (*dto.PayO
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -64,7 +65,6 @@ func (s *payment) PayOrder(ctx context.Context, req *dto.PayOrderReq) (*dto.PayO
|
||||
|
||||
// 6. 创建支付记录
|
||||
paymentRecord := &entity.PaymentRecord{
|
||||
TenantId: req.TenantID,
|
||||
OrderID: pendingOrder.Id,
|
||||
OrderNo: req.OrderNo,
|
||||
PayMethod: req.PayMethod,
|
||||
@@ -88,7 +88,7 @@ func (s *payment) PayOrder(ctx context.Context, req *dto.PayOrderReq) (*dto.PayO
|
||||
APPParams: payResp.APPParams,
|
||||
}
|
||||
|
||||
if err := dao.Order.UpdatePayInfo(ctx, req.TenantID, req.OrderNo, payInfo); err != nil {
|
||||
if err := dao.Order.UpdatePayInfo(ctx, req.OrderNo, payInfo); err != nil {
|
||||
return nil, fmt.Errorf("更新订单支付信息失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (s *payment) HandlePaymentNotify(ctx context.Context, req *dto.PaymentNotif
|
||||
"payment_channel": req.PayMethod,
|
||||
}
|
||||
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusPaid, req.TenantID, req.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusPaid, req.OrderNo, updateData); err != nil {
|
||||
return fmt.Errorf("更新订单状态失败: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -220,7 +220,7 @@ func (s *payment) RefundOrder(ctx context.Context, req *dto.RefundOrderReq) (*dt
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -252,7 +252,6 @@ func (s *payment) RefundOrder(ctx context.Context, req *dto.RefundOrderReq) (*dt
|
||||
|
||||
// 6. 创建退款记录
|
||||
refundRecord := &entity.RefundRecord{
|
||||
TenantId: req.TenantID,
|
||||
OrderID: paidOrder.Id,
|
||||
OrderNo: req.OrderNo,
|
||||
RefundNo: refundResp.RefundNo,
|
||||
@@ -271,7 +270,7 @@ func (s *payment) RefundOrder(ctx context.Context, req *dto.RefundOrderReq) (*dt
|
||||
"refund_reason": req.Reason,
|
||||
}
|
||||
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPaid, consts.OrderStatusRefunded, req.TenantID, req.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPaid, consts.OrderStatusRefunded, req.OrderNo, updateData); err != nil {
|
||||
return nil, fmt.Errorf("更新订单状态失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user