mh_goadmin_server/app/admin/models/category.go

130 lines
2.8 KiB
Go
Raw Normal View History

2023-10-17 07:33:05 +00:00
package models
import (
"fmt"
"go-admin/app/admin/models/common"
2023-10-17 08:14:39 +00:00
orm "go-admin/common/global"
2023-10-19 03:36:28 +00:00
"go-admin/logger"
2023-10-17 07:33:05 +00:00
"gorm.io/gorm"
)
type Category struct {
Model
Name string `json:"name"` // 分类名称
Number string `json:"number"` //编号
2023-10-19 02:04:13 +00:00
Display int8 `json:"display"` // 1 展示 0 隐藏
2023-10-17 07:41:15 +00:00
Pid uint32 `json:"pid" gorm:"index"` //父分类的编号
2023-10-17 07:33:05 +00:00
CooperativeBusinessId uint32 `json:"cooperative_business_id"` //合作商id
}
2023-10-19 02:04:13 +00:00
var (
Display = 1
UnDisplay = 0
)
2023-10-17 07:33:05 +00:00
func (c *Category) TableName() string {
return "erp_category"
}
func (c *Category) BeforeCreate(tx *gorm.DB) error {
if c.Number == "" {
2023-10-17 08:14:39 +00:00
n, err := GenerateNumber(c.CooperativeBusinessId, c.Pid)
if err != nil {
2023-10-19 03:36:28 +00:00
logger.Error("before create category err", logger.Field("c", c), logger.Field("err", err))
2023-10-17 08:14:39 +00:00
return err
2023-10-17 07:33:05 +00:00
}
2023-10-17 08:14:39 +00:00
c.Number = n
2023-10-17 07:33:05 +00:00
}
return nil
}
2023-10-17 08:14:39 +00:00
// GenerateNumber 生成分类编码
func GenerateNumber(cid uint32, pid uint32) (string, error) {
var count int64
var err error
2023-10-19 03:36:28 +00:00
err = orm.Eloquent.Model(Category{}).
Unscoped().
Scopes(common.ScopeBusiness(cid)).
Where("pid", pid).
Count(&count).
Error
if err != nil {
return "", err
}
2023-10-17 08:14:39 +00:00
if pid == 0 {
2023-10-19 03:36:28 +00:00
return fmt.Sprintf("%03d", count+1), nil
2023-10-17 08:14:39 +00:00
} else {
var parent Category
2023-10-19 03:36:28 +00:00
err = orm.Eloquent.Model(Category{}).
2023-10-17 08:14:39 +00:00
Scopes(common.ScopeBusiness(cid)).
Where("id", pid).
First(&parent).Error
if err != nil {
return "", err
}
2023-10-19 03:36:28 +00:00
return fmt.Sprintf("%s%03d", parent.Number, count+1), nil
2023-10-17 08:14:39 +00:00
}
}
2023-10-18 02:06:16 +00:00
// GetCategoryById 通过id获取分类
2023-10-17 08:14:39 +00:00
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
}
2023-10-18 08:22:54 +00:00
type CategoryModel struct {
Category
SubCategory []*CategoryModel
}
2023-10-19 02:04:13 +00:00
func ScopeOnlyDisplay(db *gorm.DB) *gorm.DB {
return db.Where("display", Display)
}
// GetCategoryList 分类列表
func GetCategoryList(bid uint32, all bool) ([]*CategoryModel, error) {
2023-10-18 08:22:54 +00:00
var list = make([]*CategoryModel, 0)
m := orm.Eloquent.Model(Category{})
2023-10-19 02:04:13 +00:00
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
2023-10-18 08:22:54 +00:00
if err != nil {
return nil, err
}
for _, top := range list {
2023-10-19 02:04:13 +00:00
top.SubCategory = findChildCategory(top, all)
2023-10-18 08:22:54 +00:00
}
return list, nil
}
2023-10-19 02:04:13 +00:00
// 查询子分类
func findChildCategory(prev *CategoryModel, all bool) []*CategoryModel {
2023-10-18 08:22:54 +00:00
var cs []*CategoryModel
2023-10-19 02:04:13 +00:00
m := orm.Eloquent.Model(Category{})
if !all {
m = m.Scopes(ScopeOnlyDisplay)
}
err := m.Where("pid", prev.ID).Find(&cs).Error
2023-10-18 08:22:54 +00:00
if err != nil {
return nil
}
for _, c := range cs {
2023-10-19 02:04:13 +00:00
c.SubCategory = findChildCategory(c, all)
2023-10-18 08:22:54 +00:00
}
return cs
}