e0bee00ff4
- Web: 隐藏 ICP 备案号(条件渲染),联系入口改为微信咨询(条件渲染,配置 wechat 字段后生效) - backend: 拼音回填抽取为独立 cmd/backfill-pinyin 工具,不再在启动时运行 - backend: DB 连接池参数(max_idle_conns/max_open_conns)改为配置可控,默认 10/100 - backend: Inventory 反范式字段添加注释说明快照语义 - backend: 全量 handler 采用 util.RespondSuccess/RespondCreated 统一响应格式(51 处) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
207 lines
6.6 KiB
Go
207 lines
6.6 KiB
Go
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"})
|
|
}
|