diff --git a/app/admin/apis/basic/category.go b/app/admin/apis/basic/category.go index c0b2f3d..ebb68f0 100644 --- a/app/admin/apis/basic/category.go +++ b/app/admin/apis/basic/category.go @@ -118,7 +118,7 @@ func UpdateCategory(c *gin.Context) { // @Summary 删除分类 // @Tags 商品分类 // @Produce json -// @Param id int path true "分类id" +// @Param id path int true "分类id" // @Success 200 {object} app.Response // @Router /api/v1/category/delete/:id [delete] func DeleteCategory(c *gin.Context) { @@ -136,6 +136,26 @@ func DeleteCategory(c *gin.Context) { return } -func CategoryList(c *gin.Context) { - +type CategoryListRequest struct { + IsAll bool `json:"is_all"` //是否展示全部 +} + +// CategoryList 分类列表 +// @Summary 分类列表 +// @Tags 商品分类 +// @Produce json +// @Param request body CategoryListRequest true "分类列表请求模型" +// @Success 200 {array} models.CategoryModel +// @Router /api/v1/category/list/ [get] +func CategoryList(c *gin.Context) { + req := new(CategoryListRequest) + _ = c.ShouldBindJSON(req) + list, err := models.GetCategoryList(middleware.GetCooperativeBusinessId(c), req.IsAll) + if err != nil { + app.Error(c, 500, err, "获取列表失败") + return + } + + app.OK(c, list, "ok") + return } diff --git a/app/admin/models/category.go b/app/admin/models/category.go index 8357153..1d483d5 100644 --- a/app/admin/models/category.go +++ b/app/admin/models/category.go @@ -12,11 +12,16 @@ type Category struct { Name string `json:"name"` // 分类名称 Number string `json:"number"` //编号 - Display int8 `json:"state"` // 1 展示 0 隐藏 + Display int8 `json:"display"` // 1 展示 0 隐藏 Pid uint32 `json:"pid" gorm:"index"` //父分类的编号 CooperativeBusinessId uint32 `json:"cooperative_business_id"` //合作商id } +var ( + Display = 1 + UnDisplay = 0 +) + func (c *Category) TableName() string { return "erp_category" } @@ -84,29 +89,45 @@ type CategoryModel struct { SubCategory []*CategoryModel } -func GetCategoryList(bid uint32) ([]*CategoryModel, error) { +func ScopeOnlyDisplay(db *gorm.DB) *gorm.DB { + return db.Where("display", Display) +} + +// GetCategoryList 分类列表 +func GetCategoryList(bid uint32, all bool) ([]*CategoryModel, error) { var list = make([]*CategoryModel, 0) m := orm.Eloquent.Model(Category{}) - err := m.Scopes(common.ScopeBusiness(bid)).Where("pid = ?", 0).Find(&list).Error + + var scopes = []func(db *gorm.DB) *gorm.DB{common.ScopeBusiness(bid)} + if !all { + scopes = append(scopes, ScopeOnlyDisplay) + } + + err := m.Scopes(scopes...).Where("pid = ?", 0).Find(&list).Error if err != nil { return nil, err } for _, top := range list { - top.SubCategory = findChildCategory(top) + top.SubCategory = findChildCategory(top, all) } return list, nil } -func findChildCategory(prev *CategoryModel) []*CategoryModel { +// 查询子分类 +func findChildCategory(prev *CategoryModel, all bool) []*CategoryModel { var cs []*CategoryModel - err := orm.Eloquent.Model(Category{}).Where("pid", prev.ID).Find(&cs).Error + m := orm.Eloquent.Model(Category{}) + if !all { + m = m.Scopes(ScopeOnlyDisplay) + } + err := m.Where("pid", prev.ID).Find(&cs).Error if err != nil { return nil } for _, c := range cs { - c.SubCategory = findChildCategory(c) + c.SubCategory = findChildCategory(c, all) } return cs diff --git a/app/admin/router/category.go b/app/admin/router/category.go new file mode 100644 index 0000000..ed59acf --- /dev/null +++ b/app/admin/router/category.go @@ -0,0 +1,16 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "go-admin/app/admin/apis/basic" + "go-admin/app/admin/middleware" + jwt "go-admin/pkg/jwtauth" +) + +func registerCategoryRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { + r := v1.Group("/category").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) + r.POST("create", basic.CreateCategory) + r.POST("update", basic.UpdateCategory) + r.DELETE("delete/:id", basic.DeleteCategory) + r.POST("list", basic.CategoryList) +} diff --git a/app/admin/router/router.go b/app/admin/router/router.go index e4b6a97..508b5b7 100644 --- a/app/admin/router/router.go +++ b/app/admin/router/router.go @@ -94,5 +94,9 @@ func examplesCheckRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddle // 回收卡 registerRecycleCardManageRouter(v1, authMiddleware) + //供应商 registerSupplierRouter(v1, authMiddleware) + + //商品分类 + registerCategoryRouter(v1, authMiddleware) } diff --git a/docs/docs.go b/docs/docs.go index c2763d6..24f3009 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -52,6 +52,67 @@ const docTemplate = `{ } } }, + "/api/v1/category/delete/:id": { + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "商品分类" + ], + "summary": "删除分类", + "parameters": [ + { + "type": "integer", + "description": "分类id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/app.Response" + } + } + } + } + }, + "/api/v1/category/list/": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "商品分类" + ], + "summary": "分类列表", + "parameters": [ + { + "description": "分类列表请求模型", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.CategoryListRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/models.CategoryModel" + } + } + } + } + } + }, "/api/v1/category/update": { "post": { "consumes": [ @@ -2739,6 +2800,15 @@ const docTemplate = `{ } } }, + "basic.CategoryListRequest": { + "type": "object", + "properties": { + "is_all": { + "description": "是否展示全部", + "type": "boolean" + } + } + }, "basic.CreateCategoryRequest": { "type": "object", "required": [ @@ -2947,6 +3017,43 @@ const docTemplate = `{ } } }, + "models.CategoryModel": { + "type": "object", + "properties": { + "cooperative_business_id": { + "description": "合作商id", + "type": "integer" + }, + "createdAt": { + "type": "string" + }, + "display": { + "description": "1 展示 0 隐藏", + "type": "integer" + }, + "id": { + "type": "integer" + }, + "name": { + "description": "分类名称", + "type": "string" + }, + "number": { + "description": "编号", + "type": "string" + }, + "pid": { + "description": "父分类的编号", + "type": "integer" + }, + "subCategory": { + "type": "array", + "items": { + "$ref": "#/definitions/models.CategoryModel" + } + } + } + }, "models.DictType": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index c8c921b..b5777e2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -44,6 +44,67 @@ } } }, + "/api/v1/category/delete/:id": { + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "商品分类" + ], + "summary": "删除分类", + "parameters": [ + { + "type": "integer", + "description": "分类id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/app.Response" + } + } + } + } + }, + "/api/v1/category/list/": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "商品分类" + ], + "summary": "分类列表", + "parameters": [ + { + "description": "分类列表请求模型", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/basic.CategoryListRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/models.CategoryModel" + } + } + } + } + } + }, "/api/v1/category/update": { "post": { "consumes": [ @@ -2731,6 +2792,15 @@ } } }, + "basic.CategoryListRequest": { + "type": "object", + "properties": { + "is_all": { + "description": "是否展示全部", + "type": "boolean" + } + } + }, "basic.CreateCategoryRequest": { "type": "object", "required": [ @@ -2939,6 +3009,43 @@ } } }, + "models.CategoryModel": { + "type": "object", + "properties": { + "cooperative_business_id": { + "description": "合作商id", + "type": "integer" + }, + "createdAt": { + "type": "string" + }, + "display": { + "description": "1 展示 0 隐藏", + "type": "integer" + }, + "id": { + "type": "integer" + }, + "name": { + "description": "分类名称", + "type": "string" + }, + "number": { + "description": "编号", + "type": "string" + }, + "pid": { + "description": "父分类的编号", + "type": "integer" + }, + "subCategory": { + "type": "array", + "items": { + "$ref": "#/definitions/models.CategoryModel" + } + } + } + }, "models.DictType": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 08b47e2..57d0b73 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -23,6 +23,12 @@ definitions: requestId: type: string type: object + basic.CategoryListRequest: + properties: + is_all: + description: 是否展示全部 + type: boolean + type: object basic.CreateCategoryRequest: properties: name: @@ -179,6 +185,32 @@ definitions: - id - name type: object + models.CategoryModel: + properties: + cooperative_business_id: + description: 合作商id + type: integer + createdAt: + type: string + display: + description: 1 展示 0 隐藏 + type: integer + id: + type: integer + name: + description: 分类名称 + type: string + number: + description: 编号 + type: string + pid: + description: 父分类的编号 + type: integer + subCategory: + items: + $ref: '#/definitions/models.CategoryModel' + type: array + type: object models.DictType: properties: createBy: @@ -1033,6 +1065,45 @@ paths: summary: 创建分类 tags: - 商品分类 + /api/v1/category/delete/:id: + delete: + parameters: + - description: 分类id + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/app.Response' + summary: 删除分类 + tags: + - 商品分类 + /api/v1/category/list/: + get: + parameters: + - description: 分类列表请求模型 + in: body + name: request + required: true + schema: + $ref: '#/definitions/basic.CategoryListRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/models.CategoryModel' + type: array + summary: 分类列表 + tags: + - 商品分类 /api/v1/category/update: post: consumes: