This commit is contained in:
2025-04-15 18:38:03 +08:00
commit 23537c3ae9
12 changed files with 799 additions and 0 deletions

223
render_ui.cpp Normal file
View File

@@ -0,0 +1,223 @@
#include "render_ui.h"
#include "data.h"
#include "userAuth.h"
#include <ostream>
int show_main_menu() {
int choice = 0;
std::cout << endl
<< "[=== 主菜单 ===]" << std::endl
<< "1. 车辆进场" << std::endl
<< "2. 车辆离场" << std::endl
<< "3. 查看在场车辆" << std::endl
<< "4. 查看离场车辆" << std::endl
<< "5. 添加用户" << std::endl
<< "6. 查看已有用户" << std::endl
<< "7. 修改用户密码" << std::endl
<< "8. 删除用户" << std::endl
<< "9. 退出" << std::endl
<< "请输入选项: ";
while (!(std::cin >> choice) || choice < 1 || choice > 9) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "请重新输入。" << std::endl;
}
std::cin.ignore();
return choice;
}
void display_user_records(const std::vector<UserStruct> &records) {
if (records.empty()) {
std::cout << "数据库中没有存储记录。" << std::endl;
return;
}
std::cout << "[====== 用户记录 ======]" << std::endl;
for (const auto &record : records) {
std::cout << "ID: " << record.id << std::endl
<< "用户名称: " << record.username << std::endl
<< "用户密码: " << record.userpassword << std::endl
<< "创建时间: " << record.created_time << std::endl
<< "[------------------------]" << std::endl;
}
}
void handle_show_user_records(sqlite3 *db) {
auto records = get_user_inputs(db);
display_user_records(records);
}
void display_pa_records(const std::vector<PAStruct> &records) {
if (records.empty()) {
std::cout << "数据库中没有存储记录。" << std::endl;
return;
}
std::cout << "[====== 车辆记录记录 ======]" << std::endl;
for (const auto &record : records) {
std::cout << "ID: " << record.id << std::endl
<< "车牌号: " << record.car_id << std::endl
<< "进场时间: " << record.in_pa_time << std::endl
<< "离场时间: " << record.out_pa_time << std::endl
<< "[------------------------]" <<std::endl;
}
}
void handle_show_pa_records(sqlite3 *db) {
auto records = get_pa_cars(db);
display_pa_records(records);
}
void handle_show_pa_leftcar_records (sqlite3 *db) {
auto records = get_pa_leftcars(db);
display_pa_records(records);
}
void handle_add_pa_record(sqlite3 *db) {
std::string user_input;
std::cout << "请输入要进场的车辆: " << std::endl;
std::getline(std::cin, user_input);
if (!user_input.empty()) {
register_pa_car(db, user_input);
std::cout << "车辆保存成功" << std::endl;
} else {
std::cout << "输入内容不能为空!" << std::endl;
}
}
void handle_add_user_record(sqlite3 *db) {
std::string user_input1;
std::cout << "请输入要注册的用户名: " << std::endl;
std::getline(std::cin, user_input1);
std::string user_input2;
std::cout << "请输入要注册的用户的密码: " << std::endl;
std::getline(std::cin, user_input2);
if (!user_input1.empty() && !user_input2.empty()) {
register_user(db, user_input1, user_input2);
std::cout << "用户保存成功" << std::endl;
} else {
std::cout << "输入内容不能为空!" << std::endl;
}
}
void handle_update_userpassword_record(sqlite3 *db) {
auto records = get_user_inputs(db);
display_user_records(records);
if (records.empty())
return;
int target_id;
std::cout << "请输入要更新的记录ID: " << std::endl;
if (!(std::cin >> target_id)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "无效的ID输入" << std::endl;
return;
}
std::cin.ignore();
auto it = std::find_if(
records.begin(), records.end(),
[target_id](const UserStruct &u) { return u.id == target_id; });
if (it == records.end()) {
std::cout << "未能找到ID为 " << target_id << " 的记录" << std::endl;
return;
}
std::string new_content1;
std::cout << "原用户名 " << it->username << std::endl;
std::cout << "新用户名(直接回车取消) ";
std::getline(std::cin, new_content1);
std::string new_content2;
std::cout << "原用户密码 " << it->userpassword << std::endl;
std::cout << "新用户密码(直接回车取消) ";
std::getline(std::cin, new_content2);
if (new_content1.empty()) {
new_content1 = it->username;
}
if (new_content2.empty()) {
new_content2 = it->userpassword;
}
if (update_user(db, target_id, new_content1, new_content2)) {
std::cout << "更新成功!" << std::endl;
}
else {
std::cout << "更新失败!" << std::endl;
}
}
void handle_delete_user_record(sqlite3 *db) {
auto records = get_user_inputs(db);
display_user_records(records);
if (records.empty())
return;
std::cout << "请输入要删除的记录ID(或输入exit取消): " << std::endl;
std::string input;
std::getline(std::cin, input);
// 处理取消操作
if (input == "exit" || input == "EXIT") {
std::cout << "已取消删除操作" << std::endl;
return;
}
try {
int target_id = std::stoi(input);
auto it = std::find_if(
records.begin(), records.end(),
[target_id](const UserStruct &u) { return u.id == target_id; });
if (it == records.end()) {
std::cout << "未找到ID为 " << target_id << " 的记录" << std::endl;
return;
}
// 确认删除
std::cout << "确认删除以下记录吗? " << std::endl
<< "ID: " << it->id << std::endl
<< "用户名: " << it->username << std::endl
<< "输入 y 确认,其他取消: ";
std::string confirm;
std::getline(std::cin, confirm);
if (confirm == "y" || confirm == "Y") {
if (delete_user(db, target_id)) {
std::cout << "记录删除成功! " << std::endl;
} else {
std::cout << "记录删除失败! " << std::endl;
}
} else {
std::cout << "已取消删除操作。 " << std::endl;
}
} catch (const std::invalid_argument &) {
std::cout << "无效的ID输入! " << std::endl;
} catch (const std::out_of_range &) {
std::cout << "ID超出有效范围! " << std::endl;
}
}
void handle_leave_car (sqlite3* db) {
auto records = get_pa_cars(db);
display_pa_records(records);
int choice;
std::cout << "请输入要离场的车辆:" ;
std::cin >> choice;
if (car_leave_func(db, choice)) {
std::cout << "车辆离场! " <<std::endl;
}
}