mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
实现削峰套利策略的编辑页面
This commit is contained in:
@@ -1,22 +1,7 @@
|
||||
#include "Fields.h"
|
||||
#include "common/Utils.h"
|
||||
|
||||
void Fields::set(string key, string val)
|
||||
{
|
||||
mapFields[key] = val;
|
||||
}
|
||||
void Fields::set(string key, float val)
|
||||
{
|
||||
mapFields[key] = std::to_string(val);
|
||||
}
|
||||
void Fields::set(string key, int val)
|
||||
{
|
||||
mapFields[key] = std::to_string(val);
|
||||
}
|
||||
void Fields::set(string key, int64_t val)
|
||||
{
|
||||
mapFields[key] = std::to_string(val);
|
||||
}
|
||||
|
||||
std::string& Fields::value(std::string key)
|
||||
{
|
||||
static std::string tmp;
|
||||
@@ -24,27 +9,12 @@ std::string& Fields::value(std::string key)
|
||||
return (it != mapFields.end()) ? it->second : (tmp = "");
|
||||
}
|
||||
|
||||
//string Fields::getStr(string key)
|
||||
//{
|
||||
// return (mapFields.count(key) > 0) ? mapFields[key] : "";
|
||||
//}
|
||||
|
||||
int Fields::getInt(string key)
|
||||
bool Fields::contains(std::string key)
|
||||
{
|
||||
return mapFields.count(key) > 0 ? Utils::toInt(mapFields[key]) : 0;
|
||||
return (mapFields.find(key) != mapFields.end());
|
||||
}
|
||||
|
||||
float Fields::getFloat(string key)
|
||||
{
|
||||
return mapFields.count(key) > 0 ? Utils::toFloat(mapFields[key]) : 0.0f;
|
||||
}
|
||||
|
||||
double Fields::getDouble(string key)
|
||||
{
|
||||
return mapFields.count(key) > 0 ? Utils::toDouble(mapFields[key]) : 0.0;
|
||||
}
|
||||
|
||||
std::map<string, string>::iterator Fields::remove(string key)
|
||||
std::unordered_map<string, string>::iterator Fields::remove(string key)
|
||||
{
|
||||
auto it = mapFields.find(key);
|
||||
if (it != mapFields.end())
|
||||
@@ -53,19 +23,36 @@ std::map<string, string>::iterator Fields::remove(string key)
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
void Fields::append(Fields& datafield)
|
||||
{
|
||||
auto& map_f = datafield.fields();
|
||||
for (auto it = map_f.begin(); it != map_f.end(); it++)
|
||||
auto& mapVal = datafield.map();
|
||||
for (auto it = mapVal.begin(); it != mapVal.end(); it++)
|
||||
{
|
||||
mapFields[it->first] = it->second;
|
||||
}
|
||||
}
|
||||
map<string, string>& Fields::fields()
|
||||
|
||||
std::unordered_map<string, string>& Fields::map()
|
||||
{
|
||||
return mapFields;
|
||||
}
|
||||
|
||||
int Fields::size()
|
||||
{
|
||||
return mapFields.size();
|
||||
}
|
||||
|
||||
bool Fields::isEmpty()
|
||||
{
|
||||
return mapFields.empty();
|
||||
}
|
||||
|
||||
void Fields::clear()
|
||||
{
|
||||
mapFields.clear();
|
||||
}
|
||||
|
||||
void Fields::check(string key, string val, string d)
|
||||
{
|
||||
if (mapFields.count(key) > 0 && mapFields[key] == val)
|
||||
@@ -74,84 +61,6 @@ void Fields::check(string key, string val, string d)
|
||||
}
|
||||
}
|
||||
|
||||
string Fields::get_insert_sql(string tbname)
|
||||
{
|
||||
string key;
|
||||
string val;
|
||||
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
||||
{
|
||||
if (!key.empty())
|
||||
{
|
||||
key += ",";
|
||||
val += ",";
|
||||
}
|
||||
key += ("`" + it->first + "`");
|
||||
if (it->second == "null" || it->second == "NULL")
|
||||
{
|
||||
val += "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
val += ("'" + it->second + "'");
|
||||
}
|
||||
}
|
||||
return "INSERT INTO `" + tbname + "` (" + key + ") VALUES(" + val + ");";
|
||||
}
|
||||
|
||||
string Fields::get_update_sql(string tbname, string sql_c)
|
||||
{
|
||||
ostringstream oss;
|
||||
oss << "update " << tbname << " set ";
|
||||
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
||||
{
|
||||
if (iter != mapFields.begin())
|
||||
{
|
||||
oss << ",";
|
||||
};
|
||||
oss << "`" << iter->first << "`=";
|
||||
if (iter->second == "null" || iter->second == "NULL")
|
||||
{
|
||||
oss << "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "'" << iter->second << "'";
|
||||
}
|
||||
}
|
||||
oss << " " << sql_c << ";";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
string Fields::get_update_sql(string tbname, std::vector<std::string> vec_keys, string sql_c)
|
||||
{
|
||||
std::map<std::string, bool> map_keys;
|
||||
for (auto& k : vec_keys) { map_keys[k] = true; }
|
||||
|
||||
ostringstream oss;
|
||||
oss << "update " << tbname << " set ";
|
||||
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
||||
{
|
||||
auto& k = iter->first;
|
||||
auto& v = iter->second;
|
||||
if (!map_keys[k]) { continue; }
|
||||
if (iter != mapFields.begin())
|
||||
{
|
||||
oss << ",";
|
||||
};
|
||||
oss << "`" << k << "`=";
|
||||
if (v == "null" || v == "NULL")
|
||||
{
|
||||
oss << "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "'" << v << "'";
|
||||
}
|
||||
}
|
||||
oss << " " << sql_c << ";";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void Fields::foreachItem(function<void(string key, string& val)> onForaach)
|
||||
{
|
||||
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
||||
@@ -162,11 +71,6 @@ void Fields::foreachItem(function<void(string key, string& val)> onForaach)
|
||||
}
|
||||
}
|
||||
}
|
||||
bool Fields::isEmpty(string key)
|
||||
{
|
||||
auto& s = mapFields[key];
|
||||
return s.empty();
|
||||
}
|
||||
|
||||
bool Fields::is_float_number(string key)
|
||||
{
|
||||
@@ -195,19 +99,84 @@ string Fields::toStr()
|
||||
return s;
|
||||
}
|
||||
|
||||
int Fields::size()
|
||||
string Fields::toSqlInsert(string tableName)
|
||||
{
|
||||
return mapFields.size();
|
||||
string key;
|
||||
string val;
|
||||
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
||||
{
|
||||
if (!key.empty())
|
||||
{
|
||||
key += ",";
|
||||
val += ",";
|
||||
}
|
||||
key += ("`" + it->first + "`");
|
||||
if (it->second == "null" || it->second == "NULL")
|
||||
{
|
||||
val += "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
val += ("'" + it->second + "'");
|
||||
}
|
||||
}
|
||||
return "INSERT INTO `" + tableName + "` (" + key + ") VALUES(" + val + ");";
|
||||
}
|
||||
|
||||
void Fields::clear()
|
||||
string Fields::toSqlUpdate(string tableName, string sql_c)
|
||||
{
|
||||
mapFields.clear();
|
||||
ostringstream oss;
|
||||
oss << "update " << tableName << " set ";
|
||||
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
||||
{
|
||||
if (iter != mapFields.begin())
|
||||
{
|
||||
oss << ",";
|
||||
};
|
||||
oss << "`" << iter->first << "`=";
|
||||
if (iter->second == "null" || iter->second == "NULL")
|
||||
{
|
||||
oss << "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "'" << iter->second << "'";
|
||||
}
|
||||
}
|
||||
oss << " " << sql_c << ";";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
bool Fields::hasKey(std::string key)
|
||||
string Fields::toSqlUpdate(string tableName, std::vector<std::string> vecKeys, string condition)
|
||||
{
|
||||
auto iter = mapFields.find(key);
|
||||
return (iter != mapFields.end());
|
||||
std::map<std::string, bool> map_keys;
|
||||
for (auto& k : vecKeys) { map_keys[k] = true; }
|
||||
|
||||
ostringstream oss;
|
||||
oss << "update " << tableName << " set ";
|
||||
for (auto iter = mapFields.begin(); iter != mapFields.end(); iter++)
|
||||
{
|
||||
auto& k = iter->first;
|
||||
auto& v = iter->second;
|
||||
if (!map_keys[k]) { continue; }
|
||||
if (iter != mapFields.begin())
|
||||
{
|
||||
oss << ",";
|
||||
};
|
||||
oss << "`" << k << "`=";
|
||||
if (v == "null" || v == "NULL")
|
||||
{
|
||||
oss << "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << "'" << v << "'";
|
||||
}
|
||||
}
|
||||
oss << " " << condition << ";";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#ifndef _Fields_H_
|
||||
#define _Fields_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
using namespace std;
|
||||
@@ -18,33 +19,29 @@ struct PageInfo
|
||||
class Fields
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* 设置索引名称和值
|
||||
* @param: [string key] 索引名称
|
||||
* @param: [string val] 值
|
||||
*/
|
||||
void set(string key, string val);
|
||||
template <typename T>
|
||||
void set(string key, T val, int precision=6)
|
||||
{
|
||||
stringstream ss;
|
||||
ss.precision(precision);
|
||||
ss << val;
|
||||
mapFields[key] = ss.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置索引名称和值
|
||||
* @param: [string key] 索引名称
|
||||
* @param: [float val] 值
|
||||
*/
|
||||
void set(string key, float val);
|
||||
template <typename T>
|
||||
T get(string key, int precision = 6)
|
||||
{
|
||||
T val {};
|
||||
auto iter = mapFields.find(key);
|
||||
if (iter != mapFields.end())
|
||||
{
|
||||
stringstream ss(iter->second);
|
||||
ss.precision(precision);
|
||||
ss >> val;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置索引名称和值
|
||||
* @param: [string key] 索引名称
|
||||
* @param: [int val] 值
|
||||
*/
|
||||
void set(string key, int val);
|
||||
|
||||
/**
|
||||
* 设置索引名称和值
|
||||
* @param: [string key] 索引名称
|
||||
* @param: [int64_t val] 值
|
||||
*/
|
||||
void set(string key, int64_t val);
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
@@ -53,28 +50,17 @@ public:
|
||||
std::string& value(std::string key);
|
||||
|
||||
/**
|
||||
* 获取 int 值
|
||||
* 是否包含 key
|
||||
* @param: [string key] 索引名称
|
||||
* @return: true: 包含; false:不包含
|
||||
*/
|
||||
int getInt(string key);
|
||||
bool contains(std::string key);
|
||||
|
||||
/**
|
||||
* 获取 float 值
|
||||
* @param: [string key] 索引名称
|
||||
*/
|
||||
float getFloat(string key);
|
||||
|
||||
/**
|
||||
* 获取 double 值
|
||||
* @param: [string key] 索引名称
|
||||
*/
|
||||
double getDouble(string key);
|
||||
|
||||
/**
|
||||
* 删除指定索引的值
|
||||
* @param: [string key] 索引名称
|
||||
*/
|
||||
std::map<string, string>::iterator remove(string key);
|
||||
std::unordered_map<string, string>::iterator remove(string key);
|
||||
|
||||
/**
|
||||
* 追加合并
|
||||
@@ -83,9 +69,25 @@ public:
|
||||
void append(Fields& fields);
|
||||
|
||||
/**
|
||||
* 获取数据项map
|
||||
* 获取数据项 map
|
||||
*/
|
||||
map<string, string>& fields();
|
||||
std::unordered_map<string, string>& map();
|
||||
|
||||
/**
|
||||
* 获取数据项的大小
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 判断是否含有数据项
|
||||
* @param: [string key] 索引名称
|
||||
*/
|
||||
bool isEmpty();
|
||||
|
||||
/**
|
||||
* 清空数据项
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* 检查某一数据项的值进行替换,如果该项的值是val,则替换成成d
|
||||
@@ -95,39 +97,12 @@ public:
|
||||
*/
|
||||
void check(string key, string val, string d);
|
||||
|
||||
/**
|
||||
* 转换成插入数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
*/
|
||||
string get_insert_sql(string tableName);
|
||||
|
||||
/**
|
||||
* 转换成更新数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
* @param: [string condition] sql的更新条件,例如: where id='1'
|
||||
*/
|
||||
string get_update_sql(string tableName, string condition);
|
||||
|
||||
|
||||
/**
|
||||
* 转换成更新数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
* @param: [string vecKeys] 需要更新的字段名称
|
||||
* @param: [string condition] sql的更新条件,例如: where id='1'
|
||||
*/
|
||||
string get_update_sql(string tableName, std::vector<std::string> vecKeys, string condition);
|
||||
|
||||
/**
|
||||
* 遍历数据项
|
||||
* @param: [function... onForaach] 回调函数
|
||||
*/
|
||||
void foreachItem(function<void(string key, string& val)> onForaach);
|
||||
|
||||
/**
|
||||
* 判断是否含有数据项
|
||||
* @param: [string key] 索引名称
|
||||
*/
|
||||
bool isEmpty(string key);
|
||||
|
||||
/**
|
||||
* 判断数据项是否是float数值类型
|
||||
@@ -141,20 +116,28 @@ public:
|
||||
string toStr();
|
||||
|
||||
/**
|
||||
* 获取数据项的大小
|
||||
* 转换成插入数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
*/
|
||||
int size();
|
||||
string toSqlInsert(string tableName);
|
||||
|
||||
void clear();
|
||||
/**
|
||||
* 转换成更新数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
* @param: [string condition] sql的更新条件,例如: where id='1'
|
||||
*/
|
||||
string toSqlUpdate(string tableName, string condition);
|
||||
|
||||
bool hasKey(std::string key);
|
||||
|
||||
|
||||
|
||||
std::map<string, string>& map() { return mapFields; }
|
||||
/**
|
||||
* 转换成更新数据的 sql 语句
|
||||
* @param: [string tableName] 数据表名称
|
||||
* @param: [string vecKeys] 需要更新的字段名称
|
||||
* @param: [string condition] sql的更新条件,例如: where id='1'
|
||||
*/
|
||||
string toSqlUpdate(string tableName, std::vector<std::string> vecKeys, string condition);
|
||||
|
||||
private:
|
||||
std::map<string, string> mapFields;
|
||||
std::unordered_map<string, string> mapFields;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,8 +1,9 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
using NJson = nlohmann::json;
|
||||
using NJsonNode = nlohmann::json;
|
||||
|
||||
/// =============================================================================================
|
||||
/// 使用说明:
|
||||
@@ -36,32 +37,60 @@ using NJson = nlohmann::json;
|
||||
/// 序列化为字符串 -------------
|
||||
// std::string json_str = j.dump(4); // 4 表示缩进,美化输出
|
||||
|
||||
static bool NJsonLoad(std::string jsonfile, NJson& json)
|
||||
{
|
||||
std::ifstream ifs(jsonfile);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
ifs >> json;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// 数组解析 -------------
|
||||
// json jArray = json::array()
|
||||
// jArray.push_back(1);
|
||||
// jArray.push_back(2);
|
||||
// json j;
|
||||
// j["data"] = {1,2,3,4,5};
|
||||
// std::vector<int> v1;
|
||||
// v1 = j.at["data"].get<std::vector<int>>()
|
||||
|
||||
static bool NJsonParse(std::string jsonstr, NJson& json)
|
||||
class NJson
|
||||
{
|
||||
try
|
||||
{
|
||||
json = NJson::parse(jsonstr);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& e)
|
||||
public:
|
||||
static bool load(std::string jsonfile, NJsonNode& json)
|
||||
{
|
||||
//std::cerr << "JSON 解析错误: " << e.what() << std::endl;
|
||||
std::ifstream ifs(jsonfile);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
ifs >> json;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool NJsonLHas(NJson& json, std::string key)
|
||||
{
|
||||
return json.contains("database");
|
||||
}
|
||||
static bool parse(std::string jsonstr, NJsonNode& json)
|
||||
{
|
||||
try
|
||||
{
|
||||
json = NJsonNode::parse(jsonstr);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& e)
|
||||
{
|
||||
std::cout << "JSON parse error: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool contains(NJsonNode& json, std::string key)
|
||||
{
|
||||
return json.contains("database");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void read(NJsonNode& json, std::string k, T& v)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (json.contains(k)) { v = json.at(k).get<T>(); }
|
||||
}
|
||||
catch (const nlohmann::detail::exception& e)
|
||||
{
|
||||
std::cout << "JSON read error: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -435,7 +435,7 @@ void Utils::split(string buf, string c, vector<string>& res)
|
||||
string tmp = buf;
|
||||
while (true)
|
||||
{
|
||||
auto pos = tmp.find(c);
|
||||
auto pos = tmp.find_first_of(c);
|
||||
if (pos == string::npos)
|
||||
{
|
||||
res.push_back(tmp);
|
||||
|
||||
Reference in New Issue
Block a user