PosXrayDBMgr.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 OTSCOMMONCLR;
  11. namespace OTSModelSharp
  12. {
  13. public class CPosXrayDBMgr
  14. {
  15. protected static NLog.Logger logger = null;
  16. bool m_bHasElement;
  17. List<CPosXrayClr> m_listPosXray;
  18. //database
  19. CXRayDataDB m_pXrayDataDB;
  20. CElementChemistryDB m_pElementChemistryDB;
  21. CPosXrayInfoDB m_pXrayInfoDB;
  22. const long GENERALXRAYCHANNELS = 2000;
  23. private CSQLiteDBStore m_dbStore;
  24. public CPosXrayDBMgr(CSQLiteDBStore a_dbStore)
  25. {
  26. m_dbStore = a_dbStore;
  27. m_listPosXray = new List<CPosXrayClr>();
  28. logger = NLog.LogManager.GetCurrentClassLogger();
  29. }
  30. public void InitDataTable()
  31. {
  32. m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable());
  33. m_pXrayDataDB.Init();
  34. m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable());
  35. m_pElementChemistryDB.Init();
  36. m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore, new CPosXrayInfoTable());
  37. m_pXrayInfoDB.Init();
  38. }
  39. //// Load/Save
  40. //public bool Load(int fldId, bool a_bClear)
  41. //{
  42. // // clear all data if necessary
  43. // if (a_bClear)
  44. // {
  45. // m_listPosXray.Clear();
  46. // }
  47. // if (!GetXrayList(fldId))
  48. // {
  49. // return false;
  50. // }
  51. // // ok, return TRUE
  52. // return true;
  53. //}
  54. //x-ray
  55. public List<CPosXrayClr> GetPosXrayList() { return m_listPosXray; }
  56. public void SetHasElement(bool a_bHasElement) { m_bHasElement = a_bHasElement; }
  57. public CElementChemistryDB GetElementChemistryDB()
  58. {
  59. if (m_pElementChemistryDB == null)
  60. {
  61. m_pElementChemistryDB = new CElementChemistryDB(m_dbStore, new CElementChemistryTable());
  62. }
  63. return m_pElementChemistryDB;
  64. }
  65. //protected
  66. //protected bool GetXrayList(int fldId)
  67. //{
  68. // //get x-ray info list
  69. // List<CPosXrayClr> listInfo = new List<CPosXrayClr>();
  70. // //listInfo.Clear();
  71. // if (!GetXrayInfoList(ref listInfo, fldId))
  72. // {
  73. // logger.Error("GetXrayList: get x-ray info failed.");
  74. // return false;
  75. // }
  76. // if (listInfo == null)
  77. // {
  78. // return false;
  79. // }
  80. // foreach (CPosXrayClr pInfo in listInfo)
  81. // {
  82. // //get x-ray data
  83. // CPosXrayClr pXray = new CPosXrayClr();
  84. // pXray.SetIndex(Convert.ToInt32(pInfo.GetIndex()));
  85. // pXray.SetPartTagId(Convert.ToInt32(pInfo.GetPartTagId()));
  86. // pXray.SetPosition(pInfo.GetPosition());
  87. // pXray.SetScanFieldId(Convert.ToInt32(pInfo.GetScanFieldId()));
  88. // pXray.SetFeatureId(Convert.ToInt32(pInfo.GetFeatureId()));
  89. // long a_nXrayId = pInfo.GetIndex();
  90. // long a_nFieldId = pInfo.GetScanFieldId();
  91. // if (!GetXrayData(a_nXrayId, a_nFieldId, pXray))
  92. // {
  93. // logger.Error("GetXrayList: get x-ray data failed.");
  94. // return false;
  95. // }
  96. // //get element list
  97. // if (m_bHasElement)
  98. // {
  99. // long nElementSize = pInfo.GetElementNum();
  100. // if (nElementSize == 0)
  101. // {
  102. // m_listPosXray.Add(pXray);
  103. // continue;
  104. // }
  105. // List<CElementChemistryClr> listElementChemistry = new List<CElementChemistryClr>();
  106. // if (!GetElementChemistry(a_nXrayId, a_nFieldId, nElementSize, ref listElementChemistry))
  107. // {
  108. // logger.Error("GetXrayList: get x-ray data failed.");
  109. // return false;
  110. // }
  111. // pXray.SetElementQuantifyData(listElementChemistry);
  112. // }
  113. // m_listPosXray.Add(pXray);
  114. // }
  115. // return true;
  116. //}
  117. public bool SaveXray(List<CPosXrayClr> a_listPosXray,bool m_bHasElement)
  118. {
  119. if (a_listPosXray != null)
  120. {
  121. CElementChemistryDB XElementChemistryDB = GetElementChemistryDB();
  122. m_dbStore.CloseSynchronous();
  123. m_dbStore.BeginTransaction();
  124. m_listPosXray = a_listPosXray;
  125. //save info list
  126. SaveXrayInfoList();
  127. foreach (CPosXrayClr pPosXray in a_listPosXray)
  128. {
  129. if (!SavePosXrayPtr(pPosXray))
  130. {
  131. logger.Error("Failed to save x-ray data.");
  132. return false;
  133. }
  134. //save element
  135. if (m_bHasElement)
  136. {
  137. if (!XElementChemistryDB.SaveElementChemistriesList(pPosXray))
  138. {
  139. logger.Error("Failed to save element chemistry list data.");
  140. return false;
  141. }
  142. }
  143. }
  144. m_dbStore.CommitTransaction();
  145. }
  146. return true;
  147. }
  148. public bool SavePosXrayPtr(CPosXrayClr a_pXray)
  149. {
  150. var tableInfoPtr =m_pXrayDataDB. GetTableInfo();
  151. var datastorePtr = m_pXrayDataDB.GetDatastore();
  152. String sInsertFormat = tableInfoPtr.GetInsertCommandFormatString(true);
  153. UInt32[] xrayData = a_pXray.GetXrayData();
  154. String sSQLCommand = string.Format(sInsertFormat,
  155. a_pXray.GetIndex(),
  156. a_pXray.GetScanFieldId()
  157. );
  158. byte[] bytedata = new byte[GENERALXRAYCHANNELS * 4];
  159. for (int j = 0; j < GENERALXRAYCHANNELS; j++)
  160. {
  161. uint m = xrayData[j];
  162. byte[] dwordData = BitConverter.GetBytes(m);
  163. bytedata[j * 4] = dwordData[0];
  164. bytedata[j * 4 + 1] = dwordData[1];
  165. bytedata[j * 4 + 2] = dwordData[2];
  166. bytedata[j * 4 + 3] = dwordData[3];
  167. }
  168. if (!datastorePtr.InsertBlobData(sSQLCommand, bytedata, Convert.ToInt32(otsdataconst.GENERALXRAYCHANNELS) * 4))
  169. {
  170. string.Format(a_pXray.GetIndex().ToString(),
  171. a_pXray.GetScanFieldId(),
  172. 0,
  173. sSQLCommand);
  174. return false;
  175. }
  176. return true;
  177. }
  178. protected bool SaveXrayInfoList()
  179. {
  180. var XrayInfoDB = GetXrayInfoDB();
  181. //List<CPosXray> m_listPosXray;
  182. //m_listPosXray
  183. if (!XrayInfoDB.SaveXrayInfoList(m_listPosXray))
  184. {
  185. logger.Error("Failed to save xray info list.");
  186. return false;
  187. }
  188. return true;
  189. }
  190. protected bool GetXrayInfoList(ref List<CPosXrayClr> a_listXra, int fldId)
  191. {
  192. var XrayInfoDB = GetXrayInfoDB();
  193. if (XrayInfoDB==null)
  194. {
  195. logger.Error("Failed to open result setting table");
  196. return false;
  197. }
  198. a_listXra = XrayInfoDB.GetXrayInfoListByFieldId(fldId);
  199. return true;
  200. }
  201. //protected bool GetXrayData( long a_nXrayId, long a_nFieldId, CPosXrayClr a_pPosXray)
  202. //{
  203. // if (a_pPosXray==null)
  204. // {
  205. // logger.Error("Invalid x-ray point.");
  206. // return false;
  207. // }
  208. // CXRayDataDB XrayDataDB = GetXrayDataDB();
  209. // if (XrayDataDB==null)
  210. // {
  211. // logger.Error("Failed to open result setting table");
  212. // return false;
  213. // }
  214. // CPosXrayClr pPosXray = XrayDataDB.GetXRayDataById(a_nXrayId, a_nFieldId);
  215. // a_pPosXray.SetXrayData(pPosXray.GetXrayData());
  216. // return true;
  217. //}
  218. //protected bool GetElementChemistry( long a_nXrayId, long a_nFieldId, long a_nElementSize,ref List<CElementChemistryClr> a_listElementChemistry)
  219. //{
  220. // CElementChemistryDB XElementChemistryDB = GetElementChemistryDB();
  221. // if (XElementChemistryDB==null)
  222. // {
  223. // logger.Error("Failed to open result setting table");
  224. // return false;
  225. // }
  226. // a_listElementChemistry.Clear();
  227. // a_listElementChemistry = XElementChemistryDB.GetElementChemistryListById(a_nXrayId, a_nFieldId, a_nElementSize);
  228. // return true;
  229. //}
  230. //Get DB
  231. public CXRayDataDB GetXrayDataDB()
  232. {
  233. if (m_pXrayDataDB==null)
  234. {
  235. m_pXrayDataDB = new CXRayDataDB(m_dbStore, new CXRayDataTable());
  236. }
  237. return m_pXrayDataDB;
  238. }
  239. public CPosXrayInfoDB GetXrayInfoDB()
  240. {
  241. if (m_pXrayInfoDB==null)
  242. {
  243. m_pXrayInfoDB = new CPosXrayInfoDB(m_dbStore,new CPosXrayInfoTable());
  244. }
  245. return m_pXrayInfoDB;
  246. }
  247. // cleanup
  248. protected void Cleanup()
  249. {
  250. // need to do nothing at the moment
  251. m_listPosXray.Clear();
  252. }
  253. // duplication
  254. protected void Duplicate( CPosXrayDBMgr a_oSource)
  255. {
  256. // initialization
  257. m_listPosXray.Clear();
  258. // copy data over
  259. foreach(CPosXrayClr pPosXray in a_oSource.m_listPosXray)
  260. //for (auto pPosXray : a_oSource.m_listPosXray)
  261. {
  262. CPosXrayClr pPosXrayNew = new CPosXrayClr(pPosXray);
  263. m_listPosXray.Add(pPosXrayNew);
  264. }
  265. }
  266. }
  267. }