0e42f0e417
- 项目目录结构:backend/ deploy/ schema/ migrations/ - 数据库 Schema:所有建表 SQL,含 hotel_id 多租户隔离 - Go 后端:config、model、handler、service、middleware、router - 认证:账号密码登录 + JWT(Access + Refresh Token) - 许可证:HMAC-SHA256 激活码生成 + 设备绑定验证 - 业务模块:商品、仓库、往来单位、入库、出库、库存、盘点 - 库存事务:入库/出库审核时原子更新库存 + 流水记录 - 数据导入:Excel/CSV 批量导入商品、往来单位 - Docker Compose:本地 MySQL + Adminer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/wangjia/jiu/backend/internal/middleware"
|
|
"github.com/wangjia/jiu/backend/internal/model"
|
|
)
|
|
|
|
type InventoryHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewInventoryHandler(db *gorm.DB) *InventoryHandler {
|
|
return &InventoryHandler{db: db}
|
|
}
|
|
|
|
// List GET /api/v1/inventory
|
|
func (h *InventoryHandler) List(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
query := h.db.Model(&model.Inventory{}).Where("hotel_id = ?", hotelID)
|
|
|
|
if warehouseID := c.Query("warehouse_id"); warehouseID != "" {
|
|
query = query.Where("warehouse_id = ?", warehouseID)
|
|
}
|
|
if productID := c.Query("product_id"); productID != "" {
|
|
query = query.Where("product_id = ?", productID)
|
|
}
|
|
// 仅显示有库存
|
|
if c.Query("in_stock") == "1" {
|
|
query = query.Where("quantity > 0")
|
|
}
|
|
|
|
var total int64
|
|
query.Count(&total)
|
|
|
|
var inventory []model.Inventory
|
|
query.Preload("Product").Preload("Warehouse").
|
|
Offset((page - 1) * pageSize).Limit(pageSize).
|
|
Find(&inventory)
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": inventory, "total": total, "page": page, "page_size": pageSize})
|
|
}
|
|
|
|
// Logs GET /api/v1/inventory/logs
|
|
func (h *InventoryHandler) Logs(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
query := h.db.Model(&model.InventoryLog{}).Where("hotel_id = ?", hotelID)
|
|
|
|
if productID := c.Query("product_id"); productID != "" {
|
|
query = query.Where("product_id = ?", productID)
|
|
}
|
|
|
|
var total int64
|
|
query.Count(&total)
|
|
|
|
var logs []model.InventoryLog
|
|
query.Offset((page - 1) * pageSize).Limit(pageSize).Order("id DESC").Find(&logs)
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": logs, "total": total, "page": page, "page_size": pageSize})
|
|
}
|
|
|
|
// CreateCheck POST /api/v1/inventory/checks
|
|
func (h *InventoryHandler) CreateCheck(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
operatorID := middleware.GetUserID(c)
|
|
|
|
var req model.InventoryCheck
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
req.HotelID = hotelID
|
|
req.OperatorID = operatorID
|
|
req.Status = "draft"
|
|
|
|
// 自动填入系统库存数量
|
|
for i := range req.Items {
|
|
req.Items[i].HotelID = hotelID
|
|
var inv model.Inventory
|
|
if err := h.db.Where("hotel_id = ? AND warehouse_id = ? AND product_id = ?",
|
|
hotelID, req.WarehouseID, req.Items[i].ProductID).First(&inv).Error; err == nil {
|
|
req.Items[i].SystemQty = inv.Quantity
|
|
}
|
|
}
|
|
|
|
if err := h.db.Create(&req).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": req})
|
|
}
|
|
|
|
// GetCheck GET /api/v1/inventory/checks/:id
|
|
func (h *InventoryHandler) GetCheck(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
var check model.InventoryCheck
|
|
if err := h.db.Preload("Items.Product").
|
|
Where("id = ? AND hotel_id = ?", c.Param("id"), hotelID).
|
|
First(&check).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": check})
|
|
}
|