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.
61 lines
1.4 KiB
61 lines
1.4 KiB
package category
|
|
|
|
import (
|
|
"github.com/towgo/towgo/dao/basedboperat"
|
|
)
|
|
|
|
func (Category) TableName() string {
|
|
return "category"
|
|
}
|
|
|
|
type Category struct {
|
|
Id int64 `json:"id"`
|
|
Image string `json:"image"`
|
|
Description string `json:"description"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Pid int64 `json:"pid"`
|
|
Weigh int64 `json:"weigh"`
|
|
Children []Category `json:"children" xorm:"-"`
|
|
}
|
|
|
|
func (c *Category) AfterQuery(session basedboperat.DbTransactionSession) {
|
|
var cs []Category
|
|
cs = append(cs, Category{
|
|
Id: c.Id,
|
|
Image: c.Image,
|
|
Description: c.Description,
|
|
Name: c.Name,
|
|
Type: c.Type,
|
|
Pid: c.Pid,
|
|
Weigh: c.Weigh,
|
|
Children: c.Children,
|
|
})
|
|
|
|
c.Children = findChildren(cs, session)
|
|
|
|
}
|
|
|
|
// 递归查询子集方法
|
|
func findChildren(categories []Category, session basedboperat.DbTransactionSession) []Category {
|
|
|
|
var result []Category
|
|
for _, category := range categories {
|
|
var children []Category
|
|
var list basedboperat.List
|
|
list.Limit = -1
|
|
list.Where = append(list.Where, basedboperat.Condition{
|
|
Field: "pid",
|
|
Operator: "=",
|
|
Value: category.Id,
|
|
})
|
|
session.ListScan(&list, category, children)
|
|
category.Children = findChildren(children, session)
|
|
result = append(result, category)
|
|
}
|
|
return result
|
|
}
|
|
func (c *Category) TreeList(session basedboperat.DbTransactionSession) {
|
|
|
|
}
|