Files
zgj 717aaa0274 1、调整目录结构
2、调整公共接口
2025-03-12 17:40:43 +08:00

166 lines
5.0 KiB
C++

/*
* @Description: file content
* @Author: why
* @version:
* @Date: 2025-02-25 11:02:37
* @LastEditors: why
* @LastEditTime: 2025-02-28 17:56:00
*/
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <string>
#include <ctime>
#include "json.hpp"
#include "F_UpdateRecord.h"
#include "sqlite3.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-UpdateRecord",UpdateRecord);
UpdateRecord::UpdateRecord(void){
handpara = nullptr;
initpara = nullptr;
}
UpdateRecord::~UpdateRecord(void){
}
std::string search_table_db(int afn,unsigned int dataid){
std::string table;
return table;
}
void UpdateRecord::Init(initparam_t *param){
initpara = param;
}
int UpdateRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result){
sqlite3 *db = (sqlite3 *)handpara;
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;
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);
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;
}
int UpdateRecord::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<std::string> 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";
std::cout << "afn:"<<afn<<std::endl;
if(!(afn==0x0d | afn==0x0e |afn == 0x12 |afn == 0x13))
{
afn_flag = -1;
goto resp_process;
}
if(std::stoi(subvalue["updatetype"].get<std::string>()) !=1){
update_flag = -1;
goto resp_process;
}
std::cout << __func__<<","<<__LINE__<<std::endl;
ss << "replace into " <<table_name.c_str() <<" (idx,metid,dataid,frozentime,createdtime,source,crcaux,databuff) values ("\
<< 0 << ","\
<<std::stoi(subvalue["metid"].get<std::string>())<<","\
<<subvalue["dataid"].get<std::string>().c_str()<<","\
<<ToolUtils::string_to_now(subvalue["updatefilter"].get<std::string>())<<","\
<<time(NULL)<<","\
<<std::stoi(subvalue["crcaux"].get<std::string>())<<","\
<<subvalue["databuff"].get<std::string>().c_str()<<");";
printf("update sql:%s \n",ss.str().c_str());
sql_list.push_back(ss.str());
ss.clear();
}
ret = SQLBatch_exc(sql_list,result);
resp_process:
resp["time"]=ToolUtils::now_to_string();
if(ret == -1 || afn_flag == -1 || update_flag == -1)
resp["ack"]="false";
else if(ret == 0)
resp["ack"]="true";
resp["data"]["dataidex"]=result;
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;
}
std::string UpdateRecord::GetFID(){
std::string tmp("F-UpdateRecord");
return tmp;
}