mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
搭建PVB架构,实现前端的基础布局、菜单、表格、图示等功能
This commit is contained in:
172
src/database/Dao.cpp
Normal file
172
src/database/Dao.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include "Dao.h"
|
||||
#include "common/Utils.h"
|
||||
|
||||
std::string DAO::sqlPageLimit(int index, int size)
|
||||
{
|
||||
int startIndex = index * size;
|
||||
if (startIndex < 0) { startIndex = 0; }
|
||||
return " LIMIT " + std::to_string(startIndex) + "," + std::to_string(size);
|
||||
}
|
||||
|
||||
bool DAO::count(DaoEntity& dao, std::string tableName, std::string condition, int& count)
|
||||
{
|
||||
std::string sql = "SELECT COUNT(*) count FROM " + tableName;
|
||||
if (!condition.empty()) { sql += " WHERE " + condition; };
|
||||
sql += ";";
|
||||
|
||||
std::vector<DataFields> result;
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (ret)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].getInt("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool QueryCount(DaoEntity& dao, std::string sqlFrom, int& count)
|
||||
{
|
||||
std::vector<DataFields> result;
|
||||
bool ret = dao.exec("SELECT COUNT(*) count " + sqlFrom, result);
|
||||
if (ret)
|
||||
{
|
||||
count = (result.size() > 0) ? result[0].getInt("count") : 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static bool QueryPagination(std::string sqlFrom, PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
|
||||
int count {0};
|
||||
if (!QueryCount(dao, sqlFrom, count))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pageInfo.total = count;
|
||||
std::string sql = "SELECT * " + sqlFrom + DAO::sqlPageLimit(pageInfo.pageIndex, pageInfo.pageSize);
|
||||
bool ret = dao.exec(sql, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: sql=" << sql;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryUserList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMUser::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryUserList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::updateUserById(DataFields& 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 + "'");
|
||||
}
|
||||
|
||||
bool DAO::queryRoleList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMRole::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryRoleList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryPermissionList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMPermission::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPermissionList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 查询场站信息列表
|
||||
bool DAO::queryStationList(vector<DataFields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMStation::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
|
||||
// 分页查询场站信息列表
|
||||
bool DAO::queryStationList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMStation::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryStationList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 查询设备信息列表
|
||||
bool DAO::queryDeviceList(vector<DataFields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMDevice::TABLENAME;
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
|
||||
// 分页查询设备信息列表
|
||||
bool DAO::queryDeviceList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMDevice::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryDeviceList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 策略管理
|
||||
bool DAO::queryPolicyList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMPolicy::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: queryPolicyList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 系统日志管理
|
||||
bool DAO::querySystemLogList(PageInfo& pageInfo, vector<DataFields>& result)
|
||||
{
|
||||
DaoEntity dao("");
|
||||
std::string sqlFrom = "FROM " + DMSystemLog::TABLENAME;
|
||||
bool ret = QueryPagination(sqlFrom, pageInfo, result);
|
||||
if (!ret)
|
||||
{
|
||||
XLOGE() << "DAO database error: querySystemLogList failed.";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool DAO::queryStatDataList(std::string startDate, std::string endDate, vector<DataFields>& result)
|
||||
{
|
||||
std::string sql = "SELECT * FROM " + DMStatStation::TABLENAME + " WHERE dt BETWEEN '" + startDate + "' AND '" + endDate + "';";
|
||||
return DaoEntity::execOnce(sql, result);
|
||||
}
|
||||
Reference in New Issue
Block a user