#include "ya_event.h"


uint64_t ya_event::get_list_id()
{
    return event_list::to_list_id(m_ev_type, m_obj_type,
           tool_other::id64_to_type(m_obj_id), tool_other::id64_to_id(m_obj_id));
}

void event_list::load_his_data_from_db()
{
    const char *sql = "SELECT event_id, stat,event_type_id,obj_type_id,obj_id,map_id,area_id,\
            limit_value,cur_value,x,y, cur_time FROM his_event_data \
            WHERE event_id IN (SELECT MAX(event_id) FROM his_event_data \
                               WHERE cur_time > (CASE obj_type_id \
                                                 WHEN 1 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
                                                 WHEN 2 THEN DATE_SUB(NOW(),INTERVAL 2 DAY)\
                                                 WHEN 4 THEN DATE_SUB(NOW(),INTERVAL 1 MONTH)\
                                                 WHEN 9 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
                                                 WHEN 10 THEN DATE_SUB(NOW(),INTERVAL 2 DAY) \
                                                 END) \
                               AND cur_time < NOW()\
                               GROUP BY event_type_id, obj_id) \
            AND  event_id NOT IN ( SELECT event_id FROM (SELECT * FROM his_event_data WHERE  stat=100) s);";
    std::string Error;
    YADB::CDBResultSet DBRes;
    sDBConnPool.Query(sql,DBRes,Error);
    if(!Error.empty())
        log_error("初始化事件列表 Error,%s",Error.c_str());
    uint64_t nCount = DBRes.GetRecordCount( Error );
    if (nCount > 0)
    {
        log_info( "load_his_data_from_db. The record count=%ld\n", nCount );

        while ( DBRes.GetNextRecod(Error) )
        {
            long long event_id  = 0;
            DBRes.GetField( "event_id",event_id, Error );

            int event_type_id  = 0;
            DBRes.GetField( "event_type_id",event_type_id, Error );

            int obj_type_id  = 0;
            DBRes.GetField( "obj_type_id",obj_type_id, Error );

            std::string obj_id  = "";
            DBRes.GetField( "obj_id",obj_id, Error );

            int map_id  = 0;
            DBRes.GetField( "map_id",map_id, Error );

            int area_id  = 0;
            DBRes.GetField( "area_id",area_id, Error );

            double limit_value = 0;
            DBRes.GetField( "limit_value",limit_value, Error );

            double cur_value = 0;
            DBRes.GetField( "cur_value",cur_value, Error );

            double x = 0;
            DBRes.GetField( "x",x, Error );

            double y = 0;
            DBRes.GetField( "y",y, Error );

            std::string cur_time  = "";
            DBRes.GetField( "cur_time",cur_time, Error );

            int stat  = 0;
            DBRes.GetField( "stat",stat, Error );


            std::shared_ptr<ya_event> ev = std::make_shared<ya_event>(event_id);
            ev->m_status = static_cast<EVENT_STATUS>(stat);
            ev->m_ev_type = static_cast<EVENT_TYPE>(event_type_id);

            ev->m_obj_type = static_cast<OBJECT_TYPE>(obj_type_id);
            ev->m_obj_id = tool_other::to13str(obj_id);
            ev->m_cur_time = tool_time::to_time_ex(cur_time);

            ev->m_cur_value = cur_value;
            ev->m_limit_value = limit_value;

            ev->m_map_id = map_id;
            ev->m_area_id = area_id;
            ev->x = x;
            ev->y = y;

            m_map.insert(std::make_pair(ev->get_list_id(), ev));
        }

        for(auto&iter: m_map)
        {
            auto ev = iter.second;
            log_info("event list %lld,%d,%d,%d,%s,%d,%d,%f,%f,%f,%f,%s"
                     , ev->get_id(),ev->m_status,ev->m_ev_type,ev->m_obj_type,ev->m_obj_id.c_str()
                     ,ev->m_map_id,ev->m_area_id,ev->m_limit_value,ev->m_cur_value
                     ,ev->x,ev->y
                     ,tool_time::to_str_ex(ev->m_cur_time).c_str());
        }
    }
}

template<> std::shared_ptr<event_list>
single_base<event_list, uint64_t, std::shared_ptr<ya_event>>::m_instance=std::make_shared<event_list>();


