实现系统管理表格操作接口、分页操作

This commit is contained in:
lixiaoyuan
2025-08-22 19:06:50 +08:00
parent 7e965b6fb4
commit 7fe51ea362
56 changed files with 2234 additions and 1304 deletions

View File

@@ -1,6 +1,91 @@
#include "AppData.h"
#include "common/Utils.h"
#include "app/Station.h"
#include "app/Device.h"
#include "database/Dao.h"
void InitStation(AppData* appdata)
{
// 读取数据库
std::vector<Fields> result;
DAO::queryStationList(NULL, result);
for (auto& fields: result)
{
int stationId = fields.getInt(DMStation::STATION_ID);
auto station = std::make_shared<Station>(stationId);
station->name = fields.value(DMStation::NAME);
station->energyCapacity = fields.getDouble(DMStation::CAPACITY);
appdata->mapStation[stationId] = station;
}
}
void InitDevice(AppData* appdata)
{
vector<Fields> result;
DAO::queryDeviceList(NULL, result);
for (auto& fields: result)
{
int deviceId = fields.getInt(DMDevice::DEVICE_ID);
int stationId = fields.getInt(DMDevice::STATION_ID);
auto station = appdata->getStation(stationId);
if (station)
{
auto device = Device::create(fields);
station->addDevice(deviceId, device);
}
else
{
XLOGE() << "init device error: unknown station_id:[" << stationId << "] device_id=" << deviceId;
}
}
}
void InitStatData(AppData* appdata)
{
std::string curDate = Utils::dateStr();
vector<Fields> result;
DAO::queryStatDataList(curDate, curDate, result);
for (auto& fields: result)
{
std::string dt = fields.value(DMStatStation::DT);
int stationId = fields.getInt(DMStatStation::STATION_ID);
auto station = appdata->getStation(stationId);
if (station)
{
station->storageIn = fields.getFloat(DMStatStation::STORAGE_ELECT_IN);
station->storageOut = fields.getFloat(DMStatStation::STORAGE_ELECT_OUT);
//station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM);
//station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM);
station->storageNumErr = fields.getFloat(DMStatStation::STORAGE_NUM_ERR);
station->solarGen = fields.getFloat(DMStatStation::SOLAR_ELECT_GEN);
station->solarGrid = fields.getFloat(DMStatStation::SOLAR_ELECT_GRID);
station->solarNumErr = fields.getFloat(DMStatStation::SOLAR_NUM_ERR);
station->chargeElect = fields.getFloat(DMStatStation::CHARGE_ELECT);
station->chargeNum = fields.getFloat(DMStatStation::CHARGE_NUM);
station->chargeNumErr = fields.getFloat(DMStatStation::CHARGE_NUM_ERR);
}
else
{
XLOGE() << "init staticis data error: unknown station_id:[" << stationId << "] dt=" << dt;
}
}
}
void AppData::init()
{
// 初始化场站信息
InitStation(this);
// 读取设备信息,连接设备
InitDevice(this);
// 读取基础统计信息,在系统总览中需要展示
InitStatData(this);
this->initUser();
}
std::shared_ptr<Station> AppData::getStation(int stationId)
{
@@ -24,15 +109,6 @@ std::shared_ptr<Station> AppData::getStationByName(std::string name)
return nullptr;
}
void AppData::getStationNames(std::vector<std::string>& vecNames)
{
vecNames.resize(mapStation.size());
int i = 0;
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
{
vecNames[i] = iter->second->name;
}
}
std::shared_ptr<Device> AppData::getDevice(int stationId, int deviceId)
{
@@ -44,6 +120,80 @@ std::shared_ptr<Device> AppData::getDevice(int stationId, int deviceId)
return nullptr;
}
std::unordered_map<int, std::shared_ptr<DeviceType>>& AppData::getDeviceTypeDef()
{
return mapDeviceType;
}
void AppData::loadStatData()
{
}
void AppData::initUser()
{
auto dao = DaoEntity::create("");
std::vector<Fields> result;
// 数据库读取角色定义
mapping.role.clear();
DAO::queryRoleList(dao, result);
for (auto& fields : result)
{
auto item = std::make_shared<Role>();
item->roleId = fields.getInt(DMRole::ROLE_ID);
item->name = fields.value(DMRole::NAME);
item->isOpen = fields.getInt(DMRole::IS_OPEN);
mapRole[item->roleId] = item;
mapping.role.push_back({std::to_string(item->roleId), item->name});
}
// 数据库读取设备类型定义
result.clear();
DAO::queryDeviceTypeDef(dao, result);
for (auto& fields : result)
{
auto item = std::make_shared<DeviceType>() ;
item->typeId = fields.getInt(DMDeviceTypeDef::TYPE_ID);
item->name = fields.value(DMDeviceTypeDef::NAME);
item->attrs = fields.value(DMDeviceTypeDef::ATTRS);
mapDeviceType[item->typeId] = item;
mapping.deviceType.push_back({std::to_string(item->typeId), item->name});
}
}
std::vector<std::string> AppData::getRoleNames()
{
std::vector<std::string> vec(mapRole.size());
int i = 0;
for (auto iter = mapRole.begin(); iter!=mapRole.end(); ++iter)
{
vec[i] = iter->second->name;
++i;
}
return vec;
}
std::vector<std::string> AppData::getStationNames()
{
std::vector<std::string> vec(mapStation.size());
int i = 0;
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
{
vec[i] = iter->second->name;
++i;
}
return vec;
}
std::vector<std::string> AppData::getDeviceTypes()
{
std::vector<std::string> vec(mapping.deviceType.size());
int i = 0;
for (auto iter = mapping.deviceType.begin(); iter!=mapping.deviceType.end(); ++iter)
{
vec[i] = iter->second;
++i;
}
return vec;
}

View File

@@ -9,20 +9,51 @@
class Station;
class Device;
using VecPairSS = std::vector<std::pair<std::string, std::string>>;
struct DeviceType
{
int typeId {};
std::string name;
std::string attrs;
};
struct Role
{
int roleId {};
std::string name;
bool isOpen {false};
};
class AppData
{
public:
void init();
std::shared_ptr<Station> getStation(int stationId);
std::shared_ptr<Station> getStationByName(std::string name);
void getStationNames(std::vector<std::string>& vecNames);
std::shared_ptr<Device> getDevice(int stationId, int deviceId);
///////////////////////////////////////////////////////////////////////////////////////////////
// 获取角色名称列表
std::vector<std::string> getRoleNames();
// 获取场站名称列表
std::vector<std::string> getStationNames();
// 获取设备类型
std::vector<std::string> getDeviceTypes();
// 获取设备类型定义
std::unordered_map<int, std::shared_ptr<DeviceType>>& getDeviceTypeDef();
// 读取统计数据: 今日统计数据,累计统计数据
void loadStatData();
void initUser();
public:
///////////////////////////////////////////////////////////////////////////////////////////////
@@ -38,11 +69,26 @@ public:
std::string passwd;
} db;
struct {
VecPairSS isopen {{"0", "禁用"}, {"1", "启用"}};
VecPairSS gender {{"0", ""}, {"1", ""}};
// 角色 Mapping id->name
VecPairSS role;
VecPairSS deviceType;
} mapping;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 场站信息 ===
std::unordered_map<int, std::shared_ptr<Station>> mapStation;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 角色定义 ===
std::unordered_map<int, std::shared_ptr<Role>> mapRole;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 设备类型定义 ===
std::unordered_map<int, std::shared_ptr<DeviceType>> mapDeviceType;
};

View File

@@ -8,82 +8,6 @@
#include "app/Station.h"
#include "app/Device.h"
void InitStation()
{
AppData& appdata = Application::instance().getAppData();
// 读取数据库
std::vector<DataFields> result;
DAO::queryStationList(result);
for (auto& fields: result)
{
int stationId = fields.getInt(DMStation::STATION_ID);
auto station = std::make_shared<Station>(stationId);
station->name = fields.getStr(DMStation::NAME);
station->energyCapacity = fields.getDouble(DMStation::CAPACITY);
appdata.mapStation[stationId] = station;
}
}
void InitDevice()
{
AppData& appdata = Application::instance().getAppData();
vector<DataFields> result;
DAO::queryDeviceList(result);
for (auto& fields: result)
{
int deviceId = fields.getInt(DMDevice::DEVICE_ID);
int stationId = fields.getInt(DMDevice::STATION_ID);
auto station = appdata.getStation(stationId);
if (station)
{
auto device = Device::create(fields);
station->addDevice(deviceId, device);
}
else
{
XLOGE() << "init device error: unknown station_id:[" << stationId << "] device_id=" << deviceId;
}
}
}
void InitStatData()
{
AppData& appdata = Application::instance().getAppData();
std::string curDate = Utils::dateStr();
vector<DataFields> result;
DAO::queryStatDataList(curDate, curDate, result);
for (auto& fields: result)
{
std::string dt = fields.getStr(DMStatStation::DT);
int stationId = fields.getInt(DMStatStation::STATION_ID);
auto station = appdata.getStation(stationId);
if (station)
{
station->storageIn = fields.getFloat(DMStatStation::STORAGE_ELECT_IN);
station->storageOut = fields.getFloat(DMStatStation::STORAGE_ELECT_OUT);
//station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM);
//station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM);
station->storageNumErr = fields.getFloat(DMStatStation::STORAGE_NUM_ERR);
station->solarGen = fields.getFloat(DMStatStation::SOLAR_ELECT_GEN);
station->solarGrid = fields.getFloat(DMStatStation::SOLAR_ELECT_GRID);
station->solarNumErr = fields.getFloat(DMStatStation::SOLAR_NUM_ERR);
station->chargeElect = fields.getFloat(DMStatStation::CHARGE_ELECT);
station->chargeNum = fields.getFloat(DMStatStation::CHARGE_NUM);
station->chargeNumErr = fields.getFloat(DMStatStation::CHARGE_NUM_ERR);
}
else
{
XLOGE() << "init staticis data error: unknown station_id:[" << stationId << "] dt=" << dt;
}
}
}
void Application::init()
{
// 初始化系统配置,读取配置文件
@@ -102,12 +26,8 @@ void Application::init()
// 连接数据库,读取基础信息
// 初始化场站信息
InitStation();
// 读取设备信息,连接设备
InitDevice();
// 读取基础统计信息,在系统总览中需要展示
InitStatData();
// 初始化系统基础数据
appdata_.init();
// 创建设备处理线程
std::thread([=]() { runThreadDevice(); }).detach();
@@ -116,10 +36,6 @@ void Application::init()
std::thread([=]() { runThreadMain(); }).detach();
}
AppData& Application::getAppData()
{
return appdata_;
}
void Application::runThreadMain()
{

View File

@@ -15,10 +15,13 @@ public:
return app;
}
static AppData& data()
{
return Application::instance().appdata_;
}
void init();
AppData& getAppData();
bool isQuit() { return isQuit_; }
Operator& getOperator() { return op_; }

View File

@@ -10,12 +10,12 @@ enum class EnDatabaseErr
SUCCESS = 0,
};
std::shared_ptr<DaoEntity> DAO::get(std::string tableName)
std::shared_ptr<DaoEntity> DAO1::get(std::string tableName)
{
return std::make_shared<DaoEntity>(tableName);
}
Errcode DAO::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err)
Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::string passwd, std::string& err)
{
std::string t = Utils::timeStr();
if (!dao)
@@ -25,35 +25,35 @@ Errcode DAO::login(std::shared_ptr<DaoEntity> dao, std::string account, std::str
if (!dao->isConnected())
{
err = "数据库连接错误";
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
return Errcode::ERR_DB_CONN;
}
std::string sql = "SELECT * FROM user WHERE account='" + account + "';";
std::vector<DataFields> res;
std::vector<Fields> res;
bool ret = dao->exec(sql, res);
if (!ret)
{
err = "数据库操作错误";
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
return Errcode::ERR_DB_CONN;
}
if (res.size() <=0)
{
err = "用户不存在";
DAO::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
DAO1::writeSystemLog(dao, 2, "", account, "用户登录失败:" + err);
return Errcode::ERR_LOGIN_USER_NOTEXIST;
}
DataFields& fields = res[0];
std::string userId = fields.getStr("user_id");
Fields& fields = res[0];
std::string userId = fields.value("user_id");
int loginCount = fields.getInt("login_count");
// 判断密码
if (passwd != fields.getStr("passwd"))
if (passwd != fields.value("passwd"))
{
err = "密码错误";
DAO::writeSystemLog(dao, 2, userId, account, "用户登录失败:" + err);
DAO1::writeSystemLog(dao, 2, userId, account, "用户登录失败:" + err);
return Errcode::ERR_LOGIN_PASSWD;
}
@@ -67,11 +67,11 @@ Errcode DAO::login(std::shared_ptr<DaoEntity> dao, std::string account, std::str
XLOGE() << "更新用户登录信息失败sql=" << sql;
}
DAO::writeSystemLog(dao, 2, userId, account, "用户登录成功");
DAO1::writeSystemLog(dao, 2, userId, account, "用户登录成功");
return Errcode::OK;
}
bool DAO::writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string userId, std::string account, std::string text)
bool DAO1::writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string userId, std::string account, std::string text)
{
if (!dao)
{
@@ -84,7 +84,7 @@ bool DAO::writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string u
// 数据库写入登录日志
dao->setTableName("system_log");
DataFields fieldsLog;
Fields fieldsLog;
fieldsLog.set("log_id", Snowflake::instance().getIdStr());
fieldsLog.set("type", 2);
fieldsLog.set("user_id", userId);
@@ -96,7 +96,7 @@ bool DAO::writeSystemLog(std::shared_ptr<DaoEntity> dao, int type, std::string u
}
bool DAO::queryUser(std::vector<DataFields>& res)
bool DAO1::queryUser(std::vector<Fields>& res)
{
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("");
if (!dao->isConnected())
@@ -108,7 +108,7 @@ bool DAO::queryUser(std::vector<DataFields>& res)
return ret;
}
int DAO::insertUser(DataFields& fields)
int DAO1::insertUser(Fields& fields)
{
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("user");
if (!dao->isConnected())
@@ -116,10 +116,10 @@ int DAO::insertUser(DataFields& fields)
return 1;
}
std::string account = fields.getStr("account");
std::string account = fields.value("account");
// step1: 查询
std::vector<DataFields> res;
std::vector<Fields> res;
bool ret = dao->exec("SELECT * from user WHERE account='" + account + "';", res);
if (!ret)
{
@@ -136,7 +136,7 @@ int DAO::insertUser(DataFields& fields)
return (ret) ? 0 : 1;
}
int DAO::updateUserById(std::string id, DataFields& fields)
int DAO1::updateUserById(std::string id, Fields& fields)
{
std::shared_ptr<DaoEntity> dao = std::make_shared<DaoEntity>("user");
if (!dao->isConnected())

View File

@@ -2,7 +2,7 @@
#include "app/errcode.h"
class DAO
class DAO1
{
public:
static std::shared_ptr<DaoEntity> get(std::string tableName="");
@@ -18,15 +18,15 @@ public:
/**
* 查询用户
*/
static bool queryUser(std::vector<DataFields>& res);
static bool queryUser(std::vector<Fields>& res);
/**
* 新增用户
*/
static int insertUser(DataFields& fields);
static int insertUser(Fields& fields);
/**
* 修改用户信息
*/
static int updateUserById(std::string id, DataFields& fields);
static int updateUserById(std::string id, Fields& fields);
};

View File

@@ -45,7 +45,7 @@ int Device::startComm()
}
// 从属性列表中获取通讯方式和通讯地址、端口
std::string commType = attrs.getStr("commType");
std::string commType = attrs.value("commType");
// 如果entity的通讯协议类型当前配置不一致需要关闭连接删除通讯后创建新的通讯
if (commEntity && commEntity->type != commType)
@@ -64,15 +64,15 @@ int Device::startComm()
return 0;
}
std::shared_ptr<Device> Device::create(DataFields& fields)
std::shared_ptr<Device> Device::create(Fields& fields)
{
auto device = std::make_shared<Device>();
device->deviceId = fields.getInt("device_id");
device->type = fields.getInt("type");
device->name = fields.getStr("name");
device->code = fields.getStr("code");
device->name = fields.value("name");
device->code = fields.value("code");
device->isOpen = fields.getInt("is_open");
device->attrsJson = fields.getStr("attrs");
device->attrsJson = fields.value("attrs");
// 解析属性的JSON字符串转换成键值对
NJson jsonroot;

View File

@@ -5,7 +5,7 @@
#include <vector>
#include <memory>
#include <common/DataFields.h>
#include "common/Fields.h"
class CommEntity;
@@ -26,7 +26,7 @@ public:
//std::map<std::string, std::string> mapAttrs;
DataFields attrs;
Fields attrs;
// 通讯entity
std::shared_ptr<CommEntity> commEntity;
@@ -39,7 +39,7 @@ public:
// 启动通讯
int startComm();
static std::shared_ptr<Device> create(DataFields& fields);
static std::shared_ptr<Device> create(Fields& fields);
};

View File

@@ -8,8 +8,9 @@ enum class Errcode
ERR_DB_CONN = 101, // 数据库连接错误
ERR_DB_SQL = 102, // 数据库查询SQL错误
ERR_DB_DUPLICATE, // 数据重复
ERR_LOGIN_USER_NOTEXIST = 103, // 登入错误,用户不存在
ERR_LOGIN_PASSWD = 104, // 登入错误,密码不正确
ERR_LOGIN_USER_NOTEXIST, // 登入错误,用户不存在
ERR_LOGIN_PASSWD, // 登入错误,密码不正确
};