DataManager.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "stdafx.h"
  2. #include "DataManager.h"
  3. #include "DBInterface.h"
  4. #include "MachineMgrBase.h"
  5. #include "FpClockCtrlMgr.h"
  6. #include "FKAttendCtrlMgr.h"
  7. #define INSERT_DATA _T("insert into lonindata([no],[datetime])values('%d','%s')")
  8. CDataManager::CDataManager()
  9. {
  10. _hThread = NULL;
  11. _hThread = NULL;
  12. _hCtrl1 = NULL;
  13. m_bThreadRuning = FALSE;
  14. m_bThreadRuning1 = FALSE;
  15. }
  16. CDataManager::~CDataManager()
  17. {
  18. }
  19. /*-------------------------------实时提交 begin-------------------------------*/
  20. //启动
  21. int CDataManager::StartRealTimeSubmitThread()
  22. {
  23. AutoThreadSection aSection(&s_critSection);
  24. if (_hThread) return -1;
  25. _hThread = CreateThread(NULL, 0, RealTimeSubmitThread, this, 0, NULL);
  26. if (_hThread == NULL)
  27. {
  28. LOG4C((LOG_NOTICE, "创建线程失败"));
  29. return -1;
  30. }
  31. CloseHandle(_hThread);
  32. _hThread = NULL;
  33. m_bThreadRuning = TRUE;
  34. return 1;
  35. }
  36. //结束
  37. int CDataManager::EndofRealTimeSubmitThread()
  38. {
  39. if (_hThread)
  40. WaitForSingleObject(_hThread, INFINITE);
  41. m_bThreadRuning = FALSE;
  42. return 1;
  43. }
  44. int CDataManager::RealTimeSubmitWork()
  45. {
  46. if(g_pCMachineMgrBase == NULL)
  47. {
  48. LOG4C((LOG_NOTICE, "指纹机没连接成功"));
  49. return 0;
  50. }
  51. if(CDBInterface::GetInstance()->IsOpen())
  52. {
  53. //读取数据前先设置ReadMark为TRUE
  54. g_pCMachineMgrBase->SetReadMark(TRUE);
  55. if(g_pCMachineMgrBase->SetMachineEnable(FALSE))
  56. {
  57. // LOG4C((LOG_NOTICE, "实时提交启动"));
  58. std::vector<STGeneralLogData> vtGeneralLogData;
  59. if(g_pCMachineMgrBase->ReadGeneralLogData(&vtGeneralLogData) != -1)
  60. WriteIntoDatabase(&vtGeneralLogData);
  61. }
  62. g_pCMachineMgrBase->SetMachineEnable(TRUE);
  63. }
  64. AutoThreadSection aSection(&s_critSection);
  65. m_bThreadRuning = FALSE;
  66. return 1;
  67. }
  68. DWORD CDataManager::RealTimeSubmitThread(LPVOID lpParam)
  69. {
  70. CDataManager* p = (CDataManager*)lpParam;
  71. if(p)
  72. p->RealTimeSubmitWork();
  73. return 0;
  74. }
  75. /*-------------------------------实时提交 end-------------------------------*/
  76. /*-------------------------------全部提交 begin-------------------------------*/
  77. //启动
  78. int CDataManager::StartSubmitAllThread()
  79. {
  80. _hCtrl1 = CreateEvent(NULL, TRUE, FALSE, NULL); // 无信号事件;
  81. if (_hCtrl1 == NULL)
  82. {
  83. LOG4C((LOG_NOTICE, "创建事件失败"));
  84. //PostMessage(g_hwnd,MSG_SHOWPROMPTING,0,0);
  85. return 0;
  86. }
  87. _hThread1 = CreateThread(NULL, 0, SubmitAllDataThread, this, 0, NULL);
  88. if (_hThread1 == NULL)
  89. {
  90. LOG4C((LOG_NOTICE, "创建线程失败"));
  91. return 0;
  92. }
  93. m_bThreadRuning1 = TRUE;
  94. return 1;
  95. }
  96. //结束
  97. int CDataManager::EndofSubmitAllThread()
  98. {
  99. // 1.关闭线程------------------------;;
  100. if (_hCtrl1)
  101. SetEvent(_hCtrl1);
  102. if (_hThread1)
  103. {
  104. if (WaitForSingleObject(_hThread1, INFINITE) != WAIT_TIMEOUT)
  105. CloseHandle(_hThread1);
  106. }
  107. _hThread1 = NULL;
  108. m_bThreadRuning1 = FALSE;
  109. if (_hCtrl1)
  110. CloseHandle(_hCtrl1);
  111. _hCtrl1 = NULL;
  112. return 1;
  113. }
  114. //
  115. int CDataManager::SubmitAllWork()
  116. {
  117. if(g_pCMachineMgrBase == NULL)
  118. return 0;
  119. // 1.因为全部提交数据量比较大,时间长,先设置指纹机不可用;
  120. if (g_pCMachineMgrBase->SetMachineEnable(FALSE) == FALSE)
  121. {
  122. if (_hCtrl1)
  123. CloseHandle(_hCtrl1);
  124. _hCtrl1 = NULL;
  125. if (_hThread1)
  126. CloseHandle(_hThread1);
  127. _hThread1 = NULL;
  128. AutoThreadSection aSection(&s_critSection);
  129. m_bThreadRuning1 = FALSE;
  130. return 0;
  131. }
  132. // 3.循环读取全部记录;
  133. std::vector<STGeneralLogData> vtGeneralLogData;
  134. if(g_pCMachineMgrBase->ReadAllGlLogData(&vtGeneralLogData) != -1)
  135. WriteIntoDatabase(&vtGeneralLogData);
  136. g_pCMachineMgrBase->SetMachineEnable(TRUE);
  137. if (_hCtrl1)
  138. CloseHandle(_hCtrl1);
  139. if (_hThread1)
  140. CloseHandle(_hThread1);
  141. _hCtrl1 = NULL;
  142. _hThread1 = NULL;
  143. AutoThreadSection aSection(&s_critSection);
  144. m_bThreadRuning1 = FALSE;
  145. return 1;
  146. }
  147. DWORD CDataManager::SubmitAllDataThread(LPVOID lpParam)
  148. {
  149. CDataManager* p = (CDataManager*)lpParam;
  150. if(p)
  151. p->SubmitAllWork();
  152. return 0;
  153. }
  154. /*-------------------------------全部提交 end-------------------------------*/
  155. DWORD CDataManager::WriteIntoDatabase(std::vector<STGeneralLogData>* pVData)
  156. {
  157. if(pVData == NULL)
  158. return 0;
  159. CString strSQL;
  160. if (CDBInterface::GetInstance()->IsOpen())
  161. {
  162. std::vector<STGeneralLogData>::iterator it = pVData->begin();
  163. DWORD dwInsertCount = 0;
  164. for(; it != pVData->end(); )
  165. {
  166. COleDateTime tagOleTime(it->dwYear, it->dwMonth, it->dwDay, it->dwHour, it->dwMinute, it->nVerifyMode);
  167. SendMessage(g_hwnd, MSG_SHOWREALTIMELOG, (WPARAM)&it->nEnrollNumber, (LPARAM)&tagOleTime);
  168. // PostMessage(g_hwnd, MSG_SHOWREALTIMELOG, (WPARAM)&it->nEnrollNumber, (LPARAM)&tagOleTime);
  169. strSQL.Format(INSERT_DATA, it->nEnrollNumber, tagOleTime.Format(_T("%Y-%m-%d %H:%M:%S")));
  170. ++it;
  171. if (CDBInterface::GetInstance()->Execute(strSQL) != 0)
  172. continue;
  173. dwInsertCount++;
  174. Sleep(10);
  175. }
  176. return dwInsertCount;
  177. }
  178. return 0;
  179. }
  180. DWORD CDataManager::ReadOldLog(std::vector<STAllLog>& vtAllLog)
  181. {
  182. CString strSQL;
  183. std::vector<STAllLog>::iterator it;
  184. if( CDBInterface::GetInstance()->OpenDatabase() )
  185. {
  186. DWORD dwInsertCount = 0;
  187. for ( it = vtAllLog.begin(); it != vtAllLog.end(); it++)
  188. {
  189. //COleDateTime tagOleTime(it->nDateTime);
  190. //PostMessage(g_hwnd,MSG_SHOWREALTIMELOG,(WPARAM)&it->nEnrollNumber,(LPARAM)&it->nDateTime);
  191. strSQL.Format(_T("insert into lonindata([no],[datetime])values('%s','%s')"),it->strEnrollNumber,it->strDateTime);
  192. // LOG4C((LOG_NOTICE,"%s",strSQL));
  193. if( CDBInterface::GetInstance()->Execute(strSQL.GetBuffer(0)) != 0)
  194. {
  195. strSQL.ReleaseBuffer();
  196. continue;
  197. }
  198. dwInsertCount++;
  199. strSQL.ReleaseBuffer();
  200. }
  201. strSQL.Format(_T("提交AllLog.txt完毕,去除重复数据,实际提交=%d条..."),dwInsertCount);
  202. SendMessage(g_hwnd, MSG_SHOWPROMPTING,(WPARAM)&strSQL, 0);
  203. return dwInsertCount;
  204. }
  205. return 0;
  206. }
  207. BOOL CDataManager::IsSubmitAllThreadRuning()
  208. {
  209. AutoThreadSection aSection(&s_critSection);
  210. return m_bThreadRuning1;
  211. }