Files
handleaiuinew/HanddleAI/HandleAiUi-new/appConfig/sqlManager/sqlManager.cpp
T

196 lines
5.4 KiB
C++
Raw Normal View History

2024-08-07 09:16:35 +00:00
#include <appConfig/sqlManager/sqlManager.h>
#include <appConfig.h>
2024-05-24 18:05:29 +08:00
std::vector<std::string> getFutureDates(int days)
{
std::vector<std::string> futureDates;
// 获取当前时间点
auto now = std::chrono::system_clock::now();
for (int i = 0; i < days; i++) {
// 将当前时间点加上指定天数
auto future = now + std::chrono::hours(24 * i);
// 将时间转换为时间结构体
std::time_t futureTime = std::chrono::system_clock::to_time_t(future);
// 将时间结构体格式化为 "年月日" 的字符串
std::tm* futureTm = std::localtime(&futureTime);
std::ostringstream oss;
oss << futureTm->tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << futureTm->tm_mon + 1
<< '-' << std::setw(2) << std::setfill('0') << futureTm->tm_mday;
futureDates.push_back(oss.str());
}
return futureDates;
}
std::string getCurrentDate()
{
std::time_t now = std::time(nullptr);
std::tm* localTime = std::localtime(&now);
std::ostringstream oss;
oss << localTime->tm_year + 1900 << "-"
<< std::setw(2) << std::setfill('0') << localTime->tm_mon + 1 << "-"
<< std::setw(2) << std::setfill('0') << localTime->tm_mday;
return oss.str();
}
std::string getPreviousDate()
{
std::time_t now = std::time(nullptr);
const std::time_t oneDay = 24 * 60 * 60;
std::time_t previousTime = now - oneDay;
std::tm* previousLocalTime = std::localtime(&previousTime);
std::ostringstream oss;
oss << previousLocalTime->tm_year + 1900 << "-"
<< std::setw(2) << std::setfill('0') << previousLocalTime->tm_mon + 1 << "-"
<< std::setw(2) << std::setfill('0') << previousLocalTime->tm_mday;
return oss.str();
}
void sqlManager::uninitlize()
{
2024-08-07 09:16:35 +00:00
mutex.lock();
2024-05-24 18:05:29 +08:00
sqlite3_close(db);
2024-08-07 09:16:35 +00:00
mutex.unlock();
}
std::vector<std::string> sqlManager::getTableList()
{
/*sqlTableDatas output;
dbPtr->Select("", std::vector<std::string>(),"","", output);*/
return dbPtr->getTableList();
2024-05-24 18:05:29 +08:00
}
sqlTableDatas sqlManager::selectSingleData(std::string selectStr, std::string SN)
{
2024-08-07 09:16:35 +00:00
mutex.lock();
2024-05-24 18:05:29 +08:00
sqlTableDatas selectResult;
2024-08-21 11:33:05 +08:00
dbPtr->Select(getCurrentDate(), { selectStr }, selectResult , "SN", SN);
2024-05-24 18:05:29 +08:00
if (selectResult.rowDatas.size() == 0)//无指定数据 查找前一天
2024-08-21 11:33:05 +08:00
dbPtr->Select(getPreviousDate(), { selectStr }, selectResult, "SN", SN);
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return selectResult;
}
2024-08-21 11:33:05 +08:00
sqlTableDatas sqlManager::selectRowData(std::string SN)
2024-05-24 18:05:29 +08:00
{
2024-08-07 09:16:35 +00:00
mutex.lock();
2024-05-24 18:05:29 +08:00
//dbPtr->Update(getCurrentTableName(), { {"send","1" } }, "sn", SN);
sqlTableDatas selectResult;
2024-08-21 11:33:05 +08:00
dbPtr->Select(getCurrentDate(), {}, selectResult, "SN", SN);
2024-05-24 18:05:29 +08:00
if(selectResult.rowDatas.size()==0)//无指定数据 查找前一天
2024-08-21 11:33:05 +08:00
dbPtr->Select(getPreviousDate(), {}, selectResult, "SN", SN);
mutex.unlock();
return selectResult;
}
sqlTableDatas sqlManager::selectAllData(std::string tableName)
{
mutex.lock();
//dbPtr->Update(getCurrentTableName(), { {"send","1" } }, "sn", SN);
sqlTableDatas selectResult;
dbPtr->Select(tableName, {}, selectResult);
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return selectResult;
}
2024-08-07 09:16:35 +00:00
void sqlManager::initialization(appConfig* _appConfig,std::string dataBasePath, bool backup)
2024-05-24 18:05:29 +08:00
{
2024-08-07 09:16:35 +00:00
mutex.lock();
2024-05-24 18:05:29 +08:00
std::cout << "SQL runing Init..." << std::endl;
dbPtr = new sqliteModule(dataBasePath);//初始化
dbPtr->backup(dataBasePath + getCurrentDate() + ".db");//备份
2024-08-14 15:56:29 +08:00
std::vector<std::string>tables = getFutureDates(30);
2024-05-24 18:05:29 +08:00
//整合所需要的数据名
std::vector<std::string>dataStr;
2024-08-07 09:16:35 +00:00
for (auto data : _appConfig->_sqlConfigStruct.datas)
dataStr.push_back(data.first);
2024-05-24 18:05:29 +08:00
2024-08-21 11:33:05 +08:00
//创建未来的表格
2024-05-24 18:05:29 +08:00
for (auto table : tables)
dbPtr->createTable(table, dataStr);
2024-08-21 11:33:05 +08:00
//查询是否有后期添加未创建的列
2024-05-24 18:05:29 +08:00
for (auto table : tables)
{
std::vector<std::string> res = dbPtr->getColumn(table);
std::unordered_map<std::string, int> map;
for (auto str : res)
map.insert({ str,0 });
for (auto str : dataStr)//判断此值是否存在
{
auto iter = map.find(str);//寻找
if (iter == map.end())//找不到
{
2024-08-21 11:33:05 +08:00
dbPtr->addColumn(table, str, "TEXT");//插入
2024-05-24 18:05:29 +08:00
}
}
}
std::cout << "SQL init end" << std::endl;
2024-08-07 09:16:35 +00:00
std::vector<std::string> input=getTableList();
mutex.unlock();
2024-05-24 18:05:29 +08:00
}
2024-08-26 15:45:15 +08:00
bool sqlManager::updateData(std::string SN,sqlRowDatas data)
{
mutex.lock();
sqlTableDatas selectResult;
dbPtr->Select(getCurrentDate(), {}, selectResult, "SN", SN);
if (selectResult.rowDatas.size() == 0)//无指定数据 查找前一天
{
dbPtr->Update(getPreviousDate(), data, "SN", SN);
}
else
{
dbPtr->Update(getCurrentDate(), data, "SN", SN);
}
mutex.unlock();
}
2024-08-07 09:16:35 +00:00
//将数据存入数据库
2024-05-24 18:05:29 +08:00
bool sqlManager::addData(std::string SN,std::vector<sqlData> data)
{
2024-08-07 09:16:35 +00:00
mutex.lock();
//SN码为空,停止处理
if (SN.empty())
2024-05-24 18:05:29 +08:00
{
qDebug() << "[INFO] " << " SN empty!";
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return false;
}
sqlTableDatas selectResult;
std::string currentTableName = getCurrentDate();
2024-08-21 11:33:05 +08:00
sqlErr res=dbPtr->Select(currentTableName, {}, selectResult, "SN", SN);
2024-08-07 09:16:35 +00:00
//查询出错
if (res.customType != sqlOK)
2024-05-24 18:05:29 +08:00
{
qDebug() << "[INFO] " << "select False SN:"<<QString::fromStdString(SN);
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return false;
}
2024-08-07 09:16:35 +00:00
//找到SN,进行更新操作
if (selectResult.rowDatas.size() != 0)
2024-05-24 18:05:29 +08:00
{
2024-08-21 11:33:05 +08:00
dbPtr->Update(currentTableName, data, "SN", SN);
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return true;
}
2024-08-07 09:16:35 +00:00
//未找到SN插入数据
2024-05-24 18:05:29 +08:00
res= dbPtr->Insert(currentTableName, SN, data);
2024-08-07 09:16:35 +00:00
//插入失败,新增列后再次尝试
if (res.customType == sqlFalse)
2024-05-24 18:05:29 +08:00
{
for (auto keyName : data)
2024-08-14 15:56:29 +08:00
{
2024-05-24 18:05:29 +08:00
dbPtr->addColumn(currentTableName, keyName.key, "text");
2024-08-14 15:56:29 +08:00
std::cout << "new col insert" << keyName.key << std::endl;
}
2024-08-07 09:16:35 +00:00
dbPtr->Insert(currentTableName, SN, data);
2024-05-24 18:05:29 +08:00
}
2024-08-07 09:16:35 +00:00
mutex.unlock();
2024-05-24 18:05:29 +08:00
return true;
}