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>
62 lines
1.6 KiB
Go
62 lines
1.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"
|
|
)
|
|
|
|
type WarehouseHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewWarehouseHandler(db *gorm.DB) *WarehouseHandler {
|
|
return &WarehouseHandler{db: db}
|
|
}
|
|
|
|
func (h *WarehouseHandler) List(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
var warehouses []model.Warehouse
|
|
h.db.Where("hotel_id = ? AND deleted_at IS NULL", hotelID).Find(&warehouses)
|
|
c.JSON(http.StatusOK, gin.H{"data": warehouses})
|
|
}
|
|
|
|
func (h *WarehouseHandler) Create(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
var w model.Warehouse
|
|
if err := c.ShouldBindJSON(&w); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
w.HotelID = hotelID
|
|
h.db.Create(&w)
|
|
c.JSON(http.StatusCreated, gin.H{"data": w})
|
|
}
|
|
|
|
func (h *WarehouseHandler) Update(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
var w model.Warehouse
|
|
if err := h.db.Where("id = ? AND hotel_id = ? AND deleted_at IS NULL", c.Param("id"), hotelID).
|
|
First(&w).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.ShouldBindJSON(&w)
|
|
w.HotelID = hotelID
|
|
h.db.Save(&w)
|
|
c.JSON(http.StatusOK, gin.H{"data": w})
|
|
}
|
|
|
|
func (h *WarehouseHandler) Delete(c *gin.Context) {
|
|
hotelID := middleware.GetHotelID(c)
|
|
now := timeNow()
|
|
h.db.Model(&model.Warehouse{}).
|
|
Where("id = ? AND hotel_id = ? AND deleted_at IS NULL", c.Param("id"), hotelID).
|
|
Update("deleted_at", now)
|
|
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
|
}
|