fix: 批量修复功能问题 - 购物车结算/订单库存积分/搜索/抽奖/个人资料等
This commit is contained in:
@@ -23,6 +23,14 @@ func (h *CategoryHandler) List(c *gin.Context) {
|
||||
utils.DB.Where("parent_id IS NULL").Order("sort_order ASC, id ASC").Find(&categories)
|
||||
for i := range categories {
|
||||
utils.DB.Where("parent_id = ?", categories[i].ID).Order("sort_order ASC, id ASC").Find(&categories[i].Children)
|
||||
var count int64
|
||||
utils.DB.Table("product_categories").Where("category_id = ?", categories[i].ID).Count(&count)
|
||||
categories[i].ProductCount = count
|
||||
for j := range categories[i].Children {
|
||||
var childCount int64
|
||||
utils.DB.Table("product_categories").Where("category_id = ?", categories[i].Children[j].ID).Count(&childCount)
|
||||
categories[i].Children[j].ProductCount = childCount
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": categories})
|
||||
}
|
||||
@@ -121,6 +129,21 @@ func (h *CategoryHandler) Update(c *gin.Context) {
|
||||
|
||||
func (h *CategoryHandler) Delete(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var childCount int64
|
||||
utils.DB.Model(&models.Category{}).Where("parent_id = ?", id).Count(&childCount)
|
||||
if childCount > 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "该分类下有子分类,无法删除"})
|
||||
return
|
||||
}
|
||||
|
||||
var productCount int64
|
||||
utils.DB.Table("product_categories").Where("category_id = ?", id).Count(&productCount)
|
||||
if productCount > 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "该分类下有商品,无法删除"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.DB.Delete(&models.Category{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete category"})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user