实现策略配置功能

This commit is contained in:
lixiaoyuan
2025-08-28 18:42:37 +08:00
parent 8f6c83147b
commit dda905cda0
47 changed files with 1311 additions and 863 deletions

View File

@@ -84,6 +84,9 @@ Errcode DAO::insertUser(Fields& params)
auto dao = DaoEntity::create(DMUser::TABLENAME);
std::string account = params.value(DMUser::ACCOUNT);
std::string userRoleId = params.remove(DMRole::ROLE_ID);
// step1: 查询
std::vector<Fields> result;
bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result);
@@ -101,11 +104,14 @@ Errcode DAO::insertUser(Fields& params)
{
return Errcode::ERR_DB_SQL;
}
Fields paramsUserRole;
paramsUserRole.set(DMUser::USER_ID, params.value(DMUser::USER_ID));
paramsUserRole.set(DMRole::ROLE_ID, params.value(DMRole::ROLE_ID));
paramsUserRole.set(DMUser::UPDATETIME, createTime);
ret = dao->duplicateUpdate(paramsUserRole, {DMUser::USER_ID});
if (!userRoleId.empty())
{
Fields paramsUserRole;
paramsUserRole.set(DMUser::USER_ID, params.value(DMUser::USER_ID));
paramsUserRole.set(DMRole::ROLE_ID, userRoleId);
paramsUserRole.set(DMUser::UPDATETIME, createTime);
ret = dao->duplicateUpdate(paramsUserRole, {DMUser::USER_ID});
}
return Errcode::OK;
}
@@ -127,30 +133,28 @@ bool DAO::queryUserList(PageInfo& pageInfo, vector<Fields>& result)
Errcode DAO::updateUserById(Fields& params)
{
std::string createTime = Utils::timeStr();
std::string userId = params.value(DMUser::USER_ID);
std::string roleId = "";
if (params.contains(DMRole::ROLE_ID))
{
roleId = params.value(DMRole::ROLE_ID);
params.remove(DMUser::USER_ID);
}
auto dao = DaoEntity::create(DMUser::TABLENAME);
bool ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'");
if (!ret)
{
return Errcode::ERR_DB_SQL;
}
std::string createTime = Utils::timeStr();
std::string userId = params.remove(DMUser::USER_ID);
std::string roleId = params.remove(DMRole::ROLE_ID);
if (params.size() > 0)
{
bool ret = dao->updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'");
if (!ret)
{
return Errcode::ERR_DB_SQL;
}
}
if (!roleId.empty())
{
dao->setTableName(DMUserRole::TABLENAME);
Fields paramsUserRole;
paramsUserRole.set(DMUserRole::USER_ID, params.value(DMUserRole::USER_ID));
paramsUserRole.set(DMUserRole::ROLE_ID, params.value(DMUserRole::ROLE_ID));
paramsUserRole.set(DMUserRole::USER_ID, userId);
paramsUserRole.set(DMUserRole::ROLE_ID, roleId);
paramsUserRole.set(DMUserRole::UPDATETIME, createTime);
ret = dao->duplicateUpdate(paramsUserRole, {DMUser::USER_ID});
bool ret = dao->duplicateUpdate(paramsUserRole, {DMUserRole::ROLE_ID});
if (!ret)
{
return Errcode::ERR_DB_SQL;
@@ -298,6 +302,20 @@ Errcode DAO::queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& res
std::string sql = "SELECT * FROM " + DMPolicy::TABLENAME;
return DAO::exec(dao, sql, result);
}
Errcode DAO::insertPolicy(Fields& params)
{
return DAO::exec(NULL, params.toSqlInsert(DMPolicy::TABLENAME));
}
Errcode DAO::updatePolicyById(Fields& params)
{
std::string policyId = params.value(DMPolicy::POLICY_ID);
if (policyId.empty())
{
return Errcode::ERR_DB_SQL;
}
std::string sql = params.toSqlUpdate(DMPolicy::TABLENAME, "WHERE " + DMPolicy::POLICY_ID + "='" + policyId + "'");
return DAO::exec(NULL, sql);
}
// 系统日志管理
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)

View File

@@ -68,6 +68,9 @@ public:
static bool queryPolicyList(PageInfo& pageInfo, vector<Fields>& result);
static Errcode queryPolicyList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
static Errcode insertPolicy(Fields& params);
static Errcode updatePolicyById(Fields& params);
///////////////////////////////////////////////////////////////////////////////////////////////
// === 系统日志管理 ===

View File

@@ -2,7 +2,7 @@
//#include "PvInstance.h"
//#include "spdlogger.h"
MysqlOption DaoEntity::option_;
MysqlOption DaoEntity::option;
DaoEntity::DaoEntity(string tb_name)
{
@@ -12,7 +12,7 @@ DaoEntity::DaoEntity(string tb_name)
//opts.password = "123456";
//opts.port = 3306;
//opts.dbname = "pvb";
db_ = make_shared<MysqlClient>(DaoEntity::option_);
db_ = make_shared<MysqlClient>(DaoEntity::option);
if (!db_->isConnected())
{
//Global::data().status_msg = "数据库连接异常!";
@@ -28,15 +28,15 @@ DaoEntity::~DaoEntity()
MysqlOption& DaoEntity::mysqlOption()
{
return DaoEntity::option_;
return DaoEntity::option;
}
void DaoEntity::setOption(std::string host, int port, std::string user, std::string pwd, std::string dbname)
{
option_.host = host;
option_.port = port;
option_.user = user;
option_.password = pwd;
option_.dbname = dbname;
option.host = host;
option.port = port;
option.user = user;
option.password = pwd;
option.dbname = dbname;
}
std::shared_ptr<DaoEntity> DaoEntity::create(string tb_name)
@@ -46,13 +46,13 @@ std::shared_ptr<DaoEntity> DaoEntity::create(string tb_name)
bool DaoEntity::execOnce(string sql)
{
auto db = make_shared<MysqlClient>(DaoEntity::option_);
auto db = make_shared<MysqlClient>(DaoEntity::option);
return db->exec(sql);
}
bool DaoEntity::execOnce(string sql, vector<Fields>& result)
{
auto db = make_shared<MysqlClient>(DaoEntity::option_);
auto db = make_shared<MysqlClient>(DaoEntity::option);
return db->exec(sql, result);
}
@@ -130,27 +130,27 @@ bool DaoEntity::insertFields(vector<Fields>& vec_fields)
bool DaoEntity::duplicateUpdate(Fields& fields, const vector<string>& keys)
{
//insert into device_attr(device_id, attr_id, attr_val) values('26', 'model', '型号1') on duplicate key update attr_val='型号1';
string s_key;
string s_val;
string key;
string val;
for (auto& item : fields.map())
{
if (!s_key.empty())
if (!key.empty())
{
s_key += ","; s_val += ",";
key += ","; val += ",";
}
s_key += (item.first);
s_val += ("'" + item.second + "'");
key += (item.first);
val += ("'" + item.second + "'");
}
string s_data;
string str;
for (auto& k : keys)
{
if (!s_data.empty())
if (!str.empty())
{
s_data += ",";
str += ",";
}
s_data += (k + "='" + fields.value(k) + "'");
str += (k + "='" + fields.value(k) + "'");
}
string sql = "INSERT INTO " + tableName_ + "(" + s_key + ") VALUES (" + s_val + ") ON duplicate KEY UPDATE " + s_data;
string sql = "INSERT INTO " + tableName_ + "(" + key + ") VALUES (" + val + ") ON duplicate KEY UPDATE " + str;
return this->db_->exec(sql);
}

View File

@@ -100,7 +100,7 @@ public:
bool updateFields(Fields& fields, vector<string> vecKeys, const string& cond);
protected:
static MysqlOption option_;
static MysqlOption option;
// mysql 数据库操作对象
std::shared_ptr<MysqlClient> db_ = nullptr;

View File

@@ -98,6 +98,7 @@ namespace DMDefDeviceType
const string TABLENAME = "def_device_type";
const string DEVICE_TYPE_ID = "device_type_id";
const string NAME = "name";
const string GROUP = "group";
const string ATTRS = "attrs";
}

View File

@@ -3,7 +3,7 @@
//#include "Spdlogger.h"
#include "Logger.h"
MysqlClient::MysqlClient(MysqlOption option) : option_(option)
MysqlClient::MysqlClient(MysqlOption option) : option(option)
{
conn();
}
@@ -20,7 +20,7 @@ int MysqlClient::conn()
return 0;
}
mysql_ = mysql_init(nullptr);
MYSQL* ret = mysql_real_connect(mysql_, option_.host.c_str(), option_.user.c_str(), option_.password.c_str(), option_.dbname.c_str(), option_.port, NULL, 0);
MYSQL* ret = mysql_real_connect(mysql_, option.host.c_str(), option.user.c_str(), option.password.c_str(), option.dbname.c_str(), option.port, NULL, 0);
if (ret == NULL)
{
std::string err = mysql_error(mysql_);

View File

@@ -62,7 +62,7 @@ private:
MYSQL* mysql_ = nullptr;
// 数据库连接信息
MysqlOption option_;
MysqlOption option;
};
#endif // !!! _DbMysql_H_