// MsgAttachment.cpp : implementation file // #include "stdafx.h" #include "mbamdb.h" #include "MsgAttachment.h" #include "MBAMDBFun.h" #include "Msg.h" // CMsgAttachment CMsgAttachmentItem::CMsgAttachmentItem() { m_nRowType = ROWTYPE_FOLDERMGR; Clear(); } CMsgAttachmentItem::~CMsgAttachmentItem() { } void CMsgAttachmentItem::operator =( const CMsgAttachmentItem& item ) { m_strFileName = item.m_strFileName; m_strFilePath = item.m_strFilePath; m_nFileSize = item.m_nFileSize; m_strMsgID = item.m_strMsgID; m_strFileID = item.m_strFileID; m_strFileSFlag = item.m_strFileSFlag; m_strFileMD5 = item.m_strFileMD5; m_nFileEnType = item.m_nFileEnType; m_tmSendDate = item.m_tmSendDate; } void CMsgAttachmentItem::Clear() { m_strFileName = _T(""); m_strFilePath = _T(""); m_nFileSize = 0; m_strFileID = _T(""); m_strFileSFlag = _T(""); m_nFileEnType = 0; m_strFileMD5 = _T(""); m_tmSendDate = COleDateTime::GetCurrentTime( ); } BOOL CMsgAttachmentItem::GetItemValue( CString strName, COleVariant &varData ) { varData.Clear(); if ( strName.IsEmpty() ) return false; if ( strName.CompareNoCase( _T( "MsgID" ) ) == 0 ) varData = m_strMsgID; else if ( strName.CompareNoCase( _T( "FileName" ) ) == 0 ) varData = m_strFileName; else if ( strName.CompareNoCase( _T( "FileSize" ) ) == 0 ) varData = (_int64)m_nFileSize; else if ( strName.CompareNoCase( _T( "FilePath" ) ) == 0 ) varData = m_strFilePath; else if ( strName.CompareNoCase( _T( "FileID" ) ) == 0 ) varData = m_strFileID; else if ( strName.CompareNoCase( _T( "FileSFlag" ) ) == 0 ) varData = m_strFileSFlag; else if ( strName.CompareNoCase( _T( "FileEnType" ) ) == 0 ) varData = (long)m_nFileEnType; else if ( strName.CompareNoCase( _T( "FileMD5" ) ) == 0 ) varData = m_strFileMD5; else return false; return true; } BOOL CMsgAttachmentItem::InsertRecord( CDBConnect *pAConnect) { CString strSQL; BOOL bReturn = true; CString strFileName; strFileName = CMBAMDBFun::GetDBReplaceStr( m_strFileName ); if( strFileName.Right(1) == _T("\\") ) { strFileName.TrimRight(_T("\\")); if(strFileName.IsEmpty()) strFileName = _T("AMTempFolder\\"); else strFileName += _T("\\"); } else { if(strFileName.IsEmpty()) strFileName = _T("AMTempFile"); } strSQL.Format( _T("INSERT INTO TN_STK_ATTACHMENT(G_C1, S_C2, N_C3, S_C4, G_C5,S_C6, N_C7,S_C8) VALUES ( \ '%s','%s', %I64d, '%s', '%s', '%s', %d, '%s')"),m_strMsgID, strFileName, m_nFileSize, m_strFileMD5, m_strFileID, m_strFileSFlag, m_nFileEnType, CMBAMDBFun::GetDBReplaceStr( m_strFilePath )); try { bReturn = CDBSQLiteCommFun::RunSQL(strSQL, pAConnect); } catch ( CDBSQliteException ex ) { CString strError = ex.GetErrInfo(); CString strErrInfo; strErrInfo = _T("InsertRecord - MsgAttachment- ") + strError; CMBAMDBFun::WriteDBErrToFile( strErrInfo ); return false; } return true; } BOOL CMsgAttachmentItem::UpdateRecord( CDBConnect *pAConnect ) { CString strSQL; BOOL bReturn = true; CString strFileName; strFileName = CMBAMDBFun::GetDBReplaceStr( m_strFileName ); if( strFileName.Right(1) == _T("\\") ) { strFileName.TrimRight(_T("\\")); if(strFileName.IsEmpty()) strFileName = _T("AMTempFolder\\"); else strFileName += _T("\\"); } else { if(strFileName.IsEmpty()) strFileName = _T("AMTempFile"); } strSQL.Format( _T("UPDATE TN_STK_ATTACHMENT SET N_C3 = %I64d WHERE S_C2 = '%s' AND G_C1 = '%s' AND G_C5 = '%s' AND S_C6 = '%s'"), m_nFileSize, strFileName, m_strMsgID, m_strFileID, m_strFileSFlag ); try { bReturn = CDBSQLiteCommFun::RunSQL(strSQL, pAConnect); } catch ( CDBSQliteException ex ) { CString strError = ex.GetErrInfo(); CString strErrInfo; strErrInfo.Format( _T("UpdateRecord(%s) - MsgAttachment - %s "),strSQL, strError); CMBAMDBFun::WriteDBErrToFile( strErrInfo ); return false; } return true; } CMsgAttachments::CMsgAttachments(CMBAMDBConnect *pAConnect) { m_pMsgs = NULL; m_pAConnect = pAConnect; m_bHaveDetail = false; } CMsgAttachments::~CMsgAttachments() { } // CMsgAttachment member functions void CMsgAttachments::operator =( const CMsgAttachments& item ) { m_pMsgs = item.m_pMsgs; // m_ARecordSet = item.m_ARecordSet; m_pAConnect = item.m_pAConnect; m_strSQL = item.m_strSQL; m_nRowType = item.m_nRowType; m_strFileName = item.m_strFileName; m_strFilePath = item.m_strFilePath; m_nFileSize = item.m_nFileSize; m_strMsgID = item.m_strMsgID; m_strFileSFlag = item.m_strFileSFlag; m_strFileID = item.m_strFileID; m_nFileEnType = item.m_nFileEnType; m_bHaveDetail = item.m_bHaveDetail; m_tmSendDate = item.m_tmSendDate; m_strFileMD5 = item.m_strFileMD5; } bool CMsgAttachments::GetData( ) { CString strTempValue; try { strTempValue = _T(""); m_ARecordSet.GetValue( 1, strTempValue, true ); m_strMsgID = strTempValue; strTempValue = _T(""); m_ARecordSet.GetValue( 2, strTempValue, true ); m_strFileID = strTempValue; if ( m_bHaveDetail ) { strTempValue = _T(""); m_ARecordSet.GetValue( 3, strTempValue, true ); m_strFileName = strTempValue; m_nFileSize = 0; m_ARecordSet.GetValue( 4, m_nFileSize ); strTempValue = _T(""); m_ARecordSet.GetValue( 5, strTempValue, true ); m_strFileMD5 = strTempValue; strTempValue = _T(""); m_ARecordSet.GetValue( 6, strTempValue, true ); m_strFileSFlag = strTempValue; m_ARecordSet.GetValue( 7, strTempValue ); m_nFileEnType = _tstoi( strTempValue ); strTempValue = _T(""); m_ARecordSet.GetValue( 8, strTempValue, true ); m_strFilePath = strTempValue; strTempValue = _T(""); m_ARecordSet.GetValue( 9, m_tmSendDate); /*m_ARecordSet.GetValue( 9, strTempValue, true ); m_strSendDate = strTempValue;*/ } } catch ( CDBSQliteException ex ) { CString strError; strError = (LPCTSTR)ex.GetErrInfo( ); return false; } return true; } int CMsgAttachments::ListSpMsgID(LPCTSTR lpszMsgID, LPCTSTR lpsFileID) { if ( lpszMsgID == NULL ) return 0; CString strSQL; int nCount; strSQL.Format( _T("SELECT a.G_C1, b.G_C5,b.S_C2, b.N_C3,b.S_C4, b.S_C6, b.N_C7, b.S_C8, a.T_C6 ") _T("FROM TN_STK_MSG a INNER JOIN TN_STK_ATTACHMENT b ") _T("ON a.G_C1 = b.G_C1 ") _T("WHERE b.G_C1 = '%s' AND b.G_C5 = '%s' "), lpszMsgID, lpsFileID); m_strSQL = strSQL; if( !m_pAConnect || !m_pAConnect->IsOpenDB()) return 0; try { m_ARecordSet.Close(); m_ARecordSet.Open( m_pAConnect->GetDBConnect(), strSQL ); m_ARecordSet.MoveFirst(); nCount = m_ARecordSet.GetRecordCount( ); } catch( CDBSQliteException ex ) { CString strError; strError = (LPCTSTR)ex.GetErrInfo( ); return 0; } m_bHaveDetail = TRUE; return nCount; } void CMsgAttachments::LoadData( CMsgAttachmentItem msgAttachment ) { m_strFileName = msgAttachment.m_strFileName; m_strFilePath = msgAttachment.m_strFilePath; m_strFileID = msgAttachment.m_strFileID; m_strFileSFlag = msgAttachment.m_strFileSFlag; m_nFileSize = msgAttachment.m_nFileSize; m_strMsgID = msgAttachment.m_strMsgID; m_strFileMD5 = msgAttachment.m_strFileMD5; m_nFileEnType = msgAttachment.m_nFileEnType; } bool CMsgAttachments::GetMsg( CMsgs &msg ) { if ( m_pMsgs == NULL ) return false; msg = (*m_pMsgs); return true; } long CMsgAttachments::ListRecordCount( ) { CString strTempSQL; if(m_strSQL.IsEmpty()) return m_ARecordSet.GetRecordCount( ); else { strTempSQL.Format(_T(" SELECT COUNT(*) FROM ( %s)T"), m_strSQL); long nTempValue = 0; if( !m_pAConnect || !m_pAConnect->IsOpenDB()) return nTempValue; CDBRecord ARecordset; try { ARecordset.Open( m_pAConnect->GetDBConnect(), strTempSQL ); while ( S_OK == ARecordset.MoveNext() ) { ARecordset.GetValue( 1, nTempValue ); break; } } catch ( CDBSQliteException ex ) { CString strError; strError = (LPCTSTR)ex.GetErrInfo( ); return nTempValue; } return nTempValue; } } int CMsgAttachments::ListAttachments( CString strUser,CString strUserName, CString strKey ) { CString strSQL; CString strStartDate; CString strEndDate; CString strSQLWhere= _T(""); int nCount; CString strSQLTemp = _T(""); CDBRecord ARecordSet; strUser.MakeLower(); if( !strKey.IsEmpty()) { strSQLTemp.Format( _T("AND b.S_C2 LIKE '%%%s%%' "), strKey ); strSQLWhere += strSQLTemp; } if ( m_tmStartDate.GetYear() > 1970 ) { strStartDate = CMBAMDBFun::FormatDBTIMEToStr( m_tmStartDate ); if ( m_tmEndDate > m_tmStartDate) { strEndDate = CMBAMDBFun::FormatDBTIMEToStr( m_tmEndDate ); strSQLTemp.Format( _T(" AND a.T_C6 >= '%s' AND a.T_C6 <= '%s'"), strStartDate, strEndDate ); } else { strSQLTemp.Format( _T(" AND a.T_C6 >= '%s'"), strStartDate ); } strSQLWhere += strSQLTemp; } else { if ( m_tmEndDate.GetYear() > 1970 ) { strEndDate = CMBAMDBFun::FormatDBTIMEToStr( m_tmEndDate ); strSQLTemp.Format( _T(" AND a.T_C6 <= '%s'"), strEndDate ); strSQLWhere += strSQLTemp; } } if( !strUser.IsEmpty()) { if(strUserName.IsEmpty() ) { strSQLTemp.Format( _T("AND (a.S_C4 LIKE '%s%%' OR a.S_C5 LIKE '%s%%') "), strUser,strUser ); } else strSQLTemp.Format( _T("AND a.S_C4='%s' "), strUser ); strSQLWhere += strSQLTemp; } //strSQL.Format( _T("SELECT a.G_C1, a.S_C4, a.S_C5, b.S_C2, b.N_C3, b.S_C4, b.G_C5, b.S_C6, b.N_C7, b.S_C8, a.T_C6, a.C_C9, a.N_C11 ") // _T("FROM TN_STK_MSG a INNER JOIN TN_STK_ATTACHMENT b ") // _T("ON a.G_C1 = b.G_C1 ") // _T("WHERE a.N_C12 > 0 %s ORDER BY a.T_C6 DESC, a.G_C1, b.S_C2"),strSQLWhere); strSQL.Format( _T("SELECT a.G_C1, b.G_C5 ") _T("FROM TN_STK_MSG a INNER JOIN TN_STK_ATTACHMENT b ") _T("ON a.G_C1 = b.G_C1 ") _T("WHERE a.N_C12 > 0 %s ORDER BY a.T_C6 DESC, a.G_C1, b.S_C2"),strSQLWhere); m_strSQL = strSQL; if( !m_pAConnect || !m_pAConnect->IsOpenDB()) return 0; try { nCount = ListRecordCount(); m_ARecordSet.Close( ); m_ARecordSet.Open( m_pAConnect->GetDBConnect(), strSQL ); } catch( CDBSQliteException ex ) { CString strError; strError = (LPCTSTR)ex.GetErrInfo( ); return 0; } return nCount; }