#include "stdafx.h" #include "OrderDirMgr.h" #include "Global.h" #include "DataBaseOpt.h" #include "ffscoex.h" #include "CharacterConvert.h" using namespace helper_coffs; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif OrderDirMgr::OrderDirMgr() { } OrderDirMgr::~OrderDirMgr() { ClearShareInfo(); ClearOrderInfo(); } /************************************************************************/ /* 函数: InitShareDir 描述: 初始化共享目录 参数: IN DataBaseOpt* pDBOpt 数据库操作 IN LPCTSTR lpLocalDomain 本店域名 返回: 1成功,0失败 */ /************************************************************************/ int OrderDirMgr::InitShareDir(IN DataBaseOpt* pDBOpt, IN LPCTSTR lpLocalDomain) { //获取客户端设置的备份目录和共享目录路径 GetBackupDirFromDB(pDBOpt, lpLocalDomain); //加载本地目录 LoadLocalShareDir(lpLocalDomain); GetShareDirFromDB(pDBOpt); return 1; } /************************************************************************/ /* 函数: GetBackupDirFromDB 描述: 获取备份目录 参数: IN DataBaseOpt* pDBOpt 数据库操作 IN LPCTSTR lpLocalDomain 本店域名 返回: 1成功,0失败 */ /************************************************************************/ int OrderDirMgr::GetBackupDirFromDB(IN DataBaseOpt* pDBOpt, IN LPCTSTR lpLocalDomain) { if(pDBOpt == NULL) return 0; if(!pDBOpt->IsOpenDB()) return 0; //读取vesion表里的备份目录 CArray bakDirArr; std::vector vFields; vFields.push_back(_T("bak1")); vFields.push_back(_T("bak2")); vFields.push_back(_T("bak3")); vFields.push_back(_T("bak4")); vFields.push_back(_T("bakserver1")); vFields.push_back(_T("bakserver2")); vFields.push_back(_T("bakserver3")); vFields.push_back(_T("bakserver4")); if(pDBOpt->Select(bakDirArr, _T("version"), vFields)) { for(int i=0; i < 4; i++) { //过滤不可用的目录 if(_ttoi(bakDirArr.ElementAt(0).ElementAt(i)) == 0 || bakDirArr.ElementAt(0).ElementAt(i+4) == _T("")) continue; LPTblNetShareInfo p = new TblNetShareInfo; p->bEnable = _ttoi(bakDirArr.ElementAt(0).ElementAt(i)); _tcscpy(p->szBranchId, lpLocalDomain); _tcscpy(p->szShareDirectory, bakDirArr.ElementAt(0).ElementAt(i+4)); CString strLog = _T(""); strLog.Format(_T("%s\n"), p->szShareDirectory); WriteLogin(strLog); p->byMinCapacity = 5; p->byMaxCapacity = 35; p->byPhotoType = (BYTE)(i+5); p->byPriority = 255; m_vShareDir.push_back(p); } } return 1; } /************************************************************************/ /* 函数: GetShareDirFromDB 描述: 获取备份目录通过数据库 参数: IN DataBaseOpt* pDBOpt 数据库操作 返回: 1成功,0失败 */ /************************************************************************/ int OrderDirMgr::GetShareDirFromDB(IN DataBaseOpt* pDBOpt) { if(pDBOpt == NULL) return 0; if(!pDBOpt->IsOpenDB()) return 0; //读取vesion表里的备份目录 CArray shareDirArr; std::vector vFields; vFields.push_back(_T("enable")); vFields.push_back(_T("branchid")); vFields.push_back(_T("sharePath")); vFields.push_back(_T("mincapacity")); vFields.push_back(_T("maxcapacity")); vFields.push_back(_T("photoType")); vFields.push_back(_T("priority")); if(pDBOpt->Select(shareDirArr, _T("NetShareInfo"), vFields)) { for(int i=0; ibEnable = _ttoi(shareDirArr.ElementAt(i).ElementAt(0)); #ifdef _UNICODE wcscpy(p->szBranchId, shareDirArr.ElementAt(i).ElementAt(1)); wcscpy(p->szShareDirectory, shareDirArr.ElementAt(i).ElementAt(2)); #else strcpy(p->szBranchId, shareDirArr.ElementAt(i).ElementAt(1)); strcpy(p->szShareDirectory, shareDirArr.ElementAt(i).ElementAt(2)); #endif CString strLog = _T(""); strLog.Format(_T("%s\n"), p->szShareDirectory); WriteLogin(strLog); p->byMinCapacity = _ttoi(shareDirArr.ElementAt(i).ElementAt(3)); p->byMaxCapacity = _ttoi(shareDirArr.ElementAt(i).ElementAt(4)); p->byPhotoType = _ttoi(shareDirArr.ElementAt(i).ElementAt(5)); p->byPriority = _ttoi(shareDirArr.ElementAt(i).ElementAt(6)); m_vShareDir.push_back(p); } } return 1; } /************************************************************************/ /* 函数: LoadLocalShareDir 描述: 加载本地共享目录 参数: IN LPCTSTR lpLocalDomain 本店域名 返回: */ /************************************************************************/ void OrderDirMgr::LoadLocalShareDir(IN LPCTSTR lpLocalDomain) { TCHAR* pShareHead = _T("\\\\"); TCHAR* szPath[] = {_T("客户原片(管理软件)$"), _T("修好的片(管理软件)$"), _T("精修好的片(管理软件)$"), _T("设计好的片(管理软件)$")}; TCHAR szBuffer[MAX_PATH] = {0}; DWORD dwBuffSize = sizeof(TCHAR)*MAX_PATH; GetComputerName(szBuffer, &dwBuffSize); for(int i=0; i<4; i++) { CString strOriginalFilmDir = _T(""); strOriginalFilmDir.Format(_T("%s%s\\%s"), pShareHead, szBuffer, szPath[i]); if(PathFileExists(strOriginalFilmDir)) { LPTblNetShareInfo p = new TblNetShareInfo; p->bEnable = 1; p->byMinCapacity = 3; p->byMaxCapacity = 25; p->byPriority = 255; p->byPhotoType = (BYTE)(i+1); #ifdef _UNICODE wcscpy(p->szBranchId, _T("")); wcscpy(p->szShareDirectory, strOriginalFilmDir); #else strcpy(p->szBranchId, lpLocalDomain); strcpy(p->szShareDirectory, strOriginalFilmDir); #endif //#ifdef _UNICODE CString strLog = _T(""); strLog.Format(_T("%s\n"), p->szShareDirectory); WriteLogin(strLog); m_vShareDir.push_back(p); } } } /************************************************************************/ /* 函数: GetOrderOfDir 描述: 获取订单目录 参数: IN CString strST, 初始时间 IN CString strET 结束时间 IN LPCTSTR lpLocalDomain 本地域名 返回: */ /************************************************************************/ void OrderDirMgr::GetOrderOfDir(IN CString& strST, IN CString& strET, IN LPCTSTR lpLocalDomain) { ClearOrderInfo(); if(m_vShareDir.size() == 0) return; std::vector::iterator it = m_vShareDir.begin(); for(;it != m_vShareDir.end();) { LPTblNetShareInfo p = (*it); if(p != NULL) { //共享目录是否可用 if(p->bEnable == TRUE && p->byPhotoType <= 4) FindAllOrderDir(strST, strET, lpLocalDomain, p->szShareDirectory, p->szShareDirectory, p->byPhotoType, m_vOrderDir); } ++it; } } /************************************************************************/ /* 函数: FindAllOrderDir 描述: 获取订单目录 参数: IN CString& strST, 开始时间 IN CString& strET 结束时间 IN LPCTSTR lpRootDir, 根目录 IN LPCTSTR lpDir, 查找的目录 IN const BYTE byPhotoType 相片类型 OUT std::vector& vOrderDir 所有订单目录 返回: */ /************************************************************************/ void OrderDirMgr::FindAllOrderDir(IN CString& strST, IN CString& strET, IN LPCTSTR lpLocalDomain, IN LPCTSTR lpRootDir, IN LPCTSTR lpDir, IN const BYTE byPhotoType, OUT std::vector& vOrderDir) { if(lpRootDir == NULL || _tcscmp(lpRootDir, _T("")) == 0 || lpDir == NULL || _tcscmp(lpDir, _T("")) == 0) return; CTime st(_ttoi(strST.Mid(0, 4)), _ttoi(strST.Mid(5, 2)), _ttoi(strST.Mid(8, 2)), 0, 0, 0); CTime et(_ttoi(strET.Mid(0, 4)), _ttoi(strET.Mid(5, 2)), _ttoi(strET.Mid(8, 2)), 0, 0, 0); CString strRootDir = lpRootDir; strRootDir.TrimRight('\\'); strRootDir += _T("\\"); ffscoex coex; coex.dirs(1); #ifdef _UNICODE char szDir[MAX_PATH + 1] = {0}; unicode2acsii(lpDir, szDir); coex.find(szDir, "*.*"); #else coex.find(lpDir, "*.*"); #endif //#ifdef _UNICODE ffscoex::typeT coo; ffscoex::typeT::iterator iter; coo = coex.co_dir(); for(iter = coo.begin(); iter != coo.end(); iter++) { //遍历目录 LPSFILEINFO lp = (*iter); if(_tcscmp(lp->cFileName, _T("ok")) == 0 || _tcscmp(lp->cFileName, _T("modifytime")) == 0) continue; //文件创建时间 CTime ct(lp->ftCreationTime); //CString strTime = _T(""); //strTime.Format(_T("%04d-%02d-%02d"), ct.GetYear(), ct.GetMonth(), ct.GetDay()); if(ct < st || ct > et) continue; TCHAR szPath[MAX_PATH +1] = {0}; #ifdef _UNICODE ascii2unicode(lp->strAllPath.c_str(), szPath); #else strcpy(szPath, lp->strAllPath.c_str()); #endif //#ifdef _UNICODE //共享目录是否与查找的当前目录相同 if(_tcscmp(strRootDir, szPath) == 0) continue; //去掉系统定义的目录 CString strPath = szPath; if(strPath.Find(_T("\\microablum\\")) != -1 || strPath.Find(_T("\\Qrcode\\")) != -1) continue; //去掉域名目录 strPath.TrimRight('\\'); CString strR = strPath.Right(strPath.GetLength() - strPath.ReverseFind('\\') - 1); if(strR.Find(_T(".ly.com")) != -1 || strPath == _T("microablum") || strPath == _T("Qrcode")) continue; TCHAR szDomain[64] = {0}; //去掉根目录 CString strL = strPath.Right(strPath.GetLength() - strRootDir.GetLength()); if(strL.Find(_T(".ly.com")) != -1) { int npos = strL.Find('\\'); CString strDomain = strL.Left(npos); strL = strL.Right(strL.GetLength() - npos - 1); _tcscpy(szDomain, strDomain); } else { _tcscpy(szDomain, lpLocalDomain); } if(strL.Find(_T("\\")) != -1) continue; LPSORDERDIRINFO lporderdir = new SORDERDIRINFO; _tcscpy(lporderdir->szPath, szPath); lporderdir->byPhotoType = byPhotoType; #ifdef _UNICODE wsprintf(lporderdir->szOptTime, _T("%04d-%02d-%02d %02d:%02d:%02d.000"), ct.GetYear(), ct.GetMonth(), ct.GetDay(), ct.GetHour(), ct.GetMinute(), ct.GetSecond()); #else sprintf(lporderdir->szOptTime, _T("%04d-%02d-%02d %02d:%02d:%02d.000"), ct.GetYear(), ct.GetMonth(), ct.GetDay(), ct.GetHour(), ct.GetMinute(), ct.GetSecond()); #endif _tcscpy(lporderdir->szOrder, strL); _tcscpy(lporderdir->szDomain, szDomain); vOrderDir.push_back(lporderdir); } } void OrderDirMgr::ClearShareInfo() { std::vector::iterator it = m_vShareDir.begin(); for(;it != m_vShareDir.end();) { if((*it) != NULL) delete (*it); ++it; } m_vShareDir.clear(); } void OrderDirMgr::ClearOrderInfo() { std::vector::iterator it = m_vOrderDir.begin(); for(;it != m_vOrderDir.end();) { if((*it) != NULL) delete (*it); ++it; } m_vOrderDir.clear(); } /************************************************************************/ /* 函数: ScreeningOrderDir 描述: 筛选相片订单目录 参数: IN CArray& logsArr 备份记录集 IN const CString& strLocalDomain 本地哉名 返回: 1是数字,0不是纯数字 */ /************************************************************************/ void OrderDirMgr::ScreeningOrderDir(IN CArray& logsArr, IN const CString& strLocalDomain) { for(int i=0; i::iterator it = m_vOrderDir.begin(); for(;it != m_vOrderDir.end(); it++) { LPSORDERDIRINFO lp = (*it); if(lp == NULL) continue; if(tyPhotoType != lp->byPhotoType) continue; if(_tcscmp(strDomain, strLocalDomain) == 0) { //本店 if(_tcsstr(lp->szPath, _T(".ly.com")) != NULL) continue; if(_tcsstr(lp->szPath, strOrder) != NULL) { //删除有相同订单号的 delete lp; m_vOrderDir.erase(it); break; } } else { //分店 if(_tcsstr(lp->szPath, _T(".ly.com")) == NULL) continue; if(_tcsstr(lp->szPath, strOrder) != NULL) { //删除有相同订单号的 delete lp; m_vOrderDir.erase(it); break; } } } } } /************************************************************************/ /* 函数:FindBakTargetDir 描述:增量备份完成回调 参数: IN const BYTE nPhotoType 相片类型 OUT CString& strTargetDir 备份的目标目录 返回:1成功,0失败 */ /************************************************************************/ int OrderDirMgr::FindEnableBakDir(IN const BYTE nPhotoType, OUT CString& strTargetDir) { double dTotalNum(0.0), dFreeNum(0.0); BOOL bEnable = FALSE; BYTE byOldPriority = 0; std::vector::iterator it = m_vShareDir.begin(); for(;it != m_vShareDir.end(); ++it) { LPTblNetShareInfo lp = (*it); if(lp == NULL) continue; if(nPhotoType != lp->byPhotoType) continue; // 存在且目录容量仍大于mincapacity可用; if(!PathFileExists(lp->szShareDirectory)) continue; //优先级 if(lp->byPriority > byOldPriority) { if(!GetDiskSpaceInfo(lp->szShareDirectory, dTotalNum, dFreeNum)) continue; byOldPriority = lp->byPriority; if(dFreeNum > (*it)->byMinCapacity) { bEnable = TRUE; strTargetDir = lp->szShareDirectory; break; } } } return bEnable; } /************************************************************************/ /* 函数:GetDiskSpaceInfo 描述:获取指定目录或盘符的容量信息; 参数: IN: lpRootPathName 根目录名称; INOUT: dTotalNum 总容量; INOUT: dFreeNum 可用容量; 返回: 注意: GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G); GetDiskFreeSpaceEx支持64位(16384P=1024*16384T) 只能获取本机硬盘信息,且路径必须包含":"冒号; 调用约定: */ /************************************************************************/ BOOL OrderDirMgr::GetDiskSpaceInfo(IN LPCTSTR lpRootPathName,IN OUT double &dTotalNum, IN OUT double &dFreeNum) { // 校验参数有效性; if(lpRootPathName == NULL || _tcscmp(lpRootPathName,_T("")) == 0) { //OutputDebugString(_T("目录为空\n")); return FALSE; } // 如果长度==1; int nlen = _tcslen(lpRootPathName); if((1 == nlen) || ( 2 == nlen && lpRootPathName[nlen-1] != _T(':'))) { //OutputDebugString(_T("根目录无效\n")); return FALSE; } if ( !PathFileExists(lpRootPathName)) { //OutputDebugString(_T("根目录不存在\n")); return FALSE; } BOOL fResult = FALSE; DWORD dwSectPerClust; // 每簇的扇区数; DWORD dwBytesPerSect; // 每个扇区的字节数; DWORD dwFreeClusters; // 可用簇数; DWORD dwTotalClusters; // 总簇数; typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,PULARGE_INTEGER, PULARGE_INTEGER); P_GDFSE pGetDiskFreeSpaceEx = NULL; /*unsigned*/ __int64 i64FreeBytesToCaller; /*unsigned*/ __int64 i64TotalBytes; /*unsigned*/ __int64 i64FreeBytes; // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace; #ifdef UNICODE pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetDiskFreeSpaceExW"); #else pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"),"GetDiskFreeSpaceExA"); #endif if ( pGetDiskFreeSpaceEx ) { fResult = pGetDiskFreeSpaceEx(lpRootPathName, (PULARGE_INTEGER)&i64FreeBytesToCaller, (PULARGE_INTEGER)&i64TotalBytes, (PULARGE_INTEGER)&i64FreeBytes); if ( fResult ) { // 将单位由byte转为G byte; dTotalNum = (double)i64TotalBytes/1024/1024/1024; dFreeNum = (double)i64FreeBytes/1024/1024/1024; } } else { fResult = GetDiskFreeSpace(lpRootPathName, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters); if ( fResult ) { // 成功获得容量信息; i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect; i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect; // 将单位由byte转为G byte; dTotalNum = (double)i64TotalBytes/1024/1024/1024; dFreeNum = (double)i64FreeBytes/1024/1024/1024; } } return fResult; }