From 30eb8bcf89caca3605b721d4b4779e2792475418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Sun, 4 Jan 2026 12:20:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E4=BA=A7=E5=A2=9E=E5=8A=A0=E6=89=B9?= =?UTF-8?q?=E6=AC=A1=E5=BA=93=E5=AD=98=E7=AE=A1=E7=90=86=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/dto/order.go | 30 +++++++++++++++++++++++++----- model/entity/order_base.go | 25 +++++++++++++++++++++---- service/order.go | 29 +++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/model/dto/order.go b/model/dto/order.go index 7911dac..4f4eae2 100644 --- a/model/dto/order.go +++ b/model/dto/order.go @@ -32,9 +32,19 @@ type OrderItemReq struct { // 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"` // 库存项属性(动态字段) + // 库存ID(明细模式必填,批次模式为空) + StockID string `json:"stock_id,omitempty"` // 库存ID + // 批次信息(批次模式必填,明细模式为空) + BatchID string `json:"batch_id,omitempty"` // 批次ID + BatchNo string `json:"batch_no,omitempty"` // 批次号 + // 数量 + Quantity int `json:"quantity" binding:"required,min=1"` // 使用数量 + // 价格信息 + Price int64 `json:"price" binding:"required,min=1"` // 单价(分) + // 库存管理模式 + StockMode int `json:"stock_mode" binding:"required"` // 库存管理模式:1-明细模式,2-批次模式 + // 库存项属性 + StockAttrs map[string]interface{} `json:"stock_attrs"` // 库存项属性(动态字段) } // ShippingInfoReq 收货信息请求 @@ -136,8 +146,18 @@ type OrderItem struct { // OrderItemStock 订单商品项库存明细(响应) type OrderItemStock struct { - StockID string `json:"stock_id"` // 库存ID - Price int64 `json:"price"` // 单价(分) + // 库存ID(明细模式) + StockID string `json:"stock_id,omitempty"` // 库存ID + // 批次信息(批次模式) + BatchID string `json:"batch_id,omitempty"` // 批次ID + BatchNo string `json:"batch_no,omitempty"` // 批次号 + // 数量 + Quantity int `json:"quantity"` // 使用数量 + // 价格信息 + Price int64 `json:"price"` // 单价(分) + // 库存管理模式 + StockMode int `json:"stock_mode"` // 库存管理模式:1-明细模式,2-批次模式 + // 库存项属性 StockAttrs map[string]interface{} `json:"stock_attrs"` // 库存项属性(动态字段) } diff --git a/model/entity/order_base.go b/model/entity/order_base.go index ba9353c..afab278 100644 --- a/model/entity/order_base.go +++ b/model/entity/order_base.go @@ -40,11 +40,28 @@ type OrderItem struct { } // OrderItemStock 订单商品项库存明细 -// 用于追溯具体使用了哪些库存项,一个库存项只会有一个实例 +// 用于追溯具体使用了哪些库存项,支持明细模式和批次模式 +// 明细模式:一个库存项对应一条记录,数量固定为1 +// 批次模式:一个批次记录可以包含多个数量 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"` // 库存项属性(动态字段,存储该库存项的具体规格属性) + // 库存ID(明细模式必填,批次模式为空) + StockID string `bson:"stock_id,omitempty" json:"stock_id,omitempty"` + + // 批次信息(批次模式必填,明细模式为空) + BatchID string `bson:"batch_id,omitempty" json:"batch_id,omitempty"` // 批次ID + BatchNo string `bson:"batch_no,omitempty" json:"batch_no,omitempty"` // 批次号 + + // 数量(明细模式固定为1,批次模式可以>1) + Quantity int `bson:"quantity" json:"quantity"` // 使用数量 + + // 价格信息 + Price int64 `bson:"price" json:"price"` // 该库存项的单价(分) + + // 库存管理模式 + StockMode int `bson:"stock_mode" json:"stock_mode"` // 库存管理模式:1-明细模式,2-批次模式 + + // 库存项属性(动态字段,存储该库存项的具体规格属性) + StockAttrs map[string]interface{} `bson:"stock_attrs,omitempty" json:"stock_attrs,omitempty"` } // ShippingInfo 收货信息 diff --git a/service/order.go b/service/order.go index 433ff55..ec94fe6 100644 --- a/service/order.go +++ b/service/order.go @@ -39,18 +39,23 @@ var Order = new(order) func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem { var result []entity.OrderItem for _, item := range items { - // 转换库存明细,每个库存项只会有一个实例 + // 转换库存明细,支持明细模式和批次模式 var stocks []entity.OrderItemStock - totalQuantity := len(item.Stocks) // 每个库存项数量为1 + totalQuantity := 0 totalAmount := int64(0) for _, stock := range item.Stocks { stocks = append(stocks, entity.OrderItemStock{ StockID: stock.StockID, + BatchID: stock.BatchID, + BatchNo: stock.BatchNo, + Quantity: stock.Quantity, Price: stock.Price, + StockMode: stock.StockMode, StockAttrs: stock.StockAttrs, }) - totalAmount += stock.Price // 每个库存项数量为1 + totalQuantity += stock.Quantity + totalAmount += int64(stock.Quantity) * stock.Price } result = append(result, entity.OrderItem{ @@ -70,12 +75,16 @@ 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, + BatchID: stock.BatchID, + BatchNo: stock.BatchNo, + Quantity: stock.Quantity, Price: stock.Price, + StockMode: stock.StockMode, StockAttrs: stock.StockAttrs, }) } @@ -97,12 +106,16 @@ 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, + BatchID: stock.BatchID, + BatchNo: stock.BatchNo, + Quantity: stock.Quantity, Price: stock.Price, + StockMode: stock.StockMode, StockAttrs: stock.StockAttrs, }) } @@ -412,12 +425,16 @@ 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, + BatchID: stock.BatchID, + BatchNo: stock.BatchNo, + Quantity: stock.Quantity, Price: stock.Price, + StockMode: stock.StockMode, StockAttrs: stock.StockAttrs, }) }