c1ed81dfab
后端 - 新增 shop handler:GET/PUT /shop/info(管理员权限) - 新增 finance CloseByRef:按单据 ref_type+ref_id 结清账款 - 新增 inventory UpdateRemark:PUT /inventory/:id/remark - 入库/出库审批自动生成财务应付/应收记录(去除金额>0限制) - 种子数据 S001-S003 补充真实门店信息 前端 - 设置页新增「酒行信息」Tab,管理员可编辑门店名称/地址/电话/负责人 - 入库单列表新增结清按钮(含确认弹窗),出库单同步 - 入库表单:规格、系列、生产日期、供应商、商品名称改为提交必填 - 入库/出库列表新增入库时间、出库时间、创建时间列 - 商品标签标题改为读取 shop 表门店名,扫码文案改为「扫码溯源 · TRACE」 - 标签页脚显示门店地址和电话(从 API 读取,不再依赖编译时 dart-define) - 库存备注支持点击编辑,超4字截断显示+Hover展示全文 - ApiClient 新增 patch() 方法(已改用 PUT 规避 CORS) 文档 - 新增 docs/user-manual.md 完整用户操作手册(12章) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
266 lines
7.6 KiB
Go
266 lines
7.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
qrcode "github.com/skip2/go-qrcode"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/wangjia/jiu/backend/config"
|
|
"github.com/wangjia/jiu/backend/internal/middleware"
|
|
"github.com/wangjia/jiu/backend/internal/model"
|
|
)
|
|
|
|
type ProductHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewProductHandler(db *gorm.DB) *ProductHandler {
|
|
return &ProductHandler{db: db}
|
|
}
|
|
|
|
// List GET /api/v1/products
|
|
func (h *ProductHandler) List(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
keyword := c.Query("keyword")
|
|
categoryID := c.Query("category_id")
|
|
|
|
query := h.db.Model(&model.Product{}).
|
|
Where("shop_id = ? AND deleted_at IS NULL", shopID)
|
|
|
|
if keyword != "" {
|
|
query = query.Where("name LIKE ? OR code LIKE ? OR barcode LIKE ?",
|
|
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
if categoryID != "" {
|
|
query = query.Where("category_id = ?", categoryID)
|
|
}
|
|
|
|
var total int64
|
|
query.Count(&total)
|
|
|
|
var products []model.Product
|
|
offset := (page - 1) * pageSize
|
|
query.Preload("Category").Offset(offset).Limit(pageSize).
|
|
Order("id DESC").Find(&products)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": products,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
})
|
|
}
|
|
|
|
// Create POST /api/v1/products
|
|
func (h *ProductHandler) Create(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var product model.Product
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
product.ShopID = shopID
|
|
product.PublicID = uuid.New().String()
|
|
|
|
// Auto-generate product code if not provided (e.g. P001, P002)
|
|
// Retry up to 5 times on duplicate key to handle concurrent creates
|
|
if product.Code == "" {
|
|
var count int64
|
|
h.db.Model(&model.Product{}).
|
|
Where("shop_id = ? AND deleted_at IS NULL", shopID).
|
|
Count(&count)
|
|
product.Code = fmt.Sprintf("P%03d", count+1)
|
|
}
|
|
|
|
var createErr error
|
|
for attempt := 0; attempt < 5; attempt++ {
|
|
if createErr = h.db.Create(&product).Error; createErr == nil {
|
|
break
|
|
}
|
|
if !errors.Is(createErr, gorm.ErrDuplicatedKey) {
|
|
break
|
|
}
|
|
// Duplicate code: try next slot
|
|
var count int64
|
|
h.db.Model(&model.Product{}).
|
|
Where("shop_id = ? AND deleted_at IS NULL", shopID).
|
|
Count(&count)
|
|
product.ID = 0
|
|
product.Code = fmt.Sprintf("P%03d", count+int64(attempt)+2)
|
|
}
|
|
if createErr != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": createErr.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": product})
|
|
}
|
|
|
|
// Update PUT /api/v1/products/:id
|
|
func (h *ProductHandler) Update(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
var product model.Product
|
|
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
First(&product).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
|
|
var req model.Product
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// 只更新业务字段,防止 Save() 覆盖 shop_id / created_at 等系统字段
|
|
if err := h.db.Model(&product).Updates(map[string]interface{}{
|
|
"code": req.Code,
|
|
"barcode": req.Barcode,
|
|
"name": req.Name,
|
|
"series": req.Series,
|
|
"spec": req.Spec,
|
|
"unit": req.Unit,
|
|
"category_id": req.CategoryID,
|
|
"brand": req.Brand,
|
|
"purchase_price": req.PurchasePrice,
|
|
"sale_price": req.SalePrice,
|
|
"min_stock": req.MinStock,
|
|
"description": req.Description,
|
|
"remark": req.Remark,
|
|
"custom_fields": req.CustomFields,
|
|
}).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// 重新读取完整数据返回
|
|
h.db.Preload("Category").First(&product, product.ID)
|
|
c.JSON(http.StatusOK, gin.H{"data": product})
|
|
}
|
|
|
|
// Detail GET /api/v1/products/:id/detail
|
|
func (h *ProductHandler) Detail(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
var product model.Product
|
|
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
Preload("Category").Preload("Images").
|
|
First(&product).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
|
|
// 老数据可能没有 public_id,按需补生成
|
|
if product.PublicID == "" {
|
|
product.PublicID = uuid.New().String()
|
|
h.db.Model(&product).Update("public_id", product.PublicID)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": product})
|
|
}
|
|
|
|
// QRCode GET /api/v1/products/:id/qrcode
|
|
func (h *ProductHandler) QRCode(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
var product model.Product
|
|
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
Select("id, public_id, code").First(&product).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
|
|
if product.PublicID == "" {
|
|
product.PublicID = uuid.New().String()
|
|
h.db.Model(&product).Update("public_id", product.PublicID)
|
|
}
|
|
|
|
url := config.C.Storage.PublicURL + "/product/" + product.PublicID
|
|
if product.Code != "" {
|
|
url += "?code=" + product.Code
|
|
}
|
|
png, err := qrcode.Encode(url, qrcode.Medium, 256)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.DataFromReader(http.StatusOK, int64(len(png)), "image/png", bytes.NewReader(png), nil)
|
|
}
|
|
|
|
// FindOrCreate POST /api/v1/products/find-or-create
|
|
func (h *ProductHandler) FindOrCreate(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Series string `json:"series"`
|
|
Spec string `json:"spec"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var product model.Product
|
|
err := h.db.Where("shop_id = ? AND name = ? AND series = ? AND spec = ? AND deleted_at IS NULL",
|
|
shopID, req.Name, req.Series, req.Spec).First(&product).Error
|
|
if err == nil {
|
|
c.JSON(http.StatusOK, gin.H{"data": product})
|
|
return
|
|
}
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var count int64
|
|
h.db.Model(&model.Product{}).Where("shop_id = ? AND deleted_at IS NULL", shopID).Count(&count)
|
|
product = model.Product{
|
|
TenantBase: model.TenantBase{ShopID: shopID},
|
|
PublicID: uuid.New().String(),
|
|
Name: req.Name,
|
|
Series: req.Series,
|
|
Spec: req.Spec,
|
|
Code: fmt.Sprintf("P%03d", count+1),
|
|
}
|
|
if createErr := h.db.Create(&product).Error; createErr != nil {
|
|
// Race condition: try to find the record created by another request
|
|
if h.db.Where("shop_id = ? AND name = ? AND series = ? AND spec = ? AND deleted_at IS NULL",
|
|
shopID, req.Name, req.Series, req.Spec).First(&product).Error == nil {
|
|
c.JSON(http.StatusOK, gin.H{"data": product})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": createErr.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": product})
|
|
}
|
|
|
|
// Delete DELETE /api/v1/products/:id (软删除)
|
|
func (h *ProductHandler) Delete(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
now := timeNow()
|
|
result := h.db.Model(&model.Product{}).
|
|
Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
Update("deleted_at", now)
|
|
|
|
if result.RowsAffected == 0 {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
|
}
|