#include "module_area.h"
#include"module_area_overman.h"
#include"module_area_timeout.h"
#include"module_attendance_vehicle.h"
#include"module_const.h"
#include"module_area_over_speed.h"


void module_area::on_enter(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)
{
    //与考勤，卡数统计，超员，超时无关的区域   直接返回
    if(AT_OTHER == c->m_area->m_area_type)
    {
        return;
    }

    auto card_ptr=card_list::instance()->get(card_id);
    if(!card_ptr)
    {
        log_error("卡不存在card_id=%d", card_id);
        return;
    }

    std::lock_guard<std::mutex> lock(_mutex);

    c->m_enter_time = std::chrono::system_clock::now();

    if(CT_PERSON == card_ptr->m_type)//统计人卡
    {
        c->m_area->m_person_count++;

        //区域人卡超员
        module_area_overman::instance()->on_enter(card_ptr, c, speed);
        //区域人卡超时
        module_area_timeout::instance()->on_enter(card_ptr, c, speed);
    }

    if(CT_VEHICLE == card_ptr->m_type)//统计车卡
    {
        c->m_area->m_vehicle_count++;

        //区域车卡超速
        module_area_over_speed::instance()->on_enter(card_ptr, c, speed);
        //车卡考勤
        module_attendance_vehicle::instance()->on_enter(card_ptr, c, speed);
    }
}

void module_area::on_hover(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)
{
    //与考勤，卡数统计，超员，超时无关的区域   直接返回
    if(AT_OTHER == c->m_area->m_area_type)
    {
        return;
    }

    auto card_ptr=card_list::instance()->get(card_id);
    if(!card_ptr)
    {
        log_error("卡不存在card_id=%d", card_id);
        return;
    }

    std::lock_guard<std::mutex> lock(_mutex);

    if(CT_PERSON == card_ptr->m_type)//统计人卡
    {
        //区域人卡超员
        module_area_overman::instance()->on_hover(card_ptr, c, speed, _map);
        //区域人卡超时
        module_area_timeout::instance()->on_hover(card_ptr, c, speed, _map);
    }

    if(CT_VEHICLE == card_ptr->m_type)//统计车卡
    {
        //区域车卡超速
        module_area_over_speed::instance()->on_hover(card_ptr, c, speed, _map);
        //车卡考勤
        module_attendance_vehicle::instance()->on_hover(card_ptr, c, speed, _map);
    }
}

void module_area::on_leave(uint64_t card_id,std::shared_ptr<area_hover>&c,double speed)
{
    //与考勤，卡数统计，超员，超时无关的区域   直接返回
    if(AT_OTHER == c->m_area->m_area_type)
    {
        return;
    }

    auto card_ptr=card_list::instance()->get(card_id);
    if(!card_ptr)
    {
        log_error("卡不存在card_id=%d", card_id);
        return;
    }

    std::lock_guard<std::mutex> lock(_mutex);

    if(CT_PERSON == card_ptr->m_type)//统计人卡
    {
        c->m_area->m_person_count--;

        //区域人卡超员
        module_area_overman::instance()->on_leave(card_ptr, c, speed);
        //区域人卡超时
        module_area_timeout::instance()->on_leave(card_ptr, c, speed);
    }

    if(CT_VEHICLE == card_ptr->m_type)//统计车卡
    {
        c->m_area->m_vehicle_count--;

        //区域车卡超速
        module_area_over_speed::instance()->on_leave(card_ptr, c, speed);
        //车卡考勤
        module_attendance_vehicle::instance()->on_leave(card_ptr, c, speed);
    }
}



