#include "DaoSys.h"
#include "common/Utils.h"
#include "common/Snowflake.h"
#include "Admin.h"
#include "Spdlogger.h"

#include "database/DaoEntity.h"
#include "database/dao/DaoDevice.h"
#include "database/dao/DaoCharge.h"

static void LogDaoResult(std::vector<DataFields>& vec_fields)
{
    for (int i = 0; i < vec_fields.size(); i++)
    {
        string s = "{";
        vec_fields[i].foreach_item([&](string key, string val)
            {
                s += (key + ":" + val + ", ");
            });
        s = s.substr(0, s.size() - 2);
        s += "}";
    }
}

int DaoSys::insert_price(DataFields& fields)
{
    fields.remove(DMPrice::FID_CREATE_TIME);
    fields.remove(DMPrice::FID_UPDATE_TIME);
    DaoEntity dao(DMPrice::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoSys::query_price(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMPrice::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoSys::update_price(DataFields& fields, const string& sql_c)
{
    fields.remove(DMPrice::FID_ID);
    fields.remove(DMPrice::FID_CREATE_TIME);
    fields.remove(DMPrice::FID_UPDATE_TIME);
    DaoEntity dao(DMPrice::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

bool DaoSys::update_price_attr(vector<DataFields>& vec_fields)
{
    DaoEntity dao(DMPriceAttr::TABLENAME);
    bool ret = true;
    std::vector<DataFields> result;
    string sql = "";
    for (auto& item : vec_fields)
    {
        item.remove("id");
        string price_id = item.get_str(DMPriceAttr::FID_PRICE_ID);
        if (!sql.empty())
        {
            sql += ",";
        }
        sql += ("'" + price_id + "'");
    }
    sql = "delete from " + DMPriceAttr::TABLENAME + " where " + DMPriceAttr::FID_PRICE_ID + " in (" + sql + ")";
    dao.exec(sql);
    dao.insert_fields(vec_fields);
    return ret;
}






void DaoSys::insert_sec_warn(DataFields& fields)
{
    // 生成告警示例
    //DaoSecLog::DataModel d;
    //d.id = Snowflake::instance().nextIdString(); // ID
    //d.ts; // 告警时间
    //d.type; // 告警类型:  1:站控系统类 2:换电设备类 3:充电机类 4:辅助设备类
    //d.level = 1; // 告警级别: L1 ~ L4
    //d.content; // 告警内容
    //d.device_id; // 设备ID
    //d.status = 1; // 当前状态

    DaoEntity dao(DMSecRecord::TABLENAME);
    dao.insert_fields(fields);
}

int DaoSys::update_sec_warn(string id, int status)
{
    DataFields fields;
    //d.add(DaoSecLog::FID_ID, "");
    fields.set(DMSecRecord::DISPOSE_USER, Admin::instance().optor().name);
    fields.set(DMSecRecord::DISPOSE_TIME, Utils::time_now_string());
    fields.set(DMSecRecord::STATUS, to_string(status)); // 当前状态  0: 已复位  1: 未处理  2: --  
    DaoEntity dao(DMSecRecord::TABLENAME);
    return dao.update_fields(fields, "where id='" + id + "'");
}



int DaoSys::insert_message_log(DataFields& fields)
{
    string date;
    string time = fields.get_str(DMMessageLog::FID_CREATE_TIME);
    if (time.size() < 10)
    {
        Spdlogger::error("[DB] Message log time error: time={}, set table name date={}", time, date);
        date = Utils::time_now_string().substr(0, 10);
    }
    else
    {
        date = time.substr(0, 10);
    }
    string tbname = DMMessageLog::TABLENAME + date;
    string dll_sql = "CREATE TABLE  IF NOT EXISTS `" + tbname + "` " + DMMessageLog::DDL;

    bool ret = DaoEntity::exec_once(dll_sql.c_str());
    if (!ret)
    {
        Spdlogger::error("[DB] Message log create table failed: {}", dll_sql);
        return 0;
    }
    DaoEntity::exec_once(fields.get_insert_sql(tbname).c_str());
    return 1;
}

bool DaoSys::query_system_info(std::vector<DataFields>& result)
{
    DaoEntity dao(DMSystemInfo::TABLENAME);
    return dao.query_fields("*", "", result);
}


bool DaoSys::insert_ammeter_data(DataFields& fields)
{
    DaoEntity dao(DMDeviceDataRecord::TABLENAME);
    return dao.insert_fields(fields);
}



int DaoSys::insert_sec_policy(DataFields& fields)
{
    DaoEntity dao(DMSecPolicy::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoSys::query_sec_policy(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMSecPolicy::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoSys::update_sec_policy_by_id(DataFields& fields)
{
    string id = fields.get_str(DMSecPolicy::FID_ID);
    fields.remove(DMSecPolicy::FID_ID);

    DaoEntity dao(DMSecPolicy::TABLENAME);
    return dao.update_fields(fields, "where " + DMSecPolicy::FID_ID + "='" + id + "'");
}

bool DaoSys::insert_sec_record(DataFields& fields)
{
    DaoEntity dao(DMSecRecord::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoSys::query_sec_record(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMSecRecord::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoSys::update_sec_record(DataFields& fields, const string& sql_c)
{
    DaoEntity dao(DMSecRecord::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

int DaoSys::insert_service(DataFields& fields)
{
    fields.remove(DMService::FID_CREATE_TIME);
    fields.remove(DMService::FID_UPDATE_TIME);
    DaoEntity dao(DMService::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoSys::query_service(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMService::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoSys::update_service(DataFields& fields, const string& sql_c)
{
    fields.remove(DMService::FID_INTERFACE_NAME);
    fields.remove(DMService::FID_CREATE_TIME);
    fields.remove(DMService::FID_UPDATE_TIME);
    DaoEntity dao(DMService::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

int DaoSys::insert_serv_history(DataFields& fields)
{
    fields.remove(DMServHistory::FID_CREATE_TIME);
    DaoEntity dao(DMServHistory::TABLENAME);
    return dao.insert_fields(fields);
}

bool DaoSys::query_serv_history(PageInfo& pageinfo, vector<DataFields>& result, string sql_c)
{
    DaoEntity dao(DMServHistory::TABLENAME);
    return dao.query_fields("*", sql_c, pageinfo, result);
}

bool DaoSys::update_serv_history(DataFields& fields, const string& sql_c)
{
    fields.remove(DMServHistory::FID_ID);
    fields.remove(DMServHistory::FID_CREATE_TIME);
    DaoEntity dao(DMServHistory::TABLENAME);
    return dao.update_fields(fields, sql_c);
}

int DaoSys::insert_syslog(string logtype, string optor, string device_id, string msg)
{
    DataFields fields;
    fields.set(DMSystemLog::FID_ID, Snowflake::instance().next_id_str());
    fields.set(DMSystemLog::FID_TYPE, logtype);
    fields.set(DMSystemLog::FID_USER_ID, optor);
    fields.set(DMSystemLog::FID_DEVICE_ID, device_id);
    fields.set(DMSystemLog::FID_MSG, msg);
    DaoEntity dao(DMSystemLog::TABLENAME);
    dao.insert_fields(fields);
    return 0;
}