Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YZ4DskSRKsSiheQonFtQvx
This commit is contained in:
@@ -202,12 +202,14 @@ type publicProductImage struct {
|
||||
type publicProductResp struct {
|
||||
ID uint64 `json:"id"`
|
||||
PublicID string `json:"public_id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Series string `json:"series"`
|
||||
Spec string `json:"spec"`
|
||||
Brand string `json:"brand"`
|
||||
Unit string `json:"unit"`
|
||||
SalePrice float64 `json:"sale_price"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Images []publicProductImage `json:"images"`
|
||||
}
|
||||
|
||||
@@ -234,8 +236,15 @@ func (h *PublicHandler) ListShopProducts(c *gin.Context) {
|
||||
pageSize = 20
|
||||
}
|
||||
|
||||
// 仅列「有库存」的商品:JOIN 库存按 product 聚合(数量>0)的子查询。
|
||||
stockSub := h.db.Model(&model.Inventory{}).
|
||||
Select("product_id, SUM(quantity) AS qty").
|
||||
Where("shop_id = ? AND deleted_at IS NULL AND quantity > 0", shop.ID).
|
||||
Group("product_id")
|
||||
|
||||
query := h.db.Model(&model.Product{}).
|
||||
Where("shop_id = ? AND public_id IS NOT NULL AND public_id != '' AND deleted_at IS NULL", shop.ID)
|
||||
Joins("JOIN (?) AS stk ON stk.product_id = products.id", stockSub).
|
||||
Where("products.shop_id = ? AND products.public_id IS NOT NULL AND products.public_id != '' AND products.deleted_at IS NULL", shop.ID)
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
@@ -248,12 +257,33 @@ func (h *PublicHandler) ListShopProducts(c *gin.Context) {
|
||||
if err := query.Preload("Images").
|
||||
Offset(offset).
|
||||
Limit(pageSize).
|
||||
Order("id DESC").
|
||||
Order("products.id DESC").
|
||||
Find(&products).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 取本页商品的在库总量(IN 限定在本页 ≤pageSize 个 id,开销小)
|
||||
qtyMap := make(map[uint64]float64, len(products))
|
||||
if len(products) > 0 {
|
||||
pageIDs := make([]uint64, len(products))
|
||||
for i, p := range products {
|
||||
pageIDs[i] = p.ID
|
||||
}
|
||||
var stockRows []struct {
|
||||
ProductID uint64
|
||||
Qty float64
|
||||
}
|
||||
h.db.Model(&model.Inventory{}).
|
||||
Select("product_id, SUM(quantity) AS qty").
|
||||
Where("shop_id = ? AND deleted_at IS NULL AND quantity > 0 AND product_id IN ?", shop.ID, pageIDs).
|
||||
Group("product_id").
|
||||
Scan(&stockRows)
|
||||
for _, s := range stockRows {
|
||||
qtyMap[s.ProductID] = s.Qty
|
||||
}
|
||||
}
|
||||
|
||||
listData := make([]publicProductResp, len(products))
|
||||
for i, p := range products {
|
||||
imgs := make([]publicProductImage, len(p.Images))
|
||||
@@ -263,12 +293,14 @@ func (h *PublicHandler) ListShopProducts(c *gin.Context) {
|
||||
listData[i] = publicProductResp{
|
||||
ID: p.ID,
|
||||
PublicID: p.PublicID,
|
||||
Code: p.Code,
|
||||
Name: p.Name,
|
||||
Series: p.Series,
|
||||
Spec: p.Spec,
|
||||
Brand: p.Brand,
|
||||
Unit: p.Unit,
|
||||
SalePrice: p.SalePrice,
|
||||
Quantity: qtyMap[p.ID],
|
||||
Images: imgs,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user