ModelHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using FluentNHibernate.Cfg;
  8. using NHibernate;
  9. namespace IOTransmissionAbutment
  10. {
  11. public class ModelHelper
  12. {
  13. static private ISessionFactory m_factory = null;
  14. static private object obj = new object();
  15. static private string _connectionString;
  16. static public ISession Session
  17. {
  18. get
  19. {
  20. if (m_factory == null)
  21. {
  22. lock (obj)
  23. {
  24. try
  25. {
  26. if (m_factory == null) m_factory = createSessionFactory();
  27. }
  28. catch { throw; }
  29. }
  30. }
  31. return m_factory.OpenSession();
  32. }
  33. }
  34. static public void initialize(string path)
  35. {
  36. _connectionString = "Data Source =" + path;
  37. try
  38. {
  39. if (m_factory != null) m_factory.Dispose();
  40. m_factory = createSessionFactory();
  41. }
  42. catch (Exception ex) { Console.Write(ex.ToString()); throw; }
  43. }
  44. static public void close()
  45. {
  46. if (m_factory != null) { m_factory.Close(); m_factory = null; }
  47. }
  48. static private ISessionFactory createSessionFactory()
  49. {
  50. try
  51. {
  52. string connectionString = _connectionString;
  53. FluentConfiguration config = Fluently.Configure().Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008 .ConnectionString(connectionString));
  54. Assembly curAssembly = Assembly.GetExecutingAssembly();
  55. NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly)).ExposeConfiguration(f => f.SetInterceptor(new SqlStatementInterceptor())).BuildConfiguration();
  56. return cfg.BuildSessionFactory();
  57. }
  58. catch
  59. { throw; }
  60. }
  61. public static void DeleteAllWareHouseCallStatus()
  62. {
  63. try
  64. {
  65. using (var session = Session)
  66. {
  67. string sql = @"delete from WareHouseCallStatus";
  68. ISQLQuery qry = session.CreateSQLQuery(sql);
  69. qry.ExecuteUpdate();
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. }
  75. }
  76. public static CallBoxSetting GetCallBoxSetting(int callbox)
  77. {
  78. CallBoxSetting whcs = null;
  79. try
  80. {
  81. using (var session = Session)
  82. {
  83. var result = session.QueryOver<CallBoxSetting>().Where(p => p.CallBoxNo == callbox).List();
  84. if (result.Count > 0)
  85. {
  86. whcs = result.First();
  87. }
  88. }
  89. }
  90. catch
  91. {
  92. }
  93. return whcs;
  94. }
  95. public static int GetCallBoxSettingNo(string warehouse)
  96. {
  97. int whcs = 0;
  98. try
  99. {
  100. using (var session = Session)
  101. {
  102. var result = session.QueryOver<CallBoxSetting>().Where(p => p.CallBoxType==2&&p.WareHouseID==warehouse).List();
  103. if (result.Count > 0)
  104. {
  105. whcs = result.First().CallBoxNo;
  106. }
  107. }
  108. }
  109. catch
  110. { }
  111. return whcs;
  112. }
  113. public static WareHouseCallStatus GetWareHouseCallStatus(string warehouseid)
  114. {
  115. WareHouseCallStatus whcs = null;
  116. try
  117. {
  118. using (var session = Session)
  119. {
  120. var result = session.QueryOver<WareHouseCallStatus>().Where(p => p.WareHouseID == warehouseid).List();
  121. if (result.Count > 0)
  122. {
  123. whcs = result.First();
  124. }
  125. }
  126. }
  127. catch
  128. {
  129. }
  130. return whcs;
  131. }
  132. public static ElevatorChoice GetElevatorChoice()
  133. {
  134. ElevatorChoice whcs = null;
  135. try
  136. {
  137. using (var session = Session)
  138. {
  139. var result = session.QueryOver<ElevatorChoice>().List();
  140. if (result.Count > 0)
  141. {
  142. whcs = result.First();
  143. }
  144. }
  145. }
  146. catch { }
  147. return whcs;
  148. }
  149. public class SqlStatementInterceptor : EmptyInterceptor
  150. {
  151. public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
  152. {
  153. //#if DEBUG
  154. try
  155. {
  156. //File.AppendAllText("c:\\log.txt", string.Format("{0}\n", sql.ToString()));
  157. }
  158. catch
  159. {
  160. }
  161. //#endif
  162. return sql;
  163. }
  164. }
  165. /// <summary>
  166. /// 创建重新匹配数据库表
  167. /// </summary>
  168. /// <param name="dataBasePath"></param>
  169. internal static void CreateTables(string path, string mapSign = "")
  170. {
  171. //DataHelper dh = new DataHelper();
  172. initialize(path);
  173. using (var session = Session)
  174. {
  175. using (var transaction = session.BeginTransaction())
  176. {
  177. try
  178. {
  179. string sql = @"CREATE TABLE ElevatorDevice (
  180. Name STRING NOT NULL,
  181. ID INTEGER NOT NULL
  182. PRIMARY KEY,
  183. IPAddress STRING NOT NULL,
  184. FloorCount INTEGER NOT NULL,
  185. CreationTime DATETIME NOT NULL,
  186. ChangingTime DATETIME NOT NULL,
  187. Remark STRING NOT NULL,
  188. LiftingVertex INTEGER NOT NULL
  189. );
  190. CREATE TABLE ElevatorFloor (
  191. ID INTEGER PRIMARY KEY
  192. NOT NULL,
  193. ElevatorID INTEGER NOT NULL,
  194. Floor INTEGER NOT NULL,
  195. Address INTEGER NOT NULL,
  196. ClosingEdges STRING NOT NULL,
  197. ThroughEdges STRING NOT NULL,
  198. OpeningEdges STRING NOT NULL
  199. );
  200. CREATE TABLE GateDevice (
  201. ID INTEGER PRIMARY KEY
  202. NOT NULL,
  203. Address INTEGER NOT NULL,
  204. ClosingEdges STRING NOT NULL,
  205. IPAddress STRING NOT NULL,
  206. Name STRING NOT NULL,
  207. CreationTime DATETIME NOT NULL,
  208. ChangingTime DATETIME NOT NULL,
  209. Remark STRING NOT NULL,
  210. ThroughEdges STRING NOT NULL,
  211. OpeningEdges STRING NOT NULL
  212. );
  213. CREATE TABLE PowerStationDevice (
  214. ID INTEGER PRIMARY KEY
  215. NOT NULL,
  216. Name STRING NOT NULL,
  217. IPAddress STRING NOT NULL,
  218. CreationTime DATETIME NOT NULL,
  219. ChangingTime DATETIME NOT NULL,
  220. Remark STRING NOT NULL,
  221. CarrierType INTEGER NOT NULL,
  222. Occupied BOOLEAN NOT NULL,
  223. StationID INTEGER NOT NULL,
  224. Carrier INTEGER NOT NULL,
  225. Region STRING NOT NULL,
  226. Address INTEGER,
  227. ThroughEdges STRING
  228. );
  229. CREATE TABLE General (
  230. ID INTEGER PRIMARY KEY
  231. NOT NULL,
  232. Name VARCHAR NOT NULL,
  233. Content VARCHAR NOT NULL
  234. );
  235. CREATE TABLE OtherWayPowerStationDevice (
  236. ID INTEGER PRIMARY KEY
  237. NOT NULL,
  238. Name STRING NOT NULL,
  239. IPAddress STRING,
  240. CreationTime DATETIME NOT NULL,
  241. ChangingTime DATETIME NOT NULL,
  242. Remark STRING NOT NULL,
  243. CarrierType INTEGER NOT NULL,
  244. Occupied BOOLEAN NOT NULL,
  245. StationID INTEGER NOT NULL,
  246. Carrier INTEGER NOT NULL,
  247. Region STRING NOT NULL,
  248. Address INTEGER,
  249. ThroughEdges STRING
  250. );";
  251. ISQLQuery qry = session.CreateSQLQuery(sql);
  252. qry.ExecuteUpdate();
  253. transaction.Commit();
  254. }
  255. catch (Exception ex)
  256. {
  257. transaction.Rollback();
  258. throw ex;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }