修改http统计查询接口

This commit is contained in:
lixiaoyuan
2025-12-01 18:26:42 +08:00
parent f8dad86731
commit 97e4b182de
6 changed files with 139 additions and 78 deletions

View File

@@ -629,6 +629,15 @@ Errcode HttpEntity::queryStationList(const httplib::Request& req, njson& json, s
std::vector<Fields> result;
auto err = DAO::queryStationList(pageinfo, result);
for (auto& item : result)
{
int stationId = item.get<int>("station_id");
auto station = Application::data().getStation(stationId);
if (station)
{
item.set("err", station->err);
}
}
HttpHelper::setPagination(pageinfo, result, json);
return err;
};
@@ -1111,7 +1120,7 @@ Errcode HttpEntity::queryPredictionDetail(const httplib::Request& req, njson& js
for (auto& item : Application::data().mapStation)
{
auto& station = item.second;
//auto station = Application::data().getStation(5);
for (int i = 0; i < num; ++i)
{
vecStoreIn[i] += station->predictStorageIn[i];
@@ -1155,6 +1164,8 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, st
electStorageOut += station->electStorageOut;
}
float income = 0.0;
njson jsondata;
jsondata["launch_date"] = appdata.launchDate; //: 系统上线启用日期格式yyyy-mm-dd
jsondata["income_total"] = incomeTotal; // : 累计收益精度0.01
@@ -1167,14 +1178,17 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, st
jsondata["storage_elect_in"] = electStorageIn; // : 储能充电总电量kWh精度0.001
jsondata["storage_elect_out"] = electStorageOut; // : 储能放电总电量kWh精度0.001
std::string sql = "SELECT SUM(income_elect) income_total FROM stat_day;";
// 总览页面:累计收益
std::string sql = "SELECT * FROM stat_total st INNER JOIN "
"(SELECT station_id, MAX(dt) max_dt FROM stat_total GROUP BY station_id) tmp "
"ON st.station_id = tmp.station_id AND st.dt = tmp.max_dt; ";
std::vector<Fields> result;
DAO::exec(NULL, sql, result);
if (result.size() > 0)
for (auto& item: result)
{
jsondata["income_total"] = result[0].get<float>("income_total");
income += item.get<float>("income");
}
jsondata["income_total"] = income;
json["data"] = jsondata;
return Errcode::OK;
}
@@ -1184,17 +1198,18 @@ Errcode HttpEntity::queryStatStationGroup(const httplib::Request& req, njson& js
njson jsondata = njson::array();
auto dao = DaoEntity::create("");
std::string sql = R"(SELECT s.station_id, s.name station_name, ss.income_elect, ss.income_charge, ss.storage_usage FROM station s LEFT JOIN
(SELECT station_id, SUM(income_elect ) income_elect , SUM(income_charge) income_charge, avg(storage_usage) storage_usage FROM stat_day GROUP BY station_id) AS ss
ON ss.station_id = s.station_id)";
std::string sql = R"(SELECT s.name station_name, st.* FROM stat_total st INNER JOIN
(SELECT station_id, MAX(dt) max_dt FROM stat_total GROUP BY station_id) tmp
ON st.station_id=tmp.station_id AND st.dt=tmp.max_dt
LEFT JOIN station s ON s.station_id=st.station_id;)";
std::vector<Fields> vecStations;
auto err = dao->exec(sql, vecStations);
for (auto& fields: vecStations)
{
njson jsonnode;
jsonnode["station_name"] = fields.value("station_name");
jsonnode["income"] = fields.get<float>("income_elect") + fields.get<float>("income_charge");
jsonnode["usage_rate"] = fields.get<float>("storage_usage");
jsonnode["income"] = fields.get<float>("income");
jsonnode["usage_rate"] = fields.get<float>("usage");
jsondata.push_back(jsonnode);
}
json["data"] = jsondata;
@@ -1255,23 +1270,23 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
{
Fields params;
std::string sqlCondition = GetRequestStatParams(req, params);
std::string sql = R"(SELECT
SUM(storage_elect_in) storage_elect_in,
SUM(storage_elect_out) storage_elect_out,
SUM(storage_num_in) storage_num_in,
SUM(storage_num_out) storage_num_out,
SUM(storage_num_err) storage_num_err,
SUM(solar_elect_gen) solar_elect_gen,
SUM(solar_elect_grid) solar_elect_grid,
SUM(solar_num_err) solar_num_err,
AVG(storage_usage) storage_usage,
SUM(charge_elect) charge_elect,
SUM(charge_num) charge_num,
SUM(charge_num_err) charge_num_err,
AVG(charge_usage) charge_usage,
SUM(income_elect) income_elect,
SUM(income_charge) income_charge
FROM stat_day ss)" + sqlCondition + ";";
//std::string sql = R"(SELECT
// SUM(storage_elect_in) storage_elect_in,
// SUM(storage_elect_out) storage_elect_out,
// SUM(storage_num_in) storage_num_in,
// SUM(storage_num_out) storage_num_out,
// SUM(storage_num_err) storage_num_err,
// SUM(solar_elect_gen) solar_elect_gen,
// SUM(solar_elect_grid) solar_elect_grid,
// SUM(solar_num_err) solar_num_err,
// AVG(storage_usage) storage_usage,
// SUM(charge_elect) charge_elect,
// SUM(charge_num) charge_num,
// SUM(charge_num_err) charge_num_err,
// AVG(charge_usage) charge_usage,
// SUM(income_elect) income_elect,
// SUM(income_charge) income_charge
// FROM stat_day ss)" + sqlCondition + ";";
std::string stationId = params.value("station_id");
@@ -1285,6 +1300,9 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
auto& appdata = Application::data();
std::vector<Fields> result;
std::string sql = std::format("SELECT * FROM stat_total st INNER JOIN "
"(SELECT station_id, MAX(dt) max_dt FROM stat_total ss {} GROUP BY station_id) tmp "
"ON st.station_id = tmp.station_id AND st.dt = tmp.max_dt; ", sqlCondition);
DaoEntity::execOnce(sql, result);
if (result.size() > 0)
{
@@ -1292,20 +1310,20 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
//jsondata["launch_date"] = appdata.launchDate; //: 系统上线启用日期格式yyyy-mm-dd
// jsondata["station_id"] = station_id;
jsondata["storage_elect_in"] = fields.value("storage_elect_in"); //储能充电电量kWh精度0.001
jsondata["storage_elect_out"] = fields.value("storage_elect_out"); //储能放电电量kWh精度0.001
jsondata["storage_num_in"] = fields.value("storage_elect_out"); //储能设备充电次数
jsondata["storage_num_out"] = fields.value("storage_num_out"); //储能设备放电次数
jsondata["storage_num_err"] = fields.value("storage_num_err"); //储能设备故障次数
jsondata["solar_elect_gen"] = fields.value("solar_elect_gen"); //光伏发电电量kWh精度0.001
jsondata["solar_elect_grid"] = fields.value("solar_elect_grid"); //光伏入网电量kWh精度0.001
jsondata["solar_num_err"] = fields.value("solar_num_err"); //光伏设备故障次数
jsondata["charge_elect"] = fields.value("charge_elect"); //充电设备充电电量kWh精度0.001
jsondata["charge_num"] = fields.value("charge_num"); //充电设备充电次数
jsondata["charge_num_err"] = fields.value("charge_num_err"); //充电设备故障次数
jsondata["income_elect"] = fields.value("income_elect"); //发电收益精度0.01
jsondata["storage_elect_in"] = fields.value("E_in"); //储能充电电量kWh精度0.001
jsondata["storage_elect_out"] = fields.value("E_out"); //储能放电电量kWh精度0.001
jsondata["storage_num_in"] = fields.value("t_in"); //储能设备充电次数
jsondata["storage_num_out"] = fields.value("t_out"); //储能设备放电次数
jsondata["storage_num_err"] = fields.value("n_err"); //储能设备故障次数
jsondata["solar_elect_gen"] = fields.value("E_gen"); //光伏发电电量kWh精度0.001
jsondata["solar_elect_grid"] = fields.value("E_grid"); //光伏入网电量kWh精度0.001
jsondata["solar_num_err"] = fields.value("n_err_solor"); //光伏设备故障次数
jsondata["charge_elect"] = fields.value("E_charge"); //充电设备充电电量kWh精度0.001
jsondata["charge_num"] = fields.value("n_charge"); //充电设备充电次数
jsondata["charge_num_err"] = fields.value("n_err_charge"); //充电设备故障次数
jsondata["income_elect"] = fields.value("income"); //发电收益精度0.01
jsondata["income_charge"] = fields.value("income_charge"); //充电收益精度0.01
jsondata["usage_rate"] = Utils::toStr(float(fields.get<int>("storage_usage")+fields.get<int>("storage_usage"))*0.5f, 0);
jsondata["usage_rate"] = 0; //Utils::toStr(float(fields.get<int>("storage_usage") + fields.get<int>("storage_usage")) * 0.5f, 0);
json["data"] = jsondata;
}