修复编译问题和版本兼容问题
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/cloudprint/cloudprint-backend/config"
|
||||
"github.com/cloudprint/cloudprint-backend/internal/model"
|
||||
|
||||
@@ -60,7 +60,7 @@ func (s *OrderService) CreateOrder(ctx context.Context, userID int32, items []*C
|
||||
|
||||
// Calculate total amount
|
||||
var totalAmount float64
|
||||
orderItems := make([]*model.OrderItem, 0, len(items))
|
||||
orderItems := make([]model.OrderItem, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
// Verify file exists
|
||||
@@ -69,13 +69,13 @@ func (s *OrderService) CreateOrder(ctx context.Context, userID int32, items []*C
|
||||
return nil, fmt.Errorf("file not found: %d", item.FileID)
|
||||
}
|
||||
|
||||
// Calculate price
|
||||
price, err := s.calculatePrice(ctx, item)
|
||||
// Calculate price based on file type and settings
|
||||
price, err := s.calculatePrice(ctx, item, file.FileType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderItem := &model.OrderItem{
|
||||
orderItem := model.OrderItem{
|
||||
FileID: item.FileID,
|
||||
PrinterID: intPtr(item.PrinterID),
|
||||
Copies: item.Copies,
|
||||
@@ -98,43 +98,64 @@ func (s *OrderService) CreateOrder(ctx context.Context, userID int32, items []*C
|
||||
Items: orderItems,
|
||||
}
|
||||
|
||||
// Convert to pointer slice for repository
|
||||
orderItemsPtr := make([]*model.OrderItem, len(orderItems))
|
||||
for i := range orderItems {
|
||||
orderItemsPtr[i] = &orderItems[i]
|
||||
}
|
||||
|
||||
// Save order with items
|
||||
if err := s.orderRepo.CreateWithItems(ctx, order, orderItems); err != nil {
|
||||
if err := s.orderRepo.CreateWithItems(ctx, order, orderItemsPtr); err != nil {
|
||||
return nil, fmt.Errorf("failed to create order: %w", err)
|
||||
}
|
||||
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func (s *OrderService) calculatePrice(ctx context.Context, item *CreateOrderItem) (float64, error) {
|
||||
// Get price list
|
||||
func (s *OrderService) calculatePrice(ctx context.Context, item *CreateOrderItem, fileType string) (float64, error) {
|
||||
// Get price list from database
|
||||
prices, err := s.priceRepo.GetAllActive(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("failed to get price list: %w", err)
|
||||
}
|
||||
|
||||
// Default price calculation (A4 B&W per page)
|
||||
defaultPrice := 0.10
|
||||
colorMultiplier := 3.0
|
||||
duplexDiscount := 0.8
|
||||
if len(prices) == 0 {
|
||||
return 0, fmt.Errorf("no active price items found")
|
||||
}
|
||||
|
||||
// Calculate based on file type and settings
|
||||
// For simplicity, using default values
|
||||
// In production, look up from price list based on file type
|
||||
// Find matching price based on file type and color setting
|
||||
var basePrice float64
|
||||
for _, p := range prices {
|
||||
// Match by type (A4/A3/照片纸) - default to A4 if no match
|
||||
if p.Type == "A4" || (p.Type == "" && basePrice == 0) {
|
||||
if !item.Color || (item.Color && p.ColorEnabled) {
|
||||
basePrice = p.Price
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pageCount := 1 // Default, should get actual page count from file
|
||||
// Fallback to first available price if no match found
|
||||
if basePrice == 0 {
|
||||
basePrice = prices[0].Price
|
||||
}
|
||||
|
||||
// Default values if not set
|
||||
if item.Copies <= 0 {
|
||||
item.Copies = 1
|
||||
}
|
||||
|
||||
price := float64(pageCount) * defaultPrice * float64(item.Copies)
|
||||
// Calculate total price
|
||||
price := basePrice * float64(item.Copies)
|
||||
|
||||
// Apply color multiplier if color printing
|
||||
if item.Color {
|
||||
price *= colorMultiplier
|
||||
price *= 3.0 // Color costs 3x
|
||||
}
|
||||
|
||||
// Apply duplex discount
|
||||
if item.Duplex {
|
||||
price *= duplexDiscount
|
||||
price *= 0.8 // Duplex gets 20% off
|
||||
}
|
||||
|
||||
return price, nil
|
||||
|
||||
Reference in New Issue
Block a user