// // Created by Administrator on 2019/1/17. // #include "common_data.h" #include "db_api/CDBSingletonDefine.h" #include "log.h" common_data::common_data() { m_map_scale = 2; } common_data::~common_data() { } void common_data::init_data() { init_data_workface(); init_data_bind_card_vechicle(); init_data_bind_card_workface(); init_data_map(); m_map_scale = 2; m_cur_map_id = 0; } /*初始化工作面*/ int common_data::init_data_workface() { const char * sql = "SELECT work_face_id, work_face_type,name,map_id,area_id,lastUpdate FROM dat_work_face;"; std::string Error = ""; YADB::CDBResultSet DBRes; if (0 == sDBConnPool.Query(sql,DBRes,Error)) { logn_error(2,"TR-init_data_workface Query exist error, error: %s", Error.c_str()); return 1; } int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { m_workface.clear(); while ( DBRes.GetNextRecod(Error) ) { std::shared_ptr p = std::make_shared(); DBRes.GetField( "work_face_id",p->workface_id, Error ); DBRes.GetField( "work_face_type",p->workface_type, Error ); DBRes.GetField( "name",p->workface_name, Error ); DBRes.GetField( "map_id",p->map_id, Error ); DBRes.GetField( "area_id",p->area_id, Error ); DBRes.GetField( "lastUpdate",p->lastUpdateTime, Error ); m_workface.insert(make_pair(p->workface_id,p)); } } return 0; } /*初始化车卡对应车卡ID*/ int common_data::init_data_bind_card_vechicle() { const char * sql = "SELECT vehicle_id, card_id FROM dat_vehicle_extend;"; std::string Error; YADB::CDBResultSet DBRes; if (0 == sDBConnPool.Query(sql,DBRes,Error)) { logn_error(2,"TR-init_data_bind_card_vechicle Query exist error, error: %s", Error.c_str()); return 1; } int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { m_card_vehicle.clear(); while ( DBRes.GetNextRecod(Error) ) { int vehicle_id = 0; std::string card_id = ""; DBRes.GetField( "vehicle_id",vehicle_id, Error ); DBRes.GetField( "card_id",card_id, Error ); m_card_vehicle.insert(make_pair(card_id,vehicle_id)); m_vehicle_card.insert(make_pair(vehicle_id,card_id)); } } return 0; } /*初始化车卡对应工作面 */ int common_data::init_data_bind_card_workface() { const char * sql = "SELECT b.coalface_id as face_id , a.card_id FROM dat_vehicle_extend a, dat_coalface_vehicle b " "where a.vehicle_id = b.vehicle_id " "UNION " "SELECT b.drivingface_id as face_id , a.card_id FROM dat_vehicle_extend a, dat_drivingface_vehicle b " "where a.vehicle_id = b.vehicle_id;"; std::string Error; YADB::CDBResultSet DBRes; if (0 == sDBConnPool.Query(sql,DBRes,Error)) { logn_error(2,"TR-init_data_bind_card_workface Query exist error, error: %s", Error.c_str()); return 1; } int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { m_card_workface.clear(); while ( DBRes.GetNextRecod(Error) ) { int face_id = 0; std::string card_id = ""; DBRes.GetField( "face_id",face_id, Error ); DBRes.GetField( "card_id",card_id, Error ); card_id = "00" + card_id; m_card_workface.insert(make_pair(card_id,face_id)); } } return 0; } /* * 初始化地图信息,主要获得地图的比例尺数据 */ void common_data::init_data_map() { const char* sql = "select map_id,scale from dat_map_gis;"; YADB::CDBResultSet DBRes; std::string szError; if (0 == sDBConnPool.Query(sql,DBRes,szError)) { logn_error(2,"TR-init_data_bind_card_workface Query exist error, error: %s", szError.c_str()); return ; } int nCount = DBRes.GetRecordCount( szError ); if (nCount > 0) { m_map_list.clear(); while (DBRes.GetNextRecod(szError)) { map_info mapinfo; int map_id; double map_scale; DBRes.GetField("map_id",map_id, szError); DBRes.GetField("scale", map_scale, szError); mapinfo.m_map_id = map_id; mapinfo.m_map_scale = map_scale; m_map_list[map_id] = mapinfo; } } } /*根据卡ID获取工作面ID */ int common_data::get_workface_id(std::string card_id) { auto it = m_card_workface.find(card_id); if (it != m_card_workface.end()) { return it->second; } return -1; } /*根据卡ID获取车ID */ int common_data::get_vechicle_id(std::string card_id) { auto it = m_card_vehicle.find(card_id); if (it != m_card_vehicle.end()) { return it->second; } return -1; } /*根据车ID获取卡ID * return "" : 表示不存在 * */ std::string common_data::get_card_id(int vechicle_id) { auto it = m_vehicle_card.find(vechicle_id); if (it != m_vehicle_card.end()) { return it->second; } return ""; } /*获取工作面的DB数据* */ std::shared_ptr common_data::get_workface(int work_face_id) { auto it = m_workface.find(work_face_id); if (it != m_workface.end()) { return it->second; } return nullptr; } /*设置当前的地图ID*/ void common_data::set_map_id(int map_id) { if (map_id == m_cur_map_id) { return; } auto it = m_map_list.find(map_id); if (it == m_map_list.end()) { return; } m_cur_map_id = map_id; m_map_scale = it->second.m_map_scale; }