51cfe5fc6d
#29 ApproveStockOut:FOR UPDATE 锁定批次后再内存汇总总量, 消除预检 SUM 与加锁之间的竞态窗口,减少一次 DB 往返 #30 partner/warehouse/user/product_attr/product_option Update 方法: - 绑定到独立 req struct,防止请求体覆盖记录 ID - 改用 db.Model.Where("shop_id=?").Updates(map) 白名单更新, 数据库层强制 shop_id 隔离约束 #31 新增 GET /api/v1/admin/reconcile:对比 inventories 当前库存 与 inventory_logs 流水净量,返回差异行,用于发现不平账异常 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
206 lines
6.7 KiB
Go
206 lines
6.7 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"
|
|
)
|
|
|
|
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)
|
|
c.JSON(http.StatusOK, gin.H{"data": 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
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": 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,
|
|
})
|
|
c.JSON(http.StatusOK, gin.H{"data": 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)
|
|
c.JSON(http.StatusOK, gin.H{"data": 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
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": 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,
|
|
})
|
|
c.JSON(http.StatusOK, gin.H{"data": 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)
|
|
c.JSON(http.StatusOK, gin.H{"data": 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
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": 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,
|
|
})
|
|
c.JSON(http.StatusOK, gin.H{"data": 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"})
|
|
}
|