using OTSDataType; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading.Tasks; using OTSModelSharp.DTLBase; using OTSModelSharp.ServiceInterface; using OTSCOMMONCLR; namespace OTSModelSharp { public class CPosXrayDBMgr { protected static NLog.Logger logger = null; bool m_bHasElement; List m_listPosXray; //database CXRayDataDB m_pXrayDataDB; CElementChemistryDB m_pElementChemistryDB; CPosXrayInfoDB m_pXrayInfoDB; const long GENERALXRAYCHANNELS = 2000; private CSQLiteDBStore m_dbStore; public CPosXrayDBMgr(CSQLiteDBStore a_dbStore) { m_dbStore = a_dbStore; m_listPosXray = new List(); logger = NLog.LogManager.GetCurrentClassLogger(); } public void InitDataTable() { m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable()); m_pXrayDataDB.Init(); m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable()); m_pElementChemistryDB.Init(); m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore, new CPosXrayInfoTable()); m_pXrayInfoDB.Init(); } //// Load/Save //public bool Load(int fldId, bool a_bClear) //{ // // clear all data if necessary // if (a_bClear) // { // m_listPosXray.Clear(); // } // if (!GetXrayList(fldId)) // { // return false; // } // // ok, return TRUE // return true; //} //x-ray public List GetPosXrayList() { return m_listPosXray; } public void SetHasElement(bool a_bHasElement) { m_bHasElement = a_bHasElement; } public CElementChemistryDB GetElementChemistryDB() { if (m_pElementChemistryDB == null) { m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable()); } return m_pElementChemistryDB; } //protected //protected bool GetXrayList(int fldId) //{ // //get x-ray info list // List listInfo = new List(); // //listInfo.Clear(); // if (!GetXrayInfoList(ref listInfo, fldId)) // { // logger.Error("GetXrayList: get x-ray info failed."); // return false; // } // if (listInfo == null) // { // return false; // } // foreach (CPosXrayClr pInfo in listInfo) // { // //get x-ray data // CPosXrayClr pXray = new CPosXrayClr(); // pXray.SetIndex(Convert.ToInt32(pInfo.GetIndex())); // pXray.SetPartTagId(Convert.ToInt32(pInfo.GetPartTagId())); // pXray.SetPosition(pInfo.GetPosition()); // pXray.SetScanFieldId(Convert.ToInt32(pInfo.GetScanFieldId())); // pXray.SetFeatureId(Convert.ToInt32(pInfo.GetFeatureId())); // long a_nXrayId = pInfo.GetIndex(); // long a_nFieldId = pInfo.GetScanFieldId(); // if (!GetXrayData(a_nXrayId, a_nFieldId, pXray)) // { // logger.Error("GetXrayList: get x-ray data failed."); // return false; // } // //get element list // if (m_bHasElement) // { // long nElementSize = pInfo.GetElementNum(); // if (nElementSize == 0) // { // m_listPosXray.Add(pXray); // continue; // } // List listElementChemistry = new List(); // if (!GetElementChemistry(a_nXrayId, a_nFieldId, nElementSize, ref listElementChemistry)) // { // logger.Error("GetXrayList: get x-ray data failed."); // return false; // } // pXray.SetElementQuantifyData(listElementChemistry); // } // m_listPosXray.Add(pXray); // } // return true; //} public bool SaveXray(List a_listPosXray,bool m_bHasElement) { if (a_listPosXray != null) { CElementChemistryDB XElementChemistryDB = GetElementChemistryDB(); m_dbStore.CloseSynchronous(); m_dbStore.BeginTransaction(); m_listPosXray = a_listPosXray; //save info list SaveXrayInfoList(); foreach (CPosXrayClr pPosXray in a_listPosXray) { if (!SavePosXrayPtr(pPosXray)) { logger.Error("Failed to save x-ray data."); return false; } //save element if (m_bHasElement) { if (!XElementChemistryDB.SaveElementChemistriesList(pPosXray)) { logger.Error("Failed to save element chemistry list data."); return false; } } } m_dbStore.CommitTransaction(); } return true; } public bool SavePosXrayPtr(CPosXrayClr a_pXray) { var tableInfoPtr =m_pXrayDataDB. GetTableInfo(); var datastorePtr = m_pXrayDataDB.GetDatastore(); String sInsertFormat = tableInfoPtr.GetInsertCommandFormatString(true); UInt32[] xrayData = a_pXray.GetXrayData(); String sSQLCommand = string.Format(sInsertFormat, a_pXray.GetIndex(), a_pXray.GetScanFieldId() ); byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4]; for (int j = 0; j < GENERALXRAYCHANNELS; j++) { uint m = xrayData[j]; byte[] dwordData = BitConverter.GetBytes(m); bytedata[j * 4] = dwordData[0]; bytedata[j * 4 + 1] = dwordData[1]; bytedata[j * 4 + 2] = dwordData[2]; bytedata[j * 4 + 3] = dwordData[3]; } if (!datastorePtr.InsertBlobData(sSQLCommand, bytedata, Convert.ToInt32(otsdataconst.GENERALXRAYCHANNELS) * 4)) { string.Format(a_pXray.GetIndex().ToString(), a_pXray.GetScanFieldId(), 0, sSQLCommand); return false; } return true; } protected bool SaveXrayInfoList() { var XrayInfoDB = GetXrayInfoDB(); //List m_listPosXray; //m_listPosXray if (!XrayInfoDB.SaveXrayInfoList(m_listPosXray)) { logger.Error("Failed to save xray info list."); return false; } return true; } protected bool GetXrayInfoList(ref List a_listXra, int fldId) { var XrayInfoDB = GetXrayInfoDB(); if (XrayInfoDB==null) { logger.Error("Failed to open result setting table"); return false; } a_listXra = XrayInfoDB.GetXrayInfoListByFieldId(fldId); return true; } //protected bool GetXrayData( long a_nXrayId, long a_nFieldId, CPosXrayClr a_pPosXray) //{ // if (a_pPosXray==null) // { // logger.Error("Invalid x-ray point."); // return false; // } // CXRayDataDB XrayDataDB = GetXrayDataDB(); // if (XrayDataDB==null) // { // logger.Error("Failed to open result setting table"); // return false; // } // CPosXrayClr pPosXray = XrayDataDB.GetXRayDataById(a_nXrayId, a_nFieldId); // a_pPosXray.SetXrayData(pPosXray.GetXrayData()); // return true; //} //protected bool GetElementChemistry( long a_nXrayId, long a_nFieldId, long a_nElementSize,ref List a_listElementChemistry) //{ // CElementChemistryDB XElementChemistryDB = GetElementChemistryDB(); // if (XElementChemistryDB==null) // { // logger.Error("Failed to open result setting table"); // return false; // } // a_listElementChemistry.Clear(); // a_listElementChemistry = XElementChemistryDB.GetElementChemistryListById(a_nXrayId, a_nFieldId, a_nElementSize); // return true; //} //Get DB public CXRayDataDB GetXrayDataDB() { if (m_pXrayDataDB==null) { m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable()); } return m_pXrayDataDB; } public CPosXrayInfoDB GetXrayInfoDB() { if (m_pXrayInfoDB==null) { m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore,new CPosXrayInfoTable()); } return m_pXrayInfoDB; } // cleanup protected void Cleanup() { // need to do nothing at the moment m_listPosXray.Clear(); } // duplication protected void Duplicate( CPosXrayDBMgr a_oSource) { // initialization m_listPosXray.Clear(); // copy data over foreach(CPosXrayClr pPosXray in a_oSource.m_listPosXray) //for (auto pPosXray : a_oSource.m_listPosXray) { CPosXrayClr pPosXrayNew = new CPosXrayClr(pPosXray); m_listPosXray.Add(pPosXrayNew); } } } }