1use crate::rbum::dto::rbum_domain_dto::RbumDomainSummaryResp;
2use crate::rbum::dto::rbum_kind_dto::RbumKindSummaryResp;
3use crate::rbum::dto::rbum_set_item_dto::RbumSetItemRelInfoResp;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use tardis::basic::field::TrimString;
7use tardis::chrono::{DateTime, Utc};
8
9use tardis::db::sea_orm;
10
11use tardis::web::poem_openapi;
12
13use crate::rbum::rbum_enumeration::RbumScopeLevelKind;
14
15#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
19pub struct RbumSetAddReq {
20 #[oai(validator(min_length = "2", max_length = "255"))]
24 pub code: TrimString,
25 #[oai(validator(min_length = "2", max_length = "255"))]
29 pub kind: TrimString,
30 #[oai(validator(min_length = "2", max_length = "255"))]
34 pub name: TrimString,
35 #[oai(validator(min_length = "2", max_length = "2000"))]
39 pub note: Option<String>,
40 #[oai(validator(min_length = "2", max_length = "1000"))]
44 pub icon: Option<String>,
45 pub sort: Option<i64>,
49 #[oai(validator(min_length = "2", max_length = "1000"))]
53 pub ext: Option<String>,
54
55 pub scope_level: Option<RbumScopeLevelKind>,
56 pub disabled: Option<bool>,
57}
58
59#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
63pub struct RbumSetModifyReq {
64 #[oai(validator(min_length = "2", max_length = "255"))]
68 pub name: Option<TrimString>,
69 #[oai(validator(min_length = "2", max_length = "2000"))]
73 pub note: Option<String>,
74 #[oai(validator(min_length = "2", max_length = "1000"))]
78 pub icon: Option<String>,
79 pub sort: Option<i64>,
83 #[oai(validator(min_length = "2", max_length = "1000"))]
87 pub ext: Option<String>,
88
89 pub scope_level: Option<RbumScopeLevelKind>,
90 pub disabled: Option<bool>,
91}
92
93#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
97pub struct RbumSetSummaryResp {
98 pub id: String,
102 pub code: String,
106 pub kind: String,
110 pub name: String,
114 pub icon: String,
118 pub sort: i64,
122 pub ext: String,
126
127 pub own_paths: String,
128 pub owner: String,
129 pub create_time: DateTime<Utc>,
130 pub update_time: DateTime<Utc>,
131
132 pub scope_level: RbumScopeLevelKind,
133 pub disabled: bool,
134}
135
136#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
140pub struct RbumSetDetailResp {
141 pub id: String,
145 pub code: String,
149 pub kind: String,
153 pub name: String,
157 pub note: String,
161 pub icon: String,
165 pub sort: i64,
169 pub ext: String,
173
174 pub own_paths: String,
175 pub owner: String,
176 pub owner_name: Option<String>,
177 pub create_time: DateTime<Utc>,
178 pub update_time: DateTime<Utc>,
179
180 pub scope_level: RbumScopeLevelKind,
181 pub disabled: bool,
182}
183
184#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
188pub struct RbumSetPathResp {
189 pub id: String,
193 pub name: String,
197
198 pub own_paths: String,
199}
200
201#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
205pub struct RbumSetTreeResp {
206 pub main: Vec<RbumSetTreeNodeResp>,
210 pub ext: Option<RbumSetTreeExtResp>,
214}
215
216impl RbumSetTreeResp {
217 pub fn to_trees(&self) -> RbumSetTreeCateResp {
218 let mut nodes_map: HashMap<String, RbumSetTreeCateNodeResp> = HashMap::new();
219 let mut child_map: HashMap<String, Vec<String>> = HashMap::new();
220 let mut roots: Vec<String> = Vec::new();
221
222 for node in &self.main {
224 nodes_map.insert(
225 node.id.clone(),
226 RbumSetTreeCateNodeResp {
227 id: node.id.clone(),
228 sys_code: node.sys_code.clone(),
229 bus_code: node.bus_code.clone(),
230 name: node.name.clone(),
231 icon: node.icon.clone(),
232 sort: node.sort,
233 ext: node.ext.clone(),
234 node: vec![],
235 own_paths: node.own_paths.clone(),
236 owner: node.owner.clone(),
237 create_time: node.create_time,
238 update_time: node.update_time,
239 scope_level: node.scope_level.clone(),
240 },
241 );
242 if let Some(parent_id) = node.pid.as_ref() {
243 child_map.entry(parent_id.to_string()).or_default().push(node.id.clone());
244 } else {
245 roots.push(node.id.clone());
246 }
247 }
248 fn build_tree(node_id: String, nodes_map: &mut HashMap<String, RbumSetTreeCateNodeResp>, child_map: &HashMap<String, Vec<String>>) -> Option<RbumSetTreeCateNodeResp> {
250 if let Some(mut node) = nodes_map.remove(&node_id) {
251 if let Some(children) = child_map.get(&node_id) {
252 for child_id in children.clone() {
253 let child_node = build_tree(child_id, nodes_map, child_map);
254 if let Some(child_node) = child_node {
255 node.node.push(child_node);
256 }
257 }
258 }
259 Some(node)
260 } else {
261 None
262 }
263 }
264 let mut trees: Vec<RbumSetTreeCateNodeResp> = Vec::new();
266 for root_id in roots {
267 if let Some(root_node) = build_tree(root_id, &mut nodes_map, &child_map) {
268 trees.push(root_node);
269 }
270 }
271 RbumSetTreeCateResp {
272 cate_tree: trees,
273 ext: self.ext.clone(),
274 }
275 }
276}
277
278#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
282pub struct RbumSetTreeNodeResp {
283 pub id: String,
287 pub sys_code: String,
295 pub bus_code: String,
299 pub name: String,
303 pub icon: String,
307 pub sort: i64,
311 pub ext: String,
315 pub pid: Option<String>,
319 pub rel: Option<String>,
327
328 pub own_paths: String,
329 pub owner: String,
330 pub create_time: DateTime<Utc>,
331 pub update_time: DateTime<Utc>,
332
333 pub scope_level: RbumScopeLevelKind,
334}
335
336#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
340pub struct RbumSetTreeExtResp {
341 pub items: HashMap<String, Vec<RbumSetItemRelInfoResp>>,
347 pub item_number_agg: HashMap<String, HashMap<String, u64>>,
353 pub item_kinds: HashMap<String, RbumKindSummaryResp>,
359 pub item_domains: HashMap<String, RbumDomainSummaryResp>,
365}
366
367#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
371pub struct RbumSetTreeCateResp {
372 pub cate_tree: Vec<RbumSetTreeCateNodeResp>,
376 pub ext: Option<RbumSetTreeExtResp>,
380}
381
382#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
383pub struct RbumSetTreeCateNodeResp {
387 pub id: String,
391
392 pub sys_code: String,
400
401 pub bus_code: String,
405
406 pub name: String,
410
411 pub icon: String,
415
416 pub sort: i64,
420
421 pub ext: String,
425
426 pub node: Vec<RbumSetTreeCateNodeResp>,
427
428 pub own_paths: String,
429
430 pub owner: String,
431
432 pub create_time: DateTime<Utc>,
433
434 pub update_time: DateTime<Utc>,
435
436 pub scope_level: RbumScopeLevelKind,
437}