#pragma once

#include <vector>
#include "DataFields.h"


namespace DMPrice
{
    const string TABLENAME = "price";
    const string FID_ID = "id";
    const string FID_NAME = "name";
    const string FID_TYPE = "type";
    const string FID_EFFECTIVE_TIME = "effective_time";
    const string FID_EXPIRE_TIME = "expire_time";
    const string FID_IS_OPEN = "is_open";
    const string FID_CREATE_TIME = "create_time";
    const string FID_UPDATE_TIME = "update_time";
}

namespace DMPriceAttr
{
    const string TABLENAME = "price_attr";
    const string FID_PRICE_ID = "price_id";
    const string FID_TYPE = "type";
    const string FID_START_TIME = "start_time";
    const string FID_END_TIME = "end_time";
    const string FID_VALUE = "value";
}

namespace DMSecPolicy
{
    const string TABLENAME = "sec_policy";
    const string FID_ID = "id";
    const string FID_SEC_TYPE = "sec_type";
    const string FID_EVENT_NAME = "event_name";
    const string FID_EVENT_CODE = "event_code";
    const string FID_LEVEL = "level";
    const string FID_IS_OPEN = "is_open";
    const string FID_REMARK = "remark";
    const string FID_ACTION = "action";
    const string FID_TEXT = "text";
    const string FID_VOICE = "voice";
}

namespace DMSecRecord
{
    const string TABLENAME = "sec_record";
    const string ID = "id";
    const string TIME = "warn_time";
    const string TYPE = "type";
    const string LEVEL = "level";
    const string CONTENT = "content";
    const string POLICY_ID = "policy_id";
    const string DEVICE_ID = "device_id";
    const string USER = "user";
    const string DISPOSE_USER = "dispose_user";
    const string DISPOSE_TIME = "dispose_time";
    const string PROCESS_MODE = "process_mode";
    const string STATUS = "status";
}

namespace DMSystemInfo
{
    const string TABLENAME = "system_info";
    const string FID_STATION_NAME = "station_name";
    const string FID_ACTIVATION_TIME = "activation_time";
    const string FID_WORK_MODE = "swap_mode";
}

namespace DMService
{
    const string TABLENAME = "service_monitor";
    const string FID_INTERFACE_NAME = "interface_name";
    const string FID_PROTOCOL_TYPE = "protocol_type";
    const string FID_REPORT_TYPE = "report_type";
    const string FID_REPORT_CYCLE = "report_cycle";
    const string FID_REMARK = "remark";
    const string FID_IS_OPEN = "is_open";
    const string FID_CREATE_TIME = "create_time";
    const string FID_UPDATE_TIME = "update_time";
}

namespace DMServHistory
{
    const string TABLENAME = "service_history";
    const string FID_ID = "id";
    const string FID_SERV_ID = "service_id";
    const string FID_DATA_NUM = "data_num";
    const string FID_SUCCESS_NUM = "success_num";
    const string FID_FAILURE_NUM = "failure_num";
    const string FID_REQ_TIME = "req_time";
    const string FID_RES_TIME = "res_time";
    const string FID_CREATE_TIME = "create_time";
}


namespace DMSystemLog
{
    const string TABLENAME = "system_log";
    const string FID_ID = "id";
    const string FID_TYPE = "type";
    const string FID_USER_ID = "user_id";
    const string FID_DEVICE_ID = "device_id";
    const string FID_MSG = "msg";
    const string FID_CREATE_TIME = "create_time";
    const string FID_UPDATE_TIME = "update_time";
}

// 报文日志
namespace DMMessageLog
{
    // 表名称根据日期改变（日期作为后缀: message_log2023-01-01）
    const string TABLENAME = "message_log";
    const string DDL = R"((
  `id` varchar(32) NOT NULL COMMENT '报文ID',
  `device_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '设备ID',
  `create_time` datetime NOT NULL COMMENT '采集时间',
  `type` int DEFAULT NULL COMMENT '报文类型, 1：发送，2：接收',
  `function` varchar(60) DEFAULT NULL,
  `data` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '报文数据',
  `status` int DEFAULT NULL COMMENT '报文状态',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;)";

    const string FID_ID = "id";
    const string FID_DEVICE_ID = "device_id";
    const string FID_CREATE_TIME = "create_time";
    const string FID_TYPE = "type";  // 1: 发送，2：接收
    const string FID_FUNCTION = "function";
    const string FID_DATA = "data";
    const string FID_STATUS = "status";
}

class DaoSys
{
public:

    // ************************************************************************************************
    // 计费管理
    static int insert_price(DataFields& fields);
    static bool query_price(PageInfo& pageinfo, vector<DataFields>& result, string sql_c);
    static bool update_price(DataFields& fields, const string& sql_c);
    static bool update_price_attr(vector<DataFields>& vec_fields);


    static void insert_sec_warn(DataFields& fields);
    static int update_sec_warn(string id, int status);

    // ************************************************************************************************
    // 报文管理
    static int insert_message_log(DataFields& fields);
    static bool query_system_info(std::vector<DataFields>& result);




    // ************************************************************************************************
    // 电表采集数据
    // 写入电表采集的数据
    static bool insert_ammeter_data(DataFields& fields);


    // ************************************************************************************************
    // 安全策略
    static int insert_sec_policy(DataFields& fields);
    static bool query_sec_policy(PageInfo& pageinfo, vector<DataFields>& result, string sql_c);
    static bool update_sec_policy_by_id(DataFields& fields);


    // ************************************************************************************************
    // 安全运行记录
    static bool insert_sec_record(DataFields& fields);
    static bool query_sec_record(PageInfo& pageinfo, vector<DataFields>& result, string sql_c);
    static bool update_sec_record(DataFields& fields, const string& sql_c);


    // ************************************************************************************************
    // 服务管理
    static int insert_service(DataFields& fields);
    static bool query_service(PageInfo& pageinfo, vector<DataFields>& result, string sql_c);
    static bool update_service(DataFields& fields, const string& sql_c);

    // ************************************************************************************************
    // 服务管理历史记录
    static int insert_serv_history(DataFields& fields);
    static bool query_serv_history(PageInfo& pageinfo, vector<DataFields>& result, string sql_c);
    static bool update_serv_history(DataFields& fields, const string& sql_c);

    // ************************************************************************************************
    // 日志管理
    static int insert_syslog(string logtype, string optor, string device_id, string msg);
};