diff --git a/model/dto/order.go b/model/dto/order.go index cdb9943..7cbd76a 100644 --- a/model/dto/order.go +++ b/model/dto/order.go @@ -19,11 +19,19 @@ type CreateOrderReq struct { // OrderItemReq 创建订单商品项请求 type OrderItemReq struct { - ProductID string `json:"product_id" binding:"required"` // 商品ID - ProductName string `json:"product_name" binding:"required"` // 商品名称 - Price int64 `json:"price" binding:"required,min=1"` // 单价(分) - Quantity int `json:"quantity" binding:"required,min=1"` // 数量 - ImageURL string `json:"image_url"` // 商品图片 + AssetID string `json:"asset_id" binding:"required"` // 资产ID + AssetName string `json:"asset_name" binding:"required"` // 资产名称 + AssetType string `json:"asset_type" binding:"required"` // 资产类型:product-商品型,service-服务型,software-软件型 + ImageURL string `json:"image_url"` // 资产图片 + Stocks []OrderItemStockReq `json:"stocks" binding:"required"` // 库存项列表 +} + +// OrderItemStockReq 创建订单商品项库存明细请求 + +type OrderItemStockReq struct { + StockID string `json:"stock_id" binding:"required"` // 库存ID + Price int64 `json:"price" binding:"required,min=1"` // 单价(分) + StockAttrs map[string]interface{} `json:"stock_attrs"` // 库存项属性(动态字段) } // ShippingInfoReq 收货信息请求 @@ -111,12 +119,21 @@ type OrderDetail struct { // OrderItem 订单商品项(响应) type OrderItem struct { - ProductID string `json:"product_id"` // 商品ID - ProductName string `json:"product_name"` // 商品名称 - Price int64 `json:"price"` // 单价(分) - Quantity int `json:"quantity"` // 数量 - TotalPrice int64 `json:"total_price"` // 小计(分) - ImageURL string `json:"image_url"` // 商品图片 + AssetID string `json:"asset_id"` // 资产ID + AssetName string `json:"asset_name"` // 资产名称 + AssetType string `json:"asset_type"` // 资产类型:product-商品型,service-服务型,software-软件型 + ImageURL string `json:"image_url"` // 资产图片 + Quantity int `json:"quantity"` // 总数量 + TotalPrice int64 `json:"total_price"` // 小计(分) + Stocks []OrderItemStock `json:"stocks"` // 库存项列表 +} + +// OrderItemStock 订单商品项库存明细(响应) + +type OrderItemStock struct { + StockID string `json:"stock_id"` // 库存ID + Price int64 `json:"price"` // 单价(分) + StockAttrs map[string]interface{} `json:"stock_attrs"` // 库存项属性(动态字段) } // ShippingInfo 收货信息(响应) diff --git a/model/entity/order_base.go b/model/entity/order_base.go index 0e2305b..ed5f736 100644 --- a/model/entity/order_base.go +++ b/model/entity/order_base.go @@ -31,12 +31,21 @@ type OrderBase struct { // 按状态拆分的订单表会包含这个结构 type OrderItem struct { - ProductID string `bson:"product_id" json:"product_id"` // 商品ID - ProductName string `bson:"product_name" json:"product_name"` // 商品名称 - Price int64 `bson:"price" json:"price"` // 单价(分) - Quantity int `bson:"quantity" json:"quantity"` // 数量 - TotalPrice int64 `bson:"total_price" json:"total_price"` // 小计(分) - ImageURL string `bson:"image_url,omitempty" json:"image_url"` // 商品图片 + AssetID string `bson:"asset_id" json:"asset_id"` // 资产ID + AssetName string `bson:"asset_name" json:"asset_name"` // 资产名称 + AssetType string `bson:"asset_type" json:"asset_type"` // 资产类型:product-商品型,service-服务型,software-软件型 + ImageURL string `bson:"image_url,omitempty" json:"image_url"` // 资产图片 + Quantity int `bson:"quantity" json:"quantity"` // 总数量 + TotalPrice int64 `bson:"total_price" json:"total_price"` // 小计(分) + Stocks []OrderItemStock `bson:"stocks" json:"stocks"` // 库存项列表 +} + +// OrderItemStock 订单商品项库存明细 +// 用于追溯具体使用了哪些库存项,一个库存项只会有一个实例 +type OrderItemStock struct { + StockID string `bson:"stock_id" json:"stock_id"` // 库存ID + Price int64 `bson:"price" json:"price"` // 该库存项的单价(分) + StockAttrs map[string]interface{} `bson:"stock_attrs,omitempty" json:"stock_attrs"` // 库存项属性(动态字段,存储该库存项的具体规格属性) } // ShippingInfo 收货信息 diff --git a/service/order.go b/service/order.go index aeb2dcc..cf0a5a0 100644 --- a/service/order.go +++ b/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