Skip to content

Commit e2effe3

Browse files
committed
feat(cli): 添加获取组树状结构功能
- 新增 `group get-tree` 命令用于获取组的树状结构- 实现 `handle_group_action` 中对 `GetTree` 操作的处理逻辑 - 添加 `print_group_tree` 函数以递归打印树状结构 - 更新帮助信息,包含新的 `group get-tree` 使用说明 - 修改创建组逻辑,使用 `CreateGroupDTO` 结构体传递参数- 调整交互式列表操作的格式并优化错误输出内容
1 parent 04ace8a commit e2effe3

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

file_classification_cli/src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ pub enum GroupActions {
157157
#[clap(short, long)]
158158
tag_id: i32,
159159
},
160+
/// 获取组的树状结构
161+
GetTree {
162+
#[clap(short, long)]
163+
id: i32,
164+
},
160165
/// 通过ID更新组
161166
UpdateById {
162167
#[clap(short, long)]

file_classification_cli/src/handlers.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ fn handle_file_action(
439439
Ok(())
440440
}
441441

442-
/// 处理组相关动作
442+
/// 处理组相关操作
443443
fn handle_group_action(
444444
action: cli::GroupActions,
445445
conn: &mut AnyConnection,
@@ -448,10 +448,10 @@ fn handle_group_action(
448448
match action {
449449
cli::GroupActions::Create { name } => {
450450
let name = name.unwrap_or_else(|| get_input("请输入组名称: "));
451-
452-
match service::groups::create_group_by_name(conn, &name) {
453-
Ok(count) => println!("成功创建组,影响 {}", count),
454-
Err(e) => eprintln!("创建组失败: {:?}", e),
451+
let create_dto = models::CreateGroupDTO { name };
452+
match service::groups::create_group(conn, &create_dto) {
453+
Ok(id) => println!("组创建成功,ID: {}", id),
454+
Err(e) => println!("组创建失败: {}", e),
455455
}
456456
}
457457
cli::GroupActions::Delete { id } => {
@@ -471,7 +471,9 @@ fn handle_group_action(
471471
println!("操作已取消");
472472
}
473473
}
474-
cli::GroupActions::ListInteractive => list_groups_interactive(conn, context),
474+
cli::GroupActions::ListInteractive => {
475+
list_groups_interactive(conn, context);
476+
}
475477
cli::GroupActions::ListByConditions {
476478
conditions,
477479
order_by,
@@ -531,6 +533,15 @@ fn handle_group_action(
531533
Err(e) => eprintln!("查询失败: {:?}", e),
532534
}
533535
}
536+
cli::GroupActions::GetTree { id } => {
537+
match service::groups::get_group_tree(conn, id) {
538+
Ok(tree) => {
539+
println!("组树状结构:");
540+
print_group_tree(&tree, 0);
541+
}
542+
Err(e) => println!("获取组树状结构失败: {}", e),
543+
}
544+
}
534545
cli::GroupActions::UpdateById {
535546
id,
536547
name,
@@ -996,4 +1007,19 @@ fn handle_group_relation_action(
9961007
Ok(())
9971008
}
9981009

999-
1010+
/// 打印组树状结构
1011+
fn print_group_tree(node: &models::GroupTreeNode, depth: usize) {
1012+
let indent = " ".repeat(depth);
1013+
println!("{}- {} (ID: {}, 引用数: {}, 主组: {}, 点击数: {}, 分享数: {})",
1014+
indent,
1015+
node.group.name,
1016+
node.group.id,
1017+
node.group.reference_count,
1018+
node.group.is_primary,
1019+
node.group.click_count,
1020+
node.group.share_count);
1021+
1022+
for child in &node.children {
1023+
print_group_tree(child, depth + 1);
1024+
}
1025+
}

file_classification_cli/src/helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn print_help() {
4242
println!(" group list-by-conditions -c <conditions> --order_by <order> --limit <n> --offset <n>");
4343
println!(" group list-by-file-id --file_id <id>");
4444
println!(" group list-by-tag-id --tag_id <id>");
45+
println!(" group get-tree --id <id>");
4546
println!(" group update-by-id --id <id> [options]");
4647
println!(" group update-by-conditions -c <conditions> [options]");
4748
println!(" group delete-by-conditions -c <conditions>");

0 commit comments

Comments
 (0)