feat(backend): 库存按商品过滤 + 香型(分类)字典 CRUD

商品域富数据/重组所需后端:
- GET /inventory 加 product_id 过滤 → 解锁商品详情「当前库存/货值/各仓库分布」
  (配合已支持 product_id 的 /inventory/logs,详情可拼出大半富数据)
- 新增 /product-options/categories 香型字典 CRUD(ProductCategory 模型已存在,
  补管理接口)→ 商品列表 5-tab 重组的最后一块字典
均守多租户 shop_id;新增 3 个测试(FilterByProduct/CategoryCRUD/CategoryIsolation)。
go build/vet/test 全过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TSKEiHsvauyxYUW2itzUXX
This commit is contained in:
wangjia
2026-06-25 15:07:49 +08:00
parent c4dcaaf4cd
commit a51a44056d
5 changed files with 169 additions and 0 deletions
@@ -220,3 +220,68 @@ func (h *ProductOptionHandler) DeleteSpec(c *gin.Context) {
h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).Delete(&model.ProductSpecOption{})
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}
// ── 香型 / 分类(ProductCategory)字典 CRUD ──────────────────────────────
func (h *ProductOptionHandler) ListCategories(c *gin.Context) {
shopID := middleware.GetShopID(c)
items := make([]model.ProductCategory, 0)
q := h.db.Where("shop_id = ?", shopID)
if kw := strings.TrimSpace(c.Query("keyword")); kw != "" {
q = q.Where("name LIKE ?", "%"+kw+"%")
}
q.Order("sort_order ASC, id ASC").Find(&items)
util.RespondSuccess(c, items)
}
func (h *ProductOptionHandler) CreateCategory(c *gin.Context) {
shopID := middleware.GetShopID(c)
var req struct {
Name string `json:"name" binding:"required"`
ParentID *uint64 `json:"parent_id"`
SortOrder int `json:"sort_order"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
item := model.ProductCategory{
TenantBase: model.TenantBase{ShopID: shopID},
Name: req.Name,
ParentID: req.ParentID,
SortOrder: req.SortOrder,
}
if err := h.db.Create(&item).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
util.RespondCreated(c, item)
}
func (h *ProductOptionHandler) UpdateCategory(c *gin.Context) {
shopID := middleware.GetShopID(c)
var req struct {
Name string `json:"name" binding:"required"`
ParentID *uint64 `json:"parent_id"`
SortOrder int `json:"sort_order"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var item model.ProductCategory
if err := h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).First(&item).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
h.db.Model(&item).Where("shop_id = ?", shopID).Updates(map[string]interface{}{
"name": req.Name, "parent_id": req.ParentID, "sort_order": req.SortOrder,
})
util.RespondSuccess(c, item)
}
func (h *ProductOptionHandler) DeleteCategory(c *gin.Context) {
shopID := middleware.GetShopID(c)
h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).Delete(&model.ProductCategory{})
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}