feat: 商品追踪页面 + 出库提交库存校验
后端: - feat(backend): 重命名 Batches→Products 接口,新增库存状态(在售/已卖出)和买家信息 - feat(backend): 出库单创建/提交时校验仓库库存,不足则返回明确错误信息 - fix(backend): 出库创建 status 判断逻辑修复(空值默认 draft) 前端: - feat(client): 批次追踪改为商品追踪,新增状态列(在售/已卖出)和买家/时间列 - fix(client): 无批次号时显示"无批次"而非空 - refactor(client): BatchRecord → ProductTrackingRecord,repository 接口更新 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -69,13 +70,21 @@ func (h *InventoryHandler) Logs(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"data": logs, "total": total, "page": page, "page_size": pageSize})
|
||||
}
|
||||
|
||||
// Batches GET /api/v1/inventory/batches — 批次追踪:已审核入库单的明细行
|
||||
func (h *InventoryHandler) Batches(c *gin.Context) {
|
||||
// productTrackingItem is the response shape for GET /api/v1/inventory/products
|
||||
type productTrackingItem struct {
|
||||
model.StockInItem
|
||||
CurrentQty float64 `json:"current_qty"`
|
||||
Status string `json:"status"` // in_stock | sold_out
|
||||
BuyerName string `json:"buyer_name,omitempty"`
|
||||
SoldAt string `json:"sold_at,omitempty"`
|
||||
}
|
||||
|
||||
// Products GET /api/v1/inventory/products — 商品追踪:已审核入库单的明细行,附库存状态和买家
|
||||
func (h *InventoryHandler) Products(c *gin.Context) {
|
||||
shopID := middleware.GetShopID(c)
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
// 查询已审核的入库单明细,关联商品和仓库信息
|
||||
query := h.db.Model(&model.StockInItem{}).
|
||||
Joins("JOIN stock_in_orders ON stock_in_orders.id = stock_in_items.order_id").
|
||||
Where("stock_in_orders.shop_id = ? AND stock_in_orders.status = 'approved'", shopID)
|
||||
@@ -98,10 +107,76 @@ func (h *InventoryHandler) Batches(c *gin.Context) {
|
||||
}).
|
||||
Select("stock_in_items.*").
|
||||
Order("stock_in_orders.order_date DESC, stock_in_items.id DESC").
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).
|
||||
Offset((page-1)*pageSize).Limit(pageSize).
|
||||
Find(&items)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": items, "total": total, "page": page, "page_size": pageSize})
|
||||
// 一次查出所有库存,构建 product+warehouse → qty 的 map
|
||||
var inventories []model.Inventory
|
||||
h.db.Where("shop_id = ?", shopID).Find(&inventories)
|
||||
invMap := make(map[string]float64, len(inventories))
|
||||
for _, inv := range inventories {
|
||||
key := fmt.Sprintf("%d:%d", inv.ProductID, inv.WarehouseID)
|
||||
invMap[key] = inv.Quantity
|
||||
}
|
||||
|
||||
// 查询每个 product+warehouse 最新的出库买家信息
|
||||
type soldRow struct {
|
||||
ProductID uint64 `gorm:"column:product_id"`
|
||||
WarehouseID uint64 `gorm:"column:warehouse_id"`
|
||||
BuyerName string `gorm:"column:buyer_name"`
|
||||
SoldAt string `gorm:"column:sold_at"`
|
||||
}
|
||||
var soldRows []soldRow
|
||||
h.db.Raw(`
|
||||
SELECT sooi.product_id, soo.warehouse_id,
|
||||
COALESCE(p.name, '') AS buyer_name,
|
||||
CAST(MAX(soo.order_date) AS CHAR) AS sold_at
|
||||
FROM stock_out_orders soo
|
||||
JOIN stock_out_items sooi ON sooi.order_id = soo.id
|
||||
LEFT JOIN partners p ON p.id = soo.partner_id AND p.shop_id = ?
|
||||
WHERE soo.shop_id = ? AND soo.status = 'approved'
|
||||
GROUP BY sooi.product_id, soo.warehouse_id
|
||||
`, shopID, shopID).Scan(&soldRows)
|
||||
soldMap := make(map[string]soldRow, len(soldRows))
|
||||
for _, r := range soldRows {
|
||||
key := fmt.Sprintf("%d:%d", r.ProductID, r.WarehouseID)
|
||||
soldMap[key] = r
|
||||
}
|
||||
|
||||
result := make([]productTrackingItem, 0, len(items))
|
||||
for _, item := range items {
|
||||
var warehouseID uint64
|
||||
if item.Order != nil {
|
||||
warehouseID = item.Order.WarehouseID
|
||||
}
|
||||
key := fmt.Sprintf("%d:%d", item.ProductID, warehouseID)
|
||||
currentQty := invMap[key]
|
||||
|
||||
status := "in_stock"
|
||||
buyerName := ""
|
||||
soldAt := ""
|
||||
if currentQty <= 0 {
|
||||
status = "sold_out"
|
||||
if si, ok := soldMap[key]; ok {
|
||||
buyerName = si.BuyerName
|
||||
if len(si.SoldAt) > 10 {
|
||||
soldAt = si.SoldAt[:10]
|
||||
} else {
|
||||
soldAt = si.SoldAt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result, productTrackingItem{
|
||||
StockInItem: item,
|
||||
CurrentQty: currentQty,
|
||||
Status: status,
|
||||
BuyerName: buyerName,
|
||||
SoldAt: soldAt,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": result, "total": total, "page": page, "page_size": pageSize})
|
||||
}
|
||||
|
||||
// CreateCheck POST /api/v1/inventory/checks
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -64,6 +65,31 @@ func (h *StockOutHandler) Get(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"data": order})
|
||||
}
|
||||
|
||||
// checkInventory validates that warehouse has enough stock for each item.
|
||||
// warehouseID is the order's warehouse; items are the stock-out line items.
|
||||
func (h *StockOutHandler) checkInventory(shopID, warehouseID uint64, items []model.StockOutItem) error {
|
||||
for _, item := range items {
|
||||
var inv model.Inventory
|
||||
err := h.db.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
|
||||
shopID, warehouseID, item.ProductID).First(&inv).Error
|
||||
if err != nil || inv.Quantity < item.Quantity {
|
||||
// Try to get product name for a clearer error message
|
||||
var p model.Product
|
||||
h.db.Where("id = ?", item.ProductID).First(&p)
|
||||
name := p.Name
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("商品ID %d", item.ProductID)
|
||||
}
|
||||
have := 0.0
|
||||
if err == nil {
|
||||
have = inv.Quantity
|
||||
}
|
||||
return fmt.Errorf("库存不足:%s 当前库存 %.0f,需要 %.0f", name, have, item.Quantity)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create POST /api/v1/stock-out/orders
|
||||
func (h *StockOutHandler) Create(c *gin.Context) {
|
||||
shopID := middleware.GetShopID(c)
|
||||
@@ -77,7 +103,16 @@ func (h *StockOutHandler) Create(c *gin.Context) {
|
||||
|
||||
req.ShopID = shopID
|
||||
req.OperatorID = operatorID
|
||||
req.Status = "draft"
|
||||
|
||||
// 状态只允许 draft 或 pending;直接提交审核时校验库存
|
||||
if req.Status == "pending" {
|
||||
if err := h.checkInventory(shopID, req.WarehouseID, req.Items); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
req.Status = "draft"
|
||||
}
|
||||
|
||||
orderNo, err := h.stockSvc.GenerateOrderNo(shopID, "stock_out")
|
||||
if err != nil {
|
||||
@@ -104,13 +139,21 @@ func (h *StockOutHandler) Create(c *gin.Context) {
|
||||
// Submit PUT /api/v1/stock-out/orders/:id/submit
|
||||
func (h *StockOutHandler) Submit(c *gin.Context) {
|
||||
shopID := middleware.GetShopID(c)
|
||||
result := h.db.Model(&model.StockOutOrder{}).
|
||||
|
||||
// 加载订单及明细,校验库存后再改状态
|
||||
var order model.StockOutOrder
|
||||
if err := h.db.Preload("Items").
|
||||
Where("id = ? AND shop_id = ? AND status = 'draft'", c.Param("id"), shopID).
|
||||
Update("status", "pending")
|
||||
if result.RowsAffected == 0 {
|
||||
First(&order).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "order not found or not in draft status"})
|
||||
return
|
||||
}
|
||||
if err := h.checkInventory(shopID, order.WarehouseID, order.Items); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
h.db.Model(&order).Update("status", "pending")
|
||||
c.JSON(http.StatusOK, gin.H{"message": "submitted"})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user