分类管理
This commit is contained in:
parent
28479f8e0f
commit
55f9615c5b
|
@ -1,6 +1,7 @@
|
|||
package basic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-admin/app/admin/middleware"
|
||||
"go-admin/app/admin/models"
|
||||
|
@ -28,6 +29,7 @@ func CreateCategory(c *gin.Context) {
|
|||
var req = new(CreateCategoryRequest)
|
||||
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
app.Error(c, 400, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
@ -45,7 +47,7 @@ func CreateCategory(c *gin.Context) {
|
|||
CooperativeBusinessId: middleware.GetCooperativeBusinessId(c),
|
||||
}
|
||||
|
||||
err = orm.Eloquent.Model(category).Create(category).Error
|
||||
err = orm.Eloquent.Debug().Create(&category).Error
|
||||
if err != nil {
|
||||
logger.Error("创建分类失败", logger.Field("category", category), logger.Field("err", err))
|
||||
app.Error(c, 500, err, "创建分类失败")
|
||||
|
@ -66,7 +68,7 @@ type UpdateCategoryRequest struct {
|
|||
// @Produce json
|
||||
// @Accept json
|
||||
// @Param request body UpdateCategoryRequest true "商品分类编辑模型"
|
||||
// @Success 200 {object} app.Response
|
||||
// @Success 200 {object} models.Category
|
||||
// @Router /api/v1/category/update [post]
|
||||
func UpdateCategory(c *gin.Context) {
|
||||
req := new(UpdateCategoryRequest)
|
||||
|
@ -111,7 +113,8 @@ func UpdateCategory(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
app.OK(c, nil, "ok")
|
||||
app.OK(c, category, "ok")
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteCategory 删除分类
|
||||
|
@ -159,3 +162,31 @@ func CategoryList(c *gin.Context) {
|
|||
app.OK(c, list, "ok")
|
||||
return
|
||||
}
|
||||
|
||||
type CategoryDisplayRequest struct {
|
||||
Id uint32 `json:"id"` //分类id
|
||||
Display int `json:"display"` //是否展示 1展示 0隐藏
|
||||
}
|
||||
|
||||
// CategoryDisplay 隐藏或展示分类
|
||||
// @Summary 隐藏或展示分类
|
||||
// @Tags 商品分类
|
||||
// @Produce json
|
||||
// @Param request body CategoryDisplayRequest true "隐藏或展示分类请求模型"
|
||||
// @Success 200 {object} app.Response
|
||||
// @Router /api/v1/category/display [post]
|
||||
func CategoryDisplay(c *gin.Context) {
|
||||
req := new(CategoryDisplayRequest)
|
||||
_ = c.ShouldBindJSON(req)
|
||||
|
||||
err := orm.Eloquent.Model(models.Category{}).
|
||||
Where("id", req.Id).
|
||||
UpdateColumn("display", req.Display).Error
|
||||
|
||||
if err != nil {
|
||||
app.Error(c, 500, err, "修改失败")
|
||||
return
|
||||
}
|
||||
|
||||
app.OK(c, nil, "ok")
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"go-admin/app/admin/models/common"
|
||||
orm "go-admin/common/global"
|
||||
"go-admin/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -30,6 +31,7 @@ func (c *Category) BeforeCreate(tx *gorm.DB) error {
|
|||
if c.Number == "" {
|
||||
n, err := GenerateNumber(c.CooperativeBusinessId, c.Pid)
|
||||
if err != nil {
|
||||
logger.Error("before create category err", logger.Field("c", c), logger.Field("err", err))
|
||||
return err
|
||||
}
|
||||
c.Number = n
|
||||
|
@ -39,37 +41,30 @@ func (c *Category) BeforeCreate(tx *gorm.DB) error {
|
|||
|
||||
// GenerateNumber 生成分类编码
|
||||
func GenerateNumber(cid uint32, pid uint32) (string, error) {
|
||||
m := orm.Eloquent.Model(Category{})
|
||||
var count int64
|
||||
var err error
|
||||
|
||||
err = orm.Eloquent.Model(Category{}).
|
||||
Unscoped().
|
||||
Scopes(common.ScopeBusiness(cid)).
|
||||
Where("pid", pid).
|
||||
Count(&count).
|
||||
Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if pid == 0 {
|
||||
err = m.Scopes(common.ScopeBusiness(cid)).
|
||||
Where("pid", 0).
|
||||
Count(&count).
|
||||
Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%03d", count), nil
|
||||
return fmt.Sprintf("%03d", count+1), nil
|
||||
} else {
|
||||
var parent Category
|
||||
err = m.
|
||||
err = orm.Eloquent.Model(Category{}).
|
||||
Scopes(common.ScopeBusiness(cid)).
|
||||
Where("id", pid).
|
||||
First(&parent).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = m.
|
||||
Unscoped().
|
||||
Scopes(common.ScopeBusiness(cid)).
|
||||
Where("pid", pid).
|
||||
Count(&count).
|
||||
Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s%03d", parent.Number, count), nil
|
||||
return fmt.Sprintf("%s%03d", parent.Number, count+1), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,4 +13,5 @@ func registerCategoryRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddl
|
|||
r.POST("update", basic.UpdateCategory)
|
||||
r.DELETE("delete/:id", basic.DeleteCategory)
|
||||
r.POST("list", basic.CategoryList)
|
||||
r.POST("display", basic.CategoryDisplay)
|
||||
}
|
||||
|
|
76
docs/docs.go
76
docs/docs.go
|
@ -80,6 +80,36 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/category/display": {
|
||||
"post": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"商品分类"
|
||||
],
|
||||
"summary": "隐藏或展示分类",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "隐藏或展示分类请求模型",
|
||||
"name": "request",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/basic.CategoryDisplayRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/app.Response"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/category/list/": {
|
||||
"get": {
|
||||
"produces": [
|
||||
|
@ -140,7 +170,7 @@ const docTemplate = `{
|
|||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/app.Response"
|
||||
"$ref": "#/definitions/models.Category"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2800,6 +2830,19 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"basic.CategoryDisplayRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"display": {
|
||||
"description": "是否展示 1展示 0隐藏",
|
||||
"type": "integer"
|
||||
},
|
||||
"id": {
|
||||
"description": "分类id",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"basic.CategoryListRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -3017,6 +3060,37 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"models.Category": {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
"models.CategoryModel": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
@ -72,6 +72,36 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/category/display": {
|
||||
"post": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"商品分类"
|
||||
],
|
||||
"summary": "隐藏或展示分类",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "隐藏或展示分类请求模型",
|
||||
"name": "request",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/basic.CategoryDisplayRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/app.Response"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/category/list/": {
|
||||
"get": {
|
||||
"produces": [
|
||||
|
@ -132,7 +162,7 @@
|
|||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/app.Response"
|
||||
"$ref": "#/definitions/models.Category"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2792,6 +2822,19 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"basic.CategoryDisplayRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"display": {
|
||||
"description": "是否展示 1展示 0隐藏",
|
||||
"type": "integer"
|
||||
},
|
||||
"id": {
|
||||
"description": "分类id",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
},
|
||||
"basic.CategoryListRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -3009,6 +3052,37 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"models.Category": {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
"models.CategoryModel": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
@ -23,6 +23,15 @@ definitions:
|
|||
requestId:
|
||||
type: string
|
||||
type: object
|
||||
basic.CategoryDisplayRequest:
|
||||
properties:
|
||||
display:
|
||||
description: 是否展示 1展示 0隐藏
|
||||
type: integer
|
||||
id:
|
||||
description: 分类id
|
||||
type: integer
|
||||
type: object
|
||||
basic.CategoryListRequest:
|
||||
properties:
|
||||
is_all:
|
||||
|
@ -185,6 +194,28 @@ definitions:
|
|||
- id
|
||||
- name
|
||||
type: object
|
||||
models.Category:
|
||||
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
|
||||
type: object
|
||||
models.CategoryModel:
|
||||
properties:
|
||||
cooperative_business_id:
|
||||
|
@ -1083,6 +1114,25 @@ paths:
|
|||
summary: 删除分类
|
||||
tags:
|
||||
- 商品分类
|
||||
/api/v1/category/display:
|
||||
post:
|
||||
parameters:
|
||||
- description: 隐藏或展示分类请求模型
|
||||
in: body
|
||||
name: request
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/basic.CategoryDisplayRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/app.Response'
|
||||
summary: 隐藏或展示分类
|
||||
tags:
|
||||
- 商品分类
|
||||
/api/v1/category/list/:
|
||||
get:
|
||||
parameters:
|
||||
|
@ -1121,7 +1171,7 @@ paths:
|
|||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/app.Response'
|
||||
$ref: '#/definitions/models.Category'
|
||||
summary: 编辑分类
|
||||
tags:
|
||||
- 商品分类
|
||||
|
|
Loading…
Reference in New Issue
Block a user