分类管理

This commit is contained in:
范俊成 2023-10-19 10:04:13 +08:00
parent 86d8c5211d
commit 28479f8e0f
7 changed files with 356 additions and 10 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
}

View File

@ -94,5 +94,9 @@ func examplesCheckRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddle
// 回收卡
registerRecycleCardManageRouter(v1, authMiddleware)
//供应商
registerSupplierRouter(v1, authMiddleware)
//商品分类
registerCategoryRouter(v1, authMiddleware)
}

View File

@ -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": {

View File

@ -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": {

View File

@ -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: