#include "area_business_person_attendance.h"

#include "db/db_tool.h"
#include"db/db_api/CDBSingletonDefine.h"
#include"log.h"
#include"card.h"
#include"area.h"
#include"common_tool.h"
#include"mine.h"
#include"websocket/constdef.h"
#include "websocket/wsClientMgr.h"
#include"tool_time.h"
#include "module_meta_date_changed.h"

struct person_attendance_data:business_data
{
    person_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_person_attendance::area_business_person_attendance()
{
}

void area_business_person_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_person_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_person())
    {
        return;
    }

    auto mine_tool_ptr = card_ptr->get_mine_tool();
    if(!mine_tool_ptr->m_is_attendance)
    {
        //考勤开始
        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_enter_time));

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

        //        log_info("人卡考勤开始：卡id=%d，卡type=%d，分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
        //                 card_ptr->m_id, card_ptr->m_type,
        //                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
    }

    //    auto ptr_temp = std::make_shared<person_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_person_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_person_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_person())
    {
        return;
    }

    auto mine_tool_ptr = card_ptr->get_mine_tool();
    if(!mine_tool_ptr->m_is_attendance)
    {
        return;
    }

    //考勤结束
    mine_tool_ptr->m_is_attendance=false;

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

    rapidjson::Document doc(rapidjson::kObjectType);
    rapidjson::Value datas(rapidjson::kArrayType);
    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();

    _to_json_card_up_one(card_ptr, datas, allocator);
    //module_meta_date_changed::clear_card(card_ptr);

    //    auto rea_tool = card_ptr->get_area_tool();
    //    rea_tool->on_leave(card_ptr);
    //    card_ptr->clear();

    //升井json发给web
    if(datas.Size() > 0)
    {
        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);

        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
    }

    //up_mine(card_ptr, false);

    //    if(nullptr == ptr)
    //    {
    //        log_error("结束考勤失败：nullptr == ptr");
    //        return;
    //    }

    //    auto ptr_temp = static_cast<person_attendance_data*>(ptr.get());

    //    if(ptr_temp->m_is_attendance)
    //    {
    //        ptr_temp->m_is_attendance = false;

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

/**
 * @brief 手工升井函数
 */
void area_business_person_attendance::handle_up_mine(sio::message::ptr const& data)
{
    std::vector<sio::message::ptr> card_vec;
    if(!tool_map::try_get_value(card_vec, JSON_ROOT_KEY_DATA, data) || card_vec.size() == 0)
    {
        log_error("手工升井，web发来的数据data字段为空 或者不是数组");
        return;
    }

//    rapidjson::Document doc(rapidjson::kObjectType);
//    rapidjson::Value datas(rapidjson::kArrayType);
//    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();

    std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
    int type = 0;
    std::string s_card_id;
    for(; it_card != card_vec.end(); ++it_card)
    {
        if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
                ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
        {
            log_error("手工升井，web发来的数据 card_id 或 card_type格式不对");
            continue;
        }

        uint32_t id = tool_other::id64_to_id(s_card_id);
        auto card_ptr = card_list::instance()->get(tool_other::type_id_to_u64(type, id));
        if(card_ptr && card_ptr->is_person())
        {
            std_debug("手工升井，处理，卡id=%d，卡type=%d", id, type);
            log_info("手工升井，处理，卡id=%d，卡type=%d", id, type);

            module_meta_date_changed::clear_card(card_ptr);
            //            auto rea_tool = card_ptr->get_area_tool();
            //            rea_tool->on_leave(card_ptr);
            //card_ptr->clear();

            //            auto mine_tool_ptr = card_ptr->get_mine_tool();
            //            if(mine_tool_ptr->m_is_attendance)
            //            {
            //                //考勤结束
            //                mine_tool_ptr->m_is_attendance=false;

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

            //检查井下是否超员--是否需要取消
            //CMineCardManager::instance()->OnPersonUp(card_ptr);

            //            auto rea_tool = card_ptr->get_area_tool();
            //			rea_tool->on_leave(card_ptr);
            //			card_ptr->clear();
        }
        else
        {
            log_error("手工升井，在全局列表中找不到卡，卡id=%d，卡type=%d", id, type);
        }
    }

//    //升井json发给web
//    if(datas.Size() > 0)
//    {
//        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
//        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
//        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);

//        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
//    }
}

///升井json
void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
                                                           rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
{
    if(!card_ptr->m_display)
    {
        return;
    }

    auto mine_tool_ptr = card_ptr->get_mine_tool();

    rapidjson::Value data(rapidjson::kArrayType);
    //卡号
    std::string id = tool_other::type_id_to_str(card_ptr->m_type, card_ptr->m_id);
    tool_json::push_back(data, id, allocator);

    //x,y坐标
    data.PushBack(card_ptr->x, allocator);
    data.PushBack(card_ptr->y, allocator); //CFunctions::round(card->y_offset_after(),2)

    //入井时间戳
    uint64_t t = tool_time::to_ms(mine_tool_ptr->m_attendance_start_time);	//转为ms
    data.PushBack(t, allocator);

    //进入区域时间戳
    data.PushBack(0, allocator);

    //接收时间戳
    data.PushBack(0, allocator);
    //工作时长
    t = tool_time::elapse_ms(mine_tool_ptr->m_attendance_start_time);	//转为ms
    data.PushBack(t, allocator);

    //地图编号
    data.PushBack(0, allocator);
    //区域编号
    data.PushBack(0, allocator);
    //部门编号
    data.PushBack(card_ptr->m_deptid, allocator);

    //状态
    data.PushBack(0, allocator);
    //运行状态
    data.PushBack(card_ptr->m_stat, allocator);
    //业务状态
    data.PushBack(0, allocator);
    //速度
    data.PushBack(card_ptr->m_speed, allocator);


    out_datas.PushBack(data, allocator);
}


/////升井或收到web的删除卡命令  site_ptr==nullptr表示收到web的删除卡命令
///// 保存考勤记录，发升井json，清理卡
//void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete)
//{
//    auto mine_tool_ptr = card_ptr->get_mine_tool();
//    if(!mine_tool_ptr->m_is_attendance)
//    {
//        return;
//    }

//    //考勤结束
//    mine_tool_ptr->m_is_attendance=false;

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

//    rapidjson::Document doc(rapidjson::kObjectType);
//    rapidjson::Value datas(rapidjson::kArrayType);
//    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();

//    _to_json_card_up_one(card_ptr, datas, allocator);
//    //module_meta_date_changed::clear_card(card_ptr);

//    auto rea_tool = card_ptr->get_area_tool();
//    rea_tool->on_leave(card_ptr);
//    card_ptr->clear();

//    //升井json发给web
//    if(datas.Size() > 0)
//    {
//        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
//        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
//        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);

//        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
//    }

////    if(is_web_delete)
////    {
////        log_info("人卡考勤结束：卡id=%d，卡type=%d，分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
////                 card_ptr->m_id, card_ptr->m_type,
////                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
////    }
////    else
////    {
////        log_info("收到web的删除卡命令,人卡考勤结束：卡id=%d，卡type=%d， stat_attendance=%d",
////                 card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
////    }
//}

