gomod引用
This commit is contained in:
108
service/order.go
108
service/order.go
@@ -28,13 +28,28 @@ func Init() error {
|
||||
func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
||||
var result []entity.OrderItem
|
||||
for _, item := range items {
|
||||
// 转换库存明细,每个库存项只会有一个实例
|
||||
var stocks []entity.OrderItemStock
|
||||
totalQuantity := len(item.Stocks) // 每个库存项数量为1
|
||||
totalAmount := int64(0)
|
||||
|
||||
for _, stock := range item.Stocks {
|
||||
stocks = append(stocks, entity.OrderItemStock{
|
||||
StockID: stock.StockID,
|
||||
Price: stock.Price,
|
||||
StockAttrs: stock.StockAttrs,
|
||||
})
|
||||
totalAmount += stock.Price // 每个库存项数量为1
|
||||
}
|
||||
|
||||
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,
|
||||
AssetID: item.AssetID,
|
||||
AssetName: item.AssetName,
|
||||
AssetType: item.AssetType,
|
||||
ImageURL: item.ImageURL,
|
||||
Quantity: totalQuantity,
|
||||
TotalPrice: totalAmount,
|
||||
Stocks: stocks,
|
||||
})
|
||||
}
|
||||
return result
|
||||
@@ -44,13 +59,24 @@ func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
||||
func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
// 转换库存明细
|
||||
var stocks []dto.OrderItemStock
|
||||
for _, stock := range item.Stocks {
|
||||
stocks = append(stocks, dto.OrderItemStock{
|
||||
StockID: stock.StockID,
|
||||
Price: stock.Price,
|
||||
StockAttrs: stock.StockAttrs,
|
||||
})
|
||||
}
|
||||
|
||||
result = append(result, dto.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
ImageURL: item.ImageURL,
|
||||
AssetID: item.AssetID,
|
||||
AssetName: item.AssetName,
|
||||
AssetType: item.AssetType,
|
||||
ImageURL: item.ImageURL,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
Stocks: stocks,
|
||||
})
|
||||
}
|
||||
return result
|
||||
@@ -60,13 +86,24 @@ func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
||||
func convertEntityOrderItemsToDTO(items []entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
// 转换库存明细
|
||||
var stocks []dto.OrderItemStock
|
||||
for _, stock := range item.Stocks {
|
||||
stocks = append(stocks, dto.OrderItemStock{
|
||||
StockID: stock.StockID,
|
||||
Price: stock.Price,
|
||||
StockAttrs: stock.StockAttrs,
|
||||
})
|
||||
}
|
||||
|
||||
result = append(result, dto.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
ImageURL: item.ImageURL,
|
||||
AssetID: item.AssetID,
|
||||
AssetName: item.AssetName,
|
||||
AssetType: item.AssetType,
|
||||
ImageURL: item.ImageURL,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
Stocks: stocks,
|
||||
})
|
||||
}
|
||||
return result
|
||||
@@ -87,11 +124,15 @@ func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.
|
||||
totalAmount := int64(0)
|
||||
for i := range req.OrderItems {
|
||||
item := &req.OrderItems[i]
|
||||
if item.Price <= 0 || item.Quantity <= 0 {
|
||||
return nil, fmt.Errorf("商品价格或数量无效: %s", item.ProductName)
|
||||
itemTotal := int64(0)
|
||||
for j := range item.Stocks {
|
||||
stock := &item.Stocks[j]
|
||||
if stock.Price <= 0 {
|
||||
return nil, fmt.Errorf("资产价格无效: %s", item.AssetName)
|
||||
}
|
||||
itemTotal += stock.Price // 每个库存项数量为1
|
||||
}
|
||||
totalPrice := item.Price * int64(item.Quantity)
|
||||
totalAmount += totalPrice
|
||||
totalAmount += itemTotal
|
||||
}
|
||||
|
||||
if totalAmount <= 0 {
|
||||
@@ -327,13 +368,24 @@ func (s *order) convertCompletedOrderToDetail(order *entity.OrderCompleted) dto.
|
||||
func (s *order) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
||||
var result []dto.OrderItem
|
||||
for _, item := range items {
|
||||
// 转换库存明细
|
||||
var stocks []dto.OrderItemStock
|
||||
for _, stock := range item.Stocks {
|
||||
stocks = append(stocks, dto.OrderItemStock{
|
||||
StockID: stock.StockID,
|
||||
Price: stock.Price,
|
||||
StockAttrs: stock.StockAttrs,
|
||||
})
|
||||
}
|
||||
|
||||
result = append(result, dto.OrderItem{
|
||||
ProductID: item.ProductID,
|
||||
ProductName: item.ProductName,
|
||||
Price: item.Price,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
ImageURL: item.ImageURL,
|
||||
AssetID: item.AssetID,
|
||||
AssetName: item.AssetName,
|
||||
AssetType: item.AssetType,
|
||||
ImageURL: item.ImageURL,
|
||||
Quantity: item.Quantity,
|
||||
TotalPrice: item.TotalPrice,
|
||||
Stocks: stocks,
|
||||
})
|
||||
}
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user