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;
|
|
|
|
|
dbPtr->Select(getCurrentDate(), { selectStr }, "SN", SN, selectResult);
|
|
|
|
|
if (selectResult.rowDatas.size() == 0)//无指定数据 查找前一天
|
|
|
|
|
dbPtr->Select(getPreviousDate(), { selectStr }, "SN", SN, selectResult);
|
2024-08-07 09:16:35 +00:00
|
|
|
mutex.unlock();
|
2024-05-24 18:05:29 +08:00
|
|
|
return selectResult;
|
|
|
|
|
}
|
|
|
|
|
sqlTableDatas sqlManager::selectAllData(std::string SN)
|
|
|
|
|
{
|
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;
|
|
|
|
|
dbPtr->Select(getCurrentDate(), {}, "SN", SN, selectResult);
|
|
|
|
|
if(selectResult.rowDatas.size()==0)//无指定数据 查找前一天
|
|
|
|
|
dbPtr->Select(getPreviousDate(), {}, "SN", SN, 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
|
|
|
|
|
|
|
|
//创建未来180天的表格
|
|
|
|
|
for (auto table : tables)
|
|
|
|
|
dbPtr->createTable(table, dataStr);
|
|
|
|
|
|
|
|
|
|
//查询是否有后期添加未创建的表格
|
|
|
|
|
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())//找不到
|
|
|
|
|
{
|
|
|
|
|
dbPtr->addColumn(table, str, "REAL");//插入
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-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();
|
|
|
|
|
sqlErr res=dbPtr->Select(currentTableName, {},"sn", SN, selectResult);
|
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
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|