This commit is contained in:
范俊成 2023-10-18 16:22:54 +08:00
parent 382689bee5
commit 86d8c5211d
2 changed files with 50 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/middleware"
"go-admin/app/admin/models"
"go-admin/app/admin/models/common"
orm "go-admin/common/global"
"go-admin/logger"
"go-admin/tools"
@ -113,25 +114,19 @@ func UpdateCategory(c *gin.Context) {
app.OK(c, nil, "ok")
}
type DeleteCategoryRequest struct {
Id uint32 `json:"id" validate:"required"`
}
// DeleteCategory 删除分类
// @Summary 删除分类
// @Tags 商品分类
// @Produce json
// @Param id int path true "分类id"
// @Success 200 {object} app.Response
// @Router /api/v1/category/delete/:id [delete]
func DeleteCategory(c *gin.Context) {
req := new(DeleteCategoryRequest)
_ = c.ShouldBindJSON(req)
if err := tools.Validate(req); err != nil {
app.Error(c, 400, err, "参数错误")
return
}
category, err := models.GetCategoryById(req.Id)
if err != nil {
app.Error(c, 500, err, "分类不存在")
return
}
err = orm.Eloquent.Model(category).Delete(category.ID).Error
id := c.Param("id")
err := orm.Eloquent.Model(models.Category{}).
Scopes(common.ScopeBusiness(middleware.GetCooperativeBusinessId(c))).
Delete(id).
Error
if err != nil {
app.Error(c, 500, err, "删除失败")
return
@ -140,3 +135,7 @@ func DeleteCategory(c *gin.Context) {
app.OK(c, nil, "ok")
return
}
func CategoryList(c *gin.Context) {
}

View File

@ -78,3 +78,36 @@ func GetCategoryById(id uint32) (*Category, error) {
return c, nil
}
type CategoryModel struct {
Category
SubCategory []*CategoryModel
}
func GetCategoryList(bid uint32) ([]*CategoryModel, error) {
var list = make([]*CategoryModel, 0)
m := orm.Eloquent.Model(Category{})
err := m.Scopes(common.ScopeBusiness(bid)).Where("pid = ?", 0).Find(&list).Error
if err != nil {
return nil, err
}
for _, top := range list {
top.SubCategory = findChildCategory(top)
}
return list, nil
}
func findChildCategory(prev *CategoryModel) []*CategoryModel {
var cs []*CategoryModel
err := orm.Eloquent.Model(Category{}).Where("pid", prev.ID).Find(&cs).Error
if err != nil {
return nil
}
for _, c := range cs {
c.SubCategory = findChildCategory(c)
}
return cs
}