135 lines
2.8 KiB
Go
135 lines
2.8 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"go-admin/app/admin/models/common"
|
|
orm "go-admin/common/global"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Category struct {
|
|
Model
|
|
|
|
Name string `json:"name"` // 分类名称
|
|
Number string `json:"number"` //编号
|
|
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"
|
|
}
|
|
|
|
func (c *Category) BeforeCreate(tx *gorm.DB) error {
|
|
if c.Number == "" {
|
|
n, err := GenerateNumber(c.CooperativeBusinessId, c.Pid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Number = n
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GenerateNumber 生成分类编码
|
|
func GenerateNumber(cid uint32, pid uint32) (string, error) {
|
|
m := orm.Eloquent.Model(Category{})
|
|
var count int64
|
|
var err error
|
|
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
|
|
} else {
|
|
var parent Category
|
|
err = m.
|
|
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
|
|
}
|
|
}
|
|
|
|
// GetCategoryById 通过id获取分类
|
|
func GetCategoryById(id uint32) (*Category, error) {
|
|
var c *Category
|
|
err := orm.Eloquent.Model(c).Where("id", id).First(c).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
type CategoryModel struct {
|
|
Category
|
|
SubCategory []*CategoryModel
|
|
}
|
|
|
|
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{})
|
|
|
|
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, all)
|
|
}
|
|
|
|
return list, nil
|
|
}
|
|
|
|
// 查询子分类
|
|
func findChildCategory(prev *CategoryModel, all bool) []*CategoryModel {
|
|
var cs []*CategoryModel
|
|
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, all)
|
|
}
|
|
|
|
return cs
|
|
}
|