#include "area_business_person_attendance.h"

#include "db/db_tool.h"
#include"db/db_api/CDBSingletonDefine.h"
#include"log.h"
#include"card.h"
#include "card_person.h"
#include"area.h"
#include"common_tool.h"
#include"mine.h"
#include"websocket/constdef.h"
#include "websocket/wsClientMgr.h"
#include "websocket/wsTimerThread.h"
#include"tool_time.h"
#include "module_meta_date_changed.h"
//记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
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)
{
    log_info("on_enter:person_att:%d",card_ptr->m_id);
    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::time_point(std::chrono::milliseconds(area_hover_ptr->m_enter_time));

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

    }
}

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;

    log_info("on_leave_person_att:%d",card_ptr->m_id);
    //考勤结束
    mine_tool_ptr->m_is_attendance=false;
	if(card_ptr->upmine_flag()){
        auto start = mine_tool_ptr->m_attendance_start_time;
        std::string start_time = tool_time::to_str(start);
        std::string card_id = tool_other::type_id_to_str(card_ptr->m_type,card_ptr->m_id);
        char nsql[512]{0};
        const char *sql = "update rpt_att_staff set is_auto=%d where card_id=%s and start_time='%s';";
        snprintf(nsql,512,sql,card_ptr->upmine_flag(),card_id.c_str(),start_time.c_str());
        db_tool::PushAsync(nsql);
    }
    //作为一条结束考勤记录保存到数据库
    db_tool::save_attendance(card_ptr, area_hover_ptr);

	YA::_CARD_POS_ cp;
	cp.Type=card_ptr->m_type;
	cp.ID = card_ptr->m_id;
	swsTimerThrd.del_card_pos(cp);
    
    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);
    //升井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));
    }
    
    auto per = std::dynamic_pointer_cast<person>(card_ptr);
    per->clear();
}

/**
 * @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;
    }

    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;
        }
        log_info("handle_up_card:%s,接收到手动升井的请求",s_card_id.c_str());
        if(!tool_other::is_person(type))
            continue;
        auto card_ptr = card_list::instance()->get(tool_other::card_id_to_u64(s_card_id));
        if(card_ptr && STATUS_LOST == card_ptr->m_biz_stat)
        {
            log_info("handle_up_card:%s 手工升井,处理",s_card_id.c_str());
            card_ptr->inc_upmine_flag(1);
        }
        else
            log_warn("handle_up_card:%s,手动升井的卡找不到,或者该卡不在盲区",s_card_id.c_str());
    }
}

///升井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)
{
    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);
}