You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

package category
import (
"github.com/towgo/towgo/dao/basedboperat"
)
func (Category) TableName() string {
return "category"
}
type Category struct {
Id int64 `json:"id"`
Type int64 `json:"type"`
Name string `json:"name"`
ParentId int64 `json:"parent_id"`
Children []Category `json:"children" xorm:"-"`
}
func (c *Category) AfterQuery(session basedboperat.DbTransactionSession) {
c.getChildens(session)
if len(c.Children) > 0 {
}
}
func (c *Category) getChildens(session basedboperat.DbTransactionSession) {
var children []Category
var list basedboperat.List
list.Limit = -1
session.ListScan(&list, c, &children)
c.Children = children
}
// 递归查询子集方法
func findChildren(categories []Category, parentId int64, session basedboperat.DbTransactionSession) []Category {
var result []Category
for _, category := range categories {
if category.ParentId == parentId {
children := findChildren(categories, category.Id)
category.Children = children
result = append(result, category)
}
}
return result
}