#include "area_business_car_attendance.h"

#include "db/db_tool.h"
#include"db/db_api/CDBSingletonDefine.h"
#include"log.h"
#include"card.h"
#include"area.h"
#include"mine.h"

struct car_attendance_data:business_data
{
    car_attendance_data():m_is_attendance(false)
      ,m_attendance_start_time(std::chrono::seconds(0))
    {
    }

    ///考勤状态  0初始状态 1 没在考勤 2 考勤;参看ATTENDANCE_STATUS
    bool m_is_attendance;
    ///考勤开始时间
    std::chrono::system_clock::time_point m_attendance_start_time;
};

area_business_car_attendance::area_business_car_attendance()
{
}

void area_business_car_attendance::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_car_attendance::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)
{
    if(!card_ptr->is_vehicle())
    {
        return;
    }

    //save_attendance(card_ptr, area_hover_ptr);
    auto mine_tool_ptr = card_ptr->get_mine_tool();
    if(!mine_tool_ptr->m_is_attendance
            || !is_attendance_area(area_hover_ptr->m_area->m_id, card_ptr->get_vehicle_type_id()))
    {
        return;
    }

    //考勤结束
    mine_tool_ptr->m_is_attendance=false;
    //作为一条结束考勤记录保存到数据库
    db_tool::save_attendance(card_ptr, area_hover_ptr);

    //save_attendance(card_ptr, area_hover_ptr);

    //    auto ptr_temp = std::make_shared<car_attendance_data>();
    //    ptr = ptr_temp;

    //    ptr_temp->m_is_attendance = true;
    //    ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();

    //    //作为一条开始考勤记录保存到数据库
    //    db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
}

void area_business_car_attendance::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_car_attendance::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(!card_ptr->is_vehicle())
    {
        return;
    }

    //没在考勤状态转换为考勤状态
    auto mine_tool_ptr = card_ptr->get_mine_tool();
    if(!mine_tool_ptr->m_is_attendance
            && is_attendance_area(area_hover_ptr->m_area->m_id, card_ptr->get_vehicle_type_id()))
    {
        //考勤开始
        mine_tool_ptr->m_is_attendance = true;
        //mine_tool_ptr->m_attendance_start_time=std::chrono::system_clock::now();
        mine_tool_ptr->m_attendance_start_time=
                std::chrono::system_clock::time_point(std::chrono::milliseconds(area_hover_ptr->m_last_time));

        //作为一条开始考勤记录保存到数据库
        db_tool::save_attendance(card_ptr, area_hover_ptr);

        //                log_info("车卡考勤开始：卡id=%d，卡type=%d，区域id=%d, stat_attendance=%d",
        //                         card_ptr->m_id, card_ptr->m_type,
        //                         area_ptr->m_id,mine_tool_ptr->m_stat_attendance);
    }
}

void area_business_car_attendance::init_attendance_area_from_db()
{
    const char *sql = "select a.area_id, a.att_rule_id,  vt.vehicle_type_id \
            from dat_att_rule_area a, dat_att_rule_vehicle_type  vt, dat_att_rule r\
            where a.att_rule_id=vt.att_rule_id and a.att_rule_id=r.att_rule_id;";
    std::string Error;
    YADB::CDBResultSet DBRes;
    sDBConnPool.Query(sql,DBRes,Error);
    if(!Error.empty())
        log_error("初始化attendance_area Error,%s",Error.c_str());
    uint64_t nCount = DBRes.GetRecordCount( Error );
    if(int64_t(nCount) <= 0)
    {
        log_error("初始化attendance_area Error,%s",Error.c_str());
    }

    log_info( "init_attendance_area_from_db. The record count=%ld\n", nCount );

    m_map.clear();
    while ( DBRes.GetNextRecod(Error) )
    {
        int area_id  = 0;
        DBRes.GetField( "area_id",area_id, Error );

        int att_rule_id  = 0;
        DBRes.GetField( "att_rule_id",att_rule_id, Error );

        int vehicle_type_id  = 0;
        DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );

        m_map.insert(std::make_pair(to_list_id(area_id, vehicle_type_id), att_rule_id));
    }
}

std::unordered_map<uint64_t, int> area_business_car_attendance::m_map;

