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 OTSCLRINTERFACE; using System.Data.SQLite; 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(); } 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 public bool SaveXray(List a_listPosXray,bool m_bHasElement) { if (a_listPosXray != null) { CElementChemistryDB XElementChemistryDB = GetElementChemistryDB(); var XrayInfoDB = GetXrayInfoDB(); var cmds = XrayInfoDB.GetSavingXrayInfoCmds(a_listPosXray); foreach (CPosXrayClr pPosXray in a_listPosXray) { var cmd = GetSavingPosXrayDataCmds(pPosXray); cmds.Add(cmd); //save element if (m_bHasElement) { var cmds1 = XElementChemistryDB.GetSavingElementChemistriesCmds(pPosXray); cmds.AddRange(cmds1); } } m_dbStore.ExecuteNonQueryBatch(ref cmds); } return true; } public List> GetSavingXrayCmds(List a_listPosXray, bool m_bHasElement) { List> cmds = new List>(); if (a_listPosXray != null) { CElementChemistryDB XElementChemistryDB = GetElementChemistryDB(); var xrayInfoDB = GetXrayInfoDB(); var xrayinfocmds =xrayInfoDB.GetSavingXrayInfoCmds (a_listPosXray); cmds.AddRange(xrayinfocmds); foreach (CPosXrayClr pPosXray in a_listPosXray) { var cmd= GetSavingPosXrayDataCmds(pPosXray); cmds.Add(cmd); if (m_bHasElement) { var cmdlist = XElementChemistryDB.GetSavingElementChemistriesCmds(pPosXray); cmds.AddRange(cmdlist); } } } return cmds; } 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)) { return false; } return true; } private KeyValuePair GetSavingPosXrayDataCmds(CPosXrayClr a_pXray) { KeyValuePair cmd; List ps = new List(); var tableInfoPtr = m_pXrayDataDB.GetTableInfo(); var datastorePtr = m_pXrayDataDB.GetDatastore(); var sInsertFormat = tableInfoPtr.GetInsertCommand(true); var parasTem = sInsertFormat.Value; var paras = new SQLiteParameter[3]; for (int j = 0; j < 3; j++) { paras[j] = new SQLiteParameter(parasTem[j].ParameterName); } UInt32[] xrayData = a_pXray.GetXrayData(); paras[0].Value = a_pXray.GetIndex(); paras[1].Value = 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]; } paras[2].Value = bytedata; cmd = new KeyValuePair(sInsertFormat.Key,paras); return cmd; } //protected bool SaveXrayInfoList(List a_listPosXray) //{ // var XrayInfoDB = GetXrayInfoDB(); // var cmds = XrayInfoDB.GetSavingXrayInfoCmds(a_listPosXray); // m_dbStore.ExecuteNonQueryBatch(ref cmds); // 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; } //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); //} } } }