99 lines
3.0 KiB
Go
99 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/cloudprint/cloudprint-backend/internal/service"
|
|
pb "github.com/cloudprint/cloudprint-backend/proto/generated"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type OrderServiceServer struct {
|
|
pb.UnimplementedOrderServiceServer
|
|
orderService *service.OrderService
|
|
}
|
|
|
|
func NewOrderServiceServer(orderService *service.OrderService) *OrderServiceServer {
|
|
return &OrderServiceServer{
|
|
orderService: orderService,
|
|
}
|
|
}
|
|
|
|
func (s *OrderServiceServer) CreateOrder(ctx context.Context, req *pb.CreateOrderRequest) (*pb.CreateOrderResponse, error) {
|
|
// Extract user ID from context (set by auth interceptor)
|
|
userID := int32(1) // Placeholder
|
|
|
|
items := make([]*service.CreateOrderItem, 0, len(req.Items))
|
|
for _, item := range req.Items {
|
|
items = append(items, &service.CreateOrderItem{
|
|
FileID: item.FileId,
|
|
PrinterID: item.PrinterId,
|
|
Copies: int(item.Copies),
|
|
Color: item.Color,
|
|
Duplex: item.Duplex,
|
|
PageRange: item.PageRange,
|
|
})
|
|
}
|
|
|
|
order, err := s.orderService.CreateOrder(ctx, userID, items, req.Remark)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "create order failed: %v", err)
|
|
}
|
|
|
|
return &pb.CreateOrderResponse{
|
|
OrderNo: order.OrderNo,
|
|
TotalAmount: order.TotalAmount,
|
|
}, nil
|
|
}
|
|
|
|
func (s *OrderServiceServer) GetOrder(ctx context.Context, req *pb.GetOrderRequest) (*pb.Order, error) {
|
|
order, err := s.orderService.GetOrder(ctx, req.OrderNo)
|
|
if err != nil || order == nil {
|
|
return nil, status.Errorf(codes.NotFound, "order not found")
|
|
}
|
|
|
|
return convertOrderToPB(order), nil
|
|
}
|
|
|
|
func (s *OrderServiceServer) GetOrderList(ctx context.Context, req *pb.GetOrderListRequest) (*pb.OrderListResponse, error) {
|
|
orders, total, err := s.orderService.GetOrderList(ctx, req.UserId, req.Status, int(req.Page), int(req.PageSize))
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "get order list failed: %v", err)
|
|
}
|
|
|
|
pbOrders := make([]*pb.Order, 0, len(orders))
|
|
for _, order := range orders {
|
|
pbOrders = append(pbOrders, convertOrderToPB(order))
|
|
}
|
|
|
|
return &pb.OrderListResponse{
|
|
Orders: pbOrders,
|
|
Total: int32(total),
|
|
}, nil
|
|
}
|
|
|
|
func (s *OrderServiceServer) UpdateOrderStatus(ctx context.Context, req *pb.UpdateOrderStatusRequest) (*pb.Empty, error) {
|
|
err := s.orderService.UpdateOrderStatus(ctx, req.OrderNo, int8(req.Status))
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "update order status failed: %v", err)
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|
|
|
|
func (s *OrderServiceServer) CancelOrder(ctx context.Context, req *pb.CancelOrderRequest) (*pb.Empty, error) {
|
|
err := s.orderService.CancelOrder(ctx, req.OrderNo)
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "cancel order failed: %v", err)
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|
|
|
|
func convertOrderToPB(order *service.Order) *pb.Order {
|
|
// Note: This is a simplified conversion. In production, you would need to handle all fields properly.
|
|
// The service.Order type here is the model, not the protobuf type.
|
|
return nil // Placeholder
|
|
}
|