PhotoBackupMgr.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. #include "stdafx.h"
  2. #include "PhotoBackupMgr.h"
  3. #include "Global.h"
  4. #include <process.h>
  5. #include <strsafe.h >
  6. #include <Winbase.h>
  7. #include "mysqldata.h"
  8. #include "TblDef.h"
  9. #include "CatalogObj.h"
  10. #include "BranchInfo.h"
  11. #include "BakPhotoModel\BackupFactory.h"
  12. #include "BakPhotoModel\BackupInterface.h"
  13. #include "helper\ffsco.h"
  14. using namespace helper_coffs;
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[]=__FILE__;
  18. #define new DEBUG_NEW
  19. #endif
  20. typedef HRESULT (WINAPI *GetClassBackupObject)(REFCLSID, REFIID, LPVOID*);
  21. unsigned int WINAPI IncrementalBakThread(LPVOID lpParam);
  22. unsigned int WINAPI AutoBakThread(LPVOID lpParam);
  23. CPhotoBackupMgr::CPhotoBackupMgr()
  24. {
  25. m_nBackupModel = 0; // 备份模式
  26. m_hIncrementalThread = NULL;
  27. m_hIncrementalEvent = NULL;
  28. m_hAutoThread = NULL;
  29. m_hAutoEvent = NULL;
  30. m_BakInterf = NULL;
  31. m_BakFact = NULL;
  32. m_pCCatalogObj = new CCatalogObj;
  33. }
  34. CPhotoBackupMgr::~CPhotoBackupMgr()
  35. {
  36. if(m_pCCatalogObj)
  37. delete m_pCCatalogObj;
  38. m_pCCatalogObj = NULL;
  39. }
  40. /************************************************************************/
  41. /*
  42. 函数:InitCatalog
  43. 描述:初始化数据库实例对象;
  44. 参数:
  45. IN: dwCatalogPort 数据库实例映射端口;
  46. IN: lpCatalogSource 数据库实例地址;
  47. IN: lpCatalogAccount 数据库实例登陆账号;
  48. IN: lpCatalogPsw 数据库实例登陆密码;
  49. IN: lpCatalogName 数据库名称;
  50. 返回:成功连接数据库实例返回TRUE;
  51. 要求:
  52. 注意:
  53. 示例:
  54. */
  55. /************************************************************************/
  56. BOOL CPhotoBackupMgr::InitCatalog(IN CONST DWORD &dwCatalogPort, IN LPCTSTR lpCatalogSource, IN LPCTSTR lpCatalogAccount, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpCatalogName)
  57. {
  58. if(m_pCCatalogObj == NULL)
  59. return FALSE;
  60. if ( m_pCCatalogObj->IsOpen() )
  61. return TRUE;
  62. TCHAR szSQLConnt[MAX_PATH] = _T("");
  63. if (dwCatalogPort)
  64. {
  65. _stprintf_s(szSQLConnt, _T("driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s"),
  66. lpCatalogSource, dwCatalogPort, lpCatalogName, lpCatalogAccount, lpCatalogPsw);
  67. }
  68. else
  69. {
  70. _stprintf_s(szSQLConnt, _T("driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s"),
  71. lpCatalogSource, lpCatalogName, lpCatalogAccount, lpCatalogPsw);
  72. }
  73. m_pCCatalogObj->SetConn(szSQLConnt);
  74. BOOL bResult = m_pCCatalogObj->OpenCatalog();
  75. if( !bResult )
  76. {
  77. WriteLogin(_T("增量备份打开数据库失败\n"));
  78. //LOG4C_NO_FILENUM((LOG_NOTICE,"打开数据库失败"));
  79. }
  80. return bResult;
  81. }
  82. /************************************************************************/
  83. /*
  84. 函数:StartBackup
  85. 描述:启动备份线程
  86. 参数:
  87. 返回:
  88. */
  89. /************************************************************************/
  90. void CPhotoBackupMgr::StartBackup()
  91. {
  92. #if 0
  93. if(g_nBackupModel == BACKUPMODEL_Incremental || g_nBackupModel == BACKUPMODEL_Both)
  94. StartIncrementalBackup();
  95. if(g_nBackupModel == BACKUPMODEL_Auto || g_nBackupModel == BACKUPMODEL_Both)
  96. StartAutoBackup();
  97. #endif //#if 0
  98. }
  99. /************************************************************************/
  100. /*
  101. 函数:TransformBackupModel
  102. 描述:切换模式进行备份
  103. 参数:
  104. IN CONST INT nModel 模式
  105. 返回:
  106. */
  107. /************************************************************************/
  108. void CPhotoBackupMgr::TransformBackupModel(IN CONST INT nModel)
  109. {
  110. TerminateBakThread();
  111. if(nModel == BACKUPMODEL_Incremental || nModel == BACKUPMODEL_Both)
  112. StartIncrementalBackup();
  113. if(nModel == BACKUPMODEL_Auto || nModel == BACKUPMODEL_Both)
  114. StartAutoBackup();
  115. }
  116. /************************************************************************/
  117. /*
  118. 函数:StartIncrementalBackup
  119. 描述:增量备份
  120. 参数:
  121. 返回:
  122. */
  123. /************************************************************************/
  124. void CPhotoBackupMgr::StartIncrementalBackup()
  125. {
  126. //获取备份目录
  127. GetBackupOfSaveDir(m_bakDirArr);
  128. TerminateIncrementalBak();
  129. m_hIncrementalEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  130. unsigned int nThreadID = 0;
  131. m_hIncrementalThread = (HANDLE)_beginthreadex(NULL, 0, IncrementalBakThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &nThreadID);
  132. }
  133. /************************************************************************/
  134. /*
  135. 函数:StartIncrementalBackup
  136. 描述:结束增量备份
  137. 参数:
  138. IN DWORD dwMilliseconds 等待超时
  139. 返回:
  140. */
  141. /************************************************************************/
  142. void CPhotoBackupMgr::TerminateIncrementalBak(IN DWORD dwMilliseconds)
  143. {
  144. if(m_hIncrementalEvent)
  145. SetEvent(m_hIncrementalEvent);
  146. if(m_hIncrementalThread)
  147. {
  148. WaitForSingleObject(m_hIncrementalThread, dwMilliseconds);
  149. CloseHandle(m_hIncrementalThread);
  150. OutputDebugString(_T("TerminateIncrementalBak exit"));
  151. }
  152. m_hIncrementalThread = NULL;
  153. if(m_hIncrementalEvent)
  154. CloseHandle(m_hIncrementalEvent);
  155. m_hIncrementalEvent = NULL;
  156. }
  157. /************************************************************************/
  158. /*
  159. 函数:StartIncrementalBackup
  160. 描述:自动备份
  161. 参数:
  162. 返回:
  163. */
  164. /************************************************************************/
  165. void CPhotoBackupMgr::StartAutoBackup()
  166. {
  167. TerminateAutoBak();
  168. m_hAutoEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  169. unsigned int nThreadID = 0;
  170. m_hAutoThread = (HANDLE)_beginthreadex(NULL, 0, AutoBakThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &nThreadID);
  171. }
  172. /************************************************************************/
  173. /*
  174. 函数:StartIncrementalBackup
  175. 描述:结束自动备份
  176. 参数:
  177. IN DWORD dwMilliseconds 等待超时
  178. 返回:
  179. */
  180. /************************************************************************/
  181. void CPhotoBackupMgr::TerminateAutoBak(IN DWORD dwMilliseconds)
  182. {
  183. if(m_hAutoEvent)
  184. SetEvent(m_hAutoEvent);
  185. if(m_hAutoThread)
  186. {
  187. WaitForSingleObject(m_hAutoThread, dwMilliseconds);
  188. CloseHandle(m_hAutoThread);
  189. OutputDebugString(_T("TerminateIncrementalBak exit"));
  190. }
  191. m_hAutoThread = NULL;
  192. if(m_hAutoEvent)
  193. CloseHandle(m_hAutoEvent);
  194. m_hAutoEvent = NULL;
  195. }
  196. /************************************************************************/
  197. /*
  198. 函数:StartIncrementalBackup
  199. 描述:结束自动备份
  200. 参数:
  201. LPVOID lpParam
  202. 返回:
  203. */
  204. /************************************************************************/
  205. unsigned int WINAPI IncrementalBakThread(LPVOID lpParam)
  206. {
  207. CPhotoBackupMgr *p = (CPhotoBackupMgr*)lpParam;
  208. do
  209. {
  210. p->IncrementWork();
  211. } while (WAIT_TIMEOUT == WaitForSingleObject(p->GetIncrementalEvent(), 15000) );
  212. return 0;
  213. }
  214. /************************************************************************/
  215. /*
  216. 函数:StartIncrementalBackup
  217. 描述:结束自动备份
  218. 参数:
  219. LPVOID lpParam
  220. 返回:
  221. */
  222. /************************************************************************/
  223. unsigned int WINAPI AutoBakThread(LPVOID lpParam)
  224. {
  225. CPhotoBackupMgr* p = (CPhotoBackupMgr*)lpParam;
  226. p->AutoBakWork();
  227. ::AfxGetApp()->GetMainWnd()->SetTimer(6, 3000, NULL);
  228. _endthreadex(0);
  229. return 0;
  230. }
  231. void CPhotoBackupMgr::AutoBakWork()
  232. {
  233. #if 0
  234. int step = 1;
  235. BOOL bError = 0;
  236. try
  237. {
  238. if(m_pCCatalogObj == NULL)
  239. return;
  240. g_tisks = ::GetTickCount();
  241. WriteLogin("开始备份照片");
  242. // CRstPath rsSt;
  243. CString path1 = _T("");
  244. CString path2 = _T("");
  245. CString path3 = _T("");
  246. CString path4 = _T("");
  247. path1 = path2 = path3 = path4 = g_localname;
  248. path1 = "\\\\" + path1 + "\\客户原片(管理软件)$";
  249. path2 = "\\\\" + path2 + "\\修好的片(管理软件)$";
  250. path3 = "\\\\" + path3 + "\\精修好的片(管理软件)$";
  251. path4 = "\\\\" + path4 + "\\设计好的片(管理软件)$";
  252. GetPathFromNetShareName("客户原片(管理软件)$", path1);
  253. GetPathFromNetShareName("修好的片(管理软件)$", path2);
  254. GetPathFromNetShareName("精修好的片(管理软件)$", path3);
  255. GetPathFromNetShareName("设计好的片(管理软件)$", path4);
  256. step = 2;
  257. path1.MakeLower();
  258. path2.MakeLower();
  259. path3.MakeLower();
  260. path4.MakeLower();
  261. CString bak1, bak2, bak3, bak4; // 是否备份(指定的备份目录是否需要备份);
  262. CString bak11, bak22, bak33, bak44; // 是否备份(集团版的分店备份目录是否需要备份??);
  263. CString bakserver1, bakserver2, bakserver3, bakserver4; // 相片备份目录 ;
  264. {
  265. CRstversion rsSt;
  266. rsSt.m_pDatabase = (CDatabase*)m_pCCatalogObj->GetCatalogInstance();
  267. rsSt.Open();
  268. if (!rsSt.IsEOF())
  269. {
  270. bak1 = rsSt.m_bak1;
  271. bak2 = rsSt.m_bak2;
  272. bak3 = rsSt.m_bak3;
  273. bak4 = rsSt.m_bak4;
  274. bakserver1 = rsSt.m_bakserver1;
  275. bakserver2 = rsSt.m_bakserver2;
  276. bakserver3 = rsSt.m_bakserver3;
  277. bakserver4 = rsSt.m_bakserver4;
  278. bak11 = rsSt.bak11;
  279. bak22 = rsSt.bak22;
  280. bak33 = rsSt.bak33;
  281. bak44 = rsSt.bak44;
  282. }
  283. rsSt.Close();
  284. step = 3;
  285. }
  286. #ifdef CHILD_VERSION
  287. if (bakserver1.Find(":") == -1 && bakserver1 != "")
  288. bakserver1 = "\\\\" + bakserver1 + "\\客户原片备份(儿童管理软件)$";
  289. GetPathFromNetShareName("客户原片备份(儿童管理软件)$", bakserver1);
  290. if (bakserver2.Find(":") == -1 && bakserver2 != "")
  291. bakserver2 = "\\\\" + bakserver2 + "\\修好的片备份(儿童管理软件)$";
  292. GetPathFromNetShareName("修好的片备份(儿童管理软件)$", bakserver2);
  293. if (bakserver3.Find(":") == -1 && bakserver3 != "")
  294. bakserver3 = "\\\\" + bakserver3 + "\\精修好的片备份(儿童管理软件)$";
  295. GetPathFromNetShareName("精修好的片备份(儿童管理软件)$", bakserver3);
  296. if (bakserver4.Find(":") == -1 && bakserver4 != "")
  297. bakserver4 = "\\\\" + bakserver4 + "\\设计好的片备份(儿童管理软件)$";
  298. GetPathFromNetShareName("设计好的片备份(儿童管理软件)$", bakserver4);
  299. #else
  300. if (bakserver1.Find(":") == -1 && bakserver1 != "")
  301. bakserver1 = "\\\\" + bakserver1 + "\\客户原片备份(管理软件)$";
  302. GetPathFromNetShareName("客户原片备份(管理软件)$", bakserver1);
  303. if (bakserver2.Find(":") == -1 && bakserver2 != "")
  304. bakserver2 = "\\\\" + bakserver2 + "\\修好的片备份(管理软件)$";
  305. GetPathFromNetShareName("修好的片备份(管理软件)$", bakserver2);
  306. if (bakserver3.Find(":") == -1 && bakserver3 != "")
  307. bakserver3 = "\\\\" + bakserver3 + "\\精修好的片备份(管理软件)$";
  308. GetPathFromNetShareName("精修好的片备份(管理软件)$", bakserver3);
  309. if (bakserver4.Find(":") == -1 && bakserver4 != "")
  310. bakserver4 = "\\\\" + bakserver4 + "\\设计好的片备份(管理软件)$";
  311. GetPathFromNetShareName("设计好的片备份(管理软件)$", bakserver4);
  312. #endif
  313. WriteLogin(path1); WriteLogin(path2); WriteLogin(path3); WriteLogin(path4);
  314. step = 4;
  315. CStringArray dirarray;
  316. CString str, path;
  317. BOOL bcheck1 = atoi(bak1);
  318. BOOL bcheck2 = atoi(bak2);
  319. BOOL bcheck3 = atoi(bak3);
  320. BOOL bcheck4 = atoi(bak4);
  321. BOOL bcheck11 = atoi(bak11);
  322. BOOL bcheck22 = atoi(bak22);
  323. BOOL bcheck33 = atoi(bak33);
  324. BOOL bcheck44 = atoi(bak44);
  325. // 下列4变量应该使用结构体保存;
  326. BOOL bcheck[4] = { bcheck1, bcheck2, bcheck3, bcheck4 }; //设置是否要备份组
  327. // 集团版;
  328. BOOL bcheckbranch[4] = { bcheck11, bcheck22, bcheck33, bcheck44 }; //设置分店是否要备份组
  329. CString patharray[4] = { path1, path2, path3, path4 }; //需要备份的目录组
  330. CString patharray2[4] = { bakserver1, bakserver2, bakserver3, bakserver4 }; //备份的目录组
  331. CArray<int, int> idxArray; //备份的目录组的引索
  332. int i = 0;
  333. for ( i = 0; i < 4; i++)
  334. {
  335. if (bcheck[i] == 0)continue; // 是否要备份
  336. if (patharray2[i] == "")continue; // 备份目录
  337. if (!CheckFolderFileExist(patharray2[i]))continue; //备份的目录组
  338. //多级共享目录
  339. if(CBranchInfo::GetInstance()->m_TblNetShareInfo.size() != 0)
  340. {
  341. vector<TblNetShareInfo*>::iterator iter = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
  342. for(; iter != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
  343. {
  344. //byPhotoType可能测为: 1=原片,2=初修,3=精修,4=设计好
  345. TblNetShareInfo* p = (*iter);
  346. if(p != NULL && ((int)p->byPhotoType) == (i+1) && ((int)p->byPhotoType) < 5)
  347. {
  348. CString strDir = p->szShareDirectory;
  349. if(strDir.Find(_T("")) != -1)
  350. {
  351. if(!CheckFolderFileExist(strDir))continue; //需要备份的目录组
  352. // 获取订单文件夹路径全名;
  353. ffsco o;
  354. o.dirs(1);
  355. o.find(LPCSTR(strDir), LPCSTR("*.*"));
  356. ffsco::typeT coo;
  357. ffsco::typeT::iterator it;
  358. coo = o.co_dir();
  359. it = coo.begin();
  360. it++;
  361. for (; coo.end() != it; it++)
  362. {
  363. str = (*it).c_str();
  364. if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)
  365. continue;
  366. if (FindArray(&dirarray, str) == -1)
  367. {
  368. //WriteLogin(str);
  369. dirarray.Add(str);
  370. idxArray.Add(i);
  371. }
  372. }
  373. }
  374. }
  375. ++iter;
  376. }
  377. //大于2是精修和设计好的片目录
  378. if(i >= 2 && i < 4)
  379. {
  380. if (!CheckFolderFileExist(patharray[i]))//需要备份的目录组
  381. continue;
  382. // 获取订单文件夹路径全名;
  383. ffsco o;
  384. o.dirs(1);
  385. o.find(LPCSTR(patharray[i]), LPCSTR("*.*"));
  386. ffsco::typeT coo;
  387. ffsco::typeT::iterator it;
  388. coo = o.co_dir();
  389. it = coo.begin();
  390. it++;
  391. for (; coo.end() != it; it++)
  392. {
  393. str = (*it).c_str();
  394. if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)continue;
  395. if (FindArray(&dirarray, str) == -1)
  396. {
  397. //WriteLogin(str);
  398. dirarray.Add(str);
  399. idxArray.Add(i);
  400. }
  401. }
  402. }
  403. }
  404. else
  405. {
  406. //需要备份的目录组
  407. if (!CheckFolderFileExist(patharray[i]))
  408. continue;
  409. // 获取订单文件夹路径全名;
  410. ffsco o;
  411. o.dirs(1);
  412. o.find(LPCSTR(patharray[i]), LPCSTR("*.*"));
  413. ffsco::typeT coo;
  414. ffsco::typeT::iterator it;
  415. coo = o.co_dir();
  416. it = coo.begin();
  417. it++;
  418. for (; coo.end() != it; it++)
  419. {
  420. str = (*it).c_str();
  421. if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)
  422. continue;
  423. if (FindArray(&dirarray, str) == -1)
  424. {
  425. //WriteLogin(str);
  426. dirarray.Add(str);
  427. idxArray.Add(i);
  428. }
  429. }
  430. }
  431. }
  432. step = 5;
  433. // 对每个订单文件夹进行相片复制操作;
  434. bError = 0;
  435. for (i = 0; i<dirarray.GetSize(); i++)
  436. {
  437. CTime tm = CTime::GetCurrentTime();
  438. tm -= CTimeSpan(g_nBakDays, 0, 0, 0);
  439. CString tenDaysAgo = tm.Format("%Y%m%d");
  440. DWORD dirdate = GetLastDirTime(dirarray.ElementAt(i));
  441. CString ss;
  442. if (dirdate>atol(tenDaysAgo))
  443. {
  444. step = 6;
  445. if (0 == BakAllPhoto(dirarray.ElementAt(i), patharray2[idxArray.ElementAt(i)]))
  446. bError = 1;
  447. CString path = dirarray.ElementAt(i);
  448. path.TrimRight("\\"); path += "\\";
  449. path += "modifytime";
  450. CStdioFile fp;
  451. //将备份时间写入modifytime
  452. if (!::PathFileExists(path))
  453. {
  454. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  455. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d"));
  456. fp.Close();
  457. }
  458. }
  459. else
  460. {
  461. //更新modifytime里的备份时间
  462. step = 7;
  463. CString path = dirarray.ElementAt(i);
  464. path.TrimRight("\\"); path += "\\";
  465. path += "modifytime";
  466. CStdioFile fp;
  467. if (!::PathFileExists(path))
  468. {
  469. step = 8;
  470. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  471. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d"));
  472. fp.Close();
  473. }
  474. }
  475. }
  476. if (bError)
  477. g_bakphoto = "照片备份失败";
  478. else
  479. g_bakphoto = "照片备份成功";
  480. step = 9;
  481. }
  482. catch (CException *e)
  483. {
  484. TCHAR szError[1024] = { 0 };
  485. e->GetErrorMessage(szError, 1024);
  486. WriteLogin(CString(szError));
  487. e->Delete();
  488. if (step != 8)
  489. {
  490. CString ss;
  491. ss.Format("出错在函数备份照片中-step:%d", step);
  492. WriteLogin(ss);
  493. g_bakphoto = "照片备份出错";
  494. }
  495. else
  496. {
  497. if (bError)
  498. g_bakphoto = "照片备份失败";
  499. else
  500. g_bakphoto = "照片备份成功";
  501. }
  502. }
  503. #endif //if 0
  504. }
  505. //--------------------------------------------------------------------------------
  506. /*
  507. // 函数:BakAllPhoto
  508. // 描述:将订单目录下的相片备份
  509. // 参数:
  510. dir: 当前要备份的订单目录
  511. desdir: 保存的目录
  512. // 返回:TRUE,FALSE
  513. */
  514. //--------------------------------------------------------------------------------
  515. BOOL CPhotoBackupMgr::BakAllPhoto(CString dir, CString desdir)
  516. {
  517. try
  518. {
  519. CString childdir = _T("");
  520. CString srcdir = dir;
  521. srcdir.MakeLower();
  522. int nPos = srcdir.Find(_T("$"));
  523. if(nPos != -1)
  524. childdir = srcdir.Right(srcdir.GetLength() - (nPos + 1));
  525. else
  526. return FALSE;
  527. childdir.TrimLeft("\\");
  528. childdir.TrimRight("\\");
  529. while (childdir.Find('\\') != -1)
  530. {
  531. desdir += "\\";
  532. desdir += childdir.Left(childdir.Find('\\'));
  533. CreateDirectory(desdir, NULL);
  534. childdir = childdir.Right(childdir.GetLength() - childdir.Find('\\') - 1);
  535. }
  536. desdir += "\\";
  537. desdir += childdir;
  538. CreateDirectory(desdir, NULL);
  539. desdir += "\\";
  540. //进行备份复制
  541. ffsco o;
  542. o.dirs(0);
  543. o.find(LPCSTR(dir), LPCSTR("*.*"));
  544. ffsco::typeT coo;
  545. ffsco::typeT::iterator it;
  546. coo = o.co_file();
  547. CString path, temp, despath;
  548. for (it = coo.begin(); coo.end() != it; it++)
  549. {
  550. try
  551. {
  552. path = (*it).c_str();
  553. temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  554. if (temp.GetAt(0) == 's')continue;
  555. despath = desdir + path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  556. despath.MakeLower();
  557. if (PathFileExists(despath))
  558. {
  559. if (::CopyFile(path, despath, 0))
  560. g_bakphoto = "照片备份成功";
  561. }
  562. else
  563. {
  564. if (::CopyFile(path, despath, 0))
  565. g_bakphoto = "照片备份成功";
  566. }
  567. }
  568. catch (...)
  569. {
  570. }
  571. }
  572. }
  573. catch (...)
  574. {
  575. WriteLogin("出错在函数BakAllPhoto中");
  576. return 0;
  577. }
  578. return 1;
  579. }
  580. //--------------------------------------------------------------------------------
  581. // 函数:BakAllPhoto
  582. // 描述:备份相片
  583. // dir:客户原片、修好的片、精修片、设计片目录的订单文件夹路径;
  584. // desdir:dir对就的备份目录;
  585. // 参数:
  586. // 返回:
  587. //--------------------------------------------------------------------------------
  588. BOOL CPhotoBackupMgr::BakAllPhoto(CString dir, CString desdir, CString path1, CString path2, CString path3, CString path4)
  589. {
  590. try
  591. {
  592. CString childdir;
  593. CString srcdir = dir;
  594. srcdir.MakeLower();
  595. if (srcdir.Find(path1) != -1)
  596. childdir = srcdir.Right(srcdir.GetLength() - path1.GetLength());
  597. else if (srcdir.Find(path2) != -1)
  598. childdir = srcdir.Right(srcdir.GetLength() - path2.GetLength());
  599. else if (srcdir.Find(path3) != -1)
  600. childdir = srcdir.Right(srcdir.GetLength() - path3.GetLength());
  601. else if (srcdir.Find(path4) != -1)
  602. childdir = srcdir.Right(srcdir.GetLength() - path4.GetLength());
  603. else
  604. {
  605. WriteLogin("备份出错-目录未找到!");
  606. return 0;
  607. }
  608. childdir.TrimLeft("\\");
  609. childdir.TrimRight("\\");
  610. while (childdir.Find('\\') != -1)
  611. {
  612. desdir += "\\";
  613. desdir += childdir.Left(childdir.Find('\\'));
  614. CreateDirectory(desdir, NULL);
  615. childdir = childdir.Right(childdir.GetLength() - childdir.Find('\\') - 1);
  616. }
  617. desdir += "\\";
  618. desdir += childdir;
  619. CreateDirectory(desdir, NULL);
  620. desdir += "\\";
  621. ffsco o;
  622. o.dirs(0);
  623. o.find(LPCSTR(dir), LPCSTR("*.*"));
  624. ffsco::typeT coo;
  625. ffsco::typeT::iterator it;
  626. coo = o.co_file();
  627. CString path, temp, despath;
  628. for (it = coo.begin(); coo.end() != it; it++)
  629. {
  630. try
  631. {
  632. path = (*it).c_str();
  633. temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  634. if (temp.GetAt(0) == 's')continue;
  635. despath = desdir + path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  636. if (CheckFileExist(despath))
  637. {
  638. if (::CopyFile(path, despath, 0))
  639. g_bakphoto = "照片备份成功";
  640. }
  641. else
  642. {
  643. if (::CopyFile(path, despath, 0))
  644. g_bakphoto = "照片备份成功";
  645. }
  646. }
  647. catch (...)
  648. {
  649. }
  650. }
  651. }
  652. catch (...)
  653. {
  654. WriteLogin("出错在函数BakAllPhoto中");
  655. return 0;
  656. }
  657. return 1;
  658. }
  659. //--------------------------------------------------------------------------------
  660. // 函数:IncrementWork
  661. // 描述:增量备份工作
  662. // dir:客户原片、修好的片、精修片、设计片目录的订单文件夹路径;
  663. // desdir:dir对就的备份目录;
  664. // 参数:
  665. // 返回:
  666. //--------------------------------------------------------------------------------
  667. void CPhotoBackupMgr::IncrementWork()
  668. {
  669. if(m_BakInterf == NULL)
  670. {
  671. WriteLogin(_T("备份模块没加载成功\n"));
  672. return;
  673. }
  674. if(m_BakInterf->IsBackup())
  675. return;
  676. //添加相片的共享目录
  677. vector<TblNetShareInfo*>::iterator it = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
  678. for(;it != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
  679. {
  680. TblNetShareInfo* p = (*it);
  681. if(p != NULL)
  682. m_BakInterf->InitShareDirData(p->bEnable, p->szBranchId, p->szShareDirectory, p->byMinCapacity, p->byMaxCapacity, p->byPhotoType, p->byPriority);
  683. ++it;
  684. }
  685. CString strError = _T("");
  686. //获取增量备份数据
  687. CArray<CStringArray, CStringArray> AryOfIncrement;
  688. CBranchInfo::GetInstance()->GetTblIncrementBackup(AryOfIncrement);
  689. if(AryOfIncrement.GetSize() == 0)
  690. {
  691. Sleep(1000);
  692. return;
  693. }
  694. for (int i = 0; i < AryOfIncrement.GetSize(); i++)
  695. {
  696. TCHAR szDomain[64] = {0};
  697. TCHAR szOrderNum[64] = {0};
  698. TCHAR szOptTime[32] = {0};
  699. TCHAR szPhotos[4096] = {0};
  700. TCHAR szDest[MAX_PATH] = {0};
  701. TCHAR szBackupDest[MAX_PATH] = {0};
  702. TCHAR* pPhotos = NULL;
  703. int nPhotoType = _ttoi(AryOfIncrement.ElementAt(i).ElementAt(4));
  704. _tcscpy(szDomain, AryOfIncrement.ElementAt(i).ElementAt(1));
  705. _tcscpy(szOrderNum, AryOfIncrement.ElementAt(i).ElementAt(3));
  706. _tcscpy(szOptTime, AryOfIncrement.ElementAt(i).ElementAt(2));
  707. pPhotos = new TCHAR[AryOfIncrement.ElementAt(i).ElementAt(5).GetLength() + 1];
  708. _tcscpy(pPhotos, AryOfIncrement.ElementAt(i).ElementAt(5));
  709. _tcscpy(szDest, m_bakDirArr.ElementAt(nPhotoType - 1).ElementAt(1));
  710. if(_tcscmp(szOrderNum, _T("")) == 0 || _tcscmp(szPhotos, _T("")) == 0)
  711. {
  712. if(pPhotos != NULL)
  713. delete [] pPhotos;
  714. continue;
  715. }
  716. CString strBackupDir = _T("");
  717. MakeBackupDir(nPhotoType, szDest, strBackupDir);
  718. if(szBackupDest == _T(""))
  719. {
  720. if(pPhotos != NULL)
  721. delete [] pPhotos;
  722. continue;
  723. }
  724. _tcscpy(szBackupDest, strBackupDir);
  725. m_BakInterf->InitBackupInfo(
  726. szDomain, // 店域名
  727. szOrderNum, // 订单号
  728. szOptTime, // 操作时间
  729. szPhotos, // 要备份的相片名集合
  730. szBackupDest, // 备份的目标目录,如:\\\\127.0.0.1\\客户原片备份(管理软件)$
  731. nPhotoType, // 相片类型
  732. FALSE // 是否备份m图
  733. );
  734. if(pPhotos != NULL)
  735. delete [] pPhotos;
  736. }
  737. //开始备份
  738. m_BakInterf->StartBackup(g_sdomain, BackupCallBack);
  739. Sleep(1000);
  740. }
  741. /************************************************************************/
  742. /*
  743. 函数:BackupFinish
  744. 描述:增量备份完成回调
  745. 参数:
  746. LPVOID, 回调传回值
  747. LPVOID,
  748. LPVOID,
  749. LPVOID,
  750. LPVOID
  751. 返回:1成功,0失败
  752. */
  753. /************************************************************************/
  754. int WINAPI CPhotoBackupMgr::BackupCallBack(IN LPVOID lpParam1, IN LPVOID lpParam2, IN LPVOID lpParam3, IN LPVOID lpParam4, IN LPVOID lpParam5, IN LPVOID lpParam6)
  755. {
  756. // 回调函数结束后,返回到DLL中,要再次调用DLL的对话框需要再次使用AFX_MANAGE_STATE;
  757. AfxSetResourceHandle(g_Instance);
  758. return CPhotoBackupMgr::GetInstance()->BackupCallBackPro(lpParam1, lpParam2, lpParam3, lpParam4, lpParam5, lpParam6);
  759. }
  760. /************************************************************************/
  761. /*
  762. 函数:BackupFinish
  763. 描述:增量备份完成回调
  764. 参数:
  765. LPVOID, 回调传回值
  766. LPVOID,
  767. LPVOID,
  768. LPVOID,
  769. LPVOID
  770. 返回:1成功,0失败
  771. */
  772. /************************************************************************/
  773. int CPhotoBackupMgr::BackupCallBackPro(IN LPVOID lpParam1, IN LPVOID lpParam2, IN LPVOID lpParam3, IN LPVOID lpParam4, IN LPVOID lpParam5, IN LPVOID lpParam6)
  774. {
  775. int nPercent = *((int*)lpParam1);
  776. CString strMsg = _T("");
  777. //strMsg.Format(_T("完成:%d\n"), nPercent);
  778. //OutputDebugString(strMsg);
  779. CAutoLock autolock(&m_lock);
  780. if ( nPercent == 100 )
  781. {
  782. try
  783. {
  784. CString strDate = _T("");
  785. strDate.Format(_T("update [IncrementalBackup] set [isbackup] = 1,[baktime] = getdate() where [branchid] = '%s' and [order] = '%s' and photoType = %d and content = '%s'\n"), (TCHAR*)lpParam2, (TCHAR*)lpParam4, *((BYTE*)lpParam5), (TCHAR*)lpParam6);
  786. m_pCCatalogObj->Execute(strDate);
  787. }
  788. catch (CException *e)
  789. {
  790. TCHAR szError[1024] = { 0 };
  791. e->GetErrorMessage(szError, 1024);
  792. CString strErrorMsg = _T("");
  793. strErrorMsg.Format(_T("增量备份回调错误:%s\n"), szError);
  794. WriteLogin(strErrorMsg);
  795. e->Delete();
  796. }
  797. }
  798. return 1;
  799. }
  800. /************************************************************************/
  801. /*
  802. 函数:LoadBakModule
  803. 描述:加载备份模
  804. 参数:
  805. IN HINSTANCE hInstance dll实例句柄
  806. 返回:1成功,0失败
  807. */
  808. /************************************************************************/
  809. int CPhotoBackupMgr::LoadBakModule(IN HINSTANCE hInstance)
  810. {
  811. if(hInstance == NULL)
  812. return 0;
  813. HRESULT hr = NULL;
  814. GetClassBackupObject getBackupObj = NULL;
  815. getBackupObj = (GetClassBackupObject)GetProcAddress(hInstance, "GetClassBackupObject");
  816. if(getBackupObj == NULL)
  817. {
  818. WriteLogin(_T("打开GetClassBackupObject失败\n"));
  819. return 0;
  820. }
  821. if(m_BakFact == NULL)
  822. {
  823. hr = getBackupObj(CLSID_SC_BACKUPINTERFACE, IID_IClassFactory, (void**)&m_BakFact);
  824. if(FAILED(hr))
  825. {
  826. WriteLogin(_T("获取CSC_BackupFactory指针失败\n"));
  827. return 0;
  828. }
  829. }
  830. if(m_BakInterf == NULL)
  831. {
  832. hr = m_BakFact->CreateInstance(NULL, IID_IUnknown, (void**)&m_BakInterf);
  833. if(FAILED(hr))
  834. {
  835. WriteLogin(_T("创建ISC_BackupInterface失败\n"));
  836. m_BakFact->Release();
  837. return 0;
  838. }
  839. }
  840. return 1;
  841. }
  842. /************************************************************************/
  843. /*
  844. 函数:FreeBakModule
  845. 描述:释放备份接
  846. 参数:
  847. 返回:
  848. */
  849. /************************************************************************/
  850. void CPhotoBackupMgr::FreeBakModule()
  851. {
  852. if(m_BakInterf)
  853. m_BakInterf->Release();
  854. if(m_BakFact)
  855. m_BakFact->Release();
  856. }
  857. /************************************************************************/
  858. /*
  859. 函数:GetBackupOfSaveDir
  860. 描述:获取备份的保存目录
  861. 参数:
  862. OUT CArray<CStringArray, CStringArray> &AryOfValues 备份保存的目录
  863. 返回: TRUE, FALSE
  864. */
  865. /************************************************************************/
  866. BOOL CPhotoBackupMgr::GetBackupOfSaveDir(OUT CArray<CStringArray, CStringArray> &AryOfValues)
  867. {
  868. AryOfValues.RemoveAll();
  869. AryOfValues.SetSize(4, 1);
  870. CString bak1, bak2, bak3, bak4; // 是否备份(指定的备份目录是否需要备份);
  871. CString bak11, bak22, bak33, bak44; // 是否备份(集团版的分店备份目录是否需要备份??);
  872. CString bakserver1, bakserver2, bakserver3, bakserver4; // 相片备份目录 ;
  873. CAutoLock autolock(&m_lock);
  874. if(m_pCCatalogObj == NULL)
  875. return FALSE;
  876. try
  877. {
  878. CRstversion rsSt;
  879. rsSt.m_pDatabase = (CDatabase*)m_pCCatalogObj->GetCatalogInstance();
  880. rsSt.Open();
  881. if (!rsSt.IsEOF())
  882. {
  883. AryOfValues.ElementAt(0).RemoveAll();
  884. AryOfValues.ElementAt(0).Add(rsSt.m_bak1);
  885. AryOfValues.ElementAt(0).Add(rsSt.m_bakserver1);
  886. AryOfValues.ElementAt(0).Add(rsSt.bak11);
  887. AryOfValues.ElementAt(1).RemoveAll();
  888. AryOfValues.ElementAt(1).Add(rsSt.m_bak2);
  889. AryOfValues.ElementAt(1).Add(rsSt.m_bakserver2);
  890. AryOfValues.ElementAt(1).Add(rsSt.bak22);
  891. AryOfValues.ElementAt(2).RemoveAll();
  892. AryOfValues.ElementAt(2).Add(rsSt.m_bak3);
  893. AryOfValues.ElementAt(2).Add(rsSt.m_bakserver3);
  894. AryOfValues.ElementAt(2).Add(rsSt.bak33);
  895. AryOfValues.ElementAt(3).RemoveAll();
  896. AryOfValues.ElementAt(3).Add(rsSt.m_bak4);
  897. AryOfValues.ElementAt(3).Add(rsSt.m_bakserver4);
  898. AryOfValues.ElementAt(3).Add(rsSt.bak44);
  899. }
  900. rsSt.Close();
  901. }
  902. catch (...)
  903. {
  904. WriteLogin("获取备份保存的目录出错");
  905. }
  906. return TRUE;
  907. }
  908. /************************************************************************/
  909. /*
  910. 函数:GetLastDirTime
  911. 描述:获取文件最后修改时间
  912. 参数:
  913. CString dir 文件目录
  914. 返回: DWORD类型时间
  915. */
  916. /************************************************************************/
  917. DWORD CPhotoBackupMgr::GetLastDirTime(IN const CString& dir)
  918. {
  919. try
  920. {
  921. CString path = dir;
  922. path.TrimRight("\\");
  923. path += "\\";
  924. path += "modifytime";
  925. CStdioFile fp;
  926. if (::PathFileExists(path))
  927. {
  928. fp.Open(path, CFile::modeRead);
  929. fp.ReadString(path);
  930. fp.Close();
  931. path.TrimLeft(); path.TrimRight();
  932. path.Replace("-", "");
  933. return atoi(path);
  934. }
  935. else
  936. {
  937. ffsco o;
  938. o.dirs(1);
  939. o.find(LPCSTR(dir), LPCSTR("*.jpg"));
  940. ffsco::typeT coo;
  941. ffsco::typeT::iterator it;
  942. coo = o.co_file();
  943. CString path;
  944. DWORD maxdate = 0;
  945. for (it = coo.begin(); coo.end() != it; it++)
  946. {
  947. path = (*it).c_str();
  948. maxdate = max(maxdate, atol(GetModifyTime2(path)));
  949. }
  950. return maxdate;
  951. }
  952. }
  953. catch (...)
  954. {
  955. WriteLogin("出错");
  956. }
  957. }
  958. /************************************************************************/
  959. /*
  960. 函数:TerminateBakThread
  961. 描述:结束所有线程
  962. 参数:
  963. CString dir 文件目录
  964. 返回: DWORD类型时间
  965. */
  966. /************************************************************************/
  967. void CPhotoBackupMgr::TerminateBakThread()
  968. {
  969. TerminateIncrementalBak();
  970. TerminateAutoBak();
  971. }
  972. /************************************************************************/
  973. /*
  974. 函数:MakeBackupDir
  975. 描述:生成备份目录
  976. 参数:
  977. IN CONST CString& str, // 可能是共享计算机名/目录/ip地址
  978. OUT CString& strPath // 返回备份目录
  979. 返回:
  980. */
  981. /************************************************************************/
  982. void CPhotoBackupMgr::MakeBackupDir(IN CONST int nType, IN CONST CString& str, OUT CString& strPath)
  983. {
  984. if(str == _T(""))
  985. return;
  986. CString strTemp = str;
  987. strTemp.MakeLower();
  988. CString strFolderName = _T("");
  989. #ifdef CHILD_VERSION
  990. switch(nType + 4)
  991. {
  992. case 5:
  993. strFolderName = _T("客户原片备份(儿童管理软件)$");
  994. break;
  995. case 6:
  996. strFolderName = _T("修好的片备份(儿童管理软件)$");
  997. break;
  998. case 7:
  999. strFolderName = _T("精修好的片备份(儿童管理软件)$");
  1000. break;
  1001. case 8:
  1002. strFolderName = _T("设计好的片备份(儿童管理软件)$");
  1003. break;
  1004. }
  1005. #else
  1006. switch(nType + 4)
  1007. {
  1008. case 5:
  1009. strFolderName = _T("客户原片备份(管理软件)$");
  1010. break;
  1011. case 6:
  1012. strFolderName = _T("修好的片备份(管理软件)$");
  1013. break;
  1014. case 7:
  1015. strFolderName = _T("精修好的片备份(管理软件)$");
  1016. break;
  1017. case 8:
  1018. strFolderName = _T("设计好的片备份(管理软件)$");
  1019. break;
  1020. }
  1021. #endif
  1022. char szServer[50] = {0};
  1023. DWORD leng = 50;
  1024. ::GetComputerName(szServer, &leng);
  1025. CString strMainName = szServer;
  1026. strMainName.MakeLower();
  1027. if(strTemp.Find(strFolderName) != -1) //目录
  1028. {
  1029. if(strTemp.Find(_T(":\\")) != -1 || strTemp.Find(_T("\\\\")) != -1) // 绝对路径: D:\...\... (相对: \\...\...)
  1030. strPath = strTemp;
  1031. }
  1032. else if(strTemp.CompareNoCase(strMainName) == 0) //是否为计算机名
  1033. {
  1034. strMainName.TrimLeft(_T("\\\\"));
  1035. strMainName.TrimRight(_T("\\"));
  1036. strPath = _T("\\\\") + strMainName + _T("\\") + strFolderName;
  1037. }
  1038. else //ip地址
  1039. {
  1040. if(strTemp.GetLength() <= 16)
  1041. {
  1042. CString strip = strTemp;
  1043. int nCount = 0;
  1044. while(1)
  1045. {
  1046. int nPos = strip.Find(_T("."));
  1047. if(nPos == -1)
  1048. break;
  1049. strip = strip.Right((strip.GetLength() - 1) - nPos);
  1050. if(strip == _T(""))
  1051. break;
  1052. ++nCount;
  1053. }
  1054. if(nCount == 3)
  1055. strTemp = strPath;
  1056. }
  1057. }
  1058. if(strPath != _T(""))
  1059. strPath.MakeLower();
  1060. }