66e54af8c6
后端: - feat(backend): 重命名 Batches→Products 接口,新增库存状态(在售/已卖出)和买家信息 - feat(backend): 出库单创建/提交时校验仓库库存,不足则返回明确错误信息 - fix(backend): 出库创建 status 判断逻辑修复(空值默认 draft) 前端: - feat(client): 批次追踪改为商品追踪,新增状态列(在售/已卖出)和买家/时间列 - fix(client): 无批次号时显示"无批次"而非空 - refactor(client): BatchRecord → ProductTrackingRecord,repository 接口更新 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
192 lines
5.8 KiB
Go
192 lines
5.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"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"
|
|
"github.com/wangjia/jiu/backend/internal/service"
|
|
)
|
|
|
|
type StockOutHandler struct {
|
|
db *gorm.DB
|
|
stockSvc *service.StockService
|
|
}
|
|
|
|
func NewStockOutHandler(db *gorm.DB, svc *service.StockService) *StockOutHandler {
|
|
return &StockOutHandler{db: db, stockSvc: svc}
|
|
}
|
|
|
|
// List GET /api/v1/stock-out/orders
|
|
func (h *StockOutHandler) List(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
query := h.db.Model(&model.StockOutOrder{}).
|
|
Where("shop_id = ? AND deleted_at IS NULL", shopID)
|
|
|
|
if status := c.Query("status"); status != "" {
|
|
query = query.Where("status = ?", status)
|
|
}
|
|
if startDate := c.Query("start_date"); startDate != "" {
|
|
query = query.Where("order_date >= ?", startDate)
|
|
}
|
|
if endDate := c.Query("end_date"); endDate != "" {
|
|
query = query.Where("order_date <= ?", endDate)
|
|
}
|
|
|
|
var total int64
|
|
query.Count(&total)
|
|
|
|
var orders []model.StockOutOrder
|
|
query.Preload("Warehouse").Preload("Partner").Preload("Operator").
|
|
Offset((page - 1) * pageSize).Limit(pageSize).
|
|
Order("id DESC").Find(&orders)
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": orders, "total": total, "page": page, "page_size": pageSize})
|
|
}
|
|
|
|
// Get GET /api/v1/stock-out/orders/:id
|
|
func (h *StockOutHandler) Get(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var order model.StockOutOrder
|
|
if err := h.db.Preload("Items.Product").Preload("Warehouse").Preload("Partner").
|
|
Where("id = ? AND shop_id = ? AND deleted_at IS NULL", c.Param("id"), shopID).
|
|
First(&order).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": order})
|
|
}
|
|
|
|
// checkInventory validates that warehouse has enough stock for each item.
|
|
// warehouseID is the order's warehouse; items are the stock-out line items.
|
|
func (h *StockOutHandler) checkInventory(shopID, warehouseID uint64, items []model.StockOutItem) error {
|
|
for _, item := range items {
|
|
var inv model.Inventory
|
|
err := h.db.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
|
|
shopID, warehouseID, item.ProductID).First(&inv).Error
|
|
if err != nil || inv.Quantity < item.Quantity {
|
|
// Try to get product name for a clearer error message
|
|
var p model.Product
|
|
h.db.Where("id = ?", item.ProductID).First(&p)
|
|
name := p.Name
|
|
if name == "" {
|
|
name = fmt.Sprintf("商品ID %d", item.ProductID)
|
|
}
|
|
have := 0.0
|
|
if err == nil {
|
|
have = inv.Quantity
|
|
}
|
|
return fmt.Errorf("库存不足:%s 当前库存 %.0f,需要 %.0f", name, have, item.Quantity)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Create POST /api/v1/stock-out/orders
|
|
func (h *StockOutHandler) Create(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
operatorID := middleware.GetUserID(c)
|
|
|
|
var req model.StockOutOrder
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
req.ShopID = shopID
|
|
req.OperatorID = operatorID
|
|
|
|
// 状态只允许 draft 或 pending;直接提交审核时校验库存
|
|
if req.Status == "pending" {
|
|
if err := h.checkInventory(shopID, req.WarehouseID, req.Items); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
} else {
|
|
req.Status = "draft"
|
|
}
|
|
|
|
orderNo, err := h.stockSvc.GenerateOrderNo(shopID, "stock_out")
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
req.OrderNo = orderNo
|
|
|
|
var total float64
|
|
for i := range req.Items {
|
|
req.Items[i].ShopID = shopID
|
|
req.Items[i].TotalPrice = req.Items[i].Quantity * req.Items[i].UnitPrice
|
|
total += req.Items[i].TotalPrice
|
|
}
|
|
req.TotalAmount = total
|
|
|
|
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})
|
|
}
|
|
|
|
// Submit PUT /api/v1/stock-out/orders/:id/submit
|
|
func (h *StockOutHandler) Submit(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
|
|
// 加载订单及明细,校验库存后再改状态
|
|
var order model.StockOutOrder
|
|
if err := h.db.Preload("Items").
|
|
Where("id = ? AND shop_id = ? AND status = 'draft'", c.Param("id"), shopID).
|
|
First(&order).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "order not found or not in draft status"})
|
|
return
|
|
}
|
|
if err := h.checkInventory(shopID, order.WarehouseID, order.Items); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
h.db.Model(&order).Update("status", "pending")
|
|
c.JSON(http.StatusOK, gin.H{"message": "submitted"})
|
|
}
|
|
|
|
// Approve PUT /api/v1/stock-out/orders/:id/approve
|
|
func (h *StockOutHandler) Approve(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
reviewerID := middleware.GetUserID(c)
|
|
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.stockSvc.ApproveStockOut(shopID, id, reviewerID); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "approved"})
|
|
}
|
|
|
|
// Reject PUT /api/v1/stock-out/orders/:id/reject
|
|
func (h *StockOutHandler) Reject(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
reviewerID := middleware.GetUserID(c)
|
|
now := timeNow()
|
|
|
|
result := h.db.Model(&model.StockOutOrder{}).
|
|
Where("id = ? AND shop_id = ? AND status = 'pending'", c.Param("id"), shopID).
|
|
Updates(map[string]interface{}{
|
|
"status": "rejected",
|
|
"reviewer_id": reviewerID,
|
|
"reviewed_at": now,
|
|
})
|
|
if result.RowsAffected == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "order not found or not in pending status"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "rejected"})
|
|
}
|