mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +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);
|
||||
|
||||
Reference in New Issue
Block a user