#include "module_meta_date_changed.h"
#include"area_business_person_attendance.h"
#include"area_business_car_attendance.h"

#include"common_tool.h"
#include "websocket/wsClientMgr.h"
#include"db/db_api/CDBSingletonDefine.h"
#include"event.h"

#include"ant.h"
#include"card.h"
#include"area.h"
#include "forbid_staff_down_mine.h"
#include "landmark.h"
#include <three_rates.h>
#include"area_business_geofault.h"
#include <boost/format.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <card_path.h>
#include "ya_setting.h"
///基础数据
void module_meta_date_changed::accept(sio::message::ptr const& data)
{
    std::string name="";
    if(!tool_map::try_get_value(name, JSON_KEY_NAME, data))
    {
        log_error("web发来的数据: 基础数据找不到name字段");
        return;
    }

    std::string szParam = "0";
    tool_map::try_get_value(szParam,JSON_KEY_ID,data);

   // int id=-1;
   // tool_map::try_get_value(id,JSON_KEY_ID,data);

    std::string op_type="";
    tool_map::try_get_value(op_type, JSON_KEY_OP_TYPE, data);

    log_info("web发来数据:cmd=meta_data_changed.name:%s Param:%s OpType:%s",name.c_str(),szParam.c_str(),op_type.c_str());
    if(!szParam.empty() && !op_type.empty())
    {
        EDIT_TYPE_ID edit_type_id;
        if(!try_get_edit_type_id(op_type, edit_type_id))
        {
            log_error("web发来的数据: 基础数据op_type字段错误:op_type=%s", op_type.c_str());
            return;
        }

        if(JSON_KEY_NAME_VEHICLE == name || JSON_KEY_NAME_VEHICLE_EXTEND == name)
        {
            deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
        }
        else if(JSON_KEY_NAME_STAFF == name || JSON_KEY_NAME_STAFF_EXTEND == name)
        {
            deal_call_edit_vehicle_or_staff(szParam, edit_type_id);
        }
        else if(JSON_KEY_NAME_CARD == name)
        {
            deal_call_edit_card(szParam, edit_type_id);
        }
        else if(JSON_KEY_NAME_AREA == name)
        {
            deal_call_edit_area(szParam, edit_type_id);
        }
        else if(JSON_KEY_NAME_READER == name)
        {
            deal_call_edit_reader(szParam, edit_type_id);
        }
        else if ("antenna" == name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_antenna(id,edit_type_id);
        }
        else if(JSON_KEY_NAME_PATH == name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_path(id, edit_type_id);
        }
        else if(JSON_KEY_NAME_MAP == name || JSON_KEY_NAME_MAP_GIS == name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_map(id, edit_type_id);
        }
        else if (JSON_KEY_NAME_LIGHT == name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_light(id,edit_type_id);///待实现
        }
        else if ("lights_group"== name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_lights_group(id,edit_type_id);///待实现
        }
        else if (JSON_KEY_NAME_FORBID_PERSON_DOWN_MINE == name)
        {
            deal_call_edit_forbid_person_down_mine(szParam,edit_type_id);
        }
        else if (JSON_KEY_NAME_PERSONS_DYNAMIC_THRE == name)
        {
            int id = std::stoi(szParam);
            deal_call_edit_persons_dynamic_thre(id,edit_type_id);
        }
		else if (JSON_KEY_NAME_GEO_FAULT == name)//地址断层更新
		{
			area_business_geofault::init_geofault_from_db();
		}
		else if (JSON_KEY_NAME_LANDMARK == name)//地标信息更新
		{
			deal_call_edit_landmark(szParam, edit_type_id);
		}
		else if (JSON_KEY_NAME_COALFACE==name|| JSON_KEY_NAME_COALFACE_VEHICLE == name || 
			JSON_KEY_NAME_DRIVING_FACE == name || JSON_KEY_NAME_DRIVINGFACE_VEHICLE == name
			|| JSON_KEY_NAME_DRIVINGFACE_REF_POINT == name
			|| JSON_KEY_NAME_DRIVINGFACE_WARNING_POINT == name)
		{			
			three_rates::get_instance()->update_db_data(name, szParam, edit_type_id);
		}
        else
        {
            log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
        }
    }
    else
    {
        if(JSON_KEY_NAME_SETTING == name)
        {
            //阈值限制接口
            init_setting(); ///待实现
        }
        else
        {
            log_error("web发来的数据: 基础数据name字段错误:name=%s", name.c_str());
        }
    }
}

///3.清除卡相关信息,区域相关,分站相关,考勤相关,清除定时器,
///速度,状态 呼救 呼叫 告警相关。即保留基础信息,其他的重置。
void module_meta_date_changed::clear_card(const std::shared_ptr<card_location_base>& card_ptr)
{
    card_ptr->get_area_tool()->on_leave(card_ptr);
}
/*
 * 修改车及卡的数据
 * id64 = ;员工ID或者车ID;卡ID=101;0010000001198  分号间隔
 */
void module_meta_date_changed::deal_call_edit_vehicle_or_staff(const std::string & id64, EDIT_TYPE_ID edit_type_id)
{
    std::vector<std::string> vecSegTag;
    boost::split(vecSegTag, id64, boost::is_any_of(";"));
    if (vecSegTag.size() == 1)  //数据发生错误
    {
        log_errno("Web Send Data Error!(%s)", id64.c_str());
        return;
    }
    std::string lsz_card_id = vecSegTag[1];
    int cid = std::stoi(vecSegTag[0].c_str());
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
        if (ET_UPDATE == edit_type_id)
        {
            auto c = card_list::instance()->get_card_by_cid(cid);
            if (nullptr != c && tool_other::type_id_to_str(c->m_type,c->m_id) != lsz_card_id)
            {
                //如果修改了卡号,则删除原来的卡的数据
                remove_card(c);
            }
        }
        int c_type = tool_other::id64_to_type(lsz_card_id);
        if (tool_other::is_vehicle(c_type))
        {
            card_list::instance()->init_vehicle(lsz_card_id);
        }
        else if (tool_other::is_person(c_type))
        {
            card_list::instance()->init_staffer(lsz_card_id);
        }
    }
    else
    {
        if(!lsz_card_id.empty())
            remove_card(tool_other::id64_to_id(lsz_card_id), tool_other::id64_to_type(lsz_card_id));
    }
}

void module_meta_date_changed::deal_call_edit_card(std::string & id64, EDIT_TYPE_ID edit_type_id)
{
    std::string card_id64_str = tool_other::to13str(id64);
    int type = tool_other::id64_to_type(card_id64_str);
    if(tool_other::is_person(type) || tool_other::is_vehicle(type))
    {
        auto c = card_list::instance()->get(tool_other::card_id_to_u64(card_id64_str));
        if (nullptr == c) {
            log_info("web edit card:%s Not bind staff or vehicle. editType:%d", card_id64_str.c_str(), edit_type_id);
            return ;
        }
        std::string lsz = std::to_string(c->m_cid) + ";" + card_id64_str;
        deal_call_edit_vehicle_or_staff(lsz,edit_type_id);
    }
    else
    {
        log_error("基础数据 删除卡type不对:type=%d", type);
    }
}

void module_meta_date_changed::deal_call_edit_area(const std::string& id,EDIT_TYPE_ID edit_type_id)
{
    int cid = std::stoi(id);
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id){
        area_list::instance()->init_from_db(cid);
    }else if(ET_DELETE == edit_type_id)
    {
        if(auto area_ptr = area_list::instance()->get(cid)){
            log_info("区域删除:areaid=%d", cid);
            area_ptr->clear();
            area_list::instance()->remove(cid);
        }
    }
}

void module_meta_date_changed::deal_call_edit_reader(const std::string &ids, EDIT_TYPE_ID edit_type_id)
{
    //新增发送的都是单个分站名称,所以
    if(ET_INSERT == edit_type_id){
        sit_list::instance()->load_from_db(ids);
    }
    else if(ET_UPDATE == edit_type_id){
        sit_list::instance()->init_site(ids);
    }else if(ET_DELETE == edit_type_id){
        int id =std::stoi(ids);
        if(auto sit_ptr=sit_list::instance()->get(id)){
            log_info("分站删除:sid=%d", id);
            sit_ptr->clear_event();
            sit_list::instance()->remove(id);
            card_path::init();
        }
    }
}

void module_meta_date_changed::deal_call_edit_antenna(int id,EDIT_TYPE_ID edit_type_id)
{
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
        sit_list::instance()->read_sit_list(id);
    }
    //else if(ET_DELETE == edit_type_id)
    //{
        //delete_antenna(id);
   // }
}

void module_meta_date_changed::deal_call_edit_path(int id, EDIT_TYPE_ID edit_type_id)
{
   // if(ET_INSERT == edit_type_id)
   // {
   //     log_error("path不支持增加操作");
   //     std_debug("path不支持增加操作");
   // }
   // else if(ET_UPDATE == edit_type_id)
   // {
   // }
    if(sit_list::instance()->get(id))
        sit_list::instance()->read_ant_path(id);
}

void module_meta_date_changed::deal_call_edit_map(int id, EDIT_TYPE_ID edit_type_id)
{
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
        if(!is_cur_map(id))
        {
            log_info("修改的不是当前使用的地图,要修改地图id=%d,当前使用地图id=%d", id, cur_map_id());
            return;
        }

        update_map_info(id);
    }
    else if(ET_DELETE == edit_type_id)
    {
        if(is_cur_map(id))
        {
            log_error("删除了当前使用的地图");
            return;
        }
    }
}

void module_meta_date_changed::deal_call_edit_light(int id, EDIT_TYPE_ID edit_type_id)
{
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
    }
    else if(ET_DELETE == edit_type_id)
    {
    }
}

void module_meta_date_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID edit_type_id)
{
    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
    }
    else if(ET_DELETE == edit_type_id)
    {
    }
}

// 禁止指定人员下井 id = (rt_person_forbid_down_mine)数据库中自增长ID
void module_meta_date_changed::deal_call_edit_forbid_person_down_mine(const std::string & lszId,EDIT_TYPE_ID edit_type_id)
{
    std::vector<std::string> vecSegTag;
    boost::split(vecSegTag, lszId, boost::is_any_of(";"));
    if (vecSegTag.size() == 1)  //数据发生错误
    {
        log_errno("Web Send Data Error!(%s)", lszId.c_str());
        return;
    }
    int db_id = std::stoi(vecSegTag[0].c_str());
    int staff_id = std::stoi(vecSegTag[1].c_str());

    if(ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
    {
        forbid_staff_down_mine::instance()->init_forbid_staff(db_id,edit_type_id);
    }
    else if(ET_DELETE == edit_type_id)
    {
        forbid_staff_down_mine::instance()->del_forbid_data(db_id,staff_id);
    }
}

// 设置区域中时间段人员超员阈值等数据
void module_meta_date_changed::deal_call_edit_persons_dynamic_thre(int area_id,EDIT_TYPE_ID edit_type_id)
{
    area_list::instance()->init_area_persons_dynamic_thre_from_db(area_id);
}
// 修改地标信息
void module_meta_date_changed::deal_call_edit_landmark(const std::string & landmarkid, EDIT_TYPE_ID edit_type_id)
{
	int id = atoi(landmarkid.c_str());

	if (ET_INSERT == edit_type_id || ET_UPDATE == edit_type_id)
	{
		Landmark_list::instance()->init_landmarkInfo(id);
	}
	else if (ET_DELETE == edit_type_id)
	{
		Landmark_list::instance()->remove(id);
	}
}

void module_meta_date_changed::init_setting()
{
    CYaSetting::Init_sys_setting();
}

void module_meta_date_changed::remove_card(uint32_t id, int32_t type) {
    uint64_t card_id = tool_other::type_id_to_u64(type, id);
    auto card_ptr = card_list::instance()->get(card_id);
    if (!card_ptr) {
        log_error("基础数据, 在全局列表中删除卡,全局列表中已经不存在此卡, id=%d, type=%d", id, type);
        return;
    }
    remove_card(card_ptr);
}
void module_meta_date_changed::remove_card(std::shared_ptr<card_location_base> card_ptr)
{
    if (!card_ptr)
    {
        return;
    }
    log_info("基础数据, 在全局列表中删除卡成功, type=%d, id=%d",card_ptr->m_type, card_ptr->m_id);
    //    auto area_hover_ptr = card_ptr->get_area_hover();
    //    if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
    //    {
    //        module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
    //    }

    //    if(card_ptr->is_person())
    //    {
    //        module_attendance_person::up_mine(card_ptr, nullptr);
    //    }
    //    else
    //    {
    //        module_attendance_vehicle::save_attendance(card_ptr, nullptr);

    //        module_meta_date_changed::clear_card(card_ptr);
    //    }
    module_meta_date_changed::clear_card(card_ptr);
    // 避免状态重置
    uint64_t card_id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
    card_list::instance()->remove(card_id);
}

bool module_meta_date_changed::try_get_edit_type_id(const std::string& op_type, EDIT_TYPE_ID& out_edit_type_id)
{
    if(JSON_KEY_OP_TYPE_UPDATE == op_type)
    {
        out_edit_type_id = ET_UPDATE;
    }
    else if(JSON_KEY_OP_TYPE_DELETE == op_type)
    {
        out_edit_type_id = ET_DELETE;
    }
    else if(JSON_KEY_OP_TYPE_INSERT == op_type)
    {
        out_edit_type_id = ET_INSERT;
    }
    else
    {
        return false;
    }

    return true;
}

///删除区域所有报警信息
//void module_meta_date_changed::delete_area_event(std::shared_ptr<area> area_ptr)
//{
//    for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
//    {
//        event_tool::instance()->handle_event(OT_AREA, static_cast<EVENT_TYPE>(i), area_ptr->id(), 0, 0, false);
//    }
//}

//void module_meta_date_changed::delete_antenna(int id)
//{
//    int sitid=-8;
//    auto map = sit_list::instance()->m_map;
//    for(auto&it:map)
//    {
//        auto sit_ptr = it.second;
//        if(sit_ptr->m_ant[0].m_id == id)
//        {
//            sitid=sit_ptr->m_id;
//            sit_ptr->delete_antenna(0);
//            break;
//        }
//        if(sit_ptr->m_ant[1].m_id == id)
//        {
//            sitid=sit_ptr->m_id;
//            sit_ptr->delete_antenna(1);
//            break;
//        }
//    }
//
//    if(sitid==-8)
//    {
//        std_debug("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
//        log_info("天线已经删除了,在分站列表中找不到该天线:天线id=%d", id);
//    }
//    else
//    {
//        std_debug("删除天线成功:天线id=%d,分站id=%d", id, sitid);
//        log_info("删除天线成功:天线id=%d,分站id=%d", id, sitid);
//    }
//}
//
bool module_meta_date_changed::is_cur_map(int id)
{
    return cur_map_id()==id;
}

int module_meta_date_changed::cur_map_id()
{
    auto tmp = area_list::instance()->m_map;
    for(auto&it:tmp)
    {
        if(0!=it.second->m_mapid)
        {
            return it.second->m_mapid;
        }
    }

    return -1;
}

void module_meta_date_changed::update_map_info(int id)
{
    std::string sql = "SELECT scale FROM dat_map WHERE map_id=";

    sql.append(std::to_string(id));
    sql.append(";");

    std_debug("修改地图 sql=%s", sql.c_str());
    log_info("修改地图 sql=%s", sql.c_str());

    std::string Error;
    YADB::CDBResultSet DBRes;
    sDBConnPool.Query(sql.c_str(),DBRes,Error);
    int nCount = DBRes.GetRecordCount( Error );
    if (nCount < 1)
    {
        log_error("修改地图失败,数据库中找不到: map_id=%d", id);
        return ;
    }
    double scale  = 0;
    if (DBRes.GetNextRecod(Error))
    {
        DBRes.GetField("scale", scale, Error);
    }
    if (scale < 1e-4)
    {
        log_error("修改地图,数据库地图比例尺错误: map_id=%d,scale=%.2f", id,scale);
        return;
    }
    auto area = area_list::instance()->m_map;
    for(auto& it : area)
    {
        it.second->m_scale = scale;
    }

    auto site = sit_list::instance()->m_map;
    for(auto& it : site)
    {
        it.second->m_scale = scale;
    }
}