common_data.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Created by Administrator on 2019/1/17.
  3. //
  4. #include "common_data.h"
  5. #include "db_api/CDBSingletonDefine.h"
  6. #include "log.h"
  7. common_data::common_data()
  8. {
  9. m_map_scale = 2;
  10. }
  11. common_data::~common_data()
  12. {
  13. }
  14. void common_data::init_data()
  15. {
  16. init_data_workface();
  17. init_data_bind_card_vechicle();
  18. init_data_bind_card_workface();
  19. init_data_map();
  20. m_map_scale = 2;
  21. m_cur_map_id = 0;
  22. }
  23. /*初始化工作面*/
  24. int common_data::init_data_workface()
  25. {
  26. const char * sql = "SELECT work_face_id, work_face_type,name,map_id,area_id,lastUpdate FROM dat_work_face;";
  27. std::string Error = "";
  28. YADB::CDBResultSet DBRes;
  29. if (0 == sDBConnPool.Query(sql,DBRes,Error))
  30. {
  31. logn_error(2,"TR-init_data_workface Query exist error, error: %s", Error.c_str());
  32. return 1;
  33. }
  34. int nCount = DBRes.GetRecordCount( Error );
  35. if (nCount > 0)
  36. {
  37. m_workface.clear();
  38. while ( DBRes.GetNextRecod(Error) )
  39. {
  40. std::shared_ptr<WorkfaceDBData> p = std::make_shared<WorkfaceDBData>();
  41. DBRes.GetField( "work_face_id",p->workface_id, Error );
  42. DBRes.GetField( "work_face_type",p->workface_type, Error );
  43. DBRes.GetField( "name",p->workface_name, Error );
  44. DBRes.GetField( "map_id",p->map_id, Error );
  45. DBRes.GetField( "area_id",p->area_id, Error );
  46. DBRes.GetField( "lastUpdate",p->lastUpdateTime, Error );
  47. m_workface.insert(make_pair(p->workface_id,p));
  48. }
  49. }
  50. return 0;
  51. }
  52. /*初始化车卡对应车卡ID*/
  53. int common_data::init_data_bind_card_vechicle()
  54. {
  55. const char * sql = "SELECT vehicle_id, card_id FROM dat_vehicle_extend;";
  56. std::string Error;
  57. YADB::CDBResultSet DBRes;
  58. if (0 == sDBConnPool.Query(sql,DBRes,Error))
  59. {
  60. logn_error(2,"TR-init_data_bind_card_vechicle Query exist error, error: %s", Error.c_str());
  61. return 1;
  62. }
  63. int nCount = DBRes.GetRecordCount( Error );
  64. if (nCount > 0)
  65. {
  66. m_card_vehicle.clear();
  67. while ( DBRes.GetNextRecod(Error) )
  68. {
  69. int vehicle_id = 0;
  70. std::string card_id = "";
  71. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  72. DBRes.GetField( "card_id",card_id, Error );
  73. m_card_vehicle.insert(make_pair(card_id,vehicle_id));
  74. m_vehicle_card.insert(make_pair(vehicle_id,card_id));
  75. }
  76. }
  77. return 0;
  78. }
  79. /*初始化车卡对应工作面 */
  80. int common_data::init_data_bind_card_workface()
  81. {
  82. const char * sql = "SELECT b.coalface_id as face_id , a.card_id FROM dat_vehicle_extend a, dat_coalface_vehicle b "
  83. "where a.vehicle_id = b.vehicle_id "
  84. "UNION "
  85. "SELECT b.drivingface_id as face_id , a.card_id FROM dat_vehicle_extend a, dat_drivingface_vehicle b "
  86. "where a.vehicle_id = b.vehicle_id;";
  87. std::string Error;
  88. YADB::CDBResultSet DBRes;
  89. if (0 == sDBConnPool.Query(sql,DBRes,Error))
  90. {
  91. logn_error(2,"TR-init_data_bind_card_workface Query exist error, error: %s", Error.c_str());
  92. return 1;
  93. }
  94. int nCount = DBRes.GetRecordCount( Error );
  95. if (nCount > 0)
  96. {
  97. m_card_workface.clear();
  98. while ( DBRes.GetNextRecod(Error) )
  99. {
  100. int face_id = 0;
  101. std::string card_id = "";
  102. DBRes.GetField( "face_id",face_id, Error );
  103. DBRes.GetField( "card_id",card_id, Error );
  104. card_id = "00" + card_id;
  105. m_card_workface.insert(make_pair(card_id,face_id));
  106. }
  107. }
  108. return 0;
  109. }
  110. /*
  111. * 初始化地图信息,主要获得地图的比例尺数据
  112. */
  113. void common_data::init_data_map()
  114. {
  115. const char* sql = "select map_id,scale from dat_map_gis;";
  116. YADB::CDBResultSet DBRes;
  117. std::string szError;
  118. if (0 == sDBConnPool.Query(sql,DBRes,szError))
  119. {
  120. logn_error(2,"TR-init_data_bind_card_workface Query exist error, error: %s", szError.c_str());
  121. return ;
  122. }
  123. int nCount = DBRes.GetRecordCount( szError );
  124. if (nCount > 0)
  125. {
  126. m_map_list.clear();
  127. while (DBRes.GetNextRecod(szError))
  128. {
  129. map_info mapinfo;
  130. int map_id;
  131. double map_scale;
  132. DBRes.GetField("map_id",map_id, szError);
  133. DBRes.GetField("scale", map_scale, szError);
  134. mapinfo.m_map_id = map_id;
  135. mapinfo.m_map_scale = map_scale;
  136. m_map_list[map_id] = mapinfo;
  137. }
  138. }
  139. }
  140. /*根据卡ID获取工作面ID */
  141. int common_data::get_workface_id(std::string card_id)
  142. {
  143. auto it = m_card_workface.find(card_id);
  144. if (it != m_card_workface.end())
  145. {
  146. return it->second;
  147. }
  148. return -1;
  149. }
  150. /*根据卡ID获取车ID */
  151. int common_data::get_vechicle_id(std::string card_id)
  152. {
  153. auto it = m_card_vehicle.find(card_id);
  154. if (it != m_card_vehicle.end())
  155. {
  156. return it->second;
  157. }
  158. return -1;
  159. }
  160. /*根据车ID获取卡ID
  161. * return "" : 表示不存在
  162. * */
  163. std::string common_data::get_card_id(int vechicle_id)
  164. {
  165. auto it = m_vehicle_card.find(vechicle_id);
  166. if (it != m_vehicle_card.end())
  167. {
  168. return it->second;
  169. }
  170. return "";
  171. }
  172. /*获取工作面的DB数据* */
  173. std::shared_ptr<WorkfaceDBData> common_data::get_workface(int work_face_id)
  174. {
  175. auto it = m_workface.find(work_face_id);
  176. if (it != m_workface.end())
  177. {
  178. return it->second;
  179. }
  180. return nullptr;
  181. }
  182. /*设置当前的地图ID*/
  183. void common_data::set_map_id(int map_id)
  184. {
  185. if (map_id == m_cur_map_id)
  186. {
  187. return;
  188. }
  189. auto it = m_map_list.find(map_id);
  190. if (it == m_map_list.end())
  191. {
  192. return;
  193. }
  194. m_cur_map_id = map_id;
  195. m_map_scale = it->second.m_map_scale;
  196. }