#include "module_attendance_vehicle.h"

#include<memory>
#include<string>

#include"area_business.h"
#include"event.h"
#include"log.h"
#include"common_tool.h"
#include"area.h"
#include"db/db_api/CDBSingletonDefine.h"
#include"db/db_tool.h"
#include"mine.h"


struct attendance_vehicle_data:business_data
{
    attendance_vehicle_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;
};

void module_attendance_vehicle::on_enter(std::shared_ptr<card_location_base> card_ptr, int index)
{
    auto area_hover_ptr = card_ptr->get_area_hover();
    if(!area_hover_ptr)
    {
        log_error("if(!area_hover_ptr)==true");
        return;
    }

    area_hover_ptr->m_data[index] = std::make_shared<attendance_vehicle_data>();
    auto ptr = static_cast<attendance_vehicle_data*>(area_hover_ptr->m_data[index].get());

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

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

void module_attendance_vehicle::on_hover(std::shared_ptr<card_location_base> card_ptr, int index)
{
}


void module_attendance_vehicle::on_leave(std::shared_ptr<card_location_base> card_ptr, int index)
{
    auto area_hover_ptr = card_ptr->get_area_hover();
    if(!area_hover_ptr)
    {
        log_error("if(!area_hover_ptr)==true");
        return;
    }

    area_hover_ptr->m_data[index] = std::make_shared<attendance_vehicle_data>();
    auto ptr = static_cast<attendance_vehicle_data*>(area_hover_ptr->m_data[index].get());

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

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

    //从考勤状态转换为结束考勤
    if(is_not_attendance_area(area_ptr->m_id, card_ptr->get_vehicle_type_id()))
    {
        save_attendance(card_ptr, area_ptr);
    }
    else//没在考勤状态转换为考勤状态
    {
        auto mine_tool_ptr = card_ptr->get_mine_tool();
        if(!mine_tool_ptr->is_attendance())
        {
            //考勤开始
            mine_tool_ptr->m_stat_attendance=AS_ATTENDANCE;
            mine_tool_ptr->m_attendance_start_time=std::chrono::system_clock::now();

            //作为一条开始考勤记录保存到数据库
            db_tool::save_attendance(card_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 module_attendance_vehicle::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));
    }
}

///area_ptr==nullptr 表示接收到 web的删除卡命令
void module_attendance_vehicle::save_attendance(std::shared_ptr<card_location_base>& card_ptr, std::shared_ptr<area> area_ptr)
{
    auto mine_tool_ptr = card_ptr->get_mine_tool();
    if(!mine_tool_ptr->is_attendance())
    {
        return;
    }

    //考勤结束
    mine_tool_ptr->m_stat_attendance=AS_NOT_ATTENDANCE;

    //作为一条结束考勤记录保存到数据库
    db_tool::save_attendance(card_ptr);

    if(area_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);
    }
    else
    {
        log_info("收到web的删除卡命令，车卡考勤结束：卡id=%d，卡type=%d，stat_attendance=%d",
                 card_ptr->m_id, card_ptr->m_type,mine_tool_ptr->m_stat_attendance);
    }
}




