#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"

const char * get_area_table_name(const std::shared_ptr<area_hover>&a)
{
    static const char * g_sz_area_name = "his_location_area";
    // 分站定位数据保存(进入分站,离开分站保存记录)
    static const char * g_sz_reader_area_name = "his_location_reader";
    if (a->m_area->is_reader_area())
    {
        return  g_sz_reader_area_name;
    }
    return g_sz_area_name;
}

void area_business_card_enter_or_leave::on_load_his(const std::shared_ptr<area_hover>&area_hover_ptr,
                                            const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
{

}

//进入区域则入库操作
void area_business_card_enter_or_leave::on_enter(const std::shared_ptr<area_hover>&a,
                                                 const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr)
{
    char sql[LENGTH_SQL]{0};
    std::string start_time = tool_time::to_str(a->m_enter_time/1000);
    const auto &p=a->m_enter_point;

    // 根据区域无法区分是否井上区域[分站创建的区域没有代入区域类型-导致都是井上区域] 只能通过分站区分
    if (nullptr == c->get_area_tool()->m_site || !c->get_area_tool()->m_site->is_up_site())
    {
        snprintf(sql, LENGTH_SQL, "INSERT INTO %s (obj_id,card_type_id,ident,area_id,map_id,enter_time,start_point) \
	                         VALUES ('%d','%lu','%d',%d,%d,'%s','%.2f,%.2f');",get_area_table_name(a)
	                         ,c->m_cid, c->m_type, c->m_id, a->id(), a->mapid(), start_time.c_str(), p.x, p.y);
    }
    else
    {
        // 井上分站时,插入时把离开时间也写入 防止员工出井后最后一个区域没有leave_time
        snprintf(sql, LENGTH_SQL, "INSERT INTO %s (obj_id,card_type_id,ident,area_id,map_id,enter_time,leave_time,start_point) \
	                         VALUES ('%d','%lu','%d',%d,%d,'%s','%s','%.2f,%.2f');",get_area_table_name(a)
	                         ,c->m_cid, c->m_type, c->m_id, a->id(), a->mapid(), start_time.c_str(),start_time.c_str(), p.x, p.y);
    }
    db_tool::PushAsync(sql);
   // std_info("card:%d Enter Area:%d ",c->m_cid,a->id());
}

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)
{
    // 根据区域无法区分是否井上区域[分站创建的区域没有代入区域类型-导致都是井上区域] 只能通过分站区分
    if (nullptr != c->get_area_tool()->m_site && c->get_area_tool()->m_site->is_up_site())
    {
        // 井上分站时,员工在行走过程中更新时间
        char sql[LENGTH_SQL]{0};
        std::string start_time = tool_time::to_str(a->m_enter_time/1000);
        std::string end_time = tool_time::to_str(tool_time::now_to_seconds());
        const auto &ep=a->m_last_point;
        snprintf(sql,LENGTH_SQL,"UPDATE %s SET leave_time='%s',end_point='%.2f,%.2f'"
                                " WHERE  enter_time='%s' AND obj_id=%d AND area_id=%d;"
                                ,get_area_table_name(a),end_time.c_str(),ep.x,ep.y,start_time.c_str(),c->m_cid,a->id());

        db_tool::PushAsync(sql);
    }
}

//出区域则入库
void area_business_card_enter_or_leave::on_leave(const std::shared_ptr<area_hover>&a,
                                                 const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
{
    char sql[LENGTH_SQL]{0};
    std::string start_time = tool_time::to_str(a->m_enter_time/1000);
    std::string end_time = tool_time::to_str(a->m_last_time/1000);
//    std::string card_id = tool_other::type_id_to_str(c->m_type,c->m_id);
    const auto &ep=a->m_last_point;
    snprintf(sql,LENGTH_SQL,"UPDATE %s SET leave_time='%s',end_point='%.2f,%.2f' \
	                         WHERE  enter_time='%s' AND obj_id=%d AND area_id=%d;"
	                         ,get_area_table_name(a),end_time.c_str(),ep.x,ep.y,start_time.c_str(),c->m_cid,a->id());

    log_info("card_enter_leave:%s",sql);
    //std_info("card:%d leave Area:%d ",c->m_cid,a->id());
    db_tool::PushAsync(sql);
}