db_card.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <memory>
  2. #include <unordered_map>
  3. #include "log.h"
  4. #include "config_file.h"
  5. #include "db_api/CDBResultSet.h"
  6. #include "db_api/CDBSingletonDefine.h"
  7. #include "../card_base.h"
  8. #include "../card_person.h"
  9. #include "../common.h"
  10. #include "../common_tool.h"
  11. extern config_file config;
  12. namespace db_card
  13. {
  14. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> load_car(const std::string & lszId64)
  15. {
  16. std::string sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, cf.freq_value, \
  17. ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
  18. vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
  19. vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed, ve.enable_anti_collision \
  20. FROM dat_vehicle_extend ve \
  21. LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
  22. LEFT JOIN dat_card c ON ve.card_id = c.card_id \
  23. LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
  24. LEFT JOIN dat_group g ON ve.group_id = g.group_id \
  25. LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
  26. LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
  27. LEFT JOIN dat_card_freq cf on cf.freq_id = c.freq_id \
  28. WHERE c.state_id = 0";
  29. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  30. if("" == lszId64)
  31. {
  32. sql.append(";");
  33. }
  34. else
  35. {
  36. sql.append(" AND ve.card_id ='");
  37. sql.append(lszId64);
  38. sql.append("';");
  39. }
  40. std::string Error;
  41. YADB::CDBResultSet DBRes;
  42. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  43. int nCount = DBRes.GetRecordCount( Error );
  44. if (nCount < 1)
  45. {
  46. log_error("增加或修改失败,数据库中找不到: sql=%s", sql.c_str());
  47. return map;
  48. }
  49. while ( DBRes.GetNextRecod(Error) )
  50. {
  51. unsigned int vehicle_id = 0;
  52. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  53. std::string card_id;
  54. DBRes.GetField( "card_id",card_id, Error );
  55. uint32_t vsid = atoi(card_id.substr(3).c_str());
  56. unsigned int card_type_id = 0;
  57. DBRes.GetField( "card_type_id",card_type_id, Error );
  58. double freq = 0;
  59. DBRes.GetField("freq_value", freq, Error);
  60. int dept_id = 0;
  61. DBRes.GetField( "dept_id",dept_id, Error );
  62. int group_id = 0;
  63. DBRes.GetField( "group_id",group_id, Error );
  64. int vehicle_type_id = 0;
  65. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  66. int vehicle_level_id = 0;
  67. DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );
  68. int need_display = 0;
  69. DBRes.GetField( "need_display",need_display, Error );
  70. int power_alarm = 0;
  71. DBRes.GetField( "power_alarm",power_alarm, Error );
  72. int vehicle_category_id = 0;
  73. DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );
  74. int bigger_car_flag= 0;
  75. DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );
  76. double over_speed= 0;
  77. DBRes.GetField( "over_speed",over_speed, Error );
  78. uint32_t type = tool_other::card_id_to_type(card_id);
  79. if(type != card_type_id)
  80. {
  81. log_error("init_vehicle_card error.Type is not matching..");
  82. continue;
  83. }
  84. std::string strategy;
  85. if(card_type_id ==CT_VEHICLE)
  86. strategy = config.get("car.strategy","CS_1");
  87. else if(vehicle_type_id==VT_COAL_CUTTER)
  88. strategy = config.get("coalface.strategy","WS_1");
  89. else
  90. strategy = config.get("person.strategy","PS_1");
  91. auto clb = card_location_base::make_car(strategy,vsid,need_display,card_type_id,
  92. dept_id,vehicle_category_id, vehicle_type_id,vehicle_level_id,vehicle_id);
  93. uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
  94. clb->m_freq = freq;
  95. int anti_collision = 0;
  96. DBRes.GetField("enable_anti_collision", anti_collision, Error);
  97. clb->m_enable_anti_collision = ((anti_collision == 1) ? true : false);
  98. map.insert({cardid,clb});
  99. log_info("cardId:%s,id:%d dept_id:%d,need_display:%d-cardid:%s,categoryid:%d,vchile_id:%d,type:%d,vehicle_type_id:%d, anti_collision:%d",
  100. card_id.c_str(),
  101. vsid,
  102. dept_id,
  103. need_display,
  104. card_id.c_str(),
  105. vehicle_category_id,
  106. vehicle_id,
  107. card_type_id,
  108. vehicle_type_id,
  109. anti_collision
  110. );
  111. }
  112. return map;
  113. }
  114. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> load_person(const std::string & lszId64,const std::string&strategy)
  115. {
  116. std::string sql = "SELECT s.staff_id, s.card_id, c.card_type_id, cf.freq_value, s.dept_id, s.group_id, s.occupation_id, \
  117. ol.occupation_level_id,s.worktype_id,s.need_display,s.work_line\
  118. ,ds.name as staffer_name,dd.name as dept_name\
  119. FROM dat_staff_extend s \
  120. LEFT JOIN dat_staff ds ON ds.staff_id = s.staff_id \
  121. LEFT JOIN dat_dept dd ON dd.dept_id = s.dept_id \
  122. LEFT JOIN dat_card c ON s.card_id = c.card_id \
  123. LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
  124. LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
  125. LEFT JOIN dat_card_freq cf on cf.freq_id = c.freq_id \
  126. WHERE s.duty_id = 0 AND c.state_id = 0";
  127. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  128. if("" == lszId64)
  129. {
  130. sql.append(";");
  131. }
  132. else
  133. {
  134. sql.append(" AND s.card_id = '");
  135. sql.append(lszId64);
  136. sql.append("';");
  137. }
  138. std::string Error;
  139. YADB::CDBResultSet DBRes;
  140. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  141. int nCount = DBRes.GetRecordCount( Error );
  142. if (nCount < 1)
  143. {
  144. log_error("增加或修改失败,数据库中找不到: sql=%s", sql.c_str());
  145. return map;
  146. }
  147. while ( DBRes.GetNextRecod(Error) )
  148. {
  149. unsigned int staff_id = 0;
  150. DBRes.GetField( "staff_id",staff_id, Error );
  151. std::string card_id;
  152. DBRes.GetField( "card_id",card_id, Error );
  153. uint32_t vsid = atoi(card_id.substr(3).c_str());
  154. unsigned int card_type_id = 0;
  155. DBRes.GetField( "card_type_id",card_type_id, Error );
  156. double freq = 0;
  157. DBRes.GetField("freq_value", freq, Error);
  158. int dept_id = 0;
  159. DBRes.GetField( "dept_id",dept_id, Error );
  160. int group_id = 0;
  161. DBRes.GetField( "group_id",group_id, Error );
  162. int occupation_id = 0;
  163. DBRes.GetField( "occupation_id",occupation_id, Error );
  164. int occupation_level_id = 0;
  165. DBRes.GetField( "occupation_level_id",occupation_level_id, Error );
  166. int need_display = 0;
  167. DBRes.GetField( "need_display",need_display, Error );
  168. int work_line = 0;
  169. DBRes.GetField( "work_line",work_line, Error );
  170. std::string staffer_name;
  171. DBRes.GetField( "staffer_name",staffer_name, Error );
  172. std::string dept_name;
  173. DBRes.GetField( "dept_name",dept_name, Error );
  174. int worktype_id;
  175. DBRes.GetField( "worktype_id",worktype_id, Error );
  176. uint32_t type = tool_other::card_id_to_type(card_id);
  177. if(type != card_type_id)
  178. {
  179. log_error("init_staffer_card error.Type is not matching..");
  180. continue;
  181. }
  182. std::shared_ptr<card_location_base> clb =
  183. std::make_shared<person>(strategy, vsid, need_display, card_type_id, dept_id, occupation_level_id, staff_id, work_line, staffer_name, dept_name, worktype_id);
  184. uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
  185. clb->m_freq = freq;
  186. log_info("Init_card.cardId:%llu,id:%d dept_id:%d,need_display:%d,card:%s:work_line:%d,staff_id:%d,type:%d,staffer_name:%s,dept_name:%s",
  187. cardid, vsid, dept_id, need_display, card_id.c_str(), work_line, staff_id, card_type_id,staffer_name.c_str(), dept_name.c_str());
  188. map.insert({cardid,clb});
  189. }
  190. return map;
  191. }
  192. }