最近项目,需要把数组组装成树形结构,不断的摸索,终于解决了,看代码!!!
数据库的数据是这样的:
希望把数据组装成树形结构进行返回:
1.controller层
(ns custombackend.modules.craftwork.craftwork-admin-routes
(:require [custombackend.common.response-utils :refer [success biz-error]]
[reitit.ring.middleware.exception :as exception]
[custombackend.modules.craftwork.craftwork-spec :as craftwork-spec]
[custombackend.modules.craftwork.craftwork-service :as craftwork-service]
[reitit.ring.middleware.exception :as exception]))
;;工艺业务错误middleware
(def craftwork-error-middleware
(exception/create-exception-middleware
{:craftwork/error (fn [exception request]
(let [data (ex-data exception)]
(biz-error (:key data) (:value data))))}))
(defn craftwork-admin-routes []
["/craftwork"
{:swagger {:tags ["后台-工艺接口"]}
:middleware [craftwork-error-middleware]}
["/tree/by_category_id/:category_id"
{:get {:summary "根据品类获取树形结构工艺"
:parameters {:path {:category_id string?}}
:handler (fn [{{{:keys [category_id]}:path}:parameters
{:keys [company_id]}:current-user}]
(success (craftwork-service/get-craftwork-tree-by-category-id company_id category_id)))}}]])
2.service层:
组装树型结构方法:
(defn- get-tree
[parent-node group-list]
(let [children-nodes (get group-list (:craftwork_id parent-node))]
(when (seq children-nodes)
(for [children-node children-nodes] (assoc children-node :children (get-tree children-node group-list))))))
;;service层方法:
(defn get-craftwork-tree-by-category-id
"根据品类获取工艺树形结构"
[company-id category-id]
(let [craftwork-list (craftwork-db/find-craftwork-list {:company_id company-id :category_id category-id})
root-nodes (filter #(= "0" (:parent_id %)) craftwork-list)
group-list (group-by #(get % :parent_id) craftwork-list)]
(for [parent-node root-nodes] (assoc parent-node :children (get-tree parent-node group-list)))))
获取结果:
{
"code": 0,
"msg": "操作成功",
"data": [
{
"craftwork_id": "3001",
"category_id": "1002",
"craft_name": "男西装",
"parent_id": "0",
"children": [
{
"craftwork_id": "3002",
"category_id": "1002",
"craft_name": "衬类型",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3014",
"category_id": "1002",
"craft_name": "常规粘合衬",
"parent_id": "3002",
"children": null
}
]
},
{
"craftwork_id": "3003",
"category_id": "1002",
"craft_name": "领型",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3015",
"category_id": "1002",
"craft_name": "平驳领",
"parent_id": "3003",
"children": null
},
{
"craftwork_id": "3020",
"category_id": "1002",
"craft_name": "半戗驳领",
"parent_id": "3003",
"children": null
}
]
},
{
"craftwork_id": "3004",
"category_id": "1002",
"craft_name": "前门扣",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3021",
"category_id": "1002",
"craft_name": "单排一粒扣",
"parent_id": "3004",
"children": null
},
]
},
{
"craftwork_id": "3007",
"category_id": "1002",
"craft_name": "上口袋",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3048",
"category_id": "1002",
"craft_name": "无手巾袋",
"parent_id": "3007",
"children": null
},
{
"craftwork_id": "3053",
"category_id": "1002",
"craft_name": "2.3CM直手巾袋",
"parent_id": "3007",
"children": null
},
{
"craftwork_id": "3057",
"category_id": "1002",
"craft_name": "2.7CM直手巾袋",
"parent_id": "3007",
"children": null
}
]
},
{
"craftwork_id": "3008",
"category_id": "1002",
"craft_name": "下口袋",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3060",
"category_id": "1002",
"craft_name": "斜双开线口袋",
"parent_id": "3008",
"children": null
},
{
"craftwork_id": "3073",
"category_id": "1002",
"craft_name": "斜5CM正装袋",
"parent_id": "3008",
"children": null
}
]
},
{
"craftwork_id": "3009",
"category_id": "1002",
"craft_name": "挂面",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3074",
"category_id": "1002",
"craft_name": "直挂面",
"parent_id": "3009",
"children": null
},
{
"craftwork_id": "3075",
"category_id": "1002",
"craft_name": "弯挂面",
"parent_id": "3009",
"children": null
},
{
"craftwork_id": "3076",
"category_id": "1002",
"craft_name": "宝剑挂面",
"parent_id": "3009",
"children": null
}
]
},
{
"craftwork_id": "3010",
"category_id": "1002",
"craft_name": "袖衩",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3077",
"category_id": "1002",
"craft_name": "假眼假衩钉扣",
"parent_id": "3010",
"children": null
}
]
},
{
"craftwork_id": "3011",
"category_id": "1002",
"craft_name": "袖口形状",
"parent_id": "3001",
"children": [
{
"craftwork_id": "3082",
"category_id": "1002",
"craft_name": "直角袖口",
"parent_id": "3011",
"children": null
},
{
"craftwork_id": "3083",
"category_id": "1002",
"craft_name": "圆角袖口",
"parent_id": "3011",
"children": null
}
]
}
]
}
]
}
Response Code