From b229f3df566df87d121ef898167c658e8607e304 Mon Sep 17 00:00:00 2001 From: RIceWqy <1840169763@qq.com> Date: Sun, 24 Dec 2023 18:06:21 +0800 Subject: [PATCH] init --- module/category/Category.go | 60 +++++++++++++++++++++++-------------- module/category/init.go | 3 ++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/module/category/Category.go b/module/category/Category.go index ebd7f2a..42d672e 100644 --- a/module/category/Category.go +++ b/module/category/Category.go @@ -9,38 +9,52 @@ func (Category) TableName() string { } 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:"-"` + 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) { - c.getChildens(session) - if len(c.Children) > 0 { - - } + 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 (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 { +func findChildren(categories []Category, 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) - } + 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) { + +} diff --git a/module/category/init.go b/module/category/init.go index d746204..62274b0 100644 --- a/module/category/init.go +++ b/module/category/init.go @@ -8,5 +8,8 @@ import ( func init() { xormDriver.Sync2(new(Category)) towgo.NewCRUDJsonrpcAPI("/category", Category{}, []Category{}).RegAPI() + towgo.SetFunc("/category/treeList", TreeList) +} +func TreeList(rpcConn towgo.JsonRpcConnection) { }