Files
jiu/backend/internal/service/stock.go
T
wangjia 0e42f0e417 init: 后端框架脚手架 (Go + Gin + GORM + MySQL)
- 项目目录结构: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>
2026-04-04 01:24:53 +08:00

161 lines
4.4 KiB
Go

package service
import (
"errors"
"fmt"
"time"
"gorm.io/gorm"
"github.com/wangjia/jiu/backend/internal/model"
)
var ErrInsufficientStock = errors.New("insufficient stock")
type StockService struct {
db *gorm.DB
}
func NewStockService(db *gorm.DB) *StockService {
return &StockService{db: db}
}
// ApproveStockIn 审核入库单,审核通过后更新库存(事务)
func (s *StockService) ApproveStockIn(hotelID, 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).
First(&order).Error; err != nil {
return err
}
if order.Status != "pending" {
return errors.New("order is not in pending status")
}
now := time.Now()
for _, item := range order.Items {
if err := s.updateInventory(tx, hotelID, order.WarehouseID, item.ProductID,
"in", item.Quantity, orderID, "stock_in", reviewerID); err != nil {
return err
}
}
return tx.Model(&order).Updates(map[string]interface{}{
"status": "approved",
"reviewer_id": reviewerID,
"reviewed_at": now,
}).Error
})
}
// ApproveStockOut 审核出库单
func (s *StockService) ApproveStockOut(hotelID, 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).
First(&order).Error; err != nil {
return err
}
if order.Status != "pending" {
return errors.New("order is not in pending status")
}
// 预检库存
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 {
return fmt.Errorf("product %d not in inventory", item.ProductID)
}
if inv.Quantity < item.Quantity {
return fmt.Errorf("%w: product_id=%d, available=%.3f, required=%.3f",
ErrInsufficientStock, item.ProductID, inv.Quantity, item.Quantity)
}
}
now := time.Now()
for _, item := range order.Items {
if err := s.updateInventory(tx, hotelID, order.WarehouseID, item.ProductID,
"out", item.Quantity, orderID, "stock_out", reviewerID); err != nil {
return err
}
}
return tx.Model(&order).Updates(map[string]interface{}{
"status": "approved",
"reviewer_id": reviewerID,
"reviewed_at": now,
}).Error
})
}
// updateInventory 更新库存并写流水(在事务中调用)
func (s *StockService) updateInventory(tx *gorm.DB, hotelID, 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)
qtyBefore := inv.Quantity
var qtyAfter float64
if direction == "in" {
qtyAfter = qtyBefore + qty
if result.Error != nil {
// 不存在则创建
inv = model.Inventory{HotelID: hotelID, WarehouseID: warehouseID, ProductID: productID, Quantity: qtyAfter}
if err := tx.Create(&inv).Error; err != nil {
return err
}
} else {
if err := tx.Model(&inv).Update("quantity", qtyAfter).Error; err != nil {
return err
}
}
} else {
qtyAfter = qtyBefore - qty
if err := tx.Model(&inv).Update("quantity", qtyAfter).Error; err != nil {
return err
}
}
log := model.InventoryLog{
HotelID: hotelID,
WarehouseID: warehouseID,
ProductID: productID,
Direction: direction,
Quantity: qty,
QtyBefore: qtyBefore,
QtyAfter: qtyAfter,
RefType: refType,
RefID: refID,
OperatorID: &operatorID,
}
return tx.Create(&log).Error
}
// GenerateOrderNo 生成单号(事务安全)
func (s *StockService) GenerateOrderNo(hotelID 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)
if result.Error != nil {
// 初始化规则
rule = model.NumberRule{HotelID: hotelID, Type: orderType, Prefix: orderType[:2], CurrentNo: 0}
tx.Create(&rule)
}
rule.CurrentNo++
tx.Model(&rule).Update("current_no", rule.CurrentNo)
dateStr := time.Now().Format("20060102")
no = fmt.Sprintf("%s%s%06d", rule.Prefix, dateStr, rule.CurrentNo)
return nil
})
return no, err
}