CreateSmallPhoto.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "stdafx.h"
  2. #include "CreateSmallPhoto.h"
  3. #include <process.h>
  4. #include <afxdb.h>
  5. #include "./helper/ffsco.h"
  6. #include "BranchInfo.h"
  7. #include <comutil.h>
  8. #pragma comment(lib, "comsuppw.lib")
  9. #include "IMGCommon.h"
  10. using namespace helper_coffs;
  11. unsigned __stdcall WorkThread(LPVOID lpParam);
  12. CreateSmallPhoto::CreateSmallPhoto()
  13. {
  14. m_hExitEvent = NULL;
  15. m_hThread = NULL;
  16. lCreating = 0;
  17. m_ctrl_process = NULL;
  18. m_pWnd = NULL;
  19. }
  20. CreateSmallPhoto::~CreateSmallPhoto()
  21. {
  22. TerminateThread();
  23. }
  24. //-----------------------------------------------------------------------
  25. /*
  26. // 函数:GetOrderNumFromDB
  27. // 描述:从数据库里获取订单号
  28. // 参数:
  29. CStringArray& orderArr 返回未取件的订单号
  30. CString* pField 条件限制的字段
  31. // 返回:
  32. */
  33. //-----------------------------------------------------------------------
  34. void CreateSmallPhoto::GetOrdersFromDB(OUT CStringArray& orderArr, IN CString* pField)
  35. {
  36. CDatabase *pdb = NULL;
  37. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  38. if ( NULL == pdb )
  39. return;
  40. CRecordset myset(pdb);
  41. CString sql = "select id from dindan";
  42. if(pField && (*pField) != _T(""))
  43. sql += " where " + (*pField);
  44. myset.Open(CRecordset::forwardOnly, sql);
  45. while (!myset.IsEOF())
  46. {
  47. CString strOrder = _T("");
  48. myset.GetFieldValue("id", strOrder);
  49. if(strOrder != _T(""))
  50. orderArr.Add(strOrder);
  51. myset.MoveNext();
  52. }
  53. myset.Close();
  54. }
  55. /************************************************************************/
  56. /*
  57. 函数: CreatePhoto
  58. 描述: 生成小图
  59. 参数:
  60. IN LPCTSTR lpDir, 目录
  61. IN BOOL bMPhoto 是否生成m图
  62. 返回:
  63. 示例:
  64. 注意:
  65. */
  66. /************************************************************************/
  67. BOOL CreateSmallPhoto::CreatePhoto(IN CString& strDir, IN BOOL bMPhoto)
  68. {
  69. //先删除ok文件
  70. DelOKFiles(&strDir);
  71. // 如果不是客户原片或修好的片目录,设置bMPhoto参数为0,不生成M缩略图;
  72. if (strDir.Find("客户原片(管理软件)$") == -1 && strDir.Find("修好的片(管理软件)$") == -1)
  73. bMPhoto = 0;
  74. ffsco o;
  75. o.dirs(1);
  76. o.find(LPCSTR(strDir), LPCSTR("*.*"));
  77. ffsco::typeT coo;
  78. ffsco::typeT::iterator it;
  79. coo = o.co_file();
  80. int oldsize = coo.size();
  81. int nFileCount = 0;
  82. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  83. for (it = coo.begin(); coo.end() != it; it++)
  84. {
  85. try
  86. {
  87. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  88. break;
  89. // 遍历到的jpg文件如果是缩略图则不处理,继续遍历;
  90. path = (*it).c_str();
  91. temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  92. if (temp.GetAt(0) == 's')continue;
  93. if (temp.GetAt(0) == 'm')continue;
  94. spath = path.Left(path.ReverseFind('\\') + 1);
  95. mpath = spath;
  96. spath += "s";
  97. mpath += "m";
  98. spath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  99. mpath += path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
  100. Image* pImg = NULL;
  101. IMGCommon::LoadImgFromFile(&pImg, path);
  102. if (pImg == NULL)
  103. continue;
  104. IMGCommon::ImgThumbnail(pImg, SET_PIX(100,100), spath, 100);
  105. if (bMPhoto)
  106. IMGCommon::ImgThumbnail(pImg, SET_PIX(2000,2000), mpath, 100);
  107. if (pImg)
  108. delete pImg;
  109. pImg = NULL;
  110. }
  111. catch (...)
  112. {
  113. WriteTextLog("出错在函数CreatePhoto中");
  114. }
  115. }
  116. CreateOKFiles(strDir);
  117. return TRUE;
  118. }
  119. /************************************************************************/
  120. /*
  121. 函数:IsCreateMImage
  122. 描述:选片时是否使用m缩略图来代替原片放大使用;
  123. 参数:
  124. Image **img,
  125. CString path
  126. 返回: TRUE,FALSE
  127. */
  128. /************************************************************************/
  129. BOOL CreateSmallPhoto::IsCreateMImage()
  130. {
  131. BOOL bRet = FALSE;
  132. if (g_bSteal == 0)
  133. {
  134. CDatabase *pdb = NULL;
  135. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  136. if ( NULL == pdb )
  137. return FALSE;
  138. CRecordset myset(pdb);
  139. CString sql = "select * from version";
  140. myset.Open(CRecordset::forwardOnly, sql);
  141. myset.GetFieldValue("setcheck15", sql); // 选片时是否使用m缩略图来代替原片放大使用;
  142. myset.Close();
  143. bRet = atoi(sql);
  144. }
  145. return bRet;
  146. }
  147. /************************************************************************/
  148. /*
  149. 函数:Work
  150. 描述:
  151. 参数:
  152. 返回:
  153. */
  154. /************************************************************************/
  155. void CreateSmallPhoto::Work()
  156. {
  157. // 选片时是否使用m缩略图来代替原片放大使用;
  158. BOOL bMphoto = IsCreateMImage();
  159. //读取所有未取件的订单号
  160. CStringArray orderArr;
  161. CString strField = _T("status3='未取'");
  162. GetOrdersFromDB(orderArr, &strField);
  163. if(orderArr.GetSize() == 0)
  164. {
  165. WriteTextLog("没有获取到订单号");
  166. return;
  167. }
  168. //组合目录
  169. CString initialPhotoPath = _T("");
  170. CString initialModifyPath = _T("");
  171. CString designPhotoPath = _T("");
  172. CString excellentModifyPath = _T("");
  173. char server[50] = {0};
  174. DWORD leng = 50;
  175. ::GetComputerName(server, &leng);
  176. initialPhotoPath = initialModifyPath = designPhotoPath = excellentModifyPath = server;
  177. initialPhotoPath = "\\\\" + initialPhotoPath + "\\客户原片(管理软件)$";
  178. initialModifyPath = "\\\\" + initialModifyPath + "\\修好的片(管理软件)$";
  179. designPhotoPath = "\\\\" + designPhotoPath + "\\设计好的片(管理软件)$";
  180. excellentModifyPath = "\\\\" + excellentModifyPath + "\\精修好的片(管理软件)$";
  181. GetPathFromNetShareName("客户原片(管理软件)$", initialPhotoPath);
  182. GetPathFromNetShareName("修好的片(管理软件)$", initialModifyPath);
  183. GetPathFromNetShareName("设计好的片(管理软件)$", designPhotoPath);
  184. GetPathFromNetShareName("精修好的片(管理软件)$", excellentModifyPath);
  185. initialPhotoPath.MakeLower();
  186. initialModifyPath.MakeLower();
  187. designPhotoPath.MakeLower();
  188. excellentModifyPath.MakeLower();
  189. //
  190. CStringArray pathArr;
  191. int i = 0;
  192. try
  193. {
  194. //筛选需要生成小图的目录
  195. if(CBranchInfo::GetInstance()->m_TblNetShareInfo.size() != 0)
  196. {
  197. vector<TblNetShareInfo*>::iterator iter = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
  198. for(; iter != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
  199. {
  200. TblNetShareInfo* p = (*iter);
  201. if(p != NULL)
  202. {
  203. CString strDir = p->szShareDirectory;
  204. if(strDir.Find(_T("$\\")) != -1)
  205. strDir = strDir.TrimRight(_T("\\"));
  206. ScreeningDir(&strDir, &orderArr, pathArr);
  207. }
  208. ++iter;
  209. }
  210. CString patharray[2] = { designPhotoPath, excellentModifyPath };
  211. for (i = 0; i < 2; i++)
  212. ScreeningDir(&patharray[i], &orderArr, pathArr);
  213. }
  214. strField.Format(_T("订单:0/%d"), pathArr.GetSize());
  215. if (m_ctrl_process)
  216. m_ctrl_process->SetRange(0, pathArr.GetSize());
  217. if ( m_pWnd )
  218. m_pWnd->SetWindowText(strField);
  219. // 生成小图;
  220. for(int i = 0; i < pathArr.GetSize(); i++)
  221. {
  222. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  223. break;
  224. CreatePhoto(pathArr.ElementAt(i), bMphoto);
  225. if (m_ctrl_process)
  226. m_ctrl_process->SetPos(i+1);
  227. if ( m_pWnd )
  228. {
  229. strField.Format(_T("订单:%d/%d"), i+1, pathArr.GetSize());
  230. m_pWnd->SetWindowText(strField);
  231. }
  232. }
  233. }
  234. catch (...)
  235. {
  236. WriteTextLog("出错在函数CreateSmallPhoto::GenThumb中");
  237. }
  238. InterlockedExchange(&lCreating, FALSE);
  239. _endthreadex(0);
  240. TerminateThread();
  241. return;
  242. }
  243. /************************************************************************/
  244. /*
  245. 函数:ScreeningDir
  246. 描述: 筛选目录
  247. 参数:
  248. IN const CString* pDir, 需要检测的根目录
  249. IN CStringArray* pOrderArr, 订单号集
  250. OUT CStringArray& dirArr 返回需要生成小图的目录
  251. 返回: TRUE,FALSE
  252. */
  253. /************************************************************************/
  254. BOOL CreateSmallPhoto::ScreeningDir(IN const CString* pDir, IN CStringArray* pOrderArr, OUT CStringArray& dirArr)
  255. {
  256. if(pDir == NULL || pOrderArr == NULL)
  257. return FALSE;
  258. #if 0
  259. ffsco o;
  260. o.dirs(0);
  261. o.find(LPCSTR((*pDir)), LPCSTR("*.*"));
  262. ffsco::typeT coo;
  263. ffsco::typeT::iterator it;
  264. coo = o.co_dir();
  265. int oldsize = 0;
  266. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  267. CString strOldPath = _T("");
  268. for (it = coo.begin(); coo.end() != it; it++)
  269. {
  270. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  271. break;
  272. path = it->c_str();
  273. path.MakeLower();
  274. if(path.CompareNoCase(strOldPath) == 0)
  275. continue;
  276. if(!PathFileExists(path))
  277. continue;
  278. if(path.GetAt(path.GetLength() - 1) != '\\')
  279. path += _T("\\");
  280. BOOL bNeed = FALSE;
  281. //先检测目录是否是要生成小图的目录
  282. for(int i=0; i<pOrderArr->GetSize(); i++)
  283. {
  284. CString strOrderNum = _T("\\") + pOrderArr->ElementAt(i) + _T("\\");
  285. if(path.Find(strOrderNum) != -1)
  286. {
  287. bNeed = TRUE;
  288. break;
  289. }
  290. }
  291. if(!bNeed)
  292. continue;
  293. //删除要生成小图的目录里的ok文件
  294. // DelOKFiles(&path);
  295. dirArr.Add(it->c_str());
  296. strOldPath = it->c_str();
  297. }
  298. #else
  299. CString strRootDirecotry = *pDir;
  300. CString strOrderDirectory = _T("");
  301. strRootDirecotry.TrimRight(_T('\\'));
  302. for ( int i = 0; i < pOrderArr->GetSize(); i++ )
  303. {
  304. if(WaitForSingleObject(m_hExitEvent, 0) == WAIT_OBJECT_0)
  305. break;
  306. strOrderDirectory.Format(_T("%s\\%s"), strRootDirecotry, pOrderArr->ElementAt(i));
  307. if ( PathFileExists(strOrderDirectory) )
  308. dirArr.Add(strOrderDirectory);
  309. }
  310. #endif
  311. return TRUE;
  312. }
  313. /************************************************************************/
  314. /*
  315. 函数:DelOKFiles
  316. 描述: 删除ok文件
  317. 参数:
  318. IN CString* pPath
  319. 返回: TRUE,FALSE
  320. */
  321. /************************************************************************/
  322. BOOL CreateSmallPhoto::DelOKFiles(IN CString* pPath)
  323. {
  324. if(pPath == NULL || (*pPath) == _T(""))
  325. return FALSE;
  326. ffsco o;
  327. o.dirs(1);
  328. o.find(LPCSTR((*pPath)), LPCSTR("*.*"));
  329. ffsco::typeT coo;
  330. ffsco::typeT::iterator it;
  331. coo = o.co_dir();
  332. int oldsize = coo.size();
  333. CString path(_T("")), temp(_T("")), spath(_T("")), mpath(_T(""));
  334. CString strOldPath = _T("");
  335. for (it = coo.begin(); coo.end() != it; it++)
  336. {
  337. CString str = it->c_str();
  338. if(str.GetAt(str.GetLength() - 1) != '\\')
  339. str += _T("\\");
  340. CString strTmp1 = str;
  341. CString strTmp2 = str;
  342. strTmp1 += _T("ok");
  343. strTmp2 += _T("ok.dat");
  344. if(PathFileExists(strTmp1))
  345. DeleteFile(strTmp1); // 删除ok文件;
  346. if(PathFileExists(strTmp2))
  347. DeleteFile(strTmp2); // 删除ok文件;
  348. }
  349. return TRUE;
  350. }
  351. unsigned __stdcall WorkThread(LPVOID lpParam)
  352. {
  353. CreateSmallPhoto* p = (CreateSmallPhoto*)lpParam;
  354. if(p != NULL)
  355. p->Work();
  356. return 0;
  357. }
  358. void CreateSmallPhoto::StartThread(CProgressCtrl &ctrl, CWnd *pWnd)
  359. {
  360. m_ctrl_process = &ctrl;
  361. m_pWnd = pWnd;
  362. TerminateThread();
  363. lCreating = 1;
  364. m_hExitEvent = CreateEvent(0, TRUE, FALSE, NULL);
  365. unsigned int lThreadID = 0;
  366. m_hThread = (HANDLE)_beginthreadex(NULL, 0, WorkThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &lThreadID);
  367. }
  368. void CreateSmallPhoto::TerminateThread()
  369. {
  370. if(m_hExitEvent)
  371. SetEvent(m_hExitEvent);
  372. if(m_hThread)
  373. {
  374. ::WaitForSingleObject(m_hThread, INFINITE);
  375. CloseHandle(m_hThread);
  376. m_hThread = NULL;
  377. }
  378. if(m_hExitEvent)
  379. {
  380. CloseHandle(m_hExitEvent);
  381. m_hExitEvent = NULL;
  382. }
  383. }
  384. /************************************************************************/
  385. /*
  386. 函数:CreateOKFiles
  387. 描述: 生成ok文件
  388. 参数:
  389. IN CString& strDir, 要生成ok文件的目录
  390. IN int nOldSize 文件数量
  391. 返回: TRUE,FALSE
  392. */
  393. /************************************************************************/
  394. void CreateSmallPhoto::CreateOKFiles(IN CString& strDir, IN int nOldSize)
  395. {
  396. if(strDir == _T(""))
  397. return;
  398. try
  399. {
  400. CString strPath = _T("");
  401. CString strTmpDir = strDir;
  402. if(strDir.GetAt(strDir.GetLength() - 1) != '\\')
  403. strDir += _T("\\");
  404. ffsco o;
  405. o.dirs(1);
  406. o.find(LPCSTR(strDir), LPCSTR("*.*"));
  407. ffsco::typeT cooDir;
  408. ffsco::typeT::iterator it;
  409. cooDir = o.co_dir();
  410. for(it=cooDir.begin(); it!=cooDir.end(); it++)
  411. {
  412. strPath = it->c_str();
  413. if(strPath.GetAt(strPath.GetLength() - 1) != '\\')
  414. strPath += _T("\\");
  415. strPath += _T("ok");
  416. CFile fp;
  417. fp.Open(strPath, CFile::modeCreate | CFile::modeWrite);
  418. fp.Close();
  419. }
  420. }
  421. catch (...)
  422. {
  423. WriteTextLog("出错在函数CreateOKFiles中");
  424. }
  425. }