mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
修改HTTP接口测试问题
This commit is contained in:
@@ -39,8 +39,8 @@ static void JsonToFields(njson& json, std::vector<std::string> vecKeys, Fields&
|
||||
case njson::value_t::number_unsigned: { fields.set(key, json[key].get<int>()); } break;
|
||||
case njson::value_t::number_float: { fields.set(key, json[key].get<float>()); } break;
|
||||
case njson::value_t::null: {} break;
|
||||
case njson::value_t::object: {} break;
|
||||
case njson::value_t::array: {} break;
|
||||
case njson::value_t::object: { fields.set(key, json[key].dump()); } break;
|
||||
case njson::value_t::array: { fields.set(key, json[key].dump()); } break;
|
||||
case njson::value_t::binary: {} break;
|
||||
case njson::value_t::discarded: {} break;
|
||||
default:
|
||||
@@ -165,6 +165,7 @@ static std::map<std::string, HandlerOptions> g_mapHttpHandlerGet =
|
||||
{"/queryPredictionDetail", HandlerOptions(&HttpEntity::queryPredictionDetail, {})},
|
||||
|
||||
{"/queryStatSystem", HandlerOptions(&HttpEntity::queryStatSystem, {})},
|
||||
{"/queryStatStation", HandlerOptions(&HttpEntity::queryStatStation, {})},
|
||||
{"/queryStatTotal", HandlerOptions(&HttpEntity::queryStatTotal, {})},
|
||||
{"/queryStatDayList", HandlerOptions(&HttpEntity::queryStatDayList, {})},
|
||||
{"/queryStatCharts", HandlerOptions(&HttpEntity::queryStatCharts, {})},
|
||||
@@ -390,6 +391,38 @@ Errcode HttpEntity::queryPermissionList(const httplib::Request& req, njson& json
|
||||
std::vector<Fields> result;
|
||||
auto err = DAO::queryPermissionList(pageinfo, result);
|
||||
HttpHelper::setPagination(pageinfo, result, json);
|
||||
|
||||
// 查询所有的角色权限关联
|
||||
if (err == Errcode::OK)
|
||||
{
|
||||
std::map<std::string, int> mapP;
|
||||
for (int i = 0; i<result.size(); ++i)
|
||||
{
|
||||
auto& item = result[i];
|
||||
std::string id = item.value("permission_id");
|
||||
mapP[id] = json["data"].size() - 1;
|
||||
}
|
||||
|
||||
std::vector<Fields> vecSubPermission;
|
||||
DaoEntity::execOnce("SELECT * FROM permission WHERE permission.parent_id IS NOT NULL AND permission.parent_id!='';", vecSubPermission);
|
||||
|
||||
for (int i = 0; i<vecSubPermission.size(); ++i)
|
||||
{
|
||||
auto& item = vecSubPermission[i];
|
||||
std::string parentId = item.value("parent_id");
|
||||
std::string id = item.value("permission_id");
|
||||
if (!parentId.empty())
|
||||
{
|
||||
if (mapP.count(parentId) > 0)
|
||||
{
|
||||
int index = mapP[parentId];
|
||||
njson jsonnode;
|
||||
FieldsToJson(item, jsonnode);
|
||||
json["data"][index]["children"].push_back(jsonnode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -423,6 +456,8 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std:
|
||||
|
||||
std::vector<Fields> result;
|
||||
auto err = DAO::queryRoleList(pageinfo, result);
|
||||
HttpHelper::setPagination(pageinfo, result, json);
|
||||
|
||||
// 查询所有的角色权限关联
|
||||
if (err == Errcode::OK)
|
||||
{
|
||||
@@ -432,8 +467,8 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std:
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<Fields>> mapP;
|
||||
|
||||
for (int i = 0; i<vecPermission.size(); ++i)
|
||||
{
|
||||
auto& item = vecPermission[i];
|
||||
@@ -444,7 +479,6 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std:
|
||||
mapP[roleId].push_back(vecPermission[i]);
|
||||
}
|
||||
|
||||
HttpHelper::setPagination(pageinfo, result, json);
|
||||
for (auto& item : json["data"])
|
||||
{
|
||||
auto jsonpermission = njson::array();
|
||||
@@ -487,6 +521,14 @@ Errcode HttpEntity::queryRoleList(const httplib::Request& req, njson& json, std:
|
||||
return err;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryRolePermission(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"role_id"}, params);
|
||||
if (!params.contains("role_id")) { errmsg = "缺少参数[role_id]"; return Errcode::ERR_PARAM; }
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::insertRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
@@ -496,33 +538,34 @@ Errcode HttpEntity::insertRole(const httplib::Request& req, njson& json, std::st
|
||||
Errcode HttpEntity::updateRole(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
Fields params;
|
||||
//GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
||||
|
||||
njson jsonparam;
|
||||
if (!JSON::parse(req.body, jsonparam))
|
||||
{
|
||||
return Errcode::ERR_PARAM;
|
||||
}
|
||||
JsonToFields(jsonparam, {"role_id", "name", "describe", "is_open"}, params);
|
||||
GetRequestParam(req, {"role_id", "name", "describe", "is_open", "permission"}, params);
|
||||
|
||||
auto roleId = params.value("role_id");
|
||||
|
||||
std::string permission = params.remove("permission");
|
||||
|
||||
auto dao = DaoEntity::create("");
|
||||
auto err = DAO::updateRoleById(dao, params);
|
||||
if (err == Errcode::OK && jsonparam.contains("permission"))
|
||||
auto err = Errcode::OK;
|
||||
if (params.size() > 1)
|
||||
{
|
||||
if (jsonparam["permission"].is_array())
|
||||
err = DAO::updateRoleById(dao, params);
|
||||
}
|
||||
if (err == Errcode::OK && !permission.empty())
|
||||
{
|
||||
njson jsonarray;
|
||||
if (JSON::parse(permission, jsonarray))
|
||||
{
|
||||
|
||||
auto& jsonPermission = jsonparam["permission"];
|
||||
std::vector<Fields> vecFields(jsonPermission.size());
|
||||
int i = 0;
|
||||
for (auto& item: jsonPermission)
|
||||
std::vector<Fields> vecFields;
|
||||
for (auto& item: jsonarray)
|
||||
{
|
||||
auto& fields = vecFields[i];
|
||||
i++;
|
||||
JsonToFields(item, {"permission_id", "is_add", "is_del", "is_edit", "is_view"}, fields);
|
||||
Fields fields;
|
||||
fields.set("role_id", roleId);
|
||||
fields.set("permission_id", item["permission_id"].get<std::string>());
|
||||
fields.set("is_open", item["is_open"].get<std::string>());
|
||||
fields.set("is_view", item["is_view"].get<std::string>());
|
||||
fields.set("is_add", item["is_add"].get<std::string>());
|
||||
fields.set("is_edit", item["is_edit"].get<std::string>());
|
||||
fields.set("is_del", item["is_del"].get<std::string>());
|
||||
vecFields.push_back(fields);
|
||||
}
|
||||
err = DAO::updateRolePermission(dao, roleId, vecFields);
|
||||
}
|
||||
@@ -704,13 +747,31 @@ Errcode HttpEntity::insertDevice(const httplib::Request& req, njson& json, std::
|
||||
{
|
||||
Fields params;
|
||||
GetRequestParam(req, {"station_id", "type", "name", "code", "model", "factory", "factory_tel", "is_open", "attrs"}, params);
|
||||
return DAO::insertDevice(params);
|
||||
if (!params.contains("station_id")) { errmsg = "缺少参数[station_id]"; return Errcode::ERR_PARAM; }
|
||||
|
||||
Errcode err = DAO::insertDevice(params);
|
||||
if (err == Errcode::OK)
|
||||
{
|
||||
int stationId = params.get<int>("station_id");
|
||||
auto station = Application::data().getStation(stationId);
|
||||
if (station) { station->addDevice(params); }
|
||||
}
|
||||
return err;
|
||||
};
|
||||
|
||||
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);
|
||||
return DAO::updateDeviceById(params);
|
||||
|
||||
Errcode err = DAO::updateDeviceById(params);
|
||||
if (err == Errcode::OK)
|
||||
{
|
||||
int stationId = params.get<int>("station_id");
|
||||
auto station = Application::data().getStation(stationId);
|
||||
if (station) { station->addDevice(params); }
|
||||
}
|
||||
return err;
|
||||
};
|
||||
Errcode HttpEntity::deleteDevice(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
@@ -906,21 +967,63 @@ Errcode HttpEntity::queryStatSystem(const httplib::Request& req, njson& json, st
|
||||
{
|
||||
auto& appdata = Application::data();
|
||||
|
||||
double incomeTotal {};
|
||||
double station_num = appdata.getStationCount();
|
||||
double solarDeviceNum {};
|
||||
double capacityTotal {};
|
||||
double electGenTotal {};
|
||||
double electGridTotal {};
|
||||
double electStorageIn {};
|
||||
double electStorageOut {};
|
||||
for (auto& item : appdata.mapStation)
|
||||
{
|
||||
auto& station = item.second;
|
||||
solarDeviceNum += station->getDeviceCount(3);
|
||||
capacityTotal += station->capacity;
|
||||
electGenTotal += station->electGenTotal;
|
||||
electGridTotal += station->electGridTotal;
|
||||
electStorageIn += station->electStorageIn;
|
||||
electStorageOut += station->electStorageOut;
|
||||
}
|
||||
|
||||
njson jsondata;
|
||||
jsondata["launch_date"] = appdata.launchDate; //: 系统上线启用日期,格式:yyyy-mm-dd
|
||||
jsondata["income_total"] = std::to_string(Utils::random(100, 200)); // : 累计收益(元),精度0.01
|
||||
jsondata["station_num"] = Utils::toStr(appdata.getStationCount()); // : 能源站数量
|
||||
jsondata["storage_device_num "] = Utils::toStr(appdata.getStationCount()); //: 储能设备数量
|
||||
jsondata["solar_device_num"] = "0"; // : 光伏设备数量
|
||||
jsondata["capacity_total"] = std::to_string(Utils::random(100, 200)); // : 储能总容量(kWh),精度0.001
|
||||
jsondata["solar_elect_gen"] = std::to_string(Utils::random(100, 200)); // : 发电总电量(kWh),精度0.001
|
||||
jsondata["solar_elect_grid"] = std::to_string(Utils::random(100, 200)); // : 入网种电量(kWh),精度0.001
|
||||
jsondata["storage_elect_in"] = std::to_string(Utils::random(100, 200)); // : 储能充电总电量(kWh),精度0.001
|
||||
jsondata["storage_elect_out"] = std::to_string(Utils::random(100, 200)); // : 储能放电总电量(kWh),精度0.001
|
||||
jsondata["income_total"] = incomeTotal; // : 累计收益(元),精度0.01
|
||||
jsondata["station_num"] = station_num; // : 能源站数量
|
||||
jsondata["storage_device_num"] = station_num; //: 储能设备数量
|
||||
jsondata["solar_device_num"] = solarDeviceNum; //: 光伏设备数量
|
||||
jsondata["capacity_total"] = capacityTotal; // : 储能总容量(kWh),精度0.001
|
||||
jsondata["solar_elect_gen"] = electGenTotal; // : 发电总电量(kWh),精度0.001
|
||||
jsondata["solar_elect_grid"] = electGridTotal; // : 入网种电量(kWh),精度0.001
|
||||
jsondata["storage_elect_in"] = electStorageIn; // : 储能充电总电量(kWh),精度0.001
|
||||
jsondata["storage_elect_out"] = electStorageOut; // : 储能放电总电量(kWh),精度0.001
|
||||
|
||||
json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatStation(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
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.usage_rate FROM station s LEFT JOIN
|
||||
(SELECT station_id, SUM(income_elect ) income_elect , SUM(income_charge) income_charge, avg(usage_rate) usage_rate FROM stat_staion GROUP BY station_id) AS ss
|
||||
ON ss.station_id = s.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>("usage_rate");
|
||||
jsondata.push_back(jsonnode);
|
||||
}
|
||||
json["data"] = jsondata;
|
||||
return Errcode(err);
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
@@ -948,48 +1051,61 @@ Errcode HttpEntity::queryStatTotal(const httplib::Request& req, njson& json, std
|
||||
jsondata["income_charge"] = "123.123"; //充电收益(元),精度:0.01
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
Errcode HttpEntity::queryStatDayList(const httplib::Request& req, njson& json, std::string& errmsg)
|
||||
{
|
||||
std::string station_id = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
std::string dt_start = req.get_param_value("start_date");
|
||||
std::string dt_end = req.get_param_value("end_date");
|
||||
|
||||
int64_t t1 = Utils::time(dt_start)/1000;
|
||||
int64_t t2 = Utils::time(dt_end)/1000;
|
||||
|
||||
int64_t tMax = t1+ 86400 * 30;
|
||||
njson jsondata = njson::array();
|
||||
for (int64_t t = t1; t<=t2 && t<=tMax; t += 86400)
|
||||
//std::string stationId = req.get_param_value("station_id");
|
||||
//std::string category = req.get_param_value("category");
|
||||
std::string dtStart = req.get_param_value("start_date");
|
||||
std::string dtEnd = req.get_param_value("end_date");
|
||||
if (dtEnd.empty())
|
||||
{
|
||||
njson jnode;
|
||||
jnode["station_id"] = station_id;
|
||||
if (!category.empty()) jnode["category"] = category;
|
||||
jnode["dt"] = Utils::dateStr(t*1000); //日期
|
||||
jnode["storage_elect_in"] = std::to_string(Utils::random(100, 200)); //储能充电电量(kWh),精度:0.001
|
||||
jnode["storage_elect_out"] = std::to_string(Utils::random(100, 200)); //储能放电电量(kWh),精度:0.001
|
||||
jnode["storage_num_in"] = std::to_string(Utils::random(1,5)); //储能设备充电次数
|
||||
jnode["storage_num_out"] = std::to_string(Utils::random(1, 5)); //储能设备放电次数
|
||||
jnode["storage_num_err"] = std::to_string(Utils::random(1, 5)); //储能设备故障次数
|
||||
jnode["solar_elect_gen"] = std::to_string(Utils::random(100, 200)); //光伏发电电量(kWh),精度:0.001
|
||||
jnode["solar_elect_grid "] = std::to_string(Utils::random(100, 200)); //光伏入网电量(kWh),精度:0.001
|
||||
jnode["solar_num_err"] = std::to_string(Utils::random(1, 5)); //光伏设备故障次数
|
||||
jnode["charge_elect"] = std::to_string(Utils::random(100, 200)); //充电设备充电电量(kWh),精度:0.001
|
||||
jnode["charge_num"] = std::to_string(Utils::random(1, 5)); //充电设备充电次数
|
||||
jnode["charge_num_err"] = std::to_string(Utils::random(1, 5)); //充电设备故障次数
|
||||
jnode["income_elect"] = std::to_string(Utils::random(100, 200)); //发电收益(元),精度:0.01
|
||||
jnode["income_charge"] = std::to_string(Utils::random(100, 200)); //充电收益(元),精度:0.01
|
||||
jnode["usage_rate"] = std::to_string(Utils::random(10, 50)); //利用率
|
||||
jsondata.push_back(jnode);
|
||||
if (dtStart.empty())
|
||||
{
|
||||
dtEnd = Utils::dateStr();
|
||||
dtStart = Utils::dateStr(Utils::date() - 86400*7);
|
||||
}
|
||||
else
|
||||
{
|
||||
dtEnd = Utils::dateStr(Utils::time(dtStart + " 00:00:00") + 86400*7);
|
||||
}
|
||||
}
|
||||
json["data"] = jsondata;
|
||||
|
||||
std::vector<Fields> result;
|
||||
Errcode err = DAO::queryStatDataList(NULL, dtStart, dtEnd, result);
|
||||
json["data"] = FieldsToJsonArray(result);
|
||||
return err;
|
||||
|
||||
//int64_t t1 = Utils::time(dtStart);
|
||||
//int64_t t2 = Utils::time(dtEnd);
|
||||
//int64_t tMax = t1+ 86400 * 30;
|
||||
//njson jsondata = njson::array();
|
||||
//for (int64_t t = t1; t<=t2 && t<=tMax; t += 86400)
|
||||
//{
|
||||
// njson jnode;
|
||||
// jnode["station_id"] = station_id;
|
||||
// if (!category.empty()) jnode["category"] = category;
|
||||
// jnode["dt"] = Utils::dateStr(t); //日期
|
||||
// jnode["storage_elect_in"] = std::to_string(Utils::random(100, 200)); //储能充电电量(kWh),精度:0.001
|
||||
// jnode["storage_elect_out"] = std::to_string(Utils::random(100, 200)); //储能放电电量(kWh),精度:0.001
|
||||
// jnode["storage_num_in"] = std::to_string(Utils::random(1,5)); //储能设备充电次数
|
||||
// jnode["storage_num_out"] = std::to_string(Utils::random(1, 5)); //储能设备放电次数
|
||||
// jnode["storage_num_err"] = std::to_string(Utils::random(1, 5)); //储能设备故障次数
|
||||
// jnode["solar_elect_gen"] = std::to_string(Utils::random(100, 200)); //光伏发电电量(kWh),精度:0.001
|
||||
// jnode["solar_elect_grid "] = std::to_string(Utils::random(100, 200)); //光伏入网电量(kWh),精度:0.001
|
||||
// jnode["solar_num_err"] = std::to_string(Utils::random(1, 5)); //光伏设备故障次数
|
||||
// jnode["charge_elect"] = std::to_string(Utils::random(100, 200)); //充电设备充电电量(kWh),精度:0.001
|
||||
// jnode["charge_num"] = std::to_string(Utils::random(1, 5)); //充电设备充电次数
|
||||
// jnode["charge_num_err"] = std::to_string(Utils::random(1, 5)); //充电设备故障次数
|
||||
// jnode["income_elect"] = std::to_string(Utils::random(100, 200)); //发电收益(元),精度:0.01
|
||||
// jnode["income_charge"] = std::to_string(Utils::random(100, 200)); //充电收益(元),精度:0.01
|
||||
// jnode["usage_rate"] = std::to_string(Utils::random(10, 50)); //利用率
|
||||
// jsondata.push_back(jnode);
|
||||
//}
|
||||
//json["data"] = jsondata;
|
||||
return Errcode::OK;
|
||||
}
|
||||
|
||||
@@ -999,10 +1115,11 @@ Errcode HttpEntity::queryStatCharts(const httplib::Request& req, njson& json, st
|
||||
std::string stationId = req.get_param_value("station_id");
|
||||
std::string category = req.get_param_value("category");
|
||||
|
||||
if (dt.empty()) { errmsg = "参数[dt]错误"; return Errcode::ERR_PARAM; }
|
||||
if (stationId.empty()) { errmsg = "参数[station_id]错误"; return Errcode::ERR_PARAM; }
|
||||
if (category.empty()) { errmsg = "参数[category]错误"; return Errcode::ERR_PARAM; }
|
||||
|
||||
if (dt.empty()) { dt=Utils::dateStr(); }
|
||||
|
||||
njson jsondata;
|
||||
|
||||
std::string sql = R"(SELECT hd.*, d.`type` device_type, ddt.category FROM history_day hd
|
||||
|
||||
Reference in New Issue
Block a user