PosXrayDBMgr.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. var XrayInfoDB = GetXrayInfoDB();
  56. var cmds = XrayInfoDB.GetSavingXrayInfoCmds(a_listPosXray);
  57. foreach (CPosXrayClr pPosXray in a_listPosXray)
  58. {
  59. var cmd = GetSavingPosXrayDataCmds(pPosXray);
  60. cmds.Add(cmd);
  61. //save element
  62. if (m_bHasElement)
  63. {
  64. var cmds1 = XElementChemistryDB.GetSavingElementChemistriesCmds(pPosXray);
  65. cmds.AddRange(cmds1);
  66. }
  67. }
  68. m_dbStore.ExecuteNonQueryBatch(ref cmds);
  69. }
  70. return true;
  71. }
  72. public List<KeyValuePair<string, SQLiteParameter[]>> GetSavingXrayCmds(List<CPosXrayClr> a_listPosXray, bool m_bHasElement)
  73. {
  74. List<KeyValuePair<string, SQLiteParameter[]>> cmds = new List<KeyValuePair<string, SQLiteParameter[]>>();
  75. if (a_listPosXray != null)
  76. {
  77. CElementChemistryDB XElementChemistryDB = GetElementChemistryDB();
  78. var xrayInfoDB = GetXrayInfoDB();
  79. var xrayinfocmds =xrayInfoDB.GetSavingXrayInfoCmds (a_listPosXray);
  80. cmds.AddRange(xrayinfocmds);
  81. foreach (CPosXrayClr pPosXray in a_listPosXray)
  82. {
  83. var cmd= GetSavingPosXrayDataCmds(pPosXray);
  84. cmds.Add(cmd);
  85. if (m_bHasElement)
  86. {
  87. var cmdlist = XElementChemistryDB.GetSavingElementChemistriesCmds(pPosXray);
  88. cmds.AddRange(cmdlist);
  89. }
  90. }
  91. }
  92. return cmds;
  93. }
  94. public bool SavePosXrayPtr(CPosXrayClr a_pXray)
  95. {
  96. var tableInfoPtr =m_pXrayDataDB. GetTableInfo();
  97. var datastorePtr = m_pXrayDataDB.GetDatastore();
  98. String sInsertFormat = tableInfoPtr.GetInsertCommandFormatString(true);
  99. UInt32[] xrayData = a_pXray.GetXrayData();
  100. String sSQLCommand = string.Format(sInsertFormat,
  101. a_pXray.GetIndex(),
  102. a_pXray.GetScanFieldId()
  103. );
  104. byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4];
  105. for (int j = 0; j < GENERALXRAYCHANNELS; j++)
  106. {
  107. uint m = xrayData[j];
  108. byte[] dwordData = BitConverter.GetBytes(m);
  109. bytedata[j * 4] = dwordData[0];
  110. bytedata[j * 4 + 1] = dwordData[1];
  111. bytedata[j * 4 + 2] = dwordData[2];
  112. bytedata[j * 4 + 3] = dwordData[3];
  113. }
  114. if (!datastorePtr.InsertBlobData(sSQLCommand, bytedata, Convert.ToInt32(otsdataconst.GENERALXRAYCHANNELS) * 4))
  115. {
  116. return false;
  117. }
  118. return true;
  119. }
  120. private KeyValuePair<string,SQLiteParameter[]> GetSavingPosXrayDataCmds(CPosXrayClr a_pXray)
  121. {
  122. KeyValuePair<string, SQLiteParameter[]> cmd;
  123. List<SQLiteParameter> ps = new List<SQLiteParameter>();
  124. var tableInfoPtr = m_pXrayDataDB.GetTableInfo();
  125. var datastorePtr = m_pXrayDataDB.GetDatastore();
  126. var sInsertFormat = tableInfoPtr.GetInsertCommand(true);
  127. var parasTem = sInsertFormat.Value;
  128. var paras = new SQLiteParameter[3];
  129. for (int j = 0; j < 3; j++)
  130. {
  131. paras[j] = new SQLiteParameter(parasTem[j].ParameterName);
  132. }
  133. UInt32[] xrayData = a_pXray.GetXrayData();
  134. paras[0].Value = a_pXray.GetIndex();
  135. paras[1].Value = a_pXray.GetScanFieldId();
  136. byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4];
  137. for (int j = 0; j < GENERALXRAYCHANNELS; j++)
  138. {
  139. uint m = xrayData[j];
  140. byte[] dwordData = BitConverter.GetBytes(m);
  141. bytedata[j * 4] = dwordData[0];
  142. bytedata[j * 4 + 1] = dwordData[1];
  143. bytedata[j * 4 + 2] = dwordData[2];
  144. bytedata[j * 4 + 3] = dwordData[3];
  145. }
  146. paras[2].Value = bytedata;
  147. cmd = new KeyValuePair<string, SQLiteParameter[]>(sInsertFormat.Key,paras);
  148. return cmd;
  149. }
  150. //protected bool SaveXrayInfoList(List<CPosXrayClr> a_listPosXray)
  151. //{
  152. // var XrayInfoDB = GetXrayInfoDB();
  153. // var cmds = XrayInfoDB.GetSavingXrayInfoCmds(a_listPosXray);
  154. // m_dbStore.ExecuteNonQueryBatch(ref cmds);
  155. // return true;
  156. //}
  157. protected bool GetXrayInfoList(ref List<CPosXrayClr> a_listXra, int fldId)
  158. {
  159. var XrayInfoDB = GetXrayInfoDB();
  160. if (XrayInfoDB==null)
  161. {
  162. logger.Error("Failed to open result setting table");
  163. return false;
  164. }
  165. a_listXra = XrayInfoDB.GetXrayInfoListByFieldId(fldId);
  166. return true;
  167. }
  168. //Get DB
  169. public CXRayDataDB GetXrayDataDB()
  170. {
  171. if (m_pXrayDataDB==null)
  172. {
  173. m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable());
  174. }
  175. return m_pXrayDataDB;
  176. }
  177. public CPosXrayInfoDB GetXrayInfoDB()
  178. {
  179. if (m_pXrayInfoDB==null)
  180. {
  181. m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore,new CPosXrayInfoTable());
  182. }
  183. return m_pXrayInfoDB;
  184. }
  185. // cleanup
  186. protected void Cleanup()
  187. {
  188. // need to do nothing at the moment
  189. //m_listPosXray.Clear();
  190. }
  191. // duplication
  192. protected void Duplicate( CPosXrayDBMgr a_oSource)
  193. {
  194. // initialization
  195. //m_listPosXray.Clear();
  196. // copy data over
  197. //foreach(CPosXrayClr pPosXray in a_oSource.m_listPosXray)
  198. ////for (auto pPosXray : a_oSource.m_listPosXray)
  199. //{
  200. // CPosXrayClr pPosXrayNew = new CPosXrayClr(pPosXray);
  201. // m_listPosXray.Add(pPosXrayNew);
  202. //}
  203. }
  204. }
  205. }