mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现系统管理表格操作接口、分页操作
This commit is contained in:
@@ -14,7 +14,7 @@ bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, in
|
||||
if (!condition.empty()) { sql += " WHERE " + condition; };
|
||||
sql += ";";
|
||||
|
||||
std::vector<DataFields> result;
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (ret)
|
||||
{
|
||||
@@ -25,7 +25,7 @@ bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, in
|
||||
|
||||
static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
{
|
||||
std::vector<DataFields> result;
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result);
|
||||
if (ret)
|
||||
{
|
||||
@@ -35,18 +35,18 @@ static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
}
|
||||
|
||||
|
||||
static bool QueryPagination(std::string sqlFrom, PageInfo& pageInfo, vector<DataFields>& result)
|
||||
static bool QueryPagination(std::string sqlFields, std::string sqlCondition, PageInfo& page, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
|
||||
int count {0};
|
||||
if (!QueryCount(dao, sqlFrom, count))
|
||||
if (!QueryCount(dao, sqlCondition, count))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pageInfo.total = count;
|
||||
std::string sql = "SELECT * " + sqlFrom + DAO::sqlPageLimit(pageInfo.pageIndex, pageInfo.pageSize);
|
||||
page.total = count;
|
||||
std::string sql = "SELECT " + sqlFields + " " + sqlCondition + DAO::sqlPageLimit(page.index, page.size);
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (!ret)
|
||||
{
|
||||
@@ -55,11 +55,47 @@ static bool QueryPagination(std::string sqlFrom, PageInfo& pageInfo, vector<Data
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryUserList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
// 新增用户信息
|
||||
Errcode DAO::insertUser(Fields& params)
|
||||
{
|
||||
std::string createTime = Utils::timeStr();
|
||||
auto dao = DaoEntity::create(DMUser::TABLENAME);
|
||||
std::string account = params.value(DMUser::ACCOUNT);
|
||||
|
||||
// step1: 查询
|
||||
std::vector<Fields> result;
|
||||
bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", result);
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_CONN;
|
||||
}
|
||||
if (result.size() > 0)
|
||||
{
|
||||
return Errcode::ERR_DB_DUPLICATE;
|
||||
}
|
||||
params.set(DMUser::CREATETIME, createTime);
|
||||
ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
{
|
||||
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});
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
|
||||
// 分页查询用户信息列表
|
||||
bool DAO::queryUserList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
std::string sqlFields = "u.*, r.role_id , r.name role_name";
|
||||
std::string sqlCondition = "FROM USER u LEFT JOIN user_role ur ON ur.user_id = u.user_id LEFT JOIN ROLE r ON r.role_id =ur.role_id";
|
||||
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMUser::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination(sqlFields, sqlCondition, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryUserList failed.";
|
||||
@@ -67,19 +103,52 @@ bool DAO::queryUserList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::updateUserById(DataFields& params)
|
||||
Errcode DAO::updateUserById(Fields& params)
|
||||
{
|
||||
std::string userId = params.getStr(DMUser::USER_ID);
|
||||
params.remove(DMUser::USER_ID);
|
||||
DaoEntity dao(DMUser::TABLENAME);
|
||||
return dao.updateFields(params, "WHERE " + DMUser::USER_ID + "='" + userId + "'");
|
||||
std::string createTime = Utils::timeStr();
|
||||
std::string userId = params.value(DMUser::USER_ID);
|
||||
std::string roleId = "";
|
||||
if (params.hasKey(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;
|
||||
}
|
||||
|
||||
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::UPDATETIME, createTime);
|
||||
ret = dao->duplicateUpdate(paramsUserRole, {DMUser::USER_ID});
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
bool DAO::queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMRole::TABLENAME + ";";
|
||||
return dao->exec(sql, result);
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMRole::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryRoleList failed.";
|
||||
@@ -87,11 +156,11 @@ bool DAO::queryRoleList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryPermissionList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
bool DAO::queryPermissionList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMPermission::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPermissionList failed.";
|
||||
@@ -99,19 +168,34 @@ bool DAO::queryPermissionList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 查询场站信息列表
|
||||
bool DAO::queryStationList(vector<DataFields>& result)
|
||||
Errcode DAO::insertStation(Fields& params)
|
||||
{
|
||||
auto dao = DaoEntity::create(DMStation::TABLENAME);
|
||||
params.remove(DMStation::STATION_ID);
|
||||
params.check(DMStation::LATITUDE, "", "NULL");
|
||||
params.check(DMStation::LONGITUDE, "", "NULL");
|
||||
bool ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
// 查询场站信息列表
|
||||
bool DAO::queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMStation::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
|
||||
// 分页查询场站信息列表
|
||||
bool DAO::queryStationList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
bool DAO::queryStationList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMStation::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryStationList failed.";
|
||||
@@ -119,19 +203,37 @@ bool DAO::queryStationList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 查询设备信息列表
|
||||
bool DAO::queryDeviceList(vector<DataFields>& result)
|
||||
Errcode DAO::updateStationById(Fields& params)
|
||||
{
|
||||
std::string stationId = params.value(DMStation::STATION_ID);
|
||||
if (stationId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
params.remove(DMStation::STATION_ID);
|
||||
auto dao = DaoEntity::create(DMStation::TABLENAME);
|
||||
bool ret = dao->updateFields(params, "WHERE " + DMStation::STATION_ID + "='" + stationId + "'");
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
// 查询设备信息列表
|
||||
bool DAO::queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMDevice::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
|
||||
// 分页查询设备信息列表
|
||||
bool DAO::queryDeviceList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
bool DAO::queryDeviceList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMDevice::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryDeviceList failed.";
|
||||
@@ -139,12 +241,46 @@ bool DAO::queryDeviceList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 策略管理
|
||||
bool DAO::queryPolicyList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
// 查询设备类型定义
|
||||
bool DAO::queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
if (!dao) { dao = DaoEntity::create(""); }
|
||||
std::string sql = "SELECT * FROM " + DMDeviceTypeDef::TABLENAME + ";";
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
|
||||
Errcode DAO::insertDevice(Fields& params)
|
||||
{
|
||||
auto dao = DaoEntity::create(DMDevice::TABLENAME);
|
||||
bool ret = dao->insertFields(params);
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
Errcode DAO::updateDeviceById(Fields& params)
|
||||
{
|
||||
std::string deviceId = params.value(DMDevice::DEVICE_ID);
|
||||
if (deviceId.empty())
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
auto dao = DaoEntity::create(DMDevice::TABLENAME);
|
||||
bool ret = dao->updateFields(params, "WHERE " + DMDevice::DEVICE_ID + "='" + deviceId + "'");
|
||||
if (!ret)
|
||||
{
|
||||
return Errcode::ERR_DB_SQL;
|
||||
}
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
// 策略管理
|
||||
bool DAO::queryPolicyList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
auto dao = DaoEntity::create("");
|
||||
std::string sqlFrom = "FROM " + DMPolicy::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPolicyList failed.";
|
||||
@@ -153,11 +289,11 @@ bool DAO::queryPolicyList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
}
|
||||
|
||||
// 系统日志管理
|
||||
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<Fields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMSystemLog::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
bool ret = QueryPagination("*", sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: querySystemLogList failed.";
|
||||
@@ -165,7 +301,7 @@ bool DAO::querySystemLogList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryStatDataList(std::string startDate, std::string endDate, vector<DataFields>& result)
|
||||
bool DAO::queryStatDataList(std::string startDate, std::string endDate, vector<Fields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMStatStation::TABLENAME + " WHERE dt BETWEEN '" + startDate + "' AND '" + endDate + "';";
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "DaoEntity.h"
|
||||
#include "DataModelDef.h"
|
||||
#include "common/Logger.h"
|
||||
#include "errcode.h"
|
||||
|
||||
class DAO
|
||||
{
|
||||
@@ -12,43 +13,64 @@ public:
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 用户管理
|
||||
static bool queryUserList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
// 新增用户信息
|
||||
static Errcode insertUser(Fields& params);
|
||||
// 分页查询用户信息列表
|
||||
static bool queryUserList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
static bool updateUserById(DataFields& params);
|
||||
static Errcode updateUserById(Fields& params);
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 角色管理
|
||||
static bool queryRoleList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
// === 角色管理 ===
|
||||
// 查询角色信息列表
|
||||
static bool queryRoleList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询角色信息列表
|
||||
static bool queryRoleList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 权限管理
|
||||
static bool queryPermissionList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
// === 权限管理 ===
|
||||
static bool queryPermissionList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 场站管理
|
||||
// === 场站管理 ===
|
||||
// 新增场站信息
|
||||
static Errcode insertStation(Fields& params);
|
||||
// 查询场站信息列表
|
||||
static bool queryStationList(vector<DataFields>& result);
|
||||
static bool queryStationList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询场站信息列表
|
||||
static bool queryStationList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
static bool queryStationList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
static Errcode updateStationById(Fields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 设备管理
|
||||
// === 设备管理 ===
|
||||
// 查询设备信息列表
|
||||
static bool queryDeviceList(vector<DataFields>& result);
|
||||
static bool queryDeviceList(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
// 分页查询设备信息列表
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
// 查询设备类型定义
|
||||
static bool queryDeviceTypeDef(std::shared_ptr<DaoEntity> dao, vector<Fields>& result);
|
||||
|
||||
static Errcode insertDevice(Fields& params);
|
||||
static Errcode updateDeviceById(Fields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 策略管理
|
||||
// === 策略管理 ===
|
||||
// 分页查询策略信息列表
|
||||
static bool queryPolicyList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
static bool queryPolicyList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 系统日志管理
|
||||
// === 系统日志管理 ===
|
||||
// 分页查询系统日志列表
|
||||
static bool querySystemLogList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
static bool querySystemLogList(PageInfo& pageInfo, vector<Fields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 统计数据管理
|
||||
static bool queryStatDataList(std::string startDate, std::string endDate, vector<DataFields>& result);
|
||||
// === 统计数据管理 ===
|
||||
static bool queryStatDataList(std::string startDate, std::string endDate, vector<Fields>& result);
|
||||
|
||||
|
||||
};
|
||||
@@ -51,7 +51,7 @@ bool DaoEntity::execOnce(string sql)
|
||||
return db->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::execOnce(string sql, vector<DataFields>& result)
|
||||
bool DaoEntity::execOnce(string sql, vector<Fields>& result)
|
||||
{
|
||||
auto db = make_shared<MysqlClient>(DaoEntity::option_);
|
||||
return db->exec(sql, result);
|
||||
@@ -72,18 +72,18 @@ bool DaoEntity::exec(string sql)
|
||||
return db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::exec(string sql, vector<DataFields>& result)
|
||||
bool DaoEntity::exec(string sql, vector<Fields>& result)
|
||||
{
|
||||
return db_->exec(sql, result);
|
||||
}
|
||||
|
||||
bool DaoEntity::insertFields(DataFields& fields)
|
||||
bool DaoEntity::insertFields(Fields& fields)
|
||||
{
|
||||
string sql = fields.get_insert_sql(tableName_);
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::insertFields(vector<DataFields>& vec_fields)
|
||||
bool DaoEntity::insertFields(vector<Fields>& vec_fields)
|
||||
{
|
||||
//"insert into TABLE () values ()";
|
||||
string sql = "insert into " + tableName_;
|
||||
@@ -128,7 +128,7 @@ bool DaoEntity::insertFields(vector<DataFields>& vec_fields)
|
||||
return this->db_->exec(sql);
|
||||
}
|
||||
|
||||
bool DaoEntity::duplicateUpdate(DataFields& fields, vector<string>& keys)
|
||||
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;
|
||||
@@ -149,7 +149,7 @@ bool DaoEntity::duplicateUpdate(DataFields& fields, vector<string>& keys)
|
||||
{
|
||||
s_data += ",";
|
||||
}
|
||||
s_data += (k + "='" + fields.getStr(k) + "'");
|
||||
s_data += (k + "='" + fields.value(k) + "'");
|
||||
}
|
||||
string sql = "INSERT INTO " + tableName_ + "(" + s_key + ") VALUES (" + s_val + ") ON duplicate KEY UPDATE " + s_data;
|
||||
return this->db_->exec(sql);
|
||||
@@ -163,19 +163,19 @@ bool DaoEntity::duplicateUpdate(DataFields& fields, vector<string>& keys)
|
||||
//}
|
||||
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, vector<DataFields>& result)
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, vector<Fields>& result)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "SELECT " + keys + " FROM " << tableName_ << (" " + sql_c) << "; ";
|
||||
return this->db_->exec(oss.str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<DataFields>& result)
|
||||
bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& page, vector<Fields>& result)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "SELECT count(1) total FROM `" << tableName_ << "` " << sql_c << ";";
|
||||
|
||||
vector<DataFields> res_total;
|
||||
vector<Fields> res_total;
|
||||
if (!this->db_->exec(oss.str().c_str(), res_total))
|
||||
{
|
||||
return false;
|
||||
@@ -183,27 +183,26 @@ bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& pageinfo
|
||||
|
||||
if (res_total.size() <= 0)
|
||||
{
|
||||
pageinfo.total = 0;
|
||||
page.total = 0;
|
||||
return true;
|
||||
}
|
||||
pageinfo.total = res_total[0].getInt("total");
|
||||
if (pageinfo.total <= 0)
|
||||
page.total = res_total[0].getInt("total");
|
||||
if (page.total <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
pageinfo.pageCount = pageinfo.total / pageinfo.pageSize + (pageinfo.total % pageinfo.pageSize > 0 ? 1 : 0);
|
||||
oss.str("");
|
||||
if (pageinfo.pageIndex <= 0)
|
||||
if (page.index <= 0)
|
||||
{
|
||||
pageinfo.pageIndex = 1;
|
||||
page.index = 1;
|
||||
}
|
||||
int start = (pageinfo.pageIndex - 1) * pageinfo.pageSize;
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << pageinfo.pageSize << ";";
|
||||
int start = (page.index - 1) * page.size;
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << page.size << ";";
|
||||
return this->db_->exec(oss.str().c_str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(DataFields& fields, const string& sql_c)
|
||||
bool DaoEntity::updateFields(Fields& fields, const string& sql_c)
|
||||
{
|
||||
string sql = fields.get_update_sql(tableName_, sql_c);
|
||||
std::cout << sql;
|
||||
@@ -215,7 +214,7 @@ bool DaoEntity::updateFields(DataFields& fields, const string& sql_c)
|
||||
return this->db_->exec(sql.c_str());
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(DataFields& fields, vector<string> vec_keys, const string& sql_c)
|
||||
bool DaoEntity::updateFields(Fields& fields, vector<string> vec_keys, const string& sql_c)
|
||||
{
|
||||
string sql = fields.get_update_sql(tableName_, vec_keys, sql_c);
|
||||
if (sql_c.empty())
|
||||
|
||||
@@ -25,7 +25,7 @@ public:
|
||||
* @param: sql 要执行的完整 sql 语句
|
||||
* @param: result 返回的结果数据集
|
||||
*/
|
||||
static bool execOnce(string sql, vector<DataFields>& result);
|
||||
static bool execOnce(string sql, vector<Fields>& result);
|
||||
|
||||
/**
|
||||
* 设置数据库表名称
|
||||
@@ -47,34 +47,34 @@ public:
|
||||
/**
|
||||
* 执行sql语句并返回执行(查询)的结果集
|
||||
*/
|
||||
bool exec(string sql, vector<DataFields>& result);
|
||||
bool exec(string sql, vector<Fields>& result);
|
||||
|
||||
|
||||
/**
|
||||
* 数据库插入一条数据, 需要先指定数据表名称
|
||||
* @param: fields 写入的数据字段和值
|
||||
*/
|
||||
bool insertFields(DataFields& vecFields);
|
||||
bool insertFields(Fields& vecFields);
|
||||
|
||||
/**
|
||||
* 数据库插入多条数据, 需要先指定数据表名称
|
||||
* @param: vecFields 写入的数据字段和值的集合
|
||||
*/
|
||||
bool insertFields(vector<DataFields>& vecFields);
|
||||
bool insertFields(vector<Fields>& vecFields);
|
||||
|
||||
/**
|
||||
* 数据库插入多条数据,UNIQUE索引或PRIMARY KEY重复时执行更新数据, 需要先指定数据表名称
|
||||
* @param: vecFields 写入的数据字段和值的集合
|
||||
* @param: keys 数据重复时需要更新的字段
|
||||
*/
|
||||
bool duplicateUpdate(DataFields& vecFields, vector<string>& keys);
|
||||
bool duplicateUpdate(Fields& vecFields, const vector<string>& keys);
|
||||
|
||||
/**
|
||||
* 数据库查询,需要先指定数据表名称
|
||||
* @param: sql_c 查询条件,例:"where id='1'"
|
||||
* @param: result 查询的数据结果集
|
||||
*/
|
||||
bool queryFields(string keys, const string& sql_c, vector<DataFields>& result);
|
||||
bool queryFields(string keys, const string& sql_c, vector<Fields>& result);
|
||||
|
||||
/**
|
||||
* 数据库查询,需要先指定数据表名称
|
||||
@@ -82,14 +82,14 @@ public:
|
||||
* @param: pageinfo 分页信息
|
||||
* @param: result 查询的数据结果集
|
||||
*/
|
||||
bool queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<DataFields>& result);
|
||||
bool queryFields(string keys, const string& sql_c, PageInfo& pageinfo, vector<Fields>& result);
|
||||
|
||||
/**
|
||||
* 数据库更新,需要先指定数据表名称
|
||||
* @param: fields 要更新的数据字段和值
|
||||
* @param: sql_c 更新条件
|
||||
*/
|
||||
bool updateFields(DataFields& fields, const string& sql_c);
|
||||
bool updateFields(Fields& fields, const string& sql_c);
|
||||
|
||||
/**
|
||||
* 数据库更新,需要先指定数据表名称
|
||||
@@ -97,7 +97,7 @@ public:
|
||||
* @param: vecKeys 要更新的字段名称
|
||||
* @param: cond 更新条件
|
||||
*/
|
||||
bool updateFields(DataFields& fields, vector<string> vecKeys, const string& cond);
|
||||
bool updateFields(Fields& fields, vector<string> vecKeys, const string& cond);
|
||||
|
||||
protected:
|
||||
static MysqlOption option_;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include <string>
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -18,6 +20,20 @@ namespace DMUser
|
||||
const string LOGINTIME = "login_time";
|
||||
const string CREATETIME = "create_time";
|
||||
const string UPDATETIME = "update_time";
|
||||
|
||||
// 联合查询时使用
|
||||
const string ROLE_NAME = "role_name";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// 用户角色 表结构字段
|
||||
namespace DMUserRole
|
||||
{
|
||||
const string TABLENAME = "user_role";
|
||||
const string USER_ID = "user_id";
|
||||
const string ROLE_ID = "role_id";
|
||||
const string CREATETIME = "create_time";
|
||||
const string UPDATETIME = "update_time";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -71,7 +87,7 @@ namespace DMDevice
|
||||
const string TABLENAME = "device";
|
||||
const string DEVICE_ID = "device_id";
|
||||
const string STATION_ID = "station_id";
|
||||
const string TYPE = "type";
|
||||
const string TYPE_ID = "type_id";
|
||||
const string NAME = "name";
|
||||
const string CODE = "code";
|
||||
const string MODEL = "model";
|
||||
@@ -84,6 +100,14 @@ namespace DMDevice
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMDeviceTypeDef
|
||||
{
|
||||
const string TABLENAME = "def_device_type";
|
||||
const string TYPE_ID = "type_id";
|
||||
const string NAME = "name";
|
||||
const string ATTRS = "attrs";
|
||||
}
|
||||
|
||||
namespace DMPolicy
|
||||
{
|
||||
const string TABLENAME = "policy";
|
||||
|
||||
@@ -65,7 +65,7 @@ bool MysqlClient::exec(std::string sql)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MysqlClient::exec(std::string sql, vector<DataFields>& result)
|
||||
bool MysqlClient::exec(std::string sql, vector<Fields>& result)
|
||||
{
|
||||
result.clear();
|
||||
if (!mysql_)
|
||||
@@ -108,7 +108,7 @@ bool MysqlClient::exec(std::string sql, vector<DataFields>& result)
|
||||
break;
|
||||
}
|
||||
|
||||
DataFields row_data;
|
||||
Fields row_data;
|
||||
for (size_t i = 0; i < field_names.size(); ++i)
|
||||
{
|
||||
string field_text = (row[i] == NULL) ? "" : row[i];
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "mysql.h"
|
||||
#include "DataFields.h"
|
||||
#include "Fields.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -55,7 +55,7 @@ public:
|
||||
/**
|
||||
* @brief: 执行sql语句, 获取查询结果集
|
||||
*/
|
||||
bool exec(std::string, vector<DataFields>& result);
|
||||
bool exec(std::string, vector<Fields>& result);
|
||||
|
||||
private:
|
||||
// mysql数据库连接对象
|
||||
|
||||
Reference in New Issue
Block a user