golang遞歸實現(xiàn)類別樹(CategoryTree)

需求描述

點擊一級或二級或三級類別都會進(jìn)行查詢,選中的類別變?yōu)樽仙?/p>

效果圖

數(shù)據(jù)庫設(shè)計

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"`         // 上級分類
    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"`                 // 位置
}

實現(xiàn)思想

  1. 將根類別(ParentID為0)的類別裝入 list

  2. 否則裝入categoryMap(key:id,value:類別)

  3. 遍歷 list,根據(jù)根類別 ID 從 categoryMap 中查出當(dāng)前類別下的子類別

    1. 若一個類別的 ParentID == categroyId(list.id),則該類別為 categroyId(list.id) 下的子類別
    2. 將全部子類別裝入 list
      1. 遍歷 list,遞歸查詢
  4. 根據(jù)order(位置)從小到大 排序

  5. 返回 包含子類別(Child)的類別樹(CategoryTree)

golang代碼實現(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) // 根級別 有可能是多個 所以是數(shù)組
    for _, v := range alllist {
        if v.ParentID == 0 { // 第一級別的列表 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 下的子類別
    // 若一個類別的 ParentID == categroyId,則該類別為 categroyId 下的子類別
    // 將全部子類別裝入list
    list := make([]*model.ArtAssetCategory, 0)
    for _, v := range cmap {
        if v.ParentID == categroyId { // 第一級別的列表
            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ù)庫
    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"`
}

請求結(jié)果

{
    "code": 0,
    "message": "",
    "data": {
        "list": [
            {
                "id": 1,
                "name": "場景",
                "parent_id": 0,
                "child": [
                    {
                        "id": 9,
                        "name": "自然場景",
                        "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": "場景部件",
                        "parent_id": 1,
                        "child": null,
                        "order": 7
                    },
                    {
                        "id": 16,
                        "name": "其他(場景)",
                        "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": "動物",
                "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": "機器動物",
                        "parent_id": 3,
                        "child": null,
                        "order": 8
                    },
                    {
                        "id": 32,
                        "name": "其他(動物)",
                        "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": "飛機/航空器",
                        "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
            }
        ]
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.abstract class 和interface 有什么區(qū)別? 抽象類可以有構(gòu)造方法 接口不行 抽象類可以有...
    sunnysans閱讀 966評論 0 1
  • 操作系統(tǒng) 操作系統(tǒng)的特點? – 共享:資源可被多個并發(fā)執(zhí)行的進(jìn)程使用– 并發(fā):可以在同一時間間隔處理多個進(jìn)程,需要...
    不在窩里閱讀 812評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,834評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,033評論 0 11
  • 程序設(shè)計中常使用樹型結(jié)構(gòu)來表征某些數(shù)據(jù)的關(guān)聯(lián)關(guān)系,如上下級、欄目結(jié)構(gòu)、商品分類、菜單、回復(fù)等。 分類的層級關(guān)系可以...
    JunChow520閱讀 4,186評論 4 3

友情鏈接更多精彩內(nèi)容