mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现系统管理表格操作接口、分页操作
This commit is contained in:
213
src/common/Fields.cpp
Normal file
213
src/common/Fields.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
#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;
|
||||
auto it = mapFields.find(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)
|
||||
{
|
||||
return mapFields.count(key) > 0 ? Utils::toInt(mapFields[key]) : 0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
auto it = mapFields.find(key);
|
||||
if (it != mapFields.end())
|
||||
{
|
||||
it = mapFields.erase(it);
|
||||
}
|
||||
return it;
|
||||
}
|
||||
void Fields::append(Fields& datafield)
|
||||
{
|
||||
auto& map_f = datafield.fields();
|
||||
for (auto it = map_f.begin(); it != map_f.end(); it++)
|
||||
{
|
||||
mapFields[it->first] = it->second;
|
||||
}
|
||||
}
|
||||
map<string, string>& Fields::fields()
|
||||
{
|
||||
return mapFields;
|
||||
}
|
||||
|
||||
void Fields::check(string key, string val, string d)
|
||||
{
|
||||
if (mapFields.count(key) > 0 && mapFields[key] == val)
|
||||
{
|
||||
mapFields[key] = 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++)
|
||||
{
|
||||
if (onForaach)
|
||||
{
|
||||
onForaach(it->first, it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool Fields::isEmpty(string key)
|
||||
{
|
||||
auto& s = mapFields[key];
|
||||
return s.empty();
|
||||
}
|
||||
|
||||
bool Fields::is_float_number(string key)
|
||||
{
|
||||
auto& s = mapFields[key];
|
||||
if (s.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (auto& c : s)
|
||||
{
|
||||
if (std::isdigit(c) == 0 && c != '.')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
string Fields::toStr()
|
||||
{
|
||||
string s;
|
||||
for (auto it = mapFields.begin(); it != mapFields.end(); it++)
|
||||
{
|
||||
s += ("{" + it->first + ":" + it->second + "} ");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int Fields::size()
|
||||
{
|
||||
return mapFields.size();
|
||||
}
|
||||
|
||||
void Fields::clear()
|
||||
{
|
||||
mapFields.clear();
|
||||
}
|
||||
|
||||
bool Fields::hasKey(std::string key)
|
||||
{
|
||||
auto iter = mapFields.find(key);
|
||||
return (iter != mapFields.end());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user