mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现excel报表文件生成功能
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "app/Config.h"
|
||||
#include "app/Station.h"
|
||||
#include "app/Device.h"
|
||||
#include "common/XlsxEntity.h"
|
||||
|
||||
static void FieldsToJson(Fields& fields, njson& json)
|
||||
{
|
||||
@@ -46,7 +47,6 @@ static void JsonToFields(njson& json, std::vector<std::string> vecKeys, Fields&
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ static njson FieldsToJsonArray(std::vector<Fields>& vecFields)
|
||||
return jsonnode;
|
||||
}
|
||||
|
||||
static void GetRequestParam(const httplib::Request& req, const std::vector<std::string>& vecKeys, Fields& fields)
|
||||
static void GetRequestParams(const httplib::Request& req, const std::vector<std::string>& vecKeys, Fields& fields)
|
||||
{
|
||||
if (req.method == "GET")
|
||||
{
|
||||
@@ -86,6 +86,18 @@ static void GetRequestParam(const httplib::Request& req, const std::vector<std::
|
||||
}
|
||||
}
|
||||
|
||||
static void CheckRequiredParams(Fields& params, std::vector<std::string> keys, std::string& errmsg)
|
||||
{
|
||||
for (auto& key : keys)
|
||||
{
|
||||
if (!params.contains(key))
|
||||
{
|
||||
errmsg = "参数[" + key + "]错误";;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class HttpHelper
|
||||
{
|
||||
public:
|
||||
@@ -172,9 +184,9 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
|
||||
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
|
||||
{"/queryStatDetailList", HandlerOptions(&HttpEntity::queryStatDetailList, {})},
|
||||
{"/queryStatCharts", HandlerOptions(&HttpEntity::queryStatCharts, {})},
|
||||
|
||||
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
|
||||
{"/exportStatReport", HandlerOptions(&HttpEntity::exportStatReport, {})},
|
||||
|
||||
{"/queryEnvironment", HandlerOptions(&HttpEntity::queryEnvironment, { "station_id"})},
|
||||
|
||||
{"/queryServiceApiList", HandlerOptions(&HttpEntity::queryServiceApiList, {})},
|
||||
{"/deleteServiceApi", HandlerOptions(&HttpEntity::deleteServiceApi, {"api_id"})},
|
||||
@@ -381,14 +393,14 @@ Errcode HttpEntity::queryUserList(const httplib::Request& req, njson& json, std:
|
||||
Errcode HttpEntity::insertUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
||||
GetRequestParams(req, {"account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
||||
return DAO::insertUser(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::updateUser(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"user_id", "account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
||||
GetRequestParams(req, {"user_id", "account", "name", "gender", "age", "phone", "email", "role_id"}, params);
|
||||
return DAO::updateUserById(params);
|
||||
}
|
||||
|
||||
@@ -445,14 +457,14 @@ Errcode HttpEntity::queryPermissionList(const httplib::Request& req, njson& json
|
||||
Errcode HttpEntity::insertPermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"name", "describe", "is_open"}, params);
|
||||
GetRequestParams(req, {"name", "describe", "is_open"}, params);
|
||||
return DAO::insertPermission(params);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::updatePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"permission_id", "name", "describe", "is_open"}, params);
|
||||
GetRequestParams(req, {"permission_id", "name", "describe", "is_open"}, params);
|
||||
return DAO::updatePermissionById(params);
|
||||
}
|
||||
|
||||
@@ -540,7 +552,7 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std:
|
||||
Errcode HttpEntity::queryRolePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"role_id"}, params);
|
||||
GetRequestParams(req, {"role_id"}, params);
|
||||
if (!params.contains("role_id")) { errmsg = "缺少参数[role_id]"; return Errcode::ERR_PARAM; }
|
||||
return Errcode::OK;
|
||||
}
|
||||
@@ -548,13 +560,13 @@ Errcode HttpEntity::queryRolePermission(const httplib::Request& req, njson& json
|
||||
Errcode HttpEntity::insertRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"name", "describe", "is_open", "permission"}, params);
|
||||
GetRequestParams(req, {"name", "describe", "is_open", "permission"}, params);
|
||||
return DAO::insertRole(params);
|
||||
};
|
||||
Errcode HttpEntity::updateRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
||||
GetRequestParams(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
||||
|
||||
auto roleId = params.value("role_id");
|
||||
std::string permission = params.remove("permission");
|
||||
@@ -610,14 +622,14 @@ Errcode HttpEntity::insertStation(const httplib::Request& req, njson& json, std:
|
||||
{
|
||||
|
||||
Fields params;
|
||||
GetRequestParam(req, {"name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
|
||||
GetRequestParams(req, {"name", "address", "lon", "lat", "tel", "capacity", "status"}, params);
|
||||
return DAO::insertStation(params);
|
||||
};
|
||||
|
||||
Errcode HttpEntity::updateStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status", "work_mode", "policy_id", "operation_date"}, params);
|
||||
GetRequestParams(req, {"station_id", "name", "address", "lon", "lat", "tel", "capacity", "status", "work_mode", "policy_id", "operation_date"}, params);
|
||||
std::string stationId = params.value("station_id");
|
||||
params.check("capacity", "", "0.0");
|
||||
params.check("lon", "", "0.0");
|
||||
@@ -656,7 +668,7 @@ Errcode HttpEntity::deleteStation(const httplib::Request& req, njson& json, std:
|
||||
Errcode HttpEntity::queryStationOverview(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id"}, params);
|
||||
GetRequestParams(req, {"station_id"}, params);
|
||||
std::string stationId = params.value("station_id");
|
||||
if (stationId.empty())
|
||||
{
|
||||
@@ -797,7 +809,7 @@ Errcode HttpEntity::queryDeviceList(const httplib::Request& req, njson& json, st
|
||||
Errcode HttpEntity::insertDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
||||
GetRequestParams(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
||||
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
||||
|
||||
Errcode err = DAO::insertDevice(params);
|
||||
@@ -813,7 +825,7 @@ Errcode HttpEntity::insertDevice(const httplib::Request& req, njson& json, std::
|
||||
Errcode HttpEntity::updateDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"device_id", "station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
||||
GetRequestParams(req, {"device_id", "station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
||||
|
||||
Errcode err = DAO::updateDeviceById(params);
|
||||
if (err == Errcode::OK)
|
||||
@@ -840,7 +852,7 @@ Errcode HttpEntity::queryDevicTypeDef(const httplib::Request& req, njson& json,
|
||||
Errcode HttpEntity::queryDevicByCategory(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category"}, params);
|
||||
GetRequestParams(req, {"station_id", "category"}, params);
|
||||
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
||||
if (!params.contains("category")) { errmsg = "缺少参数[category]"; return Errcode::ERR_PARAM; }
|
||||
|
||||
@@ -902,7 +914,7 @@ Errcode HttpEntity::queryDevicByCategory(const httplib::Request& req, njson& jso
|
||||
Errcode HttpEntity::queryDevicCharts(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "device_id"}, params);
|
||||
GetRequestParams(req, {"station_id", "device_id"}, params);
|
||||
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
||||
if (!params.contains("device_id")) { errmsg = "缺少参数[device_id]"; return Errcode::ERR_PARAM; }
|
||||
|
||||
@@ -924,7 +936,7 @@ Errcode HttpEntity::queryDevicCharts(const httplib::Request& req, njson& json, s
|
||||
Errcode HttpEntity::queryDeviceBCUDetail(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "device_id"}, params);
|
||||
GetRequestParams(req, {"station_id", "device_id"}, params);
|
||||
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
||||
if (!params.contains("device_id")) { errmsg = "缺少参数[device_id]"; return Errcode::ERR_PARAM; }
|
||||
|
||||
@@ -964,19 +976,19 @@ Errcode HttpEntity::queryPolicyList(const httplib::Request& req, njson& json, st
|
||||
Errcode HttpEntity::insertPolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"type", "name", "describe", "value", "is_open"}, params);
|
||||
GetRequestParams(req, {"type", "name", "describe", "value", "is_open"}, params);
|
||||
return DAO::insertPolicy(params);
|
||||
};
|
||||
Errcode HttpEntity::updatePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"policy_id", "type", "describe", "value", "is_open"}, params);
|
||||
GetRequestParams(req, {"policy_id", "type", "describe", "value", "is_open"}, params);
|
||||
return DAO::updatePolicyById(params);
|
||||
};
|
||||
Errcode HttpEntity::deletePolicy(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"policy_id"}, params);
|
||||
GetRequestParams(req, {"policy_id"}, params);
|
||||
return DAO::deletePolicyById(params.value("policy_id"));
|
||||
};
|
||||
Errcode HttpEntity::queryPolicyByType(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
@@ -1004,7 +1016,7 @@ Errcode HttpEntity::querySystemLogList(const httplib::Request& req, njson& json,
|
||||
Errcode HttpEntity::updateSystemLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"log_id", "status"}, params);
|
||||
GetRequestParams(req, {"log_id", "status"}, params);
|
||||
return DAO::updateSystemLogById(params);
|
||||
}
|
||||
|
||||
@@ -1024,7 +1036,7 @@ Errcode HttpEntity::queryAlertLogList(const httplib::Request& req, njson& json,
|
||||
Errcode HttpEntity::updateAlertLog(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"log_id", "status"}, params);
|
||||
GetRequestParams(req, {"log_id", "status"}, params);
|
||||
return DAO::updateAlertLogById(params);
|
||||
}
|
||||
|
||||
@@ -1159,7 +1171,7 @@ static std::string VerifyStatSqlCondition(Fields& params)
|
||||
static std::string GetRequestStatParams(const httplib::Request& req, Fields& params)
|
||||
{
|
||||
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
GetRequestParams(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
VerifyRequstParamsStatDate(params);
|
||||
return VerifyStatSqlCondition(params);
|
||||
}
|
||||
@@ -1226,7 +1238,7 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
GetRequestParams(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
std::string stationId = params.value("station_id");
|
||||
std::string category = params.value("category");
|
||||
std::string startDate = params.value("start_date");
|
||||
@@ -1304,7 +1316,7 @@ Errcode HttpEntity::queryStatDetailList(const httplib::Request& req, njson& json
|
||||
pageinfo.size = Utils::toInt(req.get_param_value("page_size"));
|
||||
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
GetRequestParams(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
|
||||
std::vector<Fields> result;
|
||||
auto err = DAO::queryStatStationList(pageinfo, params, result);
|
||||
@@ -1373,6 +1385,79 @@ Errcode HttpEntity::queryStatCharts(const httplib::Request& req, njson& json, st
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
|
||||
Errcode HttpEntity::exportStatReport(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParams(req, {"station_id", "category", "start_date", "end_date"}, params);
|
||||
CheckRequiredParams(params, {"station_id", "category", "start_date", "end_date"}, errmsg);
|
||||
if (!errmsg.empty())
|
||||
{
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
|
||||
std::string stationId = params.value("station_id");
|
||||
std::string category = params.value("category");
|
||||
std::string startDate = params.value("start_date");
|
||||
std::string endDate = params.value("end_date");
|
||||
|
||||
std::string sql = "SELECT s.name station_name, sd.* FROM stat_day sd LEFT JOIN station s ON sd.station_id=s.station_id";
|
||||
" WHERE station_id = '" + stationId + "' AND category = '" + category + "'"
|
||||
" AND dt BETWEEN '" + startDate + "' AND '" + endDate + "2025-09-19' AND sd.category = '1'";
|
||||
|
||||
std::string filename;
|
||||
std::vector<Fields> result;
|
||||
Errcode err = DAO::exec(NULL, sql, result);
|
||||
if (result.size() > 0)
|
||||
{
|
||||
filename = "export_" + Utils::timeStr(0, "%Y%m%d%H%M%S") + ".xlsx";
|
||||
XlsxEntity xlsx(Config::option.exportpath + "/" + filename);
|
||||
std::vector<std::pair<std::string, std::string>> headers;
|
||||
if (category == "1")
|
||||
{
|
||||
headers = {
|
||||
{"日期", "dt"}, {"场站", "station_name"}, {"类别", "category"},
|
||||
{"充电电量", "storage_elect_in"}, {"放电电量", "storage_elect_out"},
|
||||
{"充电次数", "storage_num_in"}, {"放电次数", "storage_num_out"},
|
||||
{"充电时长", "storage_t_in"}, {"放电时长", "storage_t_out"}
|
||||
};
|
||||
}
|
||||
else if (category == "2")
|
||||
{
|
||||
headers = {
|
||||
{"日期", "dt"}, {"场站", "station_name"}, {"类别", "category"},
|
||||
{"充电电量", "charge_elect"}, {"充电次数", "charge_num"}, {"充电时长", "charge_t"}
|
||||
};
|
||||
}
|
||||
else if (category == "3")
|
||||
{
|
||||
headers = {
|
||||
{"日期", "dt"}, {"场站", "station_name"}, {"类别", "category"},
|
||||
{"发电电量", "solar_elect_gen"}, {"入网电量", "solar_elect_grid"}, {"发电时长", "solar_t"},
|
||||
};
|
||||
}
|
||||
for (int row = 0; row<result.size(); ++row)
|
||||
{
|
||||
auto& fields = result[row];
|
||||
if (category == "1") { fields.value("category") = "储能设备"; }
|
||||
else if (category == "2") { fields.value("category") = "充电设备"; }
|
||||
else if (category == "3") { fields.value("category") = "发电设备"; }
|
||||
|
||||
for (int col = 0; col<headers.size(); ++col)
|
||||
{
|
||||
if (row == 0)
|
||||
{
|
||||
xlsx.setCell(row+1, col+1, headers[col].first);
|
||||
}
|
||||
xlsx.setCell(row+2, col+1, fields.value(headers[col].second));
|
||||
}
|
||||
}
|
||||
xlsx.close();
|
||||
}
|
||||
json["data"] = filename;
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryEnvironment(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string stationId = req.get_param_value("station_id");
|
||||
@@ -1480,7 +1565,7 @@ Errcode HttpEntity::queryServiceApiList(const httplib::Request& req, njson& json
|
||||
Errcode HttpEntity::insertServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"name", "describe", "params", "is_open"}, params);
|
||||
GetRequestParams(req, {"name", "describe", "params", "is_open"}, params);
|
||||
if (params.size() == 0) { return Errcode::ERR_PARAM; }
|
||||
|
||||
return DAO::insert(NULL, "serviceapi", params);
|
||||
@@ -1489,7 +1574,7 @@ Errcode HttpEntity::insertServiceApi(const httplib::Request& req, njson& json, s
|
||||
Errcode HttpEntity::updateServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"api_id", "name", "describe", "params", "is_open"}, params);
|
||||
GetRequestParams(req, {"api_id", "name", "describe", "params", "is_open"}, params);
|
||||
if (params.size() == 0) { return Errcode::ERR_PARAM; }
|
||||
|
||||
return DAO::update(NULL, "serviceapi", params, "api_id");
|
||||
@@ -1498,6 +1583,6 @@ Errcode HttpEntity::updateServiceApi(const httplib::Request& req, njson& json, s
|
||||
Errcode HttpEntity::deleteServiceApi(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"api_id"}, params);
|
||||
GetRequestParams(req, {"api_id"}, params);
|
||||
return DAO::remove(NULL, "serviceapi", "api_id", params.value("api_id"));
|
||||
}
|
||||
Reference in New Issue
Block a user