fix(backend): JWT config mapstructure tag 修复 + 模型从 hotel 重构为 shop

- 修复 JWTConfig 缺少 mapstructure tag 导致 access_expire_min 解析为 0,
  token 签发即过期,所有 API 请求返回 401
- 全部 config struct 补齐 mapstructure tag(secret/dsn/hmac_secret 等)
- 模型层从 hotel/HotelID 统一重命名为 shop/ShopID
- 删除旧 migrations(001-004),新增 001_init 综合迁移文件
- 更新 schema.sql、testutil、handler/service/model 相关引用

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-04-07 22:20:12 +08:00
parent 37112d6599
commit 31ea370cea
50 changed files with 2675 additions and 951 deletions
+16 -16
View File
@@ -21,11 +21,11 @@ func NewStockService(db *gorm.DB) *StockService {
}
// ApproveStockIn 审核入库单,审核通过后更新库存(事务)
func (s *StockService) ApproveStockIn(hotelID, orderID, reviewerID uint64) error {
func (s *StockService) ApproveStockIn(shopID, orderID, reviewerID uint64) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var order model.StockInOrder
if err := tx.Preload("Items").
Where("id = ? AND hotel_id = ?", orderID, hotelID).
Where("id = ? AND shop_id = ?", orderID, shopID).
First(&order).Error; err != nil {
return err
}
@@ -35,7 +35,7 @@ func (s *StockService) ApproveStockIn(hotelID, orderID, reviewerID uint64) error
now := time.Now()
for _, item := range order.Items {
if err := s.updateInventory(tx, hotelID, order.WarehouseID, item.ProductID,
if err := s.updateInventory(tx, shopID, order.WarehouseID, item.ProductID,
"in", item.Quantity, orderID, "stock_in", reviewerID); err != nil {
return err
}
@@ -50,11 +50,11 @@ func (s *StockService) ApproveStockIn(hotelID, orderID, reviewerID uint64) error
}
// ApproveStockOut 审核出库单
func (s *StockService) ApproveStockOut(hotelID, orderID, reviewerID uint64) error {
func (s *StockService) ApproveStockOut(shopID, orderID, reviewerID uint64) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var order model.StockOutOrder
if err := tx.Preload("Items").
Where("id = ? AND hotel_id = ?", orderID, hotelID).
Where("id = ? AND shop_id = ?", orderID, shopID).
First(&order).Error; err != nil {
return err
}
@@ -65,8 +65,8 @@ func (s *StockService) ApproveStockOut(hotelID, orderID, reviewerID uint64) erro
// 预检库存
for _, item := range order.Items {
var inv model.Inventory
if err := tx.Where("hotel_id = ? AND warehouse_id = ? AND product_id = ?",
hotelID, order.WarehouseID, item.ProductID).First(&inv).Error; err != nil {
if err := tx.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
shopID, order.WarehouseID, item.ProductID).First(&inv).Error; err != nil {
return fmt.Errorf("product %d not in inventory", item.ProductID)
}
if inv.Quantity < item.Quantity {
@@ -77,7 +77,7 @@ func (s *StockService) ApproveStockOut(hotelID, orderID, reviewerID uint64) erro
now := time.Now()
for _, item := range order.Items {
if err := s.updateInventory(tx, hotelID, order.WarehouseID, item.ProductID,
if err := s.updateInventory(tx, shopID, order.WarehouseID, item.ProductID,
"out", item.Quantity, orderID, "stock_out", reviewerID); err != nil {
return err
}
@@ -92,12 +92,12 @@ func (s *StockService) ApproveStockOut(hotelID, orderID, reviewerID uint64) erro
}
// updateInventory 更新库存并写流水(在事务中调用)
func (s *StockService) updateInventory(tx *gorm.DB, hotelID, warehouseID, productID uint64,
func (s *StockService) updateInventory(tx *gorm.DB, shopID, warehouseID, productID uint64,
direction string, qty float64, refID uint64, refType string, operatorID uint64) error {
var inv model.Inventory
result := tx.Where("hotel_id = ? AND warehouse_id = ? AND product_id = ?",
hotelID, warehouseID, productID).First(&inv)
result := tx.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
shopID, warehouseID, productID).First(&inv)
qtyBefore := inv.Quantity
var qtyAfter float64
@@ -106,7 +106,7 @@ func (s *StockService) updateInventory(tx *gorm.DB, hotelID, warehouseID, produc
qtyAfter = qtyBefore + qty
if result.Error != nil {
// 不存在则创建
inv = model.Inventory{HotelID: hotelID, WarehouseID: warehouseID, ProductID: productID, Quantity: qtyAfter}
inv = model.Inventory{ShopID: shopID, WarehouseID: warehouseID, ProductID: productID, Quantity: qtyAfter}
if err := tx.Create(&inv).Error; err != nil {
return err
}
@@ -123,7 +123,7 @@ func (s *StockService) updateInventory(tx *gorm.DB, hotelID, warehouseID, produc
}
log := model.InventoryLog{
HotelID: hotelID,
ShopID: shopID,
WarehouseID: warehouseID,
ProductID: productID,
Direction: direction,
@@ -138,14 +138,14 @@ func (s *StockService) updateInventory(tx *gorm.DB, hotelID, warehouseID, produc
}
// GenerateOrderNo 生成单号(事务安全)
func (s *StockService) GenerateOrderNo(hotelID uint64, orderType string) (string, error) {
func (s *StockService) GenerateOrderNo(shopID uint64, orderType string) (string, error) {
var no string
err := s.db.Transaction(func(tx *gorm.DB) error {
var rule model.NumberRule
result := tx.Where("hotel_id = ? AND type = ?", hotelID, orderType).First(&rule)
result := tx.Where("shop_id = ? AND type = ?", shopID, orderType).First(&rule)
if result.Error != nil {
// 初始化规则
rule = model.NumberRule{HotelID: hotelID, Type: orderType, Prefix: orderType[:2], CurrentNo: 0}
rule = model.NumberRule{ShopID: shopID, Type: orderType, Prefix: orderType[:2], CurrentNo: 0}
tx.Create(&rule)
}