package handler import ( "net/http" "github.com/gin-gonic/gin" "gorm.io/gorm" "github.com/wangjia/jiu/backend/internal/middleware" "github.com/wangjia/jiu/backend/internal/model" "github.com/wangjia/jiu/backend/internal/util" ) type ProductOptionHandler struct { db *gorm.DB } func NewProductOptionHandler(db *gorm.DB) *ProductOptionHandler { return &ProductOptionHandler{db: db} } // ── 商品名称 ────────────────────────────────────────────────── func (h *ProductOptionHandler) ListNames(c *gin.Context) { shopID := middleware.GetShopID(c) items := make([]model.ProductNameOption, 0) h.db.Where("shop_id = ?", shopID).Order("id ASC").Find(&items) util.RespondSuccess(c, items) } func (h *ProductOptionHandler) CreateName(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } item := model.ProductNameOption{ TenantBase: model.TenantBase{ShopID: shopID}, Code: req.Code, Name: req.Name, Remark: req.Remark, } 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) UpdateName(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var item model.ProductNameOption 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{}{ "code": req.Code, "name": req.Name, "remark": req.Remark, }) util.RespondSuccess(c, item) } func (h *ProductOptionHandler) DeleteName(c *gin.Context) { shopID := middleware.GetShopID(c) h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).Delete(&model.ProductNameOption{}) c.JSON(http.StatusOK, gin.H{"message": "deleted"}) } // ── 商品系列 ────────────────────────────────────────────────── func (h *ProductOptionHandler) ListSeries(c *gin.Context) { shopID := middleware.GetShopID(c) items := make([]model.ProductSeriesOption, 0) h.db.Where("shop_id = ?", shopID).Order("id ASC").Find(&items) util.RespondSuccess(c, items) } func (h *ProductOptionHandler) CreateSeries(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } item := model.ProductSeriesOption{ TenantBase: model.TenantBase{ShopID: shopID}, Code: req.Code, Name: req.Name, Remark: req.Remark, } 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) UpdateSeries(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var item model.ProductSeriesOption 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{}{ "code": req.Code, "name": req.Name, "remark": req.Remark, }) util.RespondSuccess(c, item) } func (h *ProductOptionHandler) DeleteSeries(c *gin.Context) { shopID := middleware.GetShopID(c) h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).Delete(&model.ProductSeriesOption{}) c.JSON(http.StatusOK, gin.H{"message": "deleted"}) } // ── 商品规格 ────────────────────────────────────────────────── func (h *ProductOptionHandler) ListSpecs(c *gin.Context) { shopID := middleware.GetShopID(c) items := make([]model.ProductSpecOption, 0) h.db.Where("shop_id = ?", shopID).Order("id ASC").Find(&items) util.RespondSuccess(c, items) } func (h *ProductOptionHandler) CreateSpec(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Quantity int `json:"quantity"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } item := model.ProductSpecOption{ TenantBase: model.TenantBase{ShopID: shopID}, Code: req.Code, Name: req.Name, Quantity: req.Quantity, Remark: req.Remark, } 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) UpdateSpec(c *gin.Context) { shopID := middleware.GetShopID(c) var req struct { Code string `json:"code"` Name string `json:"name" binding:"required"` Quantity int `json:"quantity"` Remark string `json:"remark"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var item model.ProductSpecOption 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{}{ "code": req.Code, "name": req.Name, "quantity": req.Quantity, "remark": req.Remark, }) util.RespondSuccess(c, item) } func (h *ProductOptionHandler) DeleteSpec(c *gin.Context) { shopID := middleware.GetShopID(c) h.db.Where("id = ? AND shop_id = ?", c.Param("id"), shopID).Delete(&model.ProductSpecOption{}) c.JSON(http.StatusOK, gin.H{"message": "deleted"}) }