Files
dcapp/datacenter/bizService/F_SetSubscribe.cpp
T
zgj 717aaa0274 1、调整目录结构
2、调整公共接口
2025-03-12 17:40:43 +08:00

137 lines
3.7 KiB
C++

//
// Created by Thinkpad on 2025/3/12.
//
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
#include <string>
#include <ctime>
#include "json.hpp"
#include "bizservice.h"
#include "sqlite3.h"
#include "ToolUtils.h"
#include "F_SetSubscribe.h"
using namespace std;
using json = nlohmann::json ;// for convenience
REGISTER_BIZSERVICE("F-SetSubscribe",SetSubscribe);
SetSubscribe::SetSubscribe() {
handpara = nullptr;
initpara = nullptr;
}
SetSubscribe::~SetSubscribe() {
}
void SetSubscribe::Init(initparam_t *param) {
initpara = param;
}
int SetSubscribe::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 SetSubscribe::HandleMsg(const msg_t msg, msg_t &outMsg, handparam_t *param) {
topic.clear();
topic = msg.subTopicV;
json value,resp;
int ret = 0;
std::vector<std::string> sql_list;
std::vector<std::string> result;
sql_list.clear();
result.clear();
std::string table_name("xxxxxx");
try{
value = json::parse(msg.payload);
}
catch(const std::exception &e){
std::cout << "topic "<<msg.topic.c_str()<<" parse error:"<<e.what();
return -1;//format fail
}
printf("insert request:%s \n",msg.payload.c_str());
if(!value.is_object())return -1;
if(value.contains("token")){
resp["token"] = value["token"];
}
if(value.contains("time")){
;// value["time"]
}
if(value.contains("data") && value["data"].contains("subparam")){
json subvalue = value["data"]["subparam"];
if(subvalue.is_array()){
for(const auto &k:subvalue){
std::string metidStr = k["metid"].get<std::string>();
std::cout << "metid:"<<metidStr.c_str()<<std::endl;
std::vector<std::string>idclassV = k["idclass"];
for(const auto &k1:idclassV){
std::cout << "idclass:"<<k1.c_str()<<std::endl;
}
//todo 保存到数据库
}
}
}
// ret = SQLBatch_exc(sql_list,result);
resp["time"]=ToolUtils::now_to_string();
if(ret == -1)
resp["ack"]="false";
else if(ret == 0)
resp["ack"]="true";
outMsg.topic = ToolUtils::makeTpopicStr(topic);
outMsg.timestamp = time(NULL);
outMsg.payload = resp.dump();
std::cout << "resp SetSubscribe payload:"<<resp.dump().c_str()<<std::endl;
return 1;
}
std::string SetSubscribe::GetFID(){
std::string value("F-SetSubscribe");
return value;
}