192 lines
5.5 KiB
C++
192 lines
5.5 KiB
C++
#include "data.h"
|
|
#include "tools.h"
|
|
#include <sqlite3.h>
|
|
|
|
const char *CREATE_PA_TABLE = "CREATE TABLE IF NOT EXISTS pa_table ("
|
|
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
|
"car_id TEXT NOT NULL,"
|
|
"in_pa_time DATETIME,"
|
|
"out_pa_time DATETIME,"
|
|
"define_in_pa INTEGER);";
|
|
const char *REGISTER_PA_CAR =
|
|
"INSERT INTO pa_table (car_id, in_pa_time, "
|
|
"out_pa_time, define_in_pa) VALUES (?1, ?2, ?3, ?4);";
|
|
const char *SELECT_PA_CAR =
|
|
"SELECT id, car_id, in_pa_time, out_pa_time, define_in_pa FROM pa_table "
|
|
"WHERE define_in_pa = 0 "
|
|
"ORDER BY id DESC;";
|
|
const char *SELECT_PA_LEFTCAR =
|
|
"SELECT id, car_id, in_pa_time, out_pa_time, define_in_pa FROM pa_table "
|
|
"WHERE define_in_pa = 1 "
|
|
"ORDER BY id DESC;";
|
|
|
|
const char *UPDATE_PA_CAR =
|
|
"UPDATE pa_table SET out_pa_time = ?1, define_in_pa = ?3 WHERE id = ?2";
|
|
const char *DELETE_PA_CAR = "DELETE FROM pa_table WHERE id = ?1;";
|
|
|
|
void init_pa_data(sqlite3 *db) {
|
|
char *err_msg = nullptr;
|
|
|
|
int rc = sqlite3_exec(db, CREATE_PA_TABLE, nullptr, nullptr, &err_msg);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "创建表失败: " << err_msg << std::endl;
|
|
sqlite3_free(err_msg);
|
|
exit(1);
|
|
};
|
|
};
|
|
|
|
void register_pa_car(sqlite3 *db, const string &car_id) {
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(db, REGISTER_PA_CAR, -1, &stmt, nullptr);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
int define_in_pa = 0;
|
|
string in_pa_time = get_utc8_time();
|
|
string NAtime = "N/A";
|
|
|
|
sqlite3_bind_text(stmt, 1, car_id.c_str(), -1, SQLITE_STATIC);
|
|
sqlite3_bind_text(stmt, 2, in_pa_time.c_str(), -1, SQLITE_STATIC);
|
|
sqlite3_bind_text(stmt, 3, NAtime.c_str(), -1, SQLITE_STATIC);
|
|
sqlite3_bind_int(stmt, 4, define_in_pa);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
if (rc != SQLITE_DONE) {
|
|
std::cerr << "执行失败" << sqlite3_errmsg(db) << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
};
|
|
|
|
vector<PAStruct> get_pa_cars(sqlite3 *db) {
|
|
vector<PAStruct> results;
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(db, SELECT_PA_CAR, -1, &stmt, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "准备查询失败: " << sqlite3_errmsg(db) << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
|
PAStruct record;
|
|
record.id = sqlite3_column_int(stmt, 0);
|
|
|
|
const unsigned char *text1 = sqlite3_column_text(stmt, 1);
|
|
record.car_id = std::string(reinterpret_cast<const char *>(text1));
|
|
|
|
const unsigned char *time1 = sqlite3_column_text(stmt, 2);
|
|
record.in_pa_time = time1 ? reinterpret_cast<const char *>(time1) : "N/A";
|
|
|
|
const unsigned char *time2 = sqlite3_column_text(stmt, 3);
|
|
record.out_pa_time = time2 ? reinterpret_cast<const char *>(time2) : "N/A";
|
|
|
|
record.define_in_pa = sqlite3_column_int(stmt, 4);
|
|
|
|
results.push_back(record);
|
|
}
|
|
|
|
if (rc != SQLITE_DONE) {
|
|
std::cerr << "查询错误失败: " << sqlite3_errmsg(db) << std::endl;
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
return results;
|
|
}
|
|
|
|
vector<PAStruct> get_pa_leftcars(sqlite3 *db) {
|
|
vector<PAStruct> results;
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(db, SELECT_PA_LEFTCAR, -1, &stmt, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "准备查询失败: " << sqlite3_errmsg(db) << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
|
PAStruct record;
|
|
record.id = sqlite3_column_int(stmt, 0);
|
|
|
|
const unsigned char *text1 = sqlite3_column_text(stmt, 1);
|
|
record.car_id = std::string(reinterpret_cast<const char *>(text1));
|
|
|
|
const unsigned char *time1 = sqlite3_column_text(stmt, 2);
|
|
record.in_pa_time = time1 ? reinterpret_cast<const char *>(time1) : "N/A";
|
|
|
|
const unsigned char *time2 = sqlite3_column_text(stmt, 3);
|
|
record.out_pa_time = time2 ? reinterpret_cast<const char *>(time2) : "N/A";
|
|
|
|
record.define_in_pa = sqlite3_column_int(stmt, 4);
|
|
|
|
results.push_back(record);
|
|
}
|
|
|
|
if (rc != SQLITE_DONE) {
|
|
std::cerr << "查询错误失败: " << sqlite3_errmsg(db) << std::endl;
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
return results;
|
|
}
|
|
|
|
bool car_leave_func(sqlite3 *db, int id) {
|
|
sqlite3_stmt *stmt;
|
|
int define_in_pa = 1;
|
|
std::string outtime = get_utc8_time();
|
|
|
|
int rc = sqlite3_prepare_v2(db, UPDATE_PA_CAR, -1, &stmt, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "准备更新语句失败: " << sqlite3_errmsg(db) << std::endl;
|
|
return false;
|
|
}
|
|
|
|
sqlite3_bind_text(stmt, 1, outtime.c_str(), -1, SQLITE_STATIC);
|
|
sqlite3_bind_int(stmt, 3, define_in_pa);
|
|
sqlite3_bind_int(stmt, 2, id);
|
|
|
|
rc = sqlite3_step(stmt);
|
|
sqlite3_finalize(stmt);
|
|
|
|
if (rc != SQLITE_DONE) {
|
|
std::cerr << "更新执行失败: " << sqlite3_errmsg(db) << std::endl;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool delete_pa_car(sqlite3 *db, int id) {
|
|
sqlite3_stmt *stmt = nullptr;
|
|
|
|
int rc = sqlite3_prepare_v2(db, DELETE_PA_CAR, -1, &stmt, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "准备删除语句失败: " << sqlite3_errmsg(db) << std::endl;
|
|
return false;
|
|
}
|
|
|
|
rc = sqlite3_bind_int(stmt, 1, id);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "绑定参数失败: " << sqlite3_errmsg(db) << std::endl;
|
|
sqlite3_finalize(stmt);
|
|
return false;
|
|
}
|
|
|
|
rc = sqlite3_step(stmt);
|
|
const bool success = (rc == SQLITE_DONE);
|
|
const char *errorMsg = sqlite3_errmsg(db);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
if (!success) {
|
|
std::cerr << "删除执行失败: " << errorMsg << std::endl;
|
|
return false;
|
|
}
|
|
return true;
|
|
}; |