Files
jiu/backend/internal/handler/inventory.go
T
wangjia ce1cbf404c feat: 完成7项功能任务
后端:
- fix(backend): InventoryLog 增加 Product/Warehouse 关联,Logs 接口加 Preload 修复流水记录显示"-"
- feat(backend): 新增 /inventory/batches 批次追踪接口
- feat(backend): 新增财务记录、编号规则接口(finance、number_rule handler)
- fix(backend): service/stock_test.go 修复 model.Date 类型错误

前端:
- feat(client): 入库/出库管理 Tab 调换顺序(审核在前),审核 Tab 加新建按钮,列表 Tab 无按钮
- feat(client): 入库单/出库单增加详情弹窗,显示完整单据信息和商品明细
- feat(client): 完善出库单新建表单(StockOutFormScreen),支持选客户/仓库/商品
- fix(client): StockInItem/StockOutItem.fromJson 修复读取嵌套 product 对象而非平铺字段
- feat(client): 批次追踪页面(BatchTrackingScreen)及侧边栏入口
- feat(client): 财务管理、盘点、编号规则接入真实后端 API
- fix(client): 入库/出库审核通过后 invalidate inventoryListProvider 刷新库存

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 22:03:19 +08:00

151 lines
4.5 KiB
Go

package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/wangjia/jiu/backend/internal/middleware"
"github.com/wangjia/jiu/backend/internal/model"
)
type InventoryHandler struct {
db *gorm.DB
}
func NewInventoryHandler(db *gorm.DB) *InventoryHandler {
return &InventoryHandler{db: db}
}
// List GET /api/v1/inventory
func (h *InventoryHandler) List(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.Inventory{}).Where("shop_id = ?", shopID)
if warehouseID := c.Query("warehouse_id"); warehouseID != "" {
query = query.Where("warehouse_id = ?", warehouseID)
}
if productID := c.Query("product_id"); productID != "" {
query = query.Where("product_id = ?", productID)
}
if c.Query("in_stock") == "1" {
query = query.Where("quantity > 0")
}
var total int64
query.Count(&total)
var inventory []model.Inventory
query.Preload("Product").Preload("Warehouse").
Offset((page - 1) * pageSize).Limit(pageSize).
Find(&inventory)
c.JSON(http.StatusOK, gin.H{"data": inventory, "total": total, "page": page, "page_size": pageSize})
}
// Logs GET /api/v1/inventory/logs
func (h *InventoryHandler) Logs(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.InventoryLog{}).Where("shop_id = ?", shopID)
if productID := c.Query("product_id"); productID != "" {
query = query.Where("product_id = ?", productID)
}
var total int64
query.Count(&total)
var logs []model.InventoryLog
query.Preload("Product").Preload("Warehouse").Offset((page-1)*pageSize).Limit(pageSize).Order("id DESC").Find(&logs)
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) {
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)
if productID := c.Query("product_id"); productID != "" {
query = query.Where("stock_in_items.product_id = ?", productID)
}
if warehouseID := c.Query("warehouse_id"); warehouseID != "" {
query = query.Where("stock_in_orders.warehouse_id = ?", warehouseID)
}
var total int64
query.Count(&total)
var items []model.StockInItem
query.
Preload("Product").
Preload("Order", func(db *gorm.DB) *gorm.DB {
return db.Preload("Warehouse").Preload("Partner")
}).
Select("stock_in_items.*").
Order("stock_in_orders.order_date DESC, stock_in_items.id DESC").
Offset((page - 1) * pageSize).Limit(pageSize).
Find(&items)
c.JSON(http.StatusOK, gin.H{"data": items, "total": total, "page": page, "page_size": pageSize})
}
// CreateCheck POST /api/v1/inventory/checks
func (h *InventoryHandler) CreateCheck(c *gin.Context) {
shopID := middleware.GetShopID(c)
operatorID := middleware.GetUserID(c)
var req model.InventoryCheck
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.ShopID = shopID
req.OperatorID = operatorID
req.Status = "draft"
// 自动填入系统库存数量
for i := range req.Items {
req.Items[i].ShopID = shopID
var inv model.Inventory
if err := h.db.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
shopID, req.WarehouseID, req.Items[i].ProductID).First(&inv).Error; err == nil {
req.Items[i].SystemQty = inv.Quantity
}
}
if err := h.db.Create(&req).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"data": req})
}
// GetCheck GET /api/v1/inventory/checks/:id
func (h *InventoryHandler) GetCheck(c *gin.Context) {
shopID := middleware.GetShopID(c)
var check model.InventoryCheck
if err := h.db.Preload("Items.Product").
Where("id = ? AND shop_id = ?", c.Param("id"), shopID).
First(&check).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.JSON(http.StatusOK, gin.H{"data": check})
}