cf087cf89a
# Conflicts: # CMakeLists.txt # common/inc/ToolUtils.h resolved by origin/develop(远端) version # common/src/ToolUtils.cpp resolved by origin/develop(远端) version # datacenter/bizService/F_DeleteData.h resolved by develop version # datacenter/bizService/F_FormatData.h resolved by develop version # datacenter/bizService/F_GetSaveCount.h resolved by develop version # datacenter/bizService/F_GetSupportNum.h resolved by develop version # datacenter/bizService/F_GetVersion.cpp resolved by develop version # datacenter/bizService/F_InsertRecord.cpp resolved by develop version # datacenter/bizService/F_Online.cpp resolved by develop version # datacenter/bizService/F_SetSupportNum.h resolved by develop version # datacenter/bizService/F_Tool.cpp resolved by develop version # datacenter/bizService/F_UpdateRecord.cpp resolved by develop version
170 lines
5.1 KiB
C++
170 lines
5.1 KiB
C++
/*
|
|
* @Description: file content
|
|
* @Author: why
|
|
* @version:
|
|
* @Date: 2025-02-28 08:22:32
|
|
* @LastEditors: why
|
|
* @LastEditTime: 2025-02-28 17:56:31
|
|
*/
|
|
#include <iostream>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <ctime>
|
|
#include "common/inc/json.hpp"
|
|
#include "F_GetSaveCount.h"
|
|
#include "bizservice.h"
|
|
#include "ToolUtils.h"
|
|
|
|
using namespace std;
|
|
using json = nlohmann::json ;// for convenience
|
|
typedef unsigned char BYTE;
|
|
|
|
typedef struct topic{
|
|
|
|
std::string src;
|
|
std::string des;
|
|
std::string sid;
|
|
std::string fid;
|
|
} topic_type;
|
|
|
|
REGISTER_BIZSERVICE("F-GetSaveCount",GetSaveCount);
|
|
|
|
int GetSaveCount::SQLBatch_exc(sqlite3 *db,std::vector<std::string> &sql_list,std::vector<int> &result){
|
|
char *errMsg = nullptr;
|
|
const char *sql;
|
|
int rc;
|
|
int resp = 0;
|
|
// 开启事务
|
|
if (sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
|
std::cerr << "事务开启失败: " << errMsg << std::endl;
|
|
sqlite3_free(errMsg);
|
|
return -1;
|
|
}
|
|
// 执行批量插入操作
|
|
sqlite3_stmt *stmt;
|
|
int idx = 0;
|
|
int count = 0;
|
|
for(const auto &k:sql_list){
|
|
std::cout << "cmd:"<<k.c_str()<<std::endl;
|
|
if (sqlite3_prepare_v2(db, k.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
|
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
|
continue;
|
|
}
|
|
rc = sqlite3_step(stmt);
|
|
while(rc == SQLITE_ROW){
|
|
result[idx] = sqlite3_column_int(stmt, 0); // For integer columns use sqlite3_column_int()
|
|
|
|
}
|
|
/*
|
|
if (rc != SQLITE_DONE) {
|
|
std::cerr << "插入失败: " << sqlite3_errmsg(db) << std::endl;
|
|
result[idx] = "-1";
|
|
}else
|
|
result[idx] = "0"; //SUCCESS
|
|
*/
|
|
sqlite3_reset(stmt); // 重置语句
|
|
}
|
|
// 提交事务
|
|
if (sqlite3_exec(db, "COMMIT;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
|
std::cerr << "事务提交失败: " << errMsg << std::endl;
|
|
sqlite3_free(errMsg);
|
|
resp = -1;
|
|
}
|
|
// 清理
|
|
sqlite3_finalize(stmt);
|
|
return resp;
|
|
}
|
|
|
|
GetSaveCount::GetSaveCount(void){
|
|
handpara = nullptr;
|
|
initpara = nullptr;
|
|
}
|
|
|
|
GetSaveCount::~GetSaveCount(void){
|
|
}
|
|
|
|
void GetSaveCount::Init(initparam_t *param){
|
|
initpara = param;
|
|
}
|
|
std::string GetSaveCount::GetFID(){
|
|
std::string value("F-GetSaveCount");
|
|
return value;
|
|
|
|
}
|
|
int GetSaveCount::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
|
|
|
topic.clear();
|
|
topic = msg.subTopicV;
|
|
json value,resp;
|
|
int ret = 0;
|
|
int afn_flag = 0, update_flag = 0;;
|
|
std::vector<std::string> sql_list;
|
|
std::vector<int> result;
|
|
sql_list.clear();
|
|
result.clear();
|
|
std::string table_name ;
|
|
try{
|
|
value = json::parse(msg.payload);
|
|
}
|
|
catch(const std::exception &e){
|
|
std::cout << "topic "<<msg.topic.c_str()<<" prase error:"<<e.what();
|
|
return -1;//format fail
|
|
}
|
|
if(!value.is_object())return -1;
|
|
if(value.contains("token")){
|
|
resp["token"] = value["token"];
|
|
}
|
|
if(value.contains("time")){
|
|
;// value["time"]
|
|
}
|
|
if(value.contains("data")){
|
|
json subvalue = value["data"];
|
|
int afn = 0;
|
|
int dataid = 0;
|
|
ToolUtils::decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
|
ToolUtils::decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
|
std::stringstream ss;
|
|
//table_name =search_table_db(afn,dataid);
|
|
table_name = "frozen";
|
|
int choice = std::stoi(subvalue["choice"].get<std::string>());
|
|
time_t from = 0,to = time(NULL);
|
|
std::string colum("");
|
|
switch(choice){
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
colum = "createdtime";
|
|
from = std::stoi(subvalue["from"].get<std::string>());
|
|
to = std::stoi(subvalue["to"].get<std::string>());
|
|
break;
|
|
case 2:
|
|
colum = "frozentime";
|
|
from = std::stoi(subvalue["from"].get<std::string>());
|
|
to = std::stoi(subvalue["to"].get<std::string>());
|
|
break;
|
|
default:
|
|
return -1;//illegal
|
|
|
|
}
|
|
ss << "seletc count (*) from " <<table_name.c_str()\
|
|
<<"where dataid = '"<<subvalue["dataid"].get<std::string>().c_str()<<"' and"\
|
|
<<"metid = "<< std::stoi(subvalue["metid"].get<std::string>())<<" and"\
|
|
<<colum.c_str() <<" >= "<< from<<" and " <<colum.c_str() <<" <= "<< to<<";";
|
|
printf("getcount sql:%s \n",ss.str().c_str());
|
|
sql_list.push_back(ss.str());
|
|
ss.clear();
|
|
}
|
|
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
|
resp_process:
|
|
resp["time"]=ToolUtils::now_to_string();
|
|
resp["data"]["savecount"]=result[0];
|
|
|
|
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
|
outMsg.timestamp = time(NULL);;
|
|
outMsg.payload = resp.dump();
|
|
std::cout << "resp UpdateRecord payload:"<<resp.dump().c_str()<<std::endl;
|
|
return 1;
|
|
}
|