#ifndef MODULE_AREA_OVER_COUNT_H
#define MODULE_AREA_OVER_COUNT_H
/**
 * @brief 人员或车辆超员
 * @author 戴月腾
 * @date 2018-10-10
 */

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


class module_area_over_count:public singleton_base<module_area_over_count>
{
private:
    friend class singleton_base<module_area_over_count>;
    module_area_over_count()
    {
    }

public:

    void on_enter(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
    {
        auto area_ptr = c->m_area;

        int limit = area_ptr->m_limit_person_count;
        EVENT_TYPE ev_type = ET_AREA_OVER_COUNT_PERSON;
        int cur_value = area_ptr->m_person_count;
        if(card_ptr->is_vehicle())
        {
            limit = area_ptr->m_limit_vehicle_count;
            ev_type = ET_AREA_OVER_COUNT_VEHICLE;
            cur_value = area_ptr->m_vehicle_count;
        }

        //超员配置是否为0，0则不会进行告警
        if(0 == limit)
        {
            return;
        }

        if(limit <= cur_value)//超员
        {
            std::lock_guard<std::mutex> lock(__lock);

            auto ev_ptr = event_list::instance()->get_event_area(area_ptr->id(), ev_type);
            if(ev_ptr)
            {
                event_list::copy_event(area_ptr, ev_ptr);

                ev_ptr->m_limit_value=limit;
                ev_ptr->m_cur_value=cur_value;

                ev_ptr->m_status = ES_START;
            }
            else//从没有告警状态转化为告警状态
            {
                ev_ptr = event_list::create_event_area(area_ptr->id(), ev_type);

                event_list::copy_event(area_ptr, ev_ptr);

                ev_ptr->m_limit_value=limit;
                ev_ptr->m_cur_value=cur_value;

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

                event_list::instance()->add(ev_ptr->get_list_id(),ev_ptr);

                log_info("区域超员开始：区域id=%d，区域门限=%d，当前卡数=%d, 卡类型=%d",
                         area_ptr->id(), limit, cur_value, card_ptr->m_type);
            }
        }
    }

//    void on_hover(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
//    {

//    }

    void on_leave(std::shared_ptr<card_location_base> card_ptr,std::shared_ptr<area_hover>&c)
    {
        auto area_ptr = c->m_area;

        int limit = area_ptr->m_limit_person_count;
        EVENT_TYPE ev_type = ET_AREA_OVER_COUNT_PERSON;
        int cur_value = area_ptr->m_person_count;
        if(card_ptr->is_vehicle())
        {
            limit = area_ptr->m_limit_vehicle_count;
            ev_type = ET_AREA_OVER_COUNT_VEHICLE;
            cur_value = area_ptr->m_vehicle_count;
        }

        //超员配置是否为0，0则不会进行告警
        if(0 == limit)
        {
            return;
        }

        if(limit > cur_value)//没有超员
        {
            //取消告警状态
            auto ev_ptr = event_list::instance()->get_event_area(area_ptr->id(), ev_type);
            if(ev_ptr && !ev_ptr->is_end())
            {
                std::lock_guard<std::mutex> lock(__lock);
                if(ev_ptr->is_end())
                {
                    return;
                }

                event_list::copy_event(area_ptr, ev_ptr);

                ev_ptr->m_limit_value=limit;
                ev_ptr->m_cur_value=cur_value;

                ev_ptr->m_status = ES_END;

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

                log_info("区域超员结束：区域id=%d，区域门限=%d，当前卡数=%d, 卡类型=%d",
                         area_ptr->id(), limit, cur_value, card_ptr->m_type);
            }
        }
    }

private:
    std::mutex __lock;
};


#endif // MODULE_AREA_OVER_COUNT_H
