mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +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);
|
||||
}
|
||||
54
src/database/Dao.h
Normal file
54
src/database/Dao.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include "DaoEntity.h"
|
||||
#include "DataModelDef.h"
|
||||
#include "common/Logger.h"
|
||||
|
||||
class DAO
|
||||
{
|
||||
public:
|
||||
static std::string sqlPageLimit(int index, int size);
|
||||
|
||||
static bool count(DaoEntity& dao, std::string tableName, std::string condition, int& count);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 用户管理
|
||||
static bool queryUserList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
static bool updateUserById(DataFields& params);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 角色管理
|
||||
static bool queryRoleList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 权限管理
|
||||
static bool queryPermissionList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 场站管理
|
||||
// 查询场站信息列表
|
||||
static bool queryStationList(vector<DataFields>& result);
|
||||
// 分页查询场站信息列表
|
||||
static bool queryStationList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 设备管理
|
||||
// 查询设备信息列表
|
||||
static bool queryDeviceList(vector<DataFields>& result);
|
||||
// 分页查询设备信息列表
|
||||
static bool queryDeviceList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 策略管理
|
||||
// 分页查询策略信息列表
|
||||
static bool queryPolicyList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 系统日志管理
|
||||
// 分页查询系统日志列表
|
||||
static bool querySystemLogList(PageInfo& pageInfo, vector<DataFields>& result);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// === 统计数据管理
|
||||
static bool queryStatDataList(std::string startDate, std::string endDate, vector<DataFields>& result);
|
||||
};
|
||||
@@ -192,20 +192,21 @@ bool DaoEntity::queryFields(string keys, const string& sql_c, PageInfo& pageinfo
|
||||
return true;
|
||||
}
|
||||
|
||||
pageinfo.page_max = pageinfo.total / pageinfo.page_size + (pageinfo.total % pageinfo.page_size > 0 ? 1 : 0);
|
||||
pageinfo.pageCount = pageinfo.total / pageinfo.pageSize + (pageinfo.total % pageinfo.pageSize > 0 ? 1 : 0);
|
||||
oss.str("");
|
||||
if (pageinfo.page_id <= 0)
|
||||
if (pageinfo.pageIndex <= 0)
|
||||
{
|
||||
pageinfo.page_id = 1;
|
||||
pageinfo.pageIndex = 1;
|
||||
}
|
||||
int start = (pageinfo.page_id - 1) * pageinfo.page_size;
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << pageinfo.page_size << ";";
|
||||
int start = (pageinfo.pageIndex - 1) * pageinfo.pageSize;
|
||||
oss << "SELECT " << keys << " FROM `" << tableName_ << "` " << sql_c << " LIMIT " << start << "," << pageinfo.pageSize << ";";
|
||||
return this->db_->exec(oss.str().c_str(), result);
|
||||
}
|
||||
|
||||
bool DaoEntity::updateFields(DataFields& fields, const string& sql_c)
|
||||
{
|
||||
string sql = fields.get_update_sql(tableName_, sql_c);
|
||||
std::cout << sql;
|
||||
if (sql_c.empty())
|
||||
{
|
||||
//Spdlogger::error("[DB] update condition is empty, not exec, sql={}", sql);
|
||||
|
||||
138
src/database/DataModelDef.h
Normal file
138
src/database/DataModelDef.h
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// 用户 表结构字段
|
||||
namespace DMUser
|
||||
{
|
||||
const string TABLENAME = "user"; // 表名称
|
||||
const string USER_ID = "user_id";
|
||||
const string ACCOUNT = "account";
|
||||
const string PASSWD = "passwd";
|
||||
const string NAME = "name";
|
||||
const string GENDER = "gender";
|
||||
const string AGE = "age";
|
||||
const string PHONE = "phone";
|
||||
const string EMAIL = "email";
|
||||
const string IS_OPEN = "is_open";
|
||||
const string LOGINTIME = "login_time";
|
||||
const string CREATETIME = "create_time";
|
||||
const string UPDATETIME = "update_time";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// 角色 表结构字段
|
||||
namespace DMRole
|
||||
{
|
||||
const string TABLENAME = "role";
|
||||
const string ROLE_ID = "role_id";
|
||||
const string NAME = "name";
|
||||
const string TYPE = "type";
|
||||
const string DESCRIBE = "describe";
|
||||
const string IS_OPEN = "is_open";
|
||||
const string CREATETIME = "create_time";
|
||||
const string UPDATETIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMPermission
|
||||
{
|
||||
const string TABLENAME = "permission";
|
||||
const string PERMISSION_ID = "permission_id";
|
||||
const string NAME = "name";
|
||||
const string DESCRIBE = "describe";
|
||||
const string IS_OPEN = "is_open";
|
||||
const string CREATETIME = "create_time";
|
||||
const string UPDATETIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMRolePermission
|
||||
{
|
||||
const string TABLENAME = "role_permission";
|
||||
const string FID_ID = "id";
|
||||
const string FID_ROLE_ID = "role_id";
|
||||
const string FID_PERMISSION_ID = "permission_id";
|
||||
}
|
||||
|
||||
namespace DMStation
|
||||
{
|
||||
const string TABLENAME = "station";
|
||||
const string STATION_ID = "station_id";
|
||||
const string NAME = "name";
|
||||
const string ADDRESS = "address";
|
||||
const string LONGITUDE = "lon";
|
||||
const string LATITUDE = "lat";
|
||||
const string TEL = "tel";
|
||||
const string CAPACITY = "capacity";
|
||||
const string STATUS = "status";
|
||||
}
|
||||
|
||||
namespace DMDevice
|
||||
{
|
||||
const string TABLENAME = "device";
|
||||
const string DEVICE_ID = "device_id";
|
||||
const string STATION_ID = "station_id";
|
||||
const string TYPE = "type";
|
||||
const string NAME = "name";
|
||||
const string CODE = "code";
|
||||
const string MODEL = "model";
|
||||
const string FACTORY = "factory";
|
||||
const string TEL = "factory_tel";
|
||||
const string ATTRS = "attrs";
|
||||
const string IS_OPEN = "is_open";
|
||||
const string STATUS = "status";
|
||||
const string CREATE_TIME = "create_time";
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMPolicy
|
||||
{
|
||||
const string TABLENAME = "policy";
|
||||
const string POLICY_ID = "policy_id";
|
||||
const string TYPE = "type";
|
||||
const string NAME = "name";
|
||||
const string DESCRIBE = "describe";
|
||||
const string VALUE = "value";
|
||||
const string IS_OPEN = "is_open";
|
||||
const string CREATE_TIME = "create_time";
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMSystemLog
|
||||
{
|
||||
const string TABLENAME = "system_log";
|
||||
const string LOG_ID = "log_id";
|
||||
const string TYPE = "type";
|
||||
const string USER_ID = "user_id";
|
||||
const string USER_ACCOUNT = "user_account";
|
||||
const string CONTENT = "content";
|
||||
const string STATUS = "status";
|
||||
const string CREATE_TIME = "create_time";
|
||||
const string UPDATE_TIME = "update_time";
|
||||
}
|
||||
|
||||
namespace DMAlertLog
|
||||
{
|
||||
const string TABLENAME = "alert_log";
|
||||
const string LOG_ID = "log_id";
|
||||
const string TYPE = "type";
|
||||
const string DEVICE_ID = "device_id";
|
||||
const string CONTENT = "content";
|
||||
const string STATUS = "status";
|
||||
const string CREATE_TIME = "create_time";
|
||||
}
|
||||
|
||||
namespace DMStatStation
|
||||
{
|
||||
const string TABLENAME = "stat_staion";
|
||||
const string DT = "dt";
|
||||
const string STATION_ID = "station_id";
|
||||
const string STORAGE_ELECT_IN = "storage_elect_in";
|
||||
const string STORAGE_ELECT_OUT = "storage_elect_out";
|
||||
const string STORAGE_NUM_ERR = "storage_num_err";
|
||||
const string SOLAR_ELECT_GEN = "solar_elect_gen";
|
||||
const string SOLAR_ELECT_GRID = "solar_elect_grid";
|
||||
const string SOLAR_NUM_ERR = "solar_num_err";
|
||||
const string CHARGE_ELECT = "charge_elect";
|
||||
const string CHARGE_NUM = "charge_num";
|
||||
const string CHARGE_NUM_ERR = "charge_num_err";
|
||||
}
|
||||
Reference in New Issue
Block a user