use crate::rbum::dto::rbum_domain_dto::RbumDomainSummaryResp;
use crate::rbum::dto::rbum_kind_dto::RbumKindSummaryResp;
use crate::rbum::dto::rbum_set_item_dto::RbumSetItemRelInfoResp;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tardis::basic::field::TrimString;
use tardis::chrono::{DateTime, Utc};
use tardis::db::sea_orm;
use tardis::web::poem_openapi;
use crate::rbum::rbum_enumeration::RbumScopeLevelKind;
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
pub struct RbumSetAddReq {
#[oai(validator(min_length = "2", max_length = "255"))]
pub code: TrimString,
#[oai(validator(min_length = "2", max_length = "255"))]
pub kind: TrimString,
#[oai(validator(min_length = "2", max_length = "255"))]
pub name: TrimString,
#[oai(validator(min_length = "2", max_length = "2000"))]
pub note: Option<String>,
#[oai(validator(min_length = "2", max_length = "1000"))]
pub icon: Option<String>,
pub sort: Option<i64>,
#[oai(validator(min_length = "2", max_length = "1000"))]
pub ext: Option<String>,
pub scope_level: Option<RbumScopeLevelKind>,
pub disabled: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object)]
pub struct RbumSetModifyReq {
#[oai(validator(min_length = "2", max_length = "255"))]
pub name: Option<TrimString>,
#[oai(validator(min_length = "2", max_length = "2000"))]
pub note: Option<String>,
#[oai(validator(min_length = "2", max_length = "1000"))]
pub icon: Option<String>,
pub sort: Option<i64>,
#[oai(validator(min_length = "2", max_length = "1000"))]
pub ext: Option<String>,
pub scope_level: Option<RbumScopeLevelKind>,
pub disabled: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
pub struct RbumSetSummaryResp {
pub id: String,
pub code: String,
pub kind: String,
pub name: String,
pub icon: String,
pub sort: i64,
pub ext: String,
pub own_paths: String,
pub owner: String,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: RbumScopeLevelKind,
pub disabled: bool,
}
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
pub struct RbumSetDetailResp {
pub id: String,
pub code: String,
pub kind: String,
pub name: String,
pub note: String,
pub icon: String,
pub sort: i64,
pub ext: String,
pub own_paths: String,
pub owner: String,
pub owner_name: Option<String>,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: RbumScopeLevelKind,
pub disabled: bool,
}
#[derive(Serialize, Deserialize, Debug, poem_openapi::Object, sea_orm::FromQueryResult)]
pub struct RbumSetPathResp {
pub id: String,
pub name: String,
pub own_paths: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
pub struct RbumSetTreeResp {
pub main: Vec<RbumSetTreeNodeResp>,
pub ext: Option<RbumSetTreeExtResp>,
}
impl RbumSetTreeResp {
pub fn to_trees(&self) -> RbumSetTreeCateResp {
let mut nodes_map: HashMap<String, RbumSetTreeCateNodeResp> = HashMap::new();
let mut child_map: HashMap<String, Vec<String>> = HashMap::new();
let mut roots: Vec<String> = Vec::new();
for node in &self.main {
nodes_map.insert(
node.id.clone(),
RbumSetTreeCateNodeResp {
id: node.id.clone(),
sys_code: node.sys_code.clone(),
bus_code: node.bus_code.clone(),
name: node.name.clone(),
icon: node.icon.clone(),
sort: node.sort,
ext: node.ext.clone(),
node: vec![],
own_paths: node.own_paths.clone(),
owner: node.owner.clone(),
create_time: node.create_time,
update_time: node.update_time,
scope_level: node.scope_level.clone(),
},
);
if let Some(parent_id) = node.pid.as_ref() {
child_map.entry(parent_id.to_string()).or_default().push(node.id.clone());
} else {
roots.push(node.id.clone());
}
}
fn build_tree(node_id: String, nodes_map: &mut HashMap<String, RbumSetTreeCateNodeResp>, child_map: &HashMap<String, Vec<String>>) -> Option<RbumSetTreeCateNodeResp> {
if let Some(mut node) = nodes_map.remove(&node_id) {
if let Some(children) = child_map.get(&node_id) {
for child_id in children.clone() {
let child_node = build_tree(child_id, nodes_map, child_map);
if let Some(child_node) = child_node {
node.node.push(child_node);
}
}
}
Some(node)
} else {
None
}
}
let mut trees: Vec<RbumSetTreeCateNodeResp> = Vec::new();
for root_id in roots {
if let Some(root_node) = build_tree(root_id, &mut nodes_map, &child_map) {
trees.push(root_node);
}
}
RbumSetTreeCateResp {
cate_tree: trees,
ext: self.ext.clone(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
pub struct RbumSetTreeNodeResp {
pub id: String,
pub sys_code: String,
pub bus_code: String,
pub name: String,
pub icon: String,
pub sort: i64,
pub ext: String,
pub pid: Option<String>,
pub rel: Option<String>,
pub own_paths: String,
pub owner: String,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: RbumScopeLevelKind,
}
#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
pub struct RbumSetTreeExtResp {
pub items: HashMap<String, Vec<RbumSetItemRelInfoResp>>,
pub item_number_agg: HashMap<String, HashMap<String, u64>>,
pub item_kinds: HashMap<String, RbumKindSummaryResp>,
pub item_domains: HashMap<String, RbumDomainSummaryResp>,
}
#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
pub struct RbumSetTreeCateResp {
pub cate_tree: Vec<RbumSetTreeCateNodeResp>,
pub ext: Option<RbumSetTreeExtResp>,
}
#[derive(Serialize, Deserialize, Debug, Clone, poem_openapi::Object)]
pub struct RbumSetTreeCateNodeResp {
pub id: String,
pub sys_code: String,
pub bus_code: String,
pub name: String,
pub icon: String,
pub sort: i64,
pub ext: String,
pub node: Vec<RbumSetTreeCateNodeResp>,
pub own_paths: String,
pub owner: String,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub scope_level: RbumScopeLevelKind,
}