初始化项目
This commit is contained in:
147
service/order.go
147
service/order.go
@@ -5,31 +5,70 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"order/consts"
|
||||
"order/dao"
|
||||
"order/model/dto"
|
||||
"order/model/entity"
|
||||
)
|
||||
|
||||
// OrderService 订单服务
|
||||
type order struct{}
|
||||
|
||||
type OrderService struct {
|
||||
orderDao *dao.OrderDao
|
||||
// Order 订单服务
|
||||
var Order = new(order)
|
||||
|
||||
// convertOrderItemsFromDTO 从DTO转换订单商品项
|
||||
func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
||||
var result []entity.OrderItem
|
||||
for _, item := range items {
|
||||
result = append(result, entity.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.Price * int64(item.Quantity),
|
||||
ImageURL: item.ImageURL,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// NewOrderService 创建订单服务实例
|
||||
func NewOrderService(orderDao *dao.OrderDao) *OrderService {
|
||||
return &OrderService{
|
||||
orderDao: orderDao,
|
||||
// convertOrderItems 转换订单商品项
|
||||
func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
result = append(result, dto.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
ImageURL: item.ImageURL,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// convertEntityOrderItemsToDTO 转换entity订单商品项到DTO
|
||||
func convertEntityOrderItemsToDTO(items []entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
result = append(result, dto.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
ImageURL: item.ImageURL,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// CreateOrder 创建订单
|
||||
func (s *OrderService) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.CreateOrderResp, error) {
|
||||
func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.CreateOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.UserID == "" || req.Subject == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
@@ -46,8 +85,8 @@ func (s *OrderService) CreateOrder(ctx context.Context, req *dto.CreateOrderReq)
|
||||
if item.Price <= 0 || item.Quantity <= 0 {
|
||||
return nil, fmt.Errorf("商品价格或数量无效: %s", item.ProductName)
|
||||
}
|
||||
item.TotalPrice = item.Price * int64(item.Quantity)
|
||||
totalAmount += item.TotalPrice
|
||||
totalPrice := item.Price * int64(item.Quantity)
|
||||
totalAmount += totalPrice
|
||||
}
|
||||
|
||||
if totalAmount <= 0 {
|
||||
@@ -73,13 +112,13 @@ func (s *OrderService) CreateOrder(ctx context.Context, req *dto.CreateOrderReq)
|
||||
Description: req.Description,
|
||||
ExpiredAt: &expiredAt,
|
||||
},
|
||||
OrderItems: gconv.Slice[*entity.OrderItem](req.OrderItems),
|
||||
OrderItems: convertOrderItemsFromDTO(req.OrderItems),
|
||||
ShippingInfo: (*entity.ShippingInfo)(&req.ShippingInfo),
|
||||
PayInfo: entity.PayInfo{},
|
||||
}
|
||||
|
||||
// 6. 保存订单
|
||||
if err := s.orderDao.CreatePendingOrder(ctx, order); err != nil {
|
||||
if err := dao.Order.CreatePendingOrder(ctx, order); err != nil {
|
||||
return nil, fmt.Errorf("创建订单失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -95,21 +134,21 @@ func (s *OrderService) CreateOrder(ctx context.Context, req *dto.CreateOrderReq)
|
||||
}
|
||||
|
||||
// generateOrderNo 生成订单号
|
||||
func (s *OrderService) generateOrderNo(tenantID string) string {
|
||||
func (s *order) generateOrderNo(tenantID string) string {
|
||||
timestamp := time.Now().Format("20060102150405")
|
||||
random := rand.Intn(10000)
|
||||
return fmt.Sprintf("%s%s%04d", tenantID, timestamp, random)
|
||||
}
|
||||
|
||||
// QueryOrder 查询订单详情
|
||||
func (s *OrderService) QueryOrder(ctx context.Context, req *dto.QueryOrderReq) (*dto.QueryOrderResp, error) {
|
||||
func (s *order) QueryOrder(ctx context.Context, req *dto.QueryOrderReq) (*dto.QueryOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.OrderNo == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := s.orderDao.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -122,19 +161,19 @@ func (s *OrderService) QueryOrder(ctx context.Context, req *dto.QueryOrderReq) (
|
||||
var resp dto.QueryOrderResp
|
||||
|
||||
switch status {
|
||||
case entity.OrderStatusPending:
|
||||
case consts.OrderStatusPending:
|
||||
if pendingOrder, ok := order.(*entity.OrderPending); ok {
|
||||
resp.Order = s.convertPendingOrderToDetail(pendingOrder)
|
||||
}
|
||||
case entity.OrderStatusPaid:
|
||||
case consts.OrderStatusPaid:
|
||||
if paidOrder, ok := order.(*entity.OrderPaid); ok {
|
||||
resp.Order = s.convertPaidOrderToDetail(paidOrder)
|
||||
}
|
||||
case entity.OrderStatusShipped:
|
||||
case consts.OrderStatusShipped:
|
||||
if shippedOrder, ok := order.(*entity.OrderShipped); ok {
|
||||
resp.Order = s.convertShippedOrderToDetail(shippedOrder)
|
||||
}
|
||||
case entity.OrderStatusCompleted:
|
||||
case consts.OrderStatusCompleted:
|
||||
if completedOrder, ok := order.(*entity.OrderCompleted); ok {
|
||||
resp.Order = s.convertCompletedOrderToDetail(completedOrder)
|
||||
}
|
||||
@@ -146,7 +185,7 @@ func (s *OrderService) QueryOrder(ctx context.Context, req *dto.QueryOrderReq) (
|
||||
}
|
||||
|
||||
// convertPendingOrderToDetail 转换待支付订单为详情
|
||||
func (s *OrderService) convertPendingOrderToDetail(order *entity.OrderPending) dto.OrderDetail {
|
||||
func (s *order) convertPendingOrderToDetail(order *entity.OrderPending) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
@@ -154,13 +193,13 @@ func (s *OrderService) convertPendingOrderToDetail(order *entity.OrderPending) d
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusPending),
|
||||
Status: string(consts.OrderStatusPending),
|
||||
PayMethod: order.PayMethod,
|
||||
PayStatus: "unpaid",
|
||||
OrderType: order.OrderType,
|
||||
Subject: order.Subject,
|
||||
Description: order.Description,
|
||||
OrderItems: s.convertOrderItems(order.OrderItems),
|
||||
OrderItems: convertOrderItems(order.OrderItems),
|
||||
ShippingInfo: dto.ShippingInfo{
|
||||
Consignee: order.ShippingInfo.Consignee,
|
||||
Phone: order.ShippingInfo.Phone,
|
||||
@@ -183,7 +222,7 @@ func (s *OrderService) convertPendingOrderToDetail(order *entity.OrderPending) d
|
||||
}
|
||||
|
||||
// convertPaidOrderToDetail 转换已支付订单为详情
|
||||
func (s *OrderService) convertPaidOrderToDetail(order *entity.OrderPaid) dto.OrderDetail {
|
||||
func (s *order) convertPaidOrderToDetail(order *entity.OrderPaid) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
@@ -191,13 +230,13 @@ func (s *OrderService) convertPaidOrderToDetail(order *entity.OrderPaid) dto.Ord
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusPaid),
|
||||
Status: string(consts.OrderStatusPaid),
|
||||
PayMethod: order.PayMethod,
|
||||
PayStatus: "paid",
|
||||
OrderType: order.OrderType,
|
||||
Subject: order.Subject,
|
||||
Description: order.Description,
|
||||
OrderItems: s.convertOrderItems(order.OrderItems),
|
||||
OrderItems: convertOrderItems(order.OrderItems),
|
||||
ShippingInfo: dto.ShippingInfo{
|
||||
Consignee: order.ShippingInfo.Consignee,
|
||||
Phone: order.ShippingInfo.Phone,
|
||||
@@ -218,7 +257,7 @@ func (s *OrderService) convertPaidOrderToDetail(order *entity.OrderPaid) dto.Ord
|
||||
}
|
||||
|
||||
// convertShippedOrderToDetail 转换已发货订单为详情
|
||||
func (s *OrderService) convertShippedOrderToDetail(order *entity.OrderShipped) dto.OrderDetail {
|
||||
func (s *order) convertShippedOrderToDetail(order *entity.OrderShipped) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
@@ -226,13 +265,13 @@ func (s *OrderService) convertShippedOrderToDetail(order *entity.OrderShipped) d
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusShipped),
|
||||
Status: string(consts.OrderStatusShipped),
|
||||
PayMethod: order.PayMethod,
|
||||
PayStatus: "paid",
|
||||
OrderType: order.OrderType,
|
||||
Subject: order.Subject,
|
||||
Description: order.Description,
|
||||
OrderItems: s.convertOrderItems(order.OrderItems),
|
||||
OrderItems: convertOrderItems(order.OrderItems),
|
||||
ShippingInfo: dto.ShippingInfo{
|
||||
Consignee: order.ShippingInfo.Consignee,
|
||||
Phone: order.ShippingInfo.Phone,
|
||||
@@ -249,7 +288,7 @@ func (s *OrderService) convertShippedOrderToDetail(order *entity.OrderShipped) d
|
||||
}
|
||||
|
||||
// convertCompletedOrderToDetail 转换已完成订单为详情
|
||||
func (s *OrderService) convertCompletedOrderToDetail(order *entity.OrderCompleted) dto.OrderDetail {
|
||||
func (s *order) convertCompletedOrderToDetail(order *entity.OrderCompleted) dto.OrderDetail {
|
||||
return dto.OrderDetail{
|
||||
ID: order.ID.Hex(),
|
||||
TenantID: order.TenantID,
|
||||
@@ -257,13 +296,13 @@ func (s *OrderService) convertCompletedOrderToDetail(order *entity.OrderComplete
|
||||
UserID: order.UserID,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusCompleted),
|
||||
Status: string(consts.OrderStatusCompleted),
|
||||
PayMethod: order.PayMethod,
|
||||
PayStatus: "paid",
|
||||
OrderType: order.OrderType,
|
||||
Subject: order.Subject,
|
||||
Description: order.Description,
|
||||
OrderItems: s.convertOrderItems(order.OrderItems),
|
||||
OrderItems: convertOrderItems(order.OrderItems),
|
||||
ShippingInfo: dto.ShippingInfo{
|
||||
Consignee: order.ShippingInfo.Consignee,
|
||||
Phone: order.ShippingInfo.Phone,
|
||||
@@ -280,7 +319,7 @@ func (s *OrderService) convertCompletedOrderToDetail(order *entity.OrderComplete
|
||||
}
|
||||
|
||||
// convertOrderItems 转换订单商品项
|
||||
func (s *OrderService) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
||||
func (s *order) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
result = append(result, dto.OrderItem{
|
||||
@@ -296,14 +335,14 @@ func (s *OrderService) convertOrderItems(items []*entity.OrderItem) []dto.OrderI
|
||||
}
|
||||
|
||||
// CancelOrder 取消订单
|
||||
func (s *OrderService) CancelOrder(ctx context.Context, req *dto.CancelOrderReq) (*dto.CancelOrderResp, error) {
|
||||
func (s *order) CancelOrder(ctx context.Context, req *dto.CancelOrderReq) (*dto.CancelOrderResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" || req.OrderNo == "" {
|
||||
return nil, errors.New("必填参数不能为空")
|
||||
}
|
||||
|
||||
// 2. 查询订单
|
||||
order, status, err := s.orderDao.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
order, status, err := dao.Order.GetOrderByNo(ctx, req.TenantID, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取订单失败: %w", err)
|
||||
}
|
||||
@@ -313,7 +352,7 @@ func (s *OrderService) CancelOrder(ctx context.Context, req *dto.CancelOrderReq)
|
||||
}
|
||||
|
||||
// 3. 检查订单状态(只有待支付订单可以取消)
|
||||
if status != entity.OrderStatusPending {
|
||||
if status != consts.OrderStatusPending {
|
||||
return nil, fmt.Errorf("订单状态不正确,当前状态: %s", status)
|
||||
}
|
||||
|
||||
@@ -322,21 +361,21 @@ func (s *OrderService) CancelOrder(ctx context.Context, req *dto.CancelOrderReq)
|
||||
"cancel_reason": req.Reason,
|
||||
}
|
||||
|
||||
if err := s.orderDao.MoveOrderToStatus(ctx, entity.OrderStatusPending, entity.OrderStatusCancelled, req.TenantID, req.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, req.TenantID, req.OrderNo, updateData); err != nil {
|
||||
return nil, fmt.Errorf("取消订单失败: %w", err)
|
||||
}
|
||||
|
||||
// 5. 返回结果
|
||||
resp := &dto.CancelOrderResp{
|
||||
OrderNo: req.OrderNo,
|
||||
Status: string(entity.OrderStatusCancelled),
|
||||
Status: string(consts.OrderStatusCancelled),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// ListOrders 查询订单列表
|
||||
func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.ListOrdersResp, error) {
|
||||
func (s *order) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (*dto.ListOrdersResp, error) {
|
||||
// 1. 参数验证
|
||||
if req.TenantID == "" {
|
||||
return nil, errors.New("租户ID不能为空")
|
||||
@@ -350,15 +389,15 @@ func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (
|
||||
}
|
||||
|
||||
// 2. 根据状态查询订单列表
|
||||
var status entity.OrderStatus
|
||||
var status consts.OrderStatus
|
||||
if req.Status != "" {
|
||||
status = entity.OrderStatus(req.Status)
|
||||
status = consts.OrderStatus(req.Status)
|
||||
} else {
|
||||
// 默认查询所有状态
|
||||
status = entity.OrderStatusPending
|
||||
status = consts.OrderStatusPending
|
||||
}
|
||||
|
||||
orders, total, err := s.orderDao.ListOrdersByStatus(ctx, status, req.TenantID, req.UserID, req.Page, req.PageSize)
|
||||
orders, total, err := dao.Order.ListOrdersByStatus(ctx, status, req.TenantID, req.UserID, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询订单列表失败: %w", err)
|
||||
}
|
||||
@@ -367,7 +406,7 @@ func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (
|
||||
var orderSummaries []dto.OrderSummary
|
||||
|
||||
switch status {
|
||||
case entity.OrderStatusPending:
|
||||
case consts.OrderStatusPending:
|
||||
if pendingOrders, ok := orders.([]entity.OrderPending); ok {
|
||||
for _, order := range pendingOrders {
|
||||
orderSummaries = append(orderSummaries, dto.OrderSummary{
|
||||
@@ -375,13 +414,13 @@ func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (
|
||||
OrderNo: order.OrderNo,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusPending),
|
||||
Status: string(consts.OrderStatusPending),
|
||||
Subject: order.Subject,
|
||||
CreatedAt: order.CreatedAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
case entity.OrderStatusPaid:
|
||||
case consts.OrderStatusPaid:
|
||||
if paidOrders, ok := orders.([]entity.OrderPaid); ok {
|
||||
for _, order := range paidOrders {
|
||||
orderSummaries = append(orderSummaries, dto.OrderSummary{
|
||||
@@ -389,7 +428,7 @@ func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (
|
||||
OrderNo: order.OrderNo,
|
||||
TotalAmount: order.TotalAmount,
|
||||
PayAmount: order.PayAmount,
|
||||
Status: string(entity.OrderStatusPaid),
|
||||
Status: string(consts.OrderStatusPaid),
|
||||
Subject: order.Subject,
|
||||
CreatedAt: order.CreatedAt,
|
||||
PaidAt: &order.PaidAt,
|
||||
@@ -411,9 +450,9 @@ func (s *OrderService) ListOrders(ctx context.Context, req *dto.ListOrdersReq) (
|
||||
}
|
||||
|
||||
// ProcessExpiredOrders 处理过期订单
|
||||
func (s *OrderService) ProcessExpiredOrders(ctx context.Context, tenantID string) error {
|
||||
func (s *order) ProcessExpiredOrders(ctx context.Context, tenantID string) error {
|
||||
// 获取过期的待支付订单
|
||||
expiredOrders, err := s.orderDao.GetExpiredPendingOrders(ctx, tenantID)
|
||||
expiredOrders, err := dao.Order.GetExpiredPendingOrders(ctx, tenantID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取过期订单失败: %w", err)
|
||||
}
|
||||
@@ -424,7 +463,7 @@ func (s *OrderService) ProcessExpiredOrders(ctx context.Context, tenantID string
|
||||
"cancel_reason": "订单超时自动取消",
|
||||
}
|
||||
|
||||
if err := s.orderDao.MoveOrderToStatus(ctx, entity.OrderStatusPending, entity.OrderStatusCancelled, tenantID, order.OrderNo, updateData); err != nil {
|
||||
if err := dao.Order.MoveOrderToStatus(ctx, consts.OrderStatusPending, consts.OrderStatusCancelled, tenantID, order.OrderNo, updateData); err != nil {
|
||||
// 记录错误但继续处理其他订单
|
||||
fmt.Printf("取消过期订单失败: %s, 错误: %v\n", order.OrderNo, err)
|
||||
}
|
||||
@@ -434,6 +473,6 @@ func (s *OrderService) ProcessExpiredOrders(ctx context.Context, tenantID string
|
||||
}
|
||||
|
||||
// UpdatePayInfo 更新支付信息
|
||||
func (s *OrderService) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
||||
return s.orderDao.UpdatePayInfo(ctx, tenantID, orderNo, payInfo)
|
||||
func (s *order) UpdatePayInfo(ctx context.Context, tenantID, orderNo string, payInfo entity.PayInfo) error {
|
||||
return dao.Order.UpdatePayInfo(ctx, tenantID, orderNo, payInfo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user