创建分类

This commit is contained in:
范俊成 2023-10-17 15:33:05 +08:00
parent 9649d389c6
commit 67df27ae49
4 changed files with 129 additions and 4 deletions

View File

@ -0,0 +1,55 @@
package basic
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/middleware"
"go-admin/app/admin/models"
orm "go-admin/common/global"
"go-admin/logger"
"go-admin/tools"
"go-admin/tools/app"
)
type CreateCategoryRequest struct {
Name string `json:"name" validate:"required"` //名称
ParentId string `json:"parent_id"` //父分类编号
}
// CreateCategory 创建分类
// @Summary 创建分类
// @Tags 商品分类
// @Produce json
// @Accept json
// @Param request body CreateCategoryRequest true "商品分类创建模型"
// @Success 200 {object} app.Response
// @Router /api/v1/category/create [post]
func CreateCategory(c *gin.Context) {
var req = new(CreateCategoryRequest)
if err := c.ShouldBindJSON(req); err != nil {
app.Error(c, 400, err, err.Error())
return
}
err := tools.Validate(req)
if err != nil {
app.Error(c, 400, err, err.Error())
return
}
category := models.Category{
Name: req.Name,
Display: 1,
Pid: req.ParentId,
CooperativeBusinessId: middleware.GetCooperativeBusinessId(c),
}
err = orm.Eloquent.Model(category).Create(category).Error
if err != nil {
logger.Error("创建分类失败", logger.Field("category", category), logger.Field("err", err))
app.Error(c, 500, err, "创建分类失败")
return
}
app.OK(c, nil, "ok")
}

View File

@ -0,0 +1,57 @@
package models
import (
"fmt"
"go-admin/app/admin/models/common"
"gorm.io/gorm"
)
type Category struct {
Model
Name string `json:"name"` // 分类名称
Number string `json:"number"` //编号
Display int8 `json:"state"` // 1 展示 0 隐藏
Pid string `json:"pid" gorm:"index"` //父分类的编号
CooperativeBusinessId uint32 `json:"cooperative_business_id"` //合作商id
}
func (c *Category) TableName() string {
return "erp_category"
}
func (c *Category) BeforeCreate(tx *gorm.DB) error {
if c.Number == "" {
var count int64
var err error
if c.Pid == "" {
err = tx.Model(c).
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("pid", "").
Count(&count).Error
if err != nil {
return err
}
c.Number = fmt.Sprintf("%03d", count)
} else {
var parent Category
err = tx.
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("number", c.Pid).
First(&parent).Error
if err != nil {
return err
}
err = tx.Model(c).
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("pid", c.Pid).
Count(&count).
Error
if err != nil {
return err
}
c.Number = fmt.Sprintf("%s%03d", parent.Number, count)
}
}
return nil
}

View File

@ -0,0 +1,9 @@
package common
import "gorm.io/gorm"
func ScopeCooperativeBusiness(id uint32) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("cooperative_business_id = ?", id)
}
}

View File

@ -2,6 +2,7 @@ package models
import (
"fmt"
"go-admin/app/admin/models/common"
orm "go-admin/common/global"
"go-admin/logger"
"gorm.io/gorm"
@ -35,13 +36,16 @@ func (s *Supplier) TableName() string {
func (s *Supplier) BeforeCreate(tx *gorm.DB) error {
if s.Number == "" {
var partnerCount int64
err := tx.Table(s.TableName()).Unscoped().Count(&partnerCount).Error
var count int64
err := tx.Table(s.TableName()).
Unscoped().
Scopes(common.ScopeCooperativeBusiness(s.CooperativeBusinessId)).
Count(&count).Error
if err != nil {
logger.Error("get supplier count failed", logger.Field("err", err), logger.Field("s", s))
return err
}
s.Number = fmt.Sprintf("%03d", partnerCount)
s.Number = fmt.Sprintf("%03d", count)
}
return nil
}
@ -63,7 +67,7 @@ func GetSupplier(req GetSupplierRequest) ([]*Supplier, error) {
m = m.Where("number = ?", req.Number)
}
m = m.Where("cooperative_business_id = ?", req.CooperativeBusinessId)
m = m.Scopes(common.ScopeCooperativeBusiness(req.CooperativeBusinessId))
err := m.
Order("created_at desc").