gomod引用
This commit is contained in:
@@ -19,11 +19,19 @@ type CreateOrderReq struct {
|
|||||||
// OrderItemReq 创建订单商品项请求
|
// OrderItemReq 创建订单商品项请求
|
||||||
|
|
||||||
type OrderItemReq struct {
|
type OrderItemReq struct {
|
||||||
ProductID string `json:"product_id" binding:"required"` // 商品ID
|
AssetID string `json:"asset_id" binding:"required"` // 资产ID
|
||||||
ProductName string `json:"product_name" binding:"required"` // 商品名称
|
AssetName string `json:"asset_name" binding:"required"` // 资产名称
|
||||||
Price int64 `json:"price" binding:"required,min=1"` // 单价(分)
|
AssetType string `json:"asset_type" binding:"required"` // 资产类型:product-商品型,service-服务型,software-软件型
|
||||||
Quantity int `json:"quantity" binding:"required,min=1"` // 数量
|
ImageURL string `json:"image_url"` // 资产图片
|
||||||
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 收货信息请求
|
// ShippingInfoReq 收货信息请求
|
||||||
@@ -111,12 +119,21 @@ type OrderDetail struct {
|
|||||||
// OrderItem 订单商品项(响应)
|
// OrderItem 订单商品项(响应)
|
||||||
|
|
||||||
type OrderItem struct {
|
type OrderItem struct {
|
||||||
ProductID string `json:"product_id"` // 商品ID
|
AssetID string `json:"asset_id"` // 资产ID
|
||||||
ProductName string `json:"product_name"` // 商品名称
|
AssetName string `json:"asset_name"` // 资产名称
|
||||||
Price int64 `json:"price"` // 单价(分)
|
AssetType string `json:"asset_type"` // 资产类型:product-商品型,service-服务型,software-软件型
|
||||||
Quantity int `json:"quantity"` // 数量
|
ImageURL string `json:"image_url"` // 资产图片
|
||||||
TotalPrice int64 `json:"total_price"` // 小计(分)
|
Quantity int `json:"quantity"` // 总数量
|
||||||
ImageURL string `json:"image_url"` // 商品图片
|
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 收货信息(响应)
|
// ShippingInfo 收货信息(响应)
|
||||||
|
|||||||
@@ -31,12 +31,21 @@ type OrderBase struct {
|
|||||||
// 按状态拆分的订单表会包含这个结构
|
// 按状态拆分的订单表会包含这个结构
|
||||||
|
|
||||||
type OrderItem struct {
|
type OrderItem struct {
|
||||||
ProductID string `bson:"product_id" json:"product_id"` // 商品ID
|
AssetID string `bson:"asset_id" json:"asset_id"` // 资产ID
|
||||||
ProductName string `bson:"product_name" json:"product_name"` // 商品名称
|
AssetName string `bson:"asset_name" json:"asset_name"` // 资产名称
|
||||||
Price int64 `bson:"price" json:"price"` // 单价(分)
|
AssetType string `bson:"asset_type" json:"asset_type"` // 资产类型:product-商品型,service-服务型,software-软件型
|
||||||
Quantity int `bson:"quantity" json:"quantity"` // 数量
|
ImageURL string `bson:"image_url,omitempty" json:"image_url"` // 资产图片
|
||||||
TotalPrice int64 `bson:"total_price" json:"total_price"` // 小计(分)
|
Quantity int `bson:"quantity" json:"quantity"` // 总数量
|
||||||
ImageURL string `bson:"image_url,omitempty" json:"image_url"` // 商品图片
|
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 收货信息
|
// ShippingInfo 收货信息
|
||||||
|
|||||||
108
service/order.go
108
service/order.go
@@ -28,13 +28,28 @@ func Init() error {
|
|||||||
func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
||||||
var result []entity.OrderItem
|
var result []entity.OrderItem
|
||||||
for _, item := range items {
|
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{
|
result = append(result, entity.OrderItem{
|
||||||
ProductID: item.ProductID,
|
AssetID: item.AssetID,
|
||||||
ProductName: item.ProductName,
|
AssetName: item.AssetName,
|
||||||
Price: item.Price,
|
AssetType: item.AssetType,
|
||||||
Quantity: item.Quantity,
|
ImageURL: item.ImageURL,
|
||||||
TotalPrice: item.Price * int64(item.Quantity),
|
Quantity: totalQuantity,
|
||||||
ImageURL: item.ImageURL,
|
TotalPrice: totalAmount,
|
||||||
|
Stocks: stocks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -44,13 +59,24 @@ func convertOrderItemsFromDTO(items []dto.OrderItemReq) []entity.OrderItem {
|
|||||||
func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
||||||
var result []dto.OrderItem
|
var result []dto.OrderItem
|
||||||
for _, item := range items {
|
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{
|
result = append(result, dto.OrderItem{
|
||||||
ProductID: item.ProductID,
|
AssetID: item.AssetID,
|
||||||
ProductName: item.ProductName,
|
AssetName: item.AssetName,
|
||||||
Price: item.Price,
|
AssetType: item.AssetType,
|
||||||
Quantity: item.Quantity,
|
ImageURL: item.ImageURL,
|
||||||
TotalPrice: item.TotalPrice,
|
Quantity: item.Quantity,
|
||||||
ImageURL: item.ImageURL,
|
TotalPrice: item.TotalPrice,
|
||||||
|
Stocks: stocks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -60,13 +86,24 @@ func convertOrderItems(items []entity.OrderItem) []dto.OrderItem {
|
|||||||
func convertEntityOrderItemsToDTO(items []entity.OrderItem) []dto.OrderItem {
|
func convertEntityOrderItemsToDTO(items []entity.OrderItem) []dto.OrderItem {
|
||||||
var result []dto.OrderItem
|
var result []dto.OrderItem
|
||||||
for _, item := range items {
|
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{
|
result = append(result, dto.OrderItem{
|
||||||
ProductID: item.ProductID,
|
AssetID: item.AssetID,
|
||||||
ProductName: item.ProductName,
|
AssetName: item.AssetName,
|
||||||
Price: item.Price,
|
AssetType: item.AssetType,
|
||||||
Quantity: item.Quantity,
|
ImageURL: item.ImageURL,
|
||||||
TotalPrice: item.TotalPrice,
|
Quantity: item.Quantity,
|
||||||
ImageURL: item.ImageURL,
|
TotalPrice: item.TotalPrice,
|
||||||
|
Stocks: stocks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -87,11 +124,15 @@ func (s *order) CreateOrder(ctx context.Context, req *dto.CreateOrderReq) (*dto.
|
|||||||
totalAmount := int64(0)
|
totalAmount := int64(0)
|
||||||
for i := range req.OrderItems {
|
for i := range req.OrderItems {
|
||||||
item := &req.OrderItems[i]
|
item := &req.OrderItems[i]
|
||||||
if item.Price <= 0 || item.Quantity <= 0 {
|
itemTotal := int64(0)
|
||||||
return nil, fmt.Errorf("商品价格或数量无效: %s", item.ProductName)
|
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 += itemTotal
|
||||||
totalAmount += totalPrice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if totalAmount <= 0 {
|
if totalAmount <= 0 {
|
||||||
@@ -327,13 +368,24 @@ func (s *order) convertCompletedOrderToDetail(order *entity.OrderCompleted) dto.
|
|||||||
func (s *order) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
func (s *order) convertOrderItems(items []*entity.OrderItem) []dto.OrderItem {
|
||||||
var result []dto.OrderItem
|
var result []dto.OrderItem
|
||||||
for _, item := range items {
|
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{
|
result = append(result, dto.OrderItem{
|
||||||
ProductID: item.ProductID,
|
AssetID: item.AssetID,
|
||||||
ProductName: item.ProductName,
|
AssetName: item.AssetName,
|
||||||
Price: item.Price,
|
AssetType: item.AssetType,
|
||||||
Quantity: item.Quantity,
|
ImageURL: item.ImageURL,
|
||||||
TotalPrice: item.TotalPrice,
|
Quantity: item.Quantity,
|
||||||
ImageURL: item.ImageURL,
|
TotalPrice: item.TotalPrice,
|
||||||
|
Stocks: stocks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user