需求描述
點(diǎn)擊一級(jí)或二級(jí)或三級(jí)類別都會(huì)進(jìn)行查詢踱蠢,選中的類別變?yōu)樽仙?/p>
效果圖
數(shù)據(jù)庫(kù)設(shè)計(jì)
model
// ArtAssetCategory 資產(chǎn)分類表 mapped from table <art_asset_category>
type ArtAssetCategory struct {
ID int32 `gorm:"column:id;primaryKey" json:"id"`
ParentID int32 `gorm:"column:parent_id;not null" json:"parent_id"` // 上級(jí)分類
Name string `gorm:"column:name;not null" json:"name"` // 分類名稱
CreatedAt int64 `gorm:"column:created_at;autoCreateTime" json:"created_at"` // 創(chuàng)建日期
Remark string `gorm:"column:remark;not null" json:"remark"` // 備注
Order int32 `gorm:"column:order;not null" json:"order"` // 位置
}
表
實(shí)現(xiàn)思想
將根類別(ParentID為0)的類別裝入 list
否則裝入categoryMap(key:id暮屡,value:類別)
遍歷 list揉燃,根據(jù)根類別 ID 從 categoryMap 中查出當(dāng)前類別下的子類別
- 若一個(gè)類別的 ParentID == categroyId(list.id)帽馋,則該類別為 categroyId(list.id) 下的子類別
- 將全部子類別裝入 list
- 遍歷 list窥妇,遞歸查詢
根據(jù)order(位置)從小到大 排序
返回 包含子類別(Child)的類別樹(CategoryTree)
golang代碼實(shí)現(xiàn)
handler
// CategoryTree 所有分類 樹形下拉接口
func (ah *ArtAssetHandler) CategoryTree(c *gin.Context) {
ctx := c.Request.Context()
resp, err := ah.artAssetLogic.AssetCategoryTree(ctx)
if err != nil {
ah.Fail(c, err)
return
}
ah.Success(c, resp)
}
logic
func (aal *ArtAssetLogic) AssetCategoryTree(ctx context.Context) (*bean.AssetCategoryTreeResp, error) {
alllist, err := aal.artAssetService.GetAllCategoryList(ctx)
if err != nil {
return nil, err
}
// 將根類別(ParentID為0)的類別裝入list吉执;
// 否則裝入categoryMap(key:id涕蚤,value:類別)
categoryMap := make(map[int32]*model.ArtAssetCategory)
list := make([]*model.ArtAssetCategory, 0) // 根級(jí)別 有可能是多個(gè) 所以是數(shù)組
for _, v := range alllist {
if v.ParentID == 0 { // 第一級(jí)別的列表 parentid ==0 默認(rèn)0
list = append(list, v)
} else {
categoryMap[v.ID] = v
}
}
// 包含子類別(Child)的類別樹(CategoryTree)
childList := make([]*bean.AssetCategoryTreeChild, 0, len(list))
for _, v := range list {
child := &bean.AssetCategoryTreeChild{
ID: v.ID,
Name: v.Name,
ParentId: v.ParentID,
Order: v.Order,
}
// 根據(jù) ID 查出當(dāng)前類別下的子類別
child.Child = aal.aggreCategoryTree(categoryMap, v.ID)
childList = append(childList, child)
}
// 根據(jù)order(位置)從小到大 排序
sort.Slice(childList, func(i, j int) bool {
return childList[i].Order < childList[j].Order
})
return &bean.AssetCategoryTreeResp{
Tree: childList,
}, nil
}
func (aal *ArtAssetLogic) aggreCategoryTree(cmap map[int32]*model.ArtAssetCategory, categroyId int32) []*bean.AssetCategoryTreeChild {
if categroyId <= 0 || len(cmap) <= 0 {
return nil
}
// 查詢 categroyId 下的子類別
// 若一個(gè)類別的 ParentID == categroyId,則該類別為 categroyId 下的子類別
// 將全部子類別裝入list
list := make([]*model.ArtAssetCategory, 0)
for _, v := range cmap {
if v.ParentID == categroyId { // 第一級(jí)別的列表
list = append(list, v)
}
}
if len(list) <= 0 {
return nil
}
// 包含子類別(Child)的類別樹(CategoryTree)
childList := make([]*bean.AssetCategoryTreeChild, 0, len(list))
for _, v := range list {
child := &bean.AssetCategoryTreeChild{
ID: v.ID,
Name: v.Name,
ParentId: v.ParentID,
Order: v.Order,
}
// 根據(jù) ID 查出當(dāng)前類別下的子類別(遞歸)
sublist := aal.aggreCategoryTree(cmap, v.ID)
if sublist != nil && len(sublist) > 0 {
child.Child = sublist
}
childList = append(childList, child)
}
// 根據(jù)order(位置)從小到大 排序
sort.Slice(childList, func(i, j int) bool {
return childList[i].Order < childList[j].Order
})
return childList
}
service
// 獲取資產(chǎn)分類 所有乘寒,內(nèi)存做樹形結(jié)構(gòu)組裝 只適合數(shù)量少的情況望众,數(shù)量大的一定讓前端組裝
func (aal *ArtAssetService) GetAllCategoryList(ctx context.Context) ([]*model.ArtAssetCategory, error) {
resList := make([]*model.ArtAssetCategory, 0)
db := gmysql.DB(ctx, config.GlobConfig.Mysql.DBName) //連接到數(shù)據(jù)庫(kù)
categorys := query.Use(db).ArtAssetCategory
err := categorys.WithContext(ctx).Order(categorys.Order).Scan(&resList)
if err != nil {
return nil, err
}
return resList, nil
}
bean
type AssetCategoryTreeResp struct {
Tree []*AssetCategoryTreeChild `json:"list"`
}
type AssetCategoryTreeChild struct {
ID int32 `json:"id"`
Name string `json:"name"`
ParentId int32 `json:"parent_id"`
Child []*AssetCategoryTreeChild `json:"child"`
Order int32 `json:"order"`
}
請(qǐng)求結(jié)果
{
"code": 0,
"message": "",
"data": {
"list": [
{
"id": 1,
"name": "場(chǎng)景",
"parent_id": 0,
"child": [
{
"id": 9,
"name": "自然場(chǎng)景",
"parent_id": 1,
"child": null,
"order": 1
},
{
"id": 10,
"name": "建筑",
"parent_id": 1,
"child": null,
"order": 2
},
{
"id": 11,
"name": "地形",
"parent_id": 1,
"child": null,
"order": 3
},
{
"id": 12,
"name": "科技風(fēng)",
"parent_id": 1,
"child": null,
"order": 4
},
{
"id": 13,
"name": "現(xiàn)代風(fēng)",
"parent_id": 1,
"child": null,
"order": 5
},
{
"id": 14,
"name": "鄉(xiāng)村風(fēng)",
"parent_id": 1,
"child": null,
"order": 6
},
{
"id": 15,
"name": "場(chǎng)景部件",
"parent_id": 1,
"child": null,
"order": 7
},
{
"id": 16,
"name": "其他(場(chǎng)景)",
"parent_id": 1,
"child": null,
"order": 8
}
],
"order": 0
},
{
"id": 2,
"name": "人物",
"parent_id": 0,
"child": [
{
"id": 17,
"name": "成年人-男",
"parent_id": 2,
"child": null,
"order": 1
},
{
"id": 18,
"name": "成年人-女",
"parent_id": 2,
"child": null,
"order": 2
},
{
"id": 19,
"name": "孩子-男",
"parent_id": 2,
"child": null,
"order": 3
},
{
"id": 20,
"name": "孩子-女",
"parent_id": 2,
"child": null,
"order": 4
},
{
"id": 21,
"name": "老人-男",
"parent_id": 2,
"child": null,
"order": 5
},
{
"id": 22,
"name": "老人-女",
"parent_id": 2,
"child": null,
"order": 6
},
{
"id": 23,
"name": "其他(人物)",
"parent_id": 2,
"child": null,
"order": 7
}
],
"order": 1
},
{
"id": 3,
"name": "動(dòng)物",
"parent_id": 0,
"child": [
{
"id": 24,
"name": "哺乳",
"parent_id": 3,
"child": null,
"order": 1
},
{
"id": 25,
"name": "飛禽",
"parent_id": 3,
"child": null,
"order": 2
},
{
"id": 26,
"name": "爬行",
"parent_id": 3,
"child": null,
"order": 3
},
{
"id": 27,
"name": "恐龍",
"parent_id": 3,
"child": null,
"order": 4
},
{
"id": 28,
"name": "昆蟲",
"parent_id": 3,
"child": null,
"order": 5
},
{
"id": 29,
"name": "魚類",
"parent_id": 3,
"child": null,
"order": 6
},
{
"id": 30,
"name": "兩棲",
"parent_id": 3,
"child": null,
"order": 7
},
{
"id": 31,
"name": "機(jī)器動(dòng)物",
"parent_id": 3,
"child": null,
"order": 8
},
{
"id": 32,
"name": "其他(動(dòng)物)",
"parent_id": 3,
"child": null,
"order": 9
}
],
"order": 2
},
{
"id": 4,
"name": "植物",
"parent_id": 0,
"child": [
{
"id": 33,
"name": "石頭",
"parent_id": 4,
"child": null,
"order": 1
},
{
"id": 34,
"name": "花卉",
"parent_id": 4,
"child": null,
"order": 2
},
{
"id": 35,
"name": "樹木",
"parent_id": 4,
"child": null,
"order": 3
},
{
"id": 36,
"name": "草類",
"parent_id": 4,
"child": null,
"order": 4
},
{
"id": 37,
"name": "水果蔬菜",
"parent_id": 4,
"child": null,
"order": 5
},
{
"id": 38,
"name": "其他(植物)",
"parent_id": 4,
"child": null,
"order": 6
}
],
"order": 3
},
{
"id": 5,
"name": "道具",
"parent_id": 0,
"child": [
{
"id": 39,
"name": "武器",
"parent_id": 5,
"child": null,
"order": 1
},
{
"id": 40,
"name": "家具/生活用具",
"parent_id": 5,
"child": [
{
"id": 49,
"name": "沙發(fā)",
"parent_id": 40,
"child": null,
"order": 1
},
{
"id": 50,
"name": "桌椅",
"parent_id": 40,
"child": null,
"order": 2
},
{
"id": 51,
"name": "床",
"parent_id": 40,
"child": null,
"order": 3
},
{
"id": 52,
"name": "柜子",
"parent_id": 40,
"child": null,
"order": 4
},
{
"id": 53,
"name": "門窗",
"parent_id": 40,
"child": null,
"order": 5
},
{
"id": 54,
"name": "燈具",
"parent_id": 40,
"child": null,
"order": 6
}
],
"order": 2
},
{
"id": 41,
"name": "食品/飲料/藥品",
"parent_id": 5,
"child": [
{
"id": 55,
"name": "水果",
"parent_id": 41,
"child": null,
"order": 1
},
{
"id": 56,
"name": "蔬菜",
"parent_id": 41,
"child": null,
"order": 2
},
{
"id": 57,
"name": "零食",
"parent_id": 41,
"child": null,
"order": 3
},
{
"id": 58,
"name": "飲料",
"parent_id": 41,
"child": null,
"order": 4
}
],
"order": 3
},
{
"id": 42,
"name": "其他(道具)",
"parent_id": 5,
"child": null,
"order": 4
}
],
"order": 4
},
{
"id": 6,
"name": "載具",
"parent_id": 0,
"child": [
{
"id": 43,
"name": "車輛",
"parent_id": 6,
"child": null,
"order": 1
},
{
"id": 44,
"name": "船艇",
"parent_id": 6,
"child": null,
"order": 2
},
{
"id": 45,
"name": "飛機(jī)/航空器",
"parent_id": 6,
"child": null,
"order": 3
},
{
"id": 46,
"name": "其他(載具)",
"parent_id": 6,
"child": null,
"order": 4
}
],
"order": 5
},
{
"id": 7,
"name": "怪物",
"parent_id": 0,
"child": [
{
"id": 47,
"name": "其他(怪物)",
"parent_id": 7,
"child": null,
"order": 0
}
],
"order": 6
},
{
"id": 8,
"name": "其他",
"parent_id": 0,
"child": [
{
"id": 48,
"name": "其他(其他)",
"parent_id": 8,
"child": null,
"order": 0
}
],
"order": 7
}
]
}
}