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