#ifndef MODULE_AREA_OVERMAN_H
#define MODULE_AREA_OVERMAN_H

/**
  *@brief 区域超员模块：当某个区域人数超过指定值时开始告警，当低于指定值时停止告警
  *@author 戴月腾
  *@date 2018-08-26
*/

#include <string>
#include <memory>
#include <atomic>
#include <mutex>
#include <map>
#include "module_const.h"

/**
 * @brief 区域超员模块：当某个区域人数超过指定值时开始告警，当低于指定值时停止告警
 */
class module_area_overman:public singleton_base<module_area_overman>
{
private:
    friend class singleton_base<module_area_overman>;
    module_area_overman()
    {
    }

public:

    void on_enter(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c,double speed)
    {

    }

    void on_hover(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c,double speed,
                  std::map<uint64_t, std::shared_ptr<ya_event>>& ev_map)
    {
        auto area_ptr = c->m_area;
        if(area_ptr->m_limit_person_count <= area_ptr->m_person_count)//超员
        {
            auto ev_ptr = tool_other::find_event(area_ptr->id(), ET_AREA_OVER_COUNT_PERSON,ev_map);
            if(ev_ptr)
            {
                ev_ptr->m_limit_value=area_ptr->m_limit_person_count;
                ev_ptr->m_cur_value=area_ptr->m_person_count;
            }
            else//从没有告警状态转化为告警状态
            {
                area_ptr->m_is_over_person = true;
                auto ev_ptr = tool_other::create_event(OT_AREA, area_ptr->id(), ET_AREA_OVER_COUNT_PERSON);

                tool_other::copy_event(card_ptr, ev_ptr);

                ev_ptr->m_limit_value = area_ptr->m_limit_person_count;
                ev_ptr->m_cur_value = area_ptr->m_person_count;

                //保存到数据库
                tool_db::save_event(ev_ptr);

                tool_other::insert_event(ev_ptr, ev_map);
            }
        }
        else//取消告警状态
        {
            if(area_ptr->m_is_over_person)
            {
                area_ptr->m_is_over_person = false;
                auto ev_ptr = tool_other::find_event(area_ptr->id(), ET_AREA_OVER_COUNT_PERSON,ev_map);
                if(ev_ptr)
                {
                    ev_ptr->m_status = ES_END;

                    ev_ptr->m_limit_value=area_ptr->m_limit_person_count;
                    ev_ptr->m_cur_value = area_ptr->m_person_count;
                    ev_ptr->m_cur_time = std::chrono::system_clock::now();

                    //保存到数据库
                    tool_db::save_event(ev_ptr);
                }
            }
        }
    }

    void on_leave(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c,double speed)
    {

    }
};
typedef std::shared_ptr<module_area_overman>  module_area_overman_ptr;


#endif // MODULE_AREA_OVERMAN_H
