#include "area_business_card_enter_or_leave.h"
#include "area.h"
#include "card_base.h"
#include"common_tool.h"
#include"tool_time.h"
#include"db/db_tool.h"
#include"log.h"


struct enter_or_leave_data:business_data
{
    enter_or_leave_data()
    {
        m_enter_time_ms = tool_time::to_ms(std::chrono::system_clock::now());
    }

    ///考勤开始时间
    uint64_t m_enter_time_ms;
};

//进入区域则入库操作
void area_business_card_enter_or_leave::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                 const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
{
    auto ptr_temp = std::make_shared<enter_or_leave_data>();
    ptr = ptr_temp;
    save_his_area_location_enter(area_hover_ptr, card_ptr, ptr_temp->m_enter_time_ms);
}

void area_business_card_enter_or_leave::on_hover(const std::shared_ptr<area_hover>&a,
                                                 const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
{

}
//出区域则入库
void area_business_card_enter_or_leave::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                 const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
{
    if(nullptr == ptr)
    {
        log_error("出区域失败：nullptr == ptr");
        return;
    }
    auto ptr_temp = static_cast<enter_or_leave_data*>(ptr.get());

    save_his_area_location_leave(area_hover_ptr, card_ptr, ptr_temp->m_enter_time_ms);
}

///入库
void area_business_card_enter_or_leave::save_his_area_location_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                                     const std::shared_ptr<card_location_base> card_ptr,
                                                                     uint64_t start_time_ms)
{
    char sql[LENGTH_SQL] = {0};

    std::string call("add_area_staff");
    if(card_ptr->is_vehicle())//车卡
    {
        call="add_area_vehicle";
    }

    std::string start_str = tool_time::to_str_ex(start_time_ms);

    sprintf(sql, "CALL %s(%s, %d, %d, %d, '%s','%s');", call.c_str(),
            tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id).c_str(),
            card_ptr->m_id, area_hover_ptr->id(), area_hover_ptr->mapid(), start_str.c_str(), start_str.c_str());

    db_tool::PushAsync(sql);
}

///入库
void area_business_card_enter_or_leave::save_his_area_location_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
                                                                     const std::shared_ptr<card_location_base> card_ptr,
                                                                     uint64_t start_time_ms)
{
    char sql[LENGTH_SQL] = {0};

    std::string call("add_area_staff");
    if(card_ptr->is_vehicle())//车卡
    {
        call="add_area_vehicle";
    }

    std::string start_str = tool_time::to_str_ex(start_time_ms);
    std::string end_str = tool_time::to_str_ex(tool_time::now_to_ms());

    sprintf(sql, "CALL %s(%s, %d, %d, %d, '%s','%s');", call.c_str(),
            tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id).c_str(),
            card_ptr->m_id, area_hover_ptr->id(), area_hover_ptr->mapid(), start_str.c_str(), end_str.c_str());

    db_tool::PushAsync(sql);
}


