db_card.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. std::string sql = "SELECT ve.vehicle_id, ve.card_id, ve.dept_id, c.card_type_id, cf.freq_value, v.vehicle_type_id \
  114. FROM dat_vehicle_extend ve \
  115. LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
  116. LEFT JOIN dat_card c ON ve.card_id = c.card_id \
  117. LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
  118. LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
  119. LEFT JOIN dat_card_freq cf on cf.freq_id = c.freq_id \
  120. WHERE c.state_id = 0";
  121. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  122. if("" == lszId64)
  123. {
  124. sql.append(";");
  125. }
  126. else
  127. {
  128. sql.append(" AND ve.card_id ='");
  129. sql.append(lszId64);
  130. sql.append("';");
  131. }
  132. std::string Error;
  133. YADB::CDBResultSet DBRes;
  134. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  135. int nCount = DBRes.GetRecordCount( Error );
  136. if (nCount < 1)
  137. {
  138. log_error("增加或修改失败,数据库中找不到: sql=%s", sql.c_str());
  139. return map;
  140. }
  141. while ( DBRes.GetNextRecod(Error) )
  142. {
  143. unsigned int vehicle_id = 0;
  144. DBRes.GetField( "vehicle_id",vehicle_id, Error );
  145. std::string card_id;
  146. DBRes.GetField( "card_id",card_id, Error );
  147. uint32_t vsid = atoi(card_id.substr(3).c_str());
  148. unsigned int card_type_id = 0;
  149. DBRes.GetField( "card_type_id",card_type_id, Error );
  150. double freq = 0;
  151. DBRes.GetField("freq_value", freq, Error);
  152. int dept_id = 0;
  153. DBRes.GetField( "dept_id",dept_id, Error );
  154. int group_id = 0;
  155. //DBRes.GetField( "group_id",group_id, Error );
  156. int vehicle_type_id = 0;
  157. DBRes.GetField( "vehicle_type_id",vehicle_type_id, Error );
  158. int vehicle_level_id = 0;
  159. //DBRes.GetField( "vehicle_level_id",vehicle_level_id, Error );
  160. int need_display = 0;
  161. //DBRes.GetField( "need_display",need_display, Error );
  162. int power_alarm = 0;
  163. //DBRes.GetField( "power_alarm",power_alarm, Error );
  164. int vehicle_category_id = 0;
  165. //DBRes.GetField( "vehicle_category_id",vehicle_category_id, Error );
  166. int bigger_car_flag= 0;
  167. DBRes.GetField( "bigger_car_flag",bigger_car_flag, Error );
  168. double over_speed= 0;
  169. //DBRes.GetField( "over_speed",over_speed, Error );
  170. uint32_t type = tool_other::card_id_to_type(card_id);
  171. if(type != card_type_id)
  172. {
  173. log_error("init_vehicle_card error.Type is not matching..");
  174. continue;
  175. }
  176. std::string strategy;
  177. if(card_type_id ==CT_VEHICLE)
  178. strategy = config.get("car.strategy","CS_1");
  179. else if(vehicle_type_id==VT_COAL_CUTTER)
  180. strategy = config.get("coalface.strategy","WS_1");
  181. else
  182. strategy = config.get("person.strategy","PS_1");
  183. auto clb = card_location_base::make_car(strategy,vsid,need_display,card_type_id,
  184. dept_id,vehicle_category_id, vehicle_type_id,vehicle_level_id,vehicle_id);
  185. uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
  186. clb->m_freq = freq;
  187. int anti_collision = 0;
  188. //DBRes.GetField("enable_anti_collision", anti_collision, Error);
  189. clb->m_enable_anti_collision = ((anti_collision == 1) ? true : false);
  190. map.insert({cardid,clb});
  191. 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",
  192. card_id.c_str(),
  193. vsid,
  194. dept_id,
  195. need_display,
  196. card_id.c_str(),
  197. vehicle_category_id,
  198. vehicle_id,
  199. card_type_id,
  200. vehicle_type_id,
  201. anti_collision
  202. );
  203. }
  204. return map;
  205. }
  206. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> load_person(const std::string & lszId64,const std::string&strategy)
  207. {
  208. 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, \
  209. ol.occupation_level_id,s.worktype_id,s.need_display,s.work_line\
  210. ,ds.name as staffer_name,dd.name as dept_name\
  211. FROM dat_staff_extend s \
  212. LEFT JOIN dat_staff ds ON ds.staff_id = s.staff_id \
  213. LEFT JOIN dat_dept dd ON dd.dept_id = s.dept_id \
  214. LEFT JOIN dat_card c ON s.card_id = c.card_id \
  215. LEFT JOIN dat_occupation o ON s.occupation_id = o.occupation_id \
  216. LEFT JOIN dat_occupation_level ol ON ol.occupation_level_id = o.occupation_level_id \
  217. LEFT JOIN dat_card_freq cf on cf.freq_id = c.freq_id \
  218. WHERE s.duty_id = 0 AND c.state_id = 0";
  219. std::unordered_map<uint64_t,std::shared_ptr<card_location_base>> map;
  220. if("" == lszId64)
  221. {
  222. sql.append(";");
  223. }
  224. else
  225. {
  226. sql.append(" AND s.card_id = '");
  227. sql.append(lszId64);
  228. sql.append("';");
  229. }
  230. std::string Error;
  231. YADB::CDBResultSet DBRes;
  232. sDBConnPool.Query(sql.c_str(),DBRes,Error);
  233. int nCount = DBRes.GetRecordCount( Error );
  234. if (nCount < 1)
  235. {
  236. log_error("增加或修改失败,数据库中找不到: sql=%s", sql.c_str());
  237. return map;
  238. }
  239. while ( DBRes.GetNextRecod(Error) )
  240. {
  241. unsigned int staff_id = 0;
  242. DBRes.GetField( "staff_id",staff_id, Error );
  243. std::string card_id;
  244. DBRes.GetField( "card_id",card_id, Error );
  245. uint32_t vsid = atoi(card_id.substr(3).c_str());
  246. unsigned int card_type_id = 0;
  247. DBRes.GetField( "card_type_id",card_type_id, Error );
  248. double freq = 0;
  249. DBRes.GetField("freq_value", freq, Error);
  250. int dept_id = 0;
  251. DBRes.GetField( "dept_id",dept_id, Error );
  252. int group_id = 0;
  253. DBRes.GetField( "group_id",group_id, Error );
  254. int occupation_id = 0;
  255. DBRes.GetField( "occupation_id",occupation_id, Error );
  256. int occupation_level_id = 0;
  257. DBRes.GetField( "occupation_level_id",occupation_level_id, Error );
  258. int need_display = 0;
  259. DBRes.GetField( "need_display",need_display, Error );
  260. int work_line = 0;
  261. DBRes.GetField( "work_line",work_line, Error );
  262. std::string staffer_name;
  263. DBRes.GetField( "staffer_name",staffer_name, Error );
  264. std::string dept_name;
  265. DBRes.GetField( "dept_name",dept_name, Error );
  266. int worktype_id;
  267. DBRes.GetField( "worktype_id",worktype_id, Error );
  268. uint32_t type = tool_other::card_id_to_type(card_id);
  269. if(type != card_type_id)
  270. {
  271. log_error("init_staffer_card error.Type is not matching..");
  272. continue;
  273. }
  274. std::shared_ptr<card_location_base> clb =
  275. 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);
  276. uint64_t cardid = tool_other::type_id_to_u64(card_type_id,vsid);
  277. clb->m_freq = freq;
  278. 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",
  279. 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());
  280. map.insert({cardid,clb});
  281. }
  282. return map;
  283. }
  284. }