web
This commit is contained in:
@@ -36,9 +36,16 @@ func (h *CartHandler) GetCart(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 计算购物车统计信息
|
||||
count, _ := h.cartService.GetCartCount(userID.(uint))
|
||||
total, _ := h.cartService.GetCartTotal(userID.(uint))
|
||||
// 优化: 在一次查询结果基础上计算统计信息,避免重复查询
|
||||
var count int
|
||||
var total float64
|
||||
for _, item := range cart {
|
||||
count += item.Quantity
|
||||
if item.Product.ID != 0 {
|
||||
// 将价格从分转换为元
|
||||
total += (float64(item.Product.Price) / 100) * float64(item.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
result := map[string]interface{}{
|
||||
"items": cart,
|
||||
|
||||
Reference in New Issue
Block a user