创建分类

This commit is contained in:
范俊成 2023-10-17 15:41:15 +08:00
parent 67df27ae49
commit 872e028e0d
2 changed files with 8 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import (
type CreateCategoryRequest struct { type CreateCategoryRequest struct {
Name string `json:"name" validate:"required"` //名称 Name string `json:"name" validate:"required"` //名称
ParentId string `json:"parent_id"` //父分类编号 ParentId uint32 `json:"parent_id"` //父分类id
} }
// CreateCategory 创建分类 // CreateCategory 创建分类

View File

@ -12,7 +12,7 @@ type Category struct {
Name string `json:"name"` // 分类名称 Name string `json:"name"` // 分类名称
Number string `json:"number"` //编号 Number string `json:"number"` //编号
Display int8 `json:"state"` // 1 展示 0 隐藏 Display int8 `json:"state"` // 1 展示 0 隐藏
Pid string `json:"pid" gorm:"index"` //父分类的编号 Pid uint32 `json:"pid" gorm:"index"` //父分类的编号
CooperativeBusinessId uint32 `json:"cooperative_business_id"` //合作商id CooperativeBusinessId uint32 `json:"cooperative_business_id"` //合作商id
} }
@ -24,11 +24,13 @@ func (c *Category) BeforeCreate(tx *gorm.DB) error {
if c.Number == "" { if c.Number == "" {
var count int64 var count int64
var err error var err error
if c.Pid == "" { if c.Pid == 0 {
err = tx.Model(c). err = tx.Model(c).
Unscoped().
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)). Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("pid", ""). Where("pid", "").
Count(&count).Error Count(&count).
Error
if err != nil { if err != nil {
return err return err
} }
@ -37,12 +39,13 @@ func (c *Category) BeforeCreate(tx *gorm.DB) error {
var parent Category var parent Category
err = tx. err = tx.
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)). Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("number", c.Pid). Where("id", c.Pid).
First(&parent).Error First(&parent).Error
if err != nil { if err != nil {
return err return err
} }
err = tx.Model(c). err = tx.Model(c).
Unscoped().
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)). Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
Where("pid", c.Pid). Where("pid", c.Pid).
Count(&count). Count(&count).