// TaskDataOper.cpp: implementation of the CLoggingDataOper class. // ////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include "GeoMative.h" #include "logging\CLoggingDataOper.h" #include "GUCodeCreator.h" extern CGeoMativeApp theApp; extern int g_iUILanguage; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CLoggingDataOper::CLoggingDataOper() { } CLoggingDataOper::~CLoggingDataOper() { } CLoggingDataOper* CLoggingDataOper::GetInstance() { static CLoggingDataOper oper; return &oper; } map CLoggingDataOper::QueryLithologyInfo() { m_mapLithologyInfo.clear(); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); int iLithologyType = -1; CString strText; CString strSql = _T(""); STLoggingLithologyInfo stLithologyInfo; strSql.Format(_T("select ID,LithologyType,LithologyName,LithologyPicPath from TPictureInfo where LanguageID=%d"), g_iUILanguage); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); while (0 == pRecTd->adoEOF) { memset(&stLithologyInfo, 0, sizeof(stLithologyInfo)); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyName")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyName")); strcpy(stLithologyInfo.szLithologyName, strText); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyType")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyType")); //stLithologyInfo.iLithologyType = pRecTd->GetCollect(_T("LithologyType")).intVal; strcpy(stLithologyInfo.szLithologyType, strText); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyPicPath")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyPicPath")); strcpy(stLithologyInfo.szLithologyPic, strText); m_mapLithologyInfo[stLithologyInfo.szLithologyType] = stLithologyInfo; pRecTd->MoveNext(); } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } return m_mapLithologyInfo; } //根据岩性名称查找数据库相关信息 STLoggingLithologyInfo CLoggingDataOper::QueryLithologyInfoByName(CString strLithologyName) { _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); int iLithologyType = -1; CString strText; CString strSql = _T(""); STLoggingLithologyInfo stLithologyInfo; strSql.Format(_T("select ID,LithologyType,LithologyName,LithologyPicPath from TPictureInfo where LanguageID=%d and LithologyName='%s'"), g_iUILanguage, strLithologyName); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyName")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyName")); strcpy(stLithologyInfo.szLithologyName, strText); //stLithologyInfo.iLithologyType = pRecTd->GetCollect(_T("LithologyType")).intVal; strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyType")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyType")); //stLithologyInfo.iLithologyType = pRecTd->GetCollect(_T("LithologyType")).intVal; strcpy(stLithologyInfo.szLithologyType, strText); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("LithologyPicPath")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("LithologyPicPath")); strcpy(stLithologyInfo.szLithologyPic, strText); } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } return stLithologyInfo; } //数据库删除岩性图片 BOOL CLoggingDataOper::DeleteDBLithologyBitmap(CString strLithologyType) { if (strLithologyType.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("图片类型为空")); else MessageBoxEx(NULL, _T("Image type is empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } //是否存在 CString strSql; UINT uiPicID = 0; strSql.Format(_T("select ID from TPictureInfo where LithologyType ='%s'"), strLithologyType); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { uiPicID = (DWORD)pRecTd->GetCollect(_T("ID")).ulVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } if (uiPicID <= 0) { return FALSE; } _CommandPtr pCmdDel; pCmdDel.CreateInstance(_uuidof(Command)); pCmdDel->ActiveConnection = theApp.m_pConnection; try { strSql.Format(_T("delete from TPictureInfo where ID = %u"), uiPicID); pCmdDel->CommandText = strSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); strSql.Format(_T("delete from TPictureCode where PicID = %u"), uiPicID); pCmdDel->CommandText = strSql.AllocSysString(); pCmdDel->Execute(NULL, NULL, adCmdText); } catch (_com_error e) { AfxMessageBox(e.Description()); } return TRUE; } //数据库新增岩性图片 BOOL CLoggingDataOper::InsertDBLithologyBitmap(CString strLithologyType, CString strLithologyName, CString strLithologyPicPath, map> mapPictureCode) { if (strLithologyType.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("图片类型为空")); else MessageBoxEx(NULL, _T("Image type is empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } if (strLithologyName.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("图片名为空")); else MessageBoxEx(NULL, _T("Image name is empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } if (strLithologyPicPath.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("图片路径为空")); else MessageBoxEx(NULL, _T("Image path is empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } if (mapPictureCode.size() < 1) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("图片二维码为空")); else MessageBoxEx(NULL, _T("Qr code in the picture is empty"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return FALSE; } _CommandPtr pCmdIns = NULL; pCmdIns.CreateInstance(_uuidof(Command)); pCmdIns->ActiveConnection = theApp.m_pConnection; CString strSql = _T(""); //插入图片信息 try { strSql.Empty(); strSql.Format(_T("insert into TPictureInfo(LithologyType,TotalRow,TotalColumn,LithologyName,LithologyPicPath,LanguageID) values('%s',%d,%d,'%s','%s',%d)"), \ strLithologyType, GRID_X_DIRECTION_SIZE, GRID_Y_DIRECTION_SIZE, strLithologyName,strLithologyPicPath, g_iUILanguage); pCmdIns->CommandText = strSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); } catch (_com_error e) { AfxMessageBox(e.Description()); } //是否存在 UINT uiPicID = 0; strSql.Format(_T("select ID from TPictureInfo where LithologyType ='%s'"), strLithologyType); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { uiPicID = (DWORD)pRecTd->GetCollect(_T("ID")).ulVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } if (uiPicID == 0) { return FALSE; } //循环插入图片二维码ID try { map>::iterator iter = mapPictureCode.begin(); map::iterator iterX; CString strColumnContent; for (; iter != mapPictureCode.end(); iter++) { strColumnContent.Empty(); for (iterX = iter->second.begin(); iterX != iter->second.end(); iterX++) { if (iterX != iter->second.begin()) { strColumnContent += _T(","); } if (iterX->second) { strColumnContent.AppendFormat(_T("%d"), iterX->first); } } if (!strColumnContent.IsEmpty()) { strSql.Empty(); strSql.Format(_T("insert into TPictureCode(PicID,ColumnIndex,ColumnContent) values(%d,%d,'%s')"), uiPicID, iter->first, strColumnContent); OutputDebugString(strSql + _T("\n")); pCmdIns->CommandText = strSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); } } } catch (_com_error e) { AfxMessageBox(e.Description()); } return TRUE; } //加载数据库图片信息 map> CLoggingDataOper::LoadLithologyBitmapFromDB(CString strLithologyName) { map> mapPictureCode; if (strLithologyName.IsEmpty()) { AfxMessageBox(_T("无效文件名")); return mapPictureCode; } int iPos = strLithologyName.ReverseFind('/'); if (iPos != -1) { strLithologyName = strLithologyName.Mid(iPos + 1); } iPos = strLithologyName.Find('.'); strLithologyName = strLithologyName.Mid(0, iPos); //是否存在 UINT uiPicID = 0; CString strSql; strSql.Format(_T("select ID from TPictureInfo where LithologyType ='%s'"), strLithologyName); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { uiPicID = (DWORD)pRecTd->GetCollect(_T("ID")).ulVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } if (uiPicID == 0) { AfxMessageBox(_T("没有找到图片信息")); return mapPictureCode; } int iColumnIndex = -1; int iIndexY = -1; CString strColumnContent; map mapColContent; iPos = -1; strSql.Format(_T("select ColumnIndex,ColumnContent from TPictureCode where PicID =%d"), uiPicID); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); while (0 == pRecTd->adoEOF) { iColumnIndex = -1; mapColContent.clear(); iColumnIndex = (DWORD)pRecTd->GetCollect(_T("ColumnIndex")).intVal; strColumnContent.Empty(); strColumnContent = (VT_NULL == pRecTd->GetCollect(_T("ColumnContent")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("ColumnContent")); if (!strColumnContent.IsEmpty()) { do { iPos = strColumnContent.Find(','); if (iPos != -1) { iIndexY = atoi(strColumnContent.Mid(0, iPos)); strColumnContent = strColumnContent.Mid(iPos + 1); } else { iIndexY = atoi(strColumnContent); } if (iIndexY >= 0) { mapColContent[iIndexY] = 1; } } while (iPos != -1); mapPictureCode[iColumnIndex] = mapColContent; } pRecTd->MoveNext(); } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } return mapPictureCode; } BOOL CLoggingDataOper::SaveTaskInfoIntoDB(CString strTaskName, CString strTestLocation, BYTE byLogTypes, CString strTrace, int iWaveform, CString strSamplingInterval, CString strInitialDepth, CString strEndDepth, int iLogDir, CString strLoggingInterval, CString strInstrumentZeroLength, CString strPeriod) { _CommandPtr pCmdIns = NULL; pCmdIns.CreateInstance(_uuidof(Command)); pCmdIns->ActiveConnection = theApp.m_pConnection; CString strSql = _T(""); CGUCodeCreator creator; CString strTaskID = creator.GenerateGUIDCode(); float fEndDepth = atof(strEndDepth); float fInitDepth = atof(strInitialDepth); float fLoggingDistance = atof(strLoggingInterval); //插入logging任务信息 try { strSql.Empty(); strSql.Format(_T("insert into TLoggingTaskInfo(TaskID,TaskName,Location,LogType,Trace,Waveform,SamplingInterval,\ InitDepth,EndDepth,LogDirection,LoggingInterval,InstrumentZeroLength,Period) \ values('%s','%s','%s',%d,%d,%d,%f,\ %f,%f,%d,%f,%f,%f)"), \ strTaskID, strTaskName, strTestLocation, byLogTypes, atoi(strTrace), iWaveform, atof(strSamplingInterval),\ fInitDepth, fEndDepth, iLogDir, fLoggingDistance, atof(strInstrumentZeroLength), atof(strPeriod)); pCmdIns->CommandText = strSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); } catch (_com_error e) { AfxMessageBox(e.Description()); return FALSE; } UINT uiTaskID = 0; strSql.Empty(); strSql.Format(_T("select ID from TLoggingTaskInfo where TaskID ='%s'"), strTaskID); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { uiTaskID = (DWORD)pRecTd->GetCollect(_T("ID")).ulVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } if (uiTaskID == 0) { AfxMessageBox(_T("没有找到任务")); return FALSE; } //插入数据测点信息 try { strSql.Empty(); float fTestCount = ((fEndDepth - fInitDepth) / fLoggingDistance); int iTestCount = (int)((fEndDepth - fInitDepth) / fLoggingDistance); if (fTestCount - iTestCount > 0.000001) { iTestCount++; } strSql.Format(_T("fTestCount=%f, iTestCount=%d\n"), fTestCount, iTestCount); OutputDebugString(strSql); for (int i = 0; i < iTestCount; i++) { strSql.Empty(); strSql.Format(_T("insert into TLoggingTaskContent(TaskID,TestID) values(%d,%d)"), uiTaskID, i+1); pCmdIns->CommandText = strSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); } } catch (_com_error e) { AfxMessageBox(e.Description()); return FALSE; } return TRUE; } BOOL CLoggingDataOper::UpdateTaskInfoIntoDB(CString strTaskName, CString strTestLocation, BYTE byLogTypes, CString strTrace, int iWaveform, CString strSamplingInterval, CString strInitialDepth, CString strEndDepth, int iLogDir, CString strLoggingInterval, CString strInstrumentZeroLength, CString strPeriod) { UINT uiTaskID = 0; CString strSql; strSql.Empty(); strSql.Format(_T("select ID from TLoggingTaskInfo where TaskName ='%s'"), strTaskName); _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { uiTaskID = (DWORD)pRecTd->GetCollect(_T("ID")).ulVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } if (uiTaskID == 0) { AfxMessageBox(_T("没有找到任务")); return FALSE; } _CommandPtr pCmdIns = NULL; pCmdIns.CreateInstance(_uuidof(Command)); pCmdIns->ActiveConnection = theApp.m_pConnection; float fEndDepth = atof(strEndDepth); float fInitDepth = atof(strInitialDepth); float fLoggingDistance = atof(strLoggingInterval); //插入logging任务信息 try { strSql.Empty(); strSql.Format(_T("update TLoggingTaskInfo set TaskName='%s',Location='%s',LogType=%d,Trace=%d,Waveform=%d,SamplingInterval=%f,InitDepth=%f,EndDepth=%f,LogDirection=%d,LoggingInterval=%f,InstrumentZeroLength=%f,Period=%f where ID = %d"),\ strTaskName, strTestLocation, byLogTypes, atoi(strTrace), iWaveform, atof(strSamplingInterval), fInitDepth, fEndDepth, iLogDir, fLoggingDistance, atof(strInstrumentZeroLength), atof(strPeriod), uiTaskID); pCmdIns->CommandText = strSql.AllocSysString(); pCmdIns->Execute(NULL, NULL, adCmdText); } catch (_com_error e) { AfxMessageBox(e.Description()); return FALSE; } return TRUE; } map CLoggingDataOper::QueryTaskListFromDB() { map mapLoggingTaskInfo; _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); int iLithologyType = -1; CString strText; CString strSql = _T(""); STLoggingTaskInfo stLoggingTaskInfo; strSql.Format(_T("select TaskID,TaskName,Location,LogType,Trace,Waveform,SamplingInterval,InitDepth,\ EndDepth,LogDirection,LoggingInterval,InstrumentZeroLength,Period from TLoggingTaskInfo")); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); while (0 == pRecTd->adoEOF) { memset(&stLoggingTaskInfo, 0, sizeof(stLoggingTaskInfo)); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("TaskName")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("TaskName")); strcpy(stLoggingTaskInfo.szTaskName, strText); stLoggingTaskInfo.byLogType = pRecTd->GetCollect(_T("LogType")).cVal; stLoggingTaskInfo.fSamplingInterval = pRecTd->GetCollect(_T("SamplingInterval")).fltVal; stLoggingTaskInfo.fInitDepth = pRecTd->GetCollect(_T("InitDepth")).fltVal; stLoggingTaskInfo.fEndDepth = pRecTd->GetCollect(_T("EndDepth")).fltVal; stLoggingTaskInfo.byLogDirection = pRecTd->GetCollect(_T("LogDirection")).cVal; stLoggingTaskInfo.fLoggingDistance = pRecTd->GetCollect(_T("LoggingInterval")).fltVal; stLoggingTaskInfo.fPeriod = pRecTd->GetCollect(_T("Period")).fltVal; mapLoggingTaskInfo[stLoggingTaskInfo.szTaskName] = stLoggingTaskInfo; pRecTd->MoveNext(); } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } return mapLoggingTaskInfo; } STLoggingTaskInfo CLoggingDataOper::QueryTaskInfoByName(CString strTaskName) { _RecordsetPtr pRecTd = NULL; pRecTd.CreateInstance(_uuidof(Recordset)); int iLithologyType = -1; CString strText; CString strSql = _T(""); STLoggingTaskInfo stLoggingTaskInfo; strSql.Format(_T("select TaskID,TaskName,Location,LogType,Trace,Waveform,SamplingInterval,InitDepth,\ EndDepth,LogDirection,LoggingInterval,InstrumentZeroLength,Period from TLoggingTaskInfo where TaskName='%s'"), strTaskName); try { pRecTd->Open(strSql.AllocSysString(), _variant_t((IDispatch*)theApp.m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); if (pRecTd->GetRecordCount() > 0) { strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("TaskName")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("TaskName")); strcpy(stLoggingTaskInfo.szTaskName, strText); strText.Empty(); strText = (VT_NULL == pRecTd->GetCollect(_T("Location")).vt) ? _T("") : (LPCTSTR)(_bstr_t)pRecTd->GetCollect(_T("Location")); strcpy(stLoggingTaskInfo.szLocation, strText); stLoggingTaskInfo.byLogType = pRecTd->GetCollect(_T("LogType")).cVal; stLoggingTaskInfo.byTrace = pRecTd->GetCollect(_T("Trace")).cVal; stLoggingTaskInfo.byWaveform = pRecTd->GetCollect(_T("Waveform")).cVal; stLoggingTaskInfo.fSamplingInterval = pRecTd->GetCollect(_T("SamplingInterval")).fltVal; stLoggingTaskInfo.fInitDepth = pRecTd->GetCollect(_T("InitDepth")).fltVal; stLoggingTaskInfo.fEndDepth = pRecTd->GetCollect(_T("EndDepth")).fltVal; stLoggingTaskInfo.byLogDirection = pRecTd->GetCollect(_T("LogDirection")).cVal; stLoggingTaskInfo.fLoggingDistance = pRecTd->GetCollect(_T("LoggingInterval")).fltVal; stLoggingTaskInfo.fInstrumentZeroLength = pRecTd->GetCollect(_T("InstrumentZeroLength")).fltVal; stLoggingTaskInfo.fPeriod = pRecTd->GetCollect(_T("Period")).fltVal; } pRecTd->Close(); } catch (_com_error e) { AfxMessageBox(e.Description()); pRecTd->Close(); } return stLoggingTaskInfo; }