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:
wangjia
2026-04-10 22:26:42 +08:00
parent ce1cbf404c
commit 66e54af8c6
12 changed files with 513 additions and 68 deletions
+80 -5
View File
@@ -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