PosXrayDBMgr.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using OTSDataType;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Threading.Tasks;
  8. using OTSModelSharp.DTLBase;
  9. using OTSModelSharp.ServiceInterface;
  10. using OTSCLRINTERFACE;
  11. using System.Data.SQLite;
  12. namespace OTSModelSharp
  13. {
  14. public class CPosXrayDBMgr
  15. {
  16. protected static NLog.Logger logger = null;
  17. bool m_bHasElement;
  18. //List<CPosXrayClr> m_listPosXray;
  19. //database
  20. CXRayDataDB m_pXrayDataDB;
  21. CElementChemistryDB m_pElementChemistryDB;
  22. CPosXrayInfoDB m_pXrayInfoDB;
  23. const long GENERALXRAYCHANNELS = 2000;
  24. private CSQLiteDBStore m_dbStore;
  25. public CPosXrayDBMgr(CSQLiteDBStore a_dbStore)
  26. {
  27. m_dbStore = a_dbStore;
  28. //m_listPosXray = new List<CPosXrayClr>();
  29. logger = NLog.LogManager.GetCurrentClassLogger();
  30. }
  31. public void InitDataTable()
  32. {
  33. m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable());
  34. m_pXrayDataDB.Init();
  35. m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable());
  36. m_pElementChemistryDB.Init();
  37. m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore, new CPosXrayInfoTable());
  38. m_pXrayInfoDB.Init();
  39. }
  40. public void SetHasElement(bool a_bHasElement) { m_bHasElement = a_bHasElement; }
  41. public CElementChemistryDB GetElementChemistryDB()
  42. {
  43. if (m_pElementChemistryDB == null)
  44. {
  45. m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable());
  46. }
  47. return m_pElementChemistryDB;
  48. }
  49. //protected
  50. public bool SaveXray(List<CPosXrayClr> a_listPosXray,bool m_bHasElement)
  51. {
  52. if (a_listPosXray != null)
  53. {
  54. CElementChemistryDB XElementChemistryDB = GetElementChemistryDB();
  55. m_dbStore.CloseSynchronous();
  56. m_dbStore.BeginTransaction();
  57. SaveXrayInfoList(a_listPosXray);
  58. foreach (CPosXrayClr pPosXray in a_listPosXray)
  59. {
  60. if (!SavePosXrayPtr(pPosXray))
  61. {
  62. logger.Error("Failed to save x-ray data.");
  63. return false;
  64. }
  65. //save element
  66. if (m_bHasElement)
  67. {
  68. if (!XElementChemistryDB.SaveElementChemistriesList(pPosXray))
  69. {
  70. logger.Error("Failed to save element chemistry list data.");
  71. return false;
  72. }
  73. }
  74. }
  75. m_dbStore.CommitTransaction();
  76. }
  77. return true;
  78. }
  79. public List<KeyValuePair<string, SQLiteParameter[]>> GetSavingXrayCmds(List<CPosXrayClr> a_listPosXray, bool m_bHasElement)
  80. {
  81. List<KeyValuePair<string, SQLiteParameter[]>> cmds = new List<KeyValuePair<string, SQLiteParameter[]>>();
  82. if (a_listPosXray != null)
  83. {
  84. CElementChemistryDB XElementChemistryDB = GetElementChemistryDB();
  85. var xrayInfoDB = GetXrayInfoDB();
  86. var xrayinfocmds =xrayInfoDB.GetSavingXrayInfoCmds (a_listPosXray);
  87. foreach (var s in xrayinfocmds)
  88. {
  89. var cmd = new KeyValuePair<string, SQLiteParameter[]>(s, null);
  90. cmds.Add(cmd);
  91. }
  92. foreach (CPosXrayClr pPosXray in a_listPosXray)
  93. {
  94. var cmd= GetSavingPosXrayDataCmds(pPosXray);
  95. cmds.Add(cmd);
  96. if (m_bHasElement)
  97. {
  98. var cmdlist = XElementChemistryDB.GetSavingElementChemistriesCmds(pPosXray);
  99. foreach (var s in cmdlist)
  100. {
  101. var cmd1 = new KeyValuePair<string, SQLiteParameter[]>(s, null);
  102. cmds.Add(cmd1);
  103. }
  104. }
  105. }
  106. }
  107. return cmds;
  108. }
  109. public bool SavePosXrayPtr(CPosXrayClr a_pXray)
  110. {
  111. var tableInfoPtr =m_pXrayDataDB. GetTableInfo();
  112. var datastorePtr = m_pXrayDataDB.GetDatastore();
  113. String sInsertFormat = tableInfoPtr.GetInsertCommandFormatString(true);
  114. UInt32[] xrayData = a_pXray.GetXrayData();
  115. String sSQLCommand = string.Format(sInsertFormat,
  116. a_pXray.GetIndex(),
  117. a_pXray.GetScanFieldId()
  118. );
  119. byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4];
  120. for (int j = 0; j < GENERALXRAYCHANNELS; j++)
  121. {
  122. uint m = xrayData[j];
  123. byte[] dwordData = BitConverter.GetBytes(m);
  124. bytedata[j * 4] = dwordData[0];
  125. bytedata[j * 4 + 1] = dwordData[1];
  126. bytedata[j * 4 + 2] = dwordData[2];
  127. bytedata[j * 4 + 3] = dwordData[3];
  128. }
  129. if (!datastorePtr.InsertBlobData(sSQLCommand, bytedata, Convert.ToInt32(otsdataconst.GENERALXRAYCHANNELS) * 4))
  130. {
  131. return false;
  132. }
  133. return true;
  134. }
  135. private KeyValuePair<string,SQLiteParameter[]> GetSavingPosXrayDataCmds(CPosXrayClr a_pXray)
  136. {
  137. KeyValuePair<string, SQLiteParameter[]> cmd;
  138. List<SQLiteParameter> ps = new List<SQLiteParameter>();
  139. var tableInfoPtr = m_pXrayDataDB.GetTableInfo();
  140. var datastorePtr = m_pXrayDataDB.GetDatastore();
  141. String sInsertFormat = tableInfoPtr.GetInsertCommandFormatString(true);
  142. UInt32[] xrayData = a_pXray.GetXrayData();
  143. String sSQLCommand = string.Format(sInsertFormat,
  144. a_pXray.GetIndex(),
  145. a_pXray.GetScanFieldId()
  146. );
  147. byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4];
  148. for (int j = 0; j < GENERALXRAYCHANNELS; j++)
  149. {
  150. uint m = xrayData[j];
  151. byte[] dwordData = BitConverter.GetBytes(m);
  152. bytedata[j * 4] = dwordData[0];
  153. bytedata[j * 4 + 1] = dwordData[1];
  154. bytedata[j * 4 + 2] = dwordData[2];
  155. bytedata[j * 4 + 3] = dwordData[3];
  156. }
  157. ps.Add(new SQLiteParameter(@"blob", bytedata));
  158. cmd = new KeyValuePair<string, SQLiteParameter[]>(sSQLCommand, ps.ToArray());
  159. return cmd;
  160. }
  161. protected bool SaveXrayInfoList(List<CPosXrayClr> a_listPosXray)
  162. {
  163. var XrayInfoDB = GetXrayInfoDB();
  164. if (!XrayInfoDB.SaveXrayInfoList(a_listPosXray))
  165. {
  166. logger.Error("Failed to save xray info list.");
  167. return false;
  168. }
  169. return true;
  170. }
  171. protected bool GetXrayInfoList(ref List<CPosXrayClr> a_listXra, int fldId)
  172. {
  173. var XrayInfoDB = GetXrayInfoDB();
  174. if (XrayInfoDB==null)
  175. {
  176. logger.Error("Failed to open result setting table");
  177. return false;
  178. }
  179. a_listXra = XrayInfoDB.GetXrayInfoListByFieldId(fldId);
  180. return true;
  181. }
  182. //Get DB
  183. public CXRayDataDB GetXrayDataDB()
  184. {
  185. if (m_pXrayDataDB==null)
  186. {
  187. m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable());
  188. }
  189. return m_pXrayDataDB;
  190. }
  191. public CPosXrayInfoDB GetXrayInfoDB()
  192. {
  193. if (m_pXrayInfoDB==null)
  194. {
  195. m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore,new CPosXrayInfoTable());
  196. }
  197. return m_pXrayInfoDB;
  198. }
  199. // cleanup
  200. protected void Cleanup()
  201. {
  202. // need to do nothing at the moment
  203. //m_listPosXray.Clear();
  204. }
  205. // duplication
  206. protected void Duplicate( CPosXrayDBMgr a_oSource)
  207. {
  208. // initialization
  209. //m_listPosXray.Clear();
  210. // copy data over
  211. //foreach(CPosXrayClr pPosXray in a_oSource.m_listPosXray)
  212. ////for (auto pPosXray : a_oSource.m_listPosXray)
  213. //{
  214. // CPosXrayClr pPosXrayNew = new CPosXrayClr(pPosXray);
  215. // m_listPosXray.Add(pPosXrayNew);
  216. //}
  217. }
  218. }
  219. }