WMSDBModelHelper.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using FluentNHibernate.Cfg;
  2. using FluentNHibernate.Mapping;
  3. using NHibernate;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ProjectManagementSystem.NHibernateDBHelper
  11. {
  12. class WMSDBModelHelper
  13. {
  14. static private volatile ISessionFactory m_factory = null;
  15. static private object obj = new object();
  16. static private string _connectionString;
  17. static public ISession Session
  18. {
  19. get
  20. {
  21. if (m_factory == null)
  22. {
  23. lock (obj)
  24. {
  25. try
  26. {
  27. if (m_factory == null) m_factory = createSessionFactory();
  28. }
  29. catch { throw; }
  30. }
  31. }
  32. return m_factory.OpenSession();
  33. }
  34. }
  35. static public void initialize(string path)
  36. {
  37. _connectionString = "Data Source =" + path;
  38. try
  39. {
  40. if (m_factory != null) m_factory.Dispose();
  41. m_factory = createSessionFactory();
  42. }
  43. catch (Exception ex) { Console.Write(ex.ToString()); throw; }
  44. }
  45. public static void initialize(string strDatabaseServer,
  46. int databasePort,
  47. string strDatabaseName,
  48. string strUserName,
  49. string strPassword)
  50. {
  51. //if (m_sessionFactory != null) return;
  52. try
  53. {
  54. string connectionString = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
  55. strDatabaseServer, databasePort, strDatabaseName, strUserName, strPassword);
  56. FluentConfiguration config = Fluently.Configure()
  57. .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
  58. .ConnectionString(connectionString));
  59. Assembly curAssembly = Assembly.GetExecutingAssembly();
  60. NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly)).BuildConfiguration();
  61. m_factory = cfg.BuildSessionFactory();
  62. // m_mappingSet.buildMappings(cfg);
  63. }
  64. catch { throw; }
  65. }
  66. internal static void InitWareHousesStatus()
  67. {
  68. using (var session = Session)
  69. {
  70. using (var transaction = session.BeginTransaction())
  71. {
  72. try
  73. {
  74. ISQLQuery qry1 = session.CreateSQLQuery("update t_wh_locationProperty set Status='2'");
  75. qry1.ExecuteUpdate();
  76. //ISQLQuery qry2 = session.CreateSQLQuery("delete from OperationRecord");
  77. //qry2.ExecuteUpdate();
  78. //ISQLQuery qry3 = session.CreateSQLQuery("delete from DemandStation");
  79. //qry3.ExecuteUpdate();
  80. //ISQLQuery qry4 = session.CreateSQLQuery("delete from Station");
  81. //qry4.ExecuteUpdate();
  82. //ISQLQuery qry5 = session.CreateSQLQuery("delete from NavigatePath");
  83. //qry5.ExecuteUpdate();
  84. //ISQLQuery qry6 = session.CreateSQLQuery("delete from NavigateNode");
  85. //qry6.ExecuteUpdate();
  86. }
  87. catch (Exception ex)
  88. {
  89. transaction.Rollback();
  90. throw ex;
  91. }
  92. }
  93. }
  94. }
  95. static public void close()
  96. {
  97. if (m_factory != null) { m_factory.Close(); m_factory = null; }
  98. }
  99. static private ISessionFactory createSessionFactory()
  100. {
  101. try
  102. {
  103. string connectionString = _connectionString;
  104. FluentConfiguration config = Fluently.Configure()
  105. .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
  106. .ConnectionString(connectionString));
  107. Assembly curAssembly = Assembly.GetExecutingAssembly();
  108. NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly))
  109. .ExposeConfiguration(f => f.SetInterceptor(new SqlStatementInterceptor()))
  110. .BuildConfiguration();
  111. return cfg.BuildSessionFactory();
  112. }
  113. catch { throw; }
  114. }
  115. public class SqlStatementInterceptor : EmptyInterceptor
  116. {
  117. public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
  118. {
  119. //#if DEBUG
  120. try
  121. {
  122. //File.AppendAllText("c:\\log.txt", string.Format("{0}\n", sql.ToString()));
  123. }
  124. catch
  125. {
  126. }
  127. return sql;
  128. }
  129. }
  130. }
  131. #region WareHouseLocation
  132. public class WareHouseLocation
  133. {
  134. private string _WareHouseCode;
  135. private string _LocationCode;
  136. private string _Area;
  137. private string _Status;
  138. private string _LocationName;
  139. //private string _WareHouseType;
  140. //private string _CallCount;
  141. private string _Material;
  142. private string _Time;
  143. private string _Column;
  144. private string _Row;
  145. //private int _Mark;
  146. public virtual string LocationCode
  147. {
  148. get
  149. {
  150. return _LocationCode;
  151. }
  152. set
  153. {
  154. _LocationCode = value;
  155. }
  156. }
  157. public virtual string Attribute1
  158. {
  159. get
  160. {
  161. return _LocationName;
  162. }
  163. set
  164. {
  165. _LocationName = value;
  166. }
  167. }
  168. public virtual string Attribute2
  169. {
  170. get
  171. {
  172. return _Column;
  173. }
  174. set
  175. {
  176. _Column = value;
  177. }
  178. }
  179. public virtual string Attribute3
  180. {
  181. get
  182. {
  183. return _Row;
  184. }
  185. set
  186. {
  187. _Row = value;
  188. }
  189. }
  190. public virtual string Attribute4
  191. {
  192. get
  193. {
  194. return _Material;
  195. }
  196. set
  197. {
  198. _Material = value;
  199. }
  200. }
  201. public virtual string Attribute5
  202. {
  203. get
  204. {
  205. return _Time;
  206. }
  207. set
  208. {
  209. _Time = value;
  210. }
  211. }
  212. public virtual string Area
  213. {
  214. get
  215. {
  216. return _Area;
  217. }
  218. set
  219. {
  220. _Area = value;
  221. }
  222. }
  223. public virtual string WareHouseCode
  224. {
  225. get
  226. {
  227. return _WareHouseCode;
  228. }
  229. set
  230. {
  231. _WareHouseCode = value;
  232. }
  233. }
  234. public virtual string Status
  235. {
  236. get
  237. {
  238. return _Status;
  239. }
  240. set
  241. {
  242. _Status = value;
  243. }
  244. }
  245. //public virtual int Mark
  246. //{
  247. // get
  248. // {
  249. // return _Mark;
  250. // }
  251. // set
  252. // {
  253. // _Mark = value;
  254. // }
  255. //}
  256. }
  257. class MappingWareHouseLocation : ClassMap<WareHouseLocation>
  258. {
  259. public MappingWareHouseLocation()
  260. {
  261. Table("t_wh_locationProperty");
  262. Id(m => m.LocationCode).GeneratedBy.Assigned();
  263. Map(m => m.WareHouseCode);//.Increment();
  264. Map(m => m.Status);
  265. Map(m => m.Area);
  266. Map(m => m.Attribute1);
  267. Map(m => m.Attribute2);
  268. Map(m => m.Attribute3);
  269. Map(m => m.Attribute4);
  270. Map(m => m.Attribute5);
  271. //Map(m => m.Mark);
  272. }
  273. }
  274. #endregion
  275. }