修改:f_insert
This commit is contained in:
@@ -4,9 +4,13 @@
|
||||
* @version:
|
||||
* @Date: 2025-02-21 14:03:54
|
||||
* @LastEditors: why
|
||||
* @LastEditTime: 2025-02-21 17:48:44
|
||||
* @LastEditTime: 2025-02-24 15:33:30
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "F_InsertRecord.h"
|
||||
using namespace std;
|
||||
@@ -30,7 +34,31 @@ InsertRecord::~InsertRecord(void){
|
||||
void InsertRecord::Init(initparam_t *param){
|
||||
initpara = param;
|
||||
}
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list){
|
||||
|
||||
std::string InsertRecord::now_to_string() {
|
||||
// 获取系统时钟时间点
|
||||
auto time_now = std::chrono::system_clock::now();
|
||||
|
||||
// 将时间点转成time_t类型的值 (秒)
|
||||
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
||||
|
||||
// 转换成tm结构体形式
|
||||
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
||||
|
||||
// 计算出毫秒部分
|
||||
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
time_now.time_since_epoch()
|
||||
).count() % 1000;
|
||||
|
||||
char buffer[40];
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
||||
|
||||
// 添加上毫秒和时区信息
|
||||
sprintf(buffer + strlen(buffer), ".%03ld+0000", milliseconds);
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result){
|
||||
sqlite3 *db = handpara;
|
||||
char *errMsg = nullptr;
|
||||
const char *sql;
|
||||
@@ -40,13 +68,13 @@ int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list){
|
||||
if (sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
||||
std::cerr << "事务开启失败: " << errMsg << std::endl;
|
||||
sqlite3_free(errMsg);
|
||||
|
||||
return 1;
|
||||
return -1;
|
||||
}
|
||||
// 执行批量插入操作
|
||||
sqlite3_stmt *stmt;
|
||||
int idx = 0;
|
||||
for(const auto &k:sql_list){
|
||||
std::cout << "cmd:"<<k.c_str()<<std::endl;
|
||||
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;
|
||||
@@ -54,7 +82,9 @@ int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list){
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "插入失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
}
|
||||
result[idx] = "0";
|
||||
}else
|
||||
result[idx] = "1";
|
||||
sqlite3_reset(stmt); // 重置语句
|
||||
}
|
||||
// 提交事务
|
||||
@@ -73,7 +103,9 @@ int InsertRecord::HandleMsg(const msg_t msg, handparam_t *param){
|
||||
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;
|
||||
try{
|
||||
value = json::prase(msg.payload)
|
||||
@@ -92,23 +124,38 @@ int InsertRecord::HandleMsg(const msg_t msg, handparam_t *param){
|
||||
if(value.contains("data") && value["data"].contains("dataarray")){
|
||||
json subvalue = value["data"]["dataarray"];
|
||||
if(subvalue.is_array()){
|
||||
int idx = 0;
|
||||
for(const auto &k:subvalue){
|
||||
std::stringstream ss;
|
||||
k["metid"]
|
||||
k["afn"]
|
||||
k["dataid"]
|
||||
k["frozentime"]
|
||||
k["crcaux"]
|
||||
k["databuff"]
|
||||
ss << "insert into " <<table_name.c_str() <<" ";
|
||||
sql_list.pushback(ss.str());
|
||||
|
||||
ss << "insert into " <<table_name.c_str() <<" (idx,metid,dataid,frozentime,savetime,crcaux,databuff) values ("\
|
||||
<<k["metid"].c_str()<<","\
|
||||
<<k["dataid"].c_str()<<","\
|
||||
<<k["frozentime"].c_str()<<","\
|
||||
<<k["crcaux"].c_str()<<","\
|
||||
<<k["databuff"]<<");"
|
||||
sql_list.pushback(ss.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
SQLBatch_exc(sql_list);
|
||||
resp["time"]=0;
|
||||
resp["ack"]=true;
|
||||
int ret = SQLBatch_exc(sql_list,result);
|
||||
resp["time"]=now_to_string();
|
||||
if(ret == -1)
|
||||
resp["ack"]="false";
|
||||
else if(ret == 0)
|
||||
resp["ack"]="true";
|
||||
|
||||
resp["data"]["dataidex"]=result;
|
||||
msg_t resp_msg;
|
||||
//std::string topic; // 消息主题
|
||||
//std::string payload; // 消息内容
|
||||
//long timestamp; // 消息时间戳
|
||||
std::stringstream ss;
|
||||
ss << resp_msg.topic[1].c_str()<<"/"<<resp_msg.topic[0].c_str()<<"/"\
|
||||
<<resp_msg.topic[2].c_str()<<"/"<<resp_msg.topic[3].c_str();
|
||||
resp_msg.topic = ss.str();
|
||||
resp_msg.timestamp = now_to_string();
|
||||
resp_msg.payload = resp.dump();
|
||||
return WriteResponseMsg(resp_msg);
|
||||
|
||||
}
|
||||
const std::string GetFID(){
|
||||
|
||||
@@ -4,12 +4,11 @@
|
||||
* @version:
|
||||
* @Date: 2025-02-21 14:04:30
|
||||
* @LastEditors: why
|
||||
* @LastEditTime: 2025-02-21 17:30:21
|
||||
* @LastEditTime: 2025-02-24 15:24:19
|
||||
*/
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
|
||||
class InsertRecord :public IBizService
|
||||
{
|
||||
public:
|
||||
@@ -23,5 +22,22 @@ class InsertRecord :public IBizService
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
void topic_split(const std::string& str, char delimiter);
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list);
|
||||
};
|
||||
InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result);
|
||||
std::string InsertRecord::now_to_string();
|
||||
};
|
||||
/*
|
||||
class UpdateRecord: public IBizService {
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, handparam_t *param = NULL);
|
||||
const std::string GetFID();
|
||||
UpdateRecord();
|
||||
~UpdateRecord();
|
||||
private:
|
||||
handparam_t * handpara;
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
void topic_split(const std::string& str, char delimiter);
|
||||
InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result);
|
||||
std::string InsertRecord::now_to_string();
|
||||
}*/
|
||||
Reference in New Issue
Block a user