|
| 1 | +use file_classification_core::model::models::FileGroupCondition; |
| 2 | +use file_classification_core::utils::database::establish_connection; |
| 3 | +use std::io::{self, Write}; |
| 4 | +use file_classification_core::service::file_group::select_file_groups_by_conditions; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let connection = &mut establish_connection(); |
| 8 | + |
| 9 | + println!("文件组关联查询"); |
| 10 | + println!("=============="); |
| 11 | + |
| 12 | + loop { |
| 13 | + let conditions = get_user_conditions(); |
| 14 | + |
| 15 | + if conditions.is_empty() { |
| 16 | + println!("未输入任何条件"); |
| 17 | + // break; |
| 18 | + } |
| 19 | + |
| 20 | + match select_file_groups_by_conditions(connection, conditions, 20) { |
| 21 | + Ok(file_groups) => { |
| 22 | + println!("\n查询结果 (共 {} 条记录):", file_groups.len()); |
| 23 | + println!("-------------------------"); |
| 24 | + if file_groups.is_empty() { |
| 25 | + println!("没有找到匹配的文件组关联。"); |
| 26 | + } else { |
| 27 | + for file_group_dto in file_groups { |
| 28 | + println!("文件ID: {}, 组ID: {}", file_group_dto.file_id, file_group_dto.group_id); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + Err(e) => { |
| 33 | + eprintln!("查询出错: {}", e); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + println!("\n是否继续查询?(y/n): "); |
| 38 | + let mut input = String::new(); |
| 39 | + io::stdin().read_line(&mut input).expect("读取输入失败"); |
| 40 | + if input.trim().to_lowercase() != "y" { |
| 41 | + break; |
| 42 | + } |
| 43 | + println!("\n"); |
| 44 | + } |
| 45 | + |
| 46 | + println!("感谢使用文件组关联查询!"); |
| 47 | +} |
| 48 | + |
| 49 | +fn get_user_conditions() -> Vec<FileGroupCondition> { |
| 50 | + let mut conditions = Vec::new(); |
| 51 | + |
| 52 | + println!("请输入查询条件(留空结束输入):"); |
| 53 | + |
| 54 | + loop { |
| 55 | + println!("\n可选条件类型:"); |
| 56 | + println!("1. 文件ID等于"); |
| 57 | + println!("2. 文件ID大于"); |
| 58 | + println!("3. 文件ID小于"); |
| 59 | + println!("4. 组ID等于"); |
| 60 | + println!("5. 组ID大于"); |
| 61 | + println!("6. 组ID小于"); |
| 62 | + println!("7. AND组合条件"); |
| 63 | + println!("8. OR组合条件"); |
| 64 | + println!("9. NOT条件"); |
| 65 | + println!("0. 结束输入"); |
| 66 | + |
| 67 | + print!("请选择条件类型 (0-9): "); |
| 68 | + io::stdout().flush().unwrap(); |
| 69 | + |
| 70 | + let choice = read_number(); |
| 71 | + |
| 72 | + match choice { |
| 73 | + 0 => break, |
| 74 | + 1 => { |
| 75 | + print!("请输入文件ID值: "); |
| 76 | + io::stdout().flush().unwrap(); |
| 77 | + if let Some(value) = read_number_optional() { |
| 78 | + conditions.push(FileGroupCondition::FileId(value)); |
| 79 | + } |
| 80 | + } |
| 81 | + 2 => { |
| 82 | + print!("请输入文件ID最小值: "); |
| 83 | + io::stdout().flush().unwrap(); |
| 84 | + if let Some(value) = read_number_optional() { |
| 85 | + conditions.push(FileGroupCondition::FileIdGreaterThan(value)); |
| 86 | + } |
| 87 | + } |
| 88 | + 3 => { |
| 89 | + print!("请输入文件ID最大值: "); |
| 90 | + io::stdout().flush().unwrap(); |
| 91 | + if let Some(value) = read_number_optional() { |
| 92 | + conditions.push(FileGroupCondition::FileIdLessThan(value)); |
| 93 | + } |
| 94 | + } |
| 95 | + 4 => { |
| 96 | + print!("请输入组ID值: "); |
| 97 | + io::stdout().flush().unwrap(); |
| 98 | + if let Some(value) = read_number_optional() { |
| 99 | + conditions.push(FileGroupCondition::GroupId(value)); |
| 100 | + } |
| 101 | + } |
| 102 | + 5 => { |
| 103 | + print!("请输入组ID最小值: "); |
| 104 | + io::stdout().flush().unwrap(); |
| 105 | + if let Some(value) = read_number_optional() { |
| 106 | + conditions.push(FileGroupCondition::GroupIdGreaterThan(value)); |
| 107 | + } |
| 108 | + } |
| 109 | + 6 => { |
| 110 | + print!("请输入组ID最大值: "); |
| 111 | + io::stdout().flush().unwrap(); |
| 112 | + if let Some(value) = read_number_optional() { |
| 113 | + conditions.push(FileGroupCondition::GroupIdLessThan(value)); |
| 114 | + } |
| 115 | + } |
| 116 | + 7 => { |
| 117 | + println!("请输入AND组合条件 (输入0结束):"); |
| 118 | + let sub_conditions = get_sub_conditions("AND"); |
| 119 | + if !sub_conditions.is_empty() { |
| 120 | + conditions.push(FileGroupCondition::And(sub_conditions)); |
| 121 | + } |
| 122 | + } |
| 123 | + 8 => { |
| 124 | + println!("请输入OR组合条件 (输入0结束):"); |
| 125 | + let sub_conditions = get_sub_conditions("OR"); |
| 126 | + if !sub_conditions.is_empty() { |
| 127 | + conditions.push(FileGroupCondition::Or(sub_conditions)); |
| 128 | + } |
| 129 | + } |
| 130 | + 9 => { |
| 131 | + println!("请输入NOT条件:"); |
| 132 | + if let Some(condition) = get_single_condition() { |
| 133 | + conditions.push(FileGroupCondition::Not(Box::new(condition))); |
| 134 | + } |
| 135 | + } |
| 136 | + _ => { |
| 137 | + println!("无效选择,请重新输入。"); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + conditions |
| 143 | +} |
| 144 | + |
| 145 | +fn get_sub_conditions(condition_type: &str) -> Vec<FileGroupCondition> { |
| 146 | + let mut conditions = Vec::new(); |
| 147 | + |
| 148 | + loop { |
| 149 | + println!("\n{}子条件类型:", condition_type); |
| 150 | + println!("1. 文件ID等于"); |
| 151 | + println!("2. 文件ID大于"); |
| 152 | + println!("3. 文件ID小于"); |
| 153 | + println!("4. 组ID等于"); |
| 154 | + println!("5. 组ID大于"); |
| 155 | + println!("6. 组ID小于"); |
| 156 | + println!("0. 结束输入"); |
| 157 | + |
| 158 | + print!("请选择条件类型 (0-6): "); |
| 159 | + io::stdout().flush().unwrap(); |
| 160 | + |
| 161 | + let choice = read_number(); |
| 162 | + |
| 163 | + match choice { |
| 164 | + 0 => break, |
| 165 | + 1 => { |
| 166 | + print!("请输入文件ID值: "); |
| 167 | + io::stdout().flush().unwrap(); |
| 168 | + if let Some(value) = read_number_optional() { |
| 169 | + conditions.push(FileGroupCondition::FileId(value)); |
| 170 | + } |
| 171 | + } |
| 172 | + 2 => { |
| 173 | + print!("请输入文件ID最小值: "); |
| 174 | + io::stdout().flush().unwrap(); |
| 175 | + if let Some(value) = read_number_optional() { |
| 176 | + conditions.push(FileGroupCondition::FileIdGreaterThan(value)); |
| 177 | + } |
| 178 | + } |
| 179 | + 3 => { |
| 180 | + print!("请输入文件ID最大值: "); |
| 181 | + io::stdout().flush().unwrap(); |
| 182 | + if let Some(value) = read_number_optional() { |
| 183 | + conditions.push(FileGroupCondition::FileIdLessThan(value)); |
| 184 | + } |
| 185 | + } |
| 186 | + 4 => { |
| 187 | + print!("请输入组ID值: "); |
| 188 | + io::stdout().flush().unwrap(); |
| 189 | + if let Some(value) = read_number_optional() { |
| 190 | + conditions.push(FileGroupCondition::GroupId(value)); |
| 191 | + } |
| 192 | + } |
| 193 | + 5 => { |
| 194 | + print!("请输入组ID最小值: "); |
| 195 | + io::stdout().flush().unwrap(); |
| 196 | + if let Some(value) = read_number_optional() { |
| 197 | + conditions.push(FileGroupCondition::GroupIdGreaterThan(value)); |
| 198 | + } |
| 199 | + } |
| 200 | + 6 => { |
| 201 | + print!("请输入组ID最大值: "); |
| 202 | + io::stdout().flush().unwrap(); |
| 203 | + if let Some(value) = read_number_optional() { |
| 204 | + conditions.push(FileGroupCondition::GroupIdLessThan(value)); |
| 205 | + } |
| 206 | + } |
| 207 | + _ => { |
| 208 | + println!("无效选择,请重新输入。"); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + conditions |
| 214 | +} |
| 215 | + |
| 216 | +fn get_single_condition() -> Option<FileGroupCondition> { |
| 217 | + println!("NOT条件类型:"); |
| 218 | + println!("1. 文件ID等于"); |
| 219 | + println!("2. 文件ID大于"); |
| 220 | + println!("3. 文件ID小于"); |
| 221 | + println!("4. 组ID等于"); |
| 222 | + println!("5. 组ID大于"); |
| 223 | + println!("6. 组ID小于"); |
| 224 | + |
| 225 | + print!("请选择条件类型 (1-6): "); |
| 226 | + io::stdout().flush().unwrap(); |
| 227 | + |
| 228 | + let choice = read_number(); |
| 229 | + |
| 230 | + match choice { |
| 231 | + 1 => { |
| 232 | + print!("请输入文件ID值: "); |
| 233 | + io::stdout().flush().unwrap(); |
| 234 | + read_number_optional().map(FileGroupCondition::FileId) |
| 235 | + } |
| 236 | + 2 => { |
| 237 | + print!("请输入文件ID最小值: "); |
| 238 | + io::stdout().flush().unwrap(); |
| 239 | + read_number_optional().map(FileGroupCondition::FileIdGreaterThan) |
| 240 | + } |
| 241 | + 3 => { |
| 242 | + print!("请输入文件ID最大值: "); |
| 243 | + io::stdout().flush().unwrap(); |
| 244 | + read_number_optional().map(FileGroupCondition::FileIdLessThan) |
| 245 | + } |
| 246 | + 4 => { |
| 247 | + print!("请输入组ID值: "); |
| 248 | + io::stdout().flush().unwrap(); |
| 249 | + read_number_optional().map(FileGroupCondition::GroupId) |
| 250 | + } |
| 251 | + 5 => { |
| 252 | + print!("请输入组ID最小值: "); |
| 253 | + io::stdout().flush().unwrap(); |
| 254 | + read_number_optional().map(FileGroupCondition::GroupIdGreaterThan) |
| 255 | + } |
| 256 | + 6 => { |
| 257 | + print!("请输入组ID最大值: "); |
| 258 | + io::stdout().flush().unwrap(); |
| 259 | + read_number_optional().map(FileGroupCondition::GroupIdLessThan) |
| 260 | + } |
| 261 | + _ => { |
| 262 | + println!("无效选择。"); |
| 263 | + None |
| 264 | + } |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +fn read_number() -> i32 { |
| 269 | + let mut input = String::new(); |
| 270 | + io::stdin().read_line(&mut input).expect("读取输入失败"); |
| 271 | + input.trim().parse().unwrap_or(0) |
| 272 | +} |
| 273 | + |
| 274 | +fn read_number_optional() -> Option<i32> { |
| 275 | + let mut input = String::new(); |
| 276 | + io::stdin().read_line(&mut input).expect("读取输入失败"); |
| 277 | + match input.trim().parse() { |
| 278 | + Ok(num) => Some(num), |
| 279 | + Err(_) => { |
| 280 | + println!("输入无效,已忽略该条件。"); |
| 281 | + None |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments