MacStatusDal.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using DllEapEntity;
  5. using DllHsms;
  6. using RabbitMQ.Client;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. namespace DllStatusShowDal
  13. {
  14. public class MacStatusDal
  15. {
  16. private IDatabase CurrDb = null;
  17. private string EapDbCode = "eap";
  18. public long CurrMaxBlock = 0;
  19. public string CurrShareFileDir = "";
  20. private string MqIpAddress = "";
  21. private string MqUser = "";
  22. private string MqPasswd = "";
  23. private string MacQueue = "";//机台对应的队列名称
  24. public MacStatusDal(IDatabase db)
  25. {
  26. CurrDb = db;
  27. }
  28. public MacStatus IMacStatus(MacStatus mst, string usercode, ref string errorinfo, bool update01 = true)
  29. {
  30. try
  31. {
  32. int result = UpdatePreStatus(mst, usercode, ref errorinfo, update01);
  33. if (result <= 0)
  34. return null;
  35. result = CurrDb.InsertFor(mst, usercode);
  36. if (result < 0)
  37. {
  38. return null;
  39. }
  40. object objid = CurrDb.FindObject("select @@IDENTITY");
  41. if (objid.ToString() == "")
  42. {
  43. return null;
  44. }
  45. int id = int.Parse(objid.ToString());
  46. mst = CurrDb.FindEntityFor<MacStatus>(id);
  47. //mst.StatusID = 6;
  48. //关闭广播状态功能
  49. //Start(mst);
  50. return mst;
  51. }
  52. catch (Exception e)
  53. {
  54. errorinfo = e.Message;
  55. return null;
  56. }
  57. }
  58. public MacStatus UpdateLastSvidAlarm(MacStatus lastStatus, MacStatus mst, string usercode, ref string errorinfo)
  59. {
  60. try
  61. {
  62. var result = 0;
  63. if (lastStatus.AlarmCode == "0000")
  64. {
  65. result = CurrDb.DeleteFor<MacStatus>(lastStatus.ID);
  66. if (result <= 0)
  67. return null;
  68. mst.STime = lastStatus.STime;
  69. }
  70. result = AddMacStatus01(mst, string.Empty, ref errorinfo);
  71. if (result < 0)
  72. return null;
  73. result = CurrDb.InsertFor(mst, usercode);
  74. if (result < 0)
  75. {
  76. return null;
  77. }
  78. object objid = CurrDb.FindObject("select @@IDENTITY");
  79. if (objid.ToString() == "")
  80. {
  81. return null;
  82. }
  83. int id = int.Parse(objid.ToString());
  84. mst = CurrDb.FindEntityFor<MacStatus>(id);
  85. //mst.StatusID = 6;
  86. //关闭广播状态功能
  87. //Start(mst);
  88. return mst;
  89. }
  90. catch (Exception e)
  91. {
  92. errorinfo = e.Message;
  93. return null;
  94. }
  95. }
  96. /// <summary>
  97. /// 将状态发送到queue
  98. /// </summary>
  99. /// <param name="mst"></param>
  100. private void Start(MacStatus mst)
  101. {
  102. IModel channel = null;
  103. IConnection connection = null;
  104. try
  105. {
  106. string error = string.Empty;
  107. connection = CreateQueueConn(mst.MacCode, ref error);
  108. channel = connection.CreateModel();
  109. BroadcastStatus(channel, mst, ref error);
  110. }
  111. catch (Exception ex)
  112. {
  113. }
  114. finally
  115. {
  116. if (channel != null)
  117. channel.Close();
  118. if (connection != null)
  119. connection.Close();
  120. }
  121. }
  122. /// <summary>
  123. /// 创建消息队列连接
  124. /// </summary>
  125. /// <param name="maccode"></param>
  126. /// <param name="errorinfo"></param>
  127. /// <returns></returns>
  128. private IConnection CreateQueueConn(string maccode, ref string errorinfo)
  129. {
  130. MacQueue = GetMacQueueName(maccode, ref errorinfo);
  131. if (MacQueue == "")
  132. return null;
  133. var factory = new ConnectionFactory();
  134. factory.HostName = MqIpAddress;
  135. factory.UserName = MqUser;
  136. factory.Password = MqPasswd;
  137. return factory.CreateConnection();
  138. }
  139. //获取机台对应的消息队列名称
  140. private string GetMacQueueName(string maccode, ref string errorinfo)
  141. {
  142. IDatabase CurrDb = null;
  143. try
  144. {
  145. CurrDb = DbFactory.Base(EapDbCode);
  146. string condition = $" and a.FCode='{maccode}'";
  147. Machine mst = CurrDb.FindListForCondition<Machine>(condition, ref errorinfo).ToList()[0];
  148. condition = $" and a.MacID={mst.ID}";
  149. EapAppserverMac eapmac = CurrDb.FindListForCondition<EapAppserverMac>(condition, ref errorinfo).ToList()[0];
  150. condition = $" and a.APServerID={eapmac.EapAppserverID}";
  151. MQServerDetail mqdetail = CurrDb.FindListForCondition<MQServerDetail>(condition, ref errorinfo).ToList()[0];
  152. MqIpAddress = mqdetail.IpAddress;
  153. MqUser = mqdetail.FUser;
  154. MqPasswd = mqdetail.FPasswd;
  155. CurrShareFileDir = mqdetail.ShareDir;
  156. return eapmac.EapAppserverFCode;
  157. }
  158. catch (Exception ex)
  159. {
  160. errorinfo = "获取机台对应的队列名称发生错误:" + ex.Message.ToString();
  161. return "";
  162. }
  163. finally
  164. {
  165. if (CurrDb != null)
  166. CurrDb.Close();
  167. }
  168. }
  169. /// <summary>
  170. /// 广播状态
  171. /// </summary>
  172. /// <param name="status"></param>
  173. /// <param name="errorinfo"></param>
  174. /// <returns></returns>
  175. private int BroadcastStatus(IModel channel, MacStatus status, ref string errorinfo)
  176. {
  177. try
  178. {
  179. byte[] queuedata = EntityHelper.SerializeBytes<MacStatus>(status);
  180. if (queuedata == null)
  181. return -1;
  182. string StatusExchange = AppConfigurtaionServices.Configuration["rabbitmq:StatusExchange"];
  183. channel.ExchangeDeclare(exchange: StatusExchange, type: "fanout", durable: false, autoDelete: true);
  184. channel.BasicPublish(StatusExchange, "", null, queuedata); //开始传递
  185. return 1;
  186. }
  187. catch (Exception ex)
  188. {
  189. errorinfo = ex.Message.ToString();
  190. return -1;
  191. }
  192. }
  193. //public MacStatus IMacStatus(MacStatus mst, ref string errorinfo)
  194. //{
  195. // try
  196. // {
  197. // int result = 0;
  198. // result = CurrDb.Insert(mst);
  199. // if (result < 0)
  200. // {
  201. // return null;
  202. // }
  203. // //using (CurrDb = DbFactory.Base("eap"))
  204. // //{
  205. // string sqlstr = $"select max(id) from MacStatus where MacCode='{mst.MacCode}'";
  206. // object objid = CurrDb.FindObject(sqlstr);
  207. // // object objid = CurrDb.FindObject("select @@IDENTITY");
  208. // if (objid.ToString() == "")
  209. // {
  210. // return null;
  211. // }
  212. // int id = int.Parse(objid.ToString());
  213. // mst = CurrDb.FindEntityFor<MacStatus>(id);
  214. // return mst;
  215. // //}
  216. // }
  217. // catch (Exception e)
  218. // {
  219. // errorinfo = e.Message;
  220. // return null;
  221. // }
  222. //}
  223. /// <summary>
  224. /// 更新上个状态结束时间
  225. /// </summary>
  226. /// <param name="maccode">机台编号</param>
  227. /// <param name="usercode"></param>
  228. /// <param name="errorinfo"></param>
  229. /// <returns></returns>
  230. public int UpdatePreStatus(MacStatus mst, string usercode, ref string errorinfo, bool update01 = true)
  231. {
  232. try
  233. {
  234. int result = 1;
  235. //插入最后状态
  236. if (update01)
  237. {
  238. result = AddMacStatus01(mst, usercode, ref errorinfo);
  239. if (result <= 0)
  240. {
  241. errorinfo = $"添加MacStatus01发生错误:{errorinfo}";
  242. return -1;
  243. }
  244. }
  245. string sqlstr = $"select max(id) from MacStatus where MacCode='{mst.MacCode}'";
  246. object maxobj = CurrDb.FindObject(sqlstr);
  247. if (maxobj.ToString() == "")
  248. {
  249. return 1;
  250. }
  251. int maxid = int.Parse(maxobj.ToString());
  252. MacStatus entity = CurrDb.FindEntityFor<MacStatus>(maxid);
  253. if (entity.ETime > entity.STime)//说明已经更新,无需处理
  254. return 1;
  255. //if (entity.ETime != null)//说明已经更新
  256. // return 1;
  257. entity.ETime = mst.STime;
  258. TimeSpan ts1 = new TimeSpan(entity.STime.Ticks);
  259. TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks);
  260. TimeSpan ts = ts1.Subtract(ts2).Duration();
  261. entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
  262. result = CurrDb.UpdateFor<MacStatus>(entity, usercode);
  263. if (result < 0)
  264. return -1;
  265. return 1;
  266. }
  267. catch (Exception e)
  268. {
  269. errorinfo = e.Message;
  270. return -1;
  271. }
  272. }
  273. /// <summary>
  274. /// 更新上个状态结束时间
  275. /// </summary>
  276. /// <param name="maccode">机台编号</param>
  277. /// <param name="usercode"></param>
  278. /// <param name="errorinfo"></param>
  279. /// <returns></returns>
  280. public int AddMacStatus01(MacStatus mst, string usercode, ref string errorinfo)
  281. {
  282. try
  283. {
  284. string condition = $" and a.MacCode='{mst.MacCode}'";
  285. List<MacStatus01> templist = CurrDb.FindListForCondition<MacStatus01>(condition, ref errorinfo).ToList();
  286. if (templist.Count > 0)
  287. {
  288. if (templist[0].StatusID == mst.StatusID)
  289. {
  290. templist[0].Remark = mst.Remark;
  291. templist[0].AlarmCode = mst.AlarmCode;
  292. templist[0].AlarmDescribe = mst.AlarmDescribe;
  293. CurrDb.UpdateFor<MacStatus01>(templist[0], usercode);
  294. return 1;
  295. }
  296. templist[0].StatusID = mst.StatusID;
  297. templist[0].STime = mst.STime;
  298. templist[0].ETime = mst.ETime;
  299. templist[0].Remark = mst.Remark;
  300. templist[0].AlarmCode = mst.AlarmCode;
  301. templist[0].AlarmDescribe = mst.AlarmDescribe;
  302. CurrDb.UpdateFor<MacStatus01>(templist[0], usercode);
  303. }
  304. else
  305. {
  306. MacStatus01 entity = new MacStatus01();
  307. Type type = typeof(MacStatus01);
  308. PropertyInfo[] props = type.GetProperties();
  309. foreach (var item in props)
  310. {
  311. EntityHelper.SetPropertyValue<MacStatus01>(entity, item.Name, EntityHelper.GetPropertyValue<MacStatus>(mst, item.Name));
  312. }
  313. CurrDb.InsertFor<MacStatus01>(entity, usercode);
  314. }
  315. return 1;
  316. }
  317. catch (Exception e)
  318. {
  319. errorinfo = e.Message;
  320. return -1;
  321. }
  322. }
  323. public MacStatus GetLastStatus(string maccode, ref string errorinfo)
  324. {
  325. try
  326. {
  327. string sqlstr = $"select max(id) from MacStatus where MacCode='{maccode}'";
  328. object maxobj = CurrDb.FindObject(sqlstr);
  329. if (maxobj.ToString() == "")
  330. {
  331. return null;
  332. }
  333. int maxid = int.Parse(maxobj.ToString());
  334. MacStatus entity = CurrDb.FindEntityFor<MacStatus>(maxid);
  335. return entity;
  336. }
  337. catch (Exception e)
  338. {
  339. errorinfo = e.Message;
  340. return null;
  341. }
  342. }
  343. /// <summary>
  344. /// 更新上个状态结束时间
  345. /// </summary>
  346. /// <param name="maccode">机台编号</param>
  347. /// <param name="usercode"></param>
  348. /// <param name="errorinfo"></param>
  349. /// <returns></returns>
  350. private int UpdatePreStatus(string code, string usercode, ref string errorinfo)
  351. {
  352. try
  353. {
  354. string sqlstr = $"select max(id) from MacStatus where MacCode='{code}'";
  355. object maxobj = CurrDb.FindObject(sqlstr);
  356. if (maxobj.ToString() == "")
  357. {
  358. return 1;
  359. }
  360. int maxid = int.Parse(maxobj.ToString());
  361. MacStatus entity = CurrDb.FindEntityFor<MacStatus>(maxid);
  362. if (entity.ETime > entity.STime)//说明已经更新,无需处理
  363. return 1;
  364. //if (entity.ETime != null)//说明已经更新
  365. // return 1;
  366. entity.ETime = DateTime.Now;
  367. TimeSpan ts1 = new TimeSpan(entity.STime.Ticks);
  368. TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks);
  369. TimeSpan ts = ts1.Subtract(ts2).Duration();
  370. entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
  371. int result = CurrDb.UpdateFor<MacStatus>(entity, usercode);
  372. if (result < 0)
  373. return -1;
  374. return 1;
  375. }
  376. catch (Exception e)
  377. {
  378. errorinfo = e.Message;
  379. return -1;
  380. }
  381. }
  382. /// <summary>
  383. /// 修改当前结束时间
  384. /// </summary>
  385. /// <param name="mst"></param>
  386. /// <param name="usercode"></param>
  387. /// <param name="errorinfo"></param>
  388. /// <returns></returns>
  389. public int UpdateStatus(MacStatus mst, ref string errorinfo)
  390. {
  391. try
  392. {
  393. MacStatus entity = CurrDb.FindEntityFor<MacStatus>(mst.ID);
  394. if (entity.ETime > entity.STime)//说明已经更新,无需处理
  395. return 1;
  396. //if (entity.ETime != null)//说明已经更新
  397. // return 1;
  398. entity.ETime = DateTime.Now;
  399. TimeSpan ts1 = new TimeSpan(entity.STime.Ticks);
  400. TimeSpan ts2 = new TimeSpan(entity.ETime.Ticks);
  401. TimeSpan ts = ts1.Subtract(ts2).Duration();
  402. entity.FLen = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;//时长,秒为单位
  403. int result = CurrDb.Update<MacStatus>(entity);
  404. if (result < 0)
  405. return -1;
  406. return 1;
  407. }
  408. catch (Exception e)
  409. {
  410. errorinfo = e.Message;
  411. return -1;
  412. }
  413. }
  414. public LayoutDetail BatUpdatePreStatus(List<LayoutDetail> details, string usercode, ref string errorinfo)
  415. {
  416. try
  417. {
  418. foreach (LayoutDetail item in details)
  419. {
  420. if (item.EntityTypeID != EntityType.Machine)
  421. continue;
  422. int result = UpdatePreStatus(item.FCode, usercode, ref errorinfo);
  423. if (result <= 0)
  424. return null;
  425. }
  426. return details[0];
  427. }
  428. catch (Exception e)
  429. {
  430. errorinfo = e.Message;
  431. return null;
  432. }
  433. }
  434. public IEnumerable<MacStatus01> GetMacStatus01ByStatusID(int statusID)
  435. {
  436. return CurrDb.FindList<MacStatus01>($@"select * from MacStatus01 where StatusID={statusID}
  437. and STime>='{DateTime.Today.AddDays(-1)}' and STime<'{DateTime.Today}'
  438. order by STime");
  439. }
  440. public MacStatus01 GetMacStatus01ByMacode(string macCode)
  441. {
  442. return CurrDb.FindList<MacStatus01>($@"select * from MacStatus01 where MacCode='{macCode}' order by id desc LIMIT 1").FirstOrDefault();
  443. }
  444. #region MQ
  445. /// <summary>
  446. /// 根据MACID注册对应的通道
  447. /// </summary>
  448. /// <param name="macId"></param>
  449. /// <param name="errorinfo"></param>
  450. /// <returns></returns>
  451. private IModel RegeditChannel(int macId, ref string errorinfo)
  452. {
  453. try
  454. {
  455. var server = this.GetCurrentMqServer(macId);
  456. if (server == null)
  457. {
  458. errorinfo = "当前机台没有绑定对应的MQ服务器";
  459. return null;
  460. }
  461. var factory = new ConnectionFactory();
  462. factory.HostName = server.IpAddress;
  463. factory.UserName = server.FUser;
  464. factory.Password = server.FPasswd;
  465. factory.VirtualHost = "/test";
  466. var connection = factory.CreateConnection();
  467. var channel = connection.CreateModel();
  468. return channel;
  469. }
  470. catch (Exception ex)
  471. {
  472. errorinfo = ex.Message.ToString();
  473. return null;
  474. }
  475. }
  476. /// <summary>
  477. /// 根据MACID获取对应的队列服务器信息
  478. /// </summary>
  479. /// <param name="macId"></param>
  480. /// <returns></returns>
  481. private MQServer GetCurrentMqServer(int macId)
  482. {
  483. using (IDatabase db = DbFactory.Base("eapslave"))
  484. {
  485. var sql = $@"select * from mqserver where id = (
  486. select mstid from mqserverdetail where APServerID=(
  487. select eapappserverid from eapappservermac where macid={macId}))";
  488. var mqServer = db.FindList<MQServer>(sql).FirstOrDefault();
  489. return mqServer;
  490. }
  491. }
  492. /// <summary>
  493. /// 将状态推送到队列上
  494. /// </summary>
  495. /// <param name="macStatus"></param>
  496. /// <returns></returns>
  497. public int PushStatusToMQ(int macId, MacStatus macStatus, ref string errorinfo)
  498. {
  499. var mqName = AppConfigurtaionServices.Configuration["mqname:statusmqname"];
  500. if (string.IsNullOrEmpty(mqName))
  501. {
  502. errorinfo = "从接口配置文件中获取MQ名称失败";
  503. return -1;
  504. }
  505. IModel chanel = null;
  506. try
  507. {
  508. chanel = this.RegeditChannel(macId, ref errorinfo);
  509. if (chanel == null)
  510. return -1;
  511. chanel.QueueDeclare(mqName, false, false, false, null);
  512. byte[] queuedata = EntityHelper.SerializeBytes<MacStatus>(macStatus);
  513. chanel.BasicPublish("", mqName, null, queuedata);
  514. return 1;
  515. }
  516. catch (Exception ex)
  517. {
  518. errorinfo = ex.Message;
  519. return -1;
  520. }
  521. finally
  522. {
  523. if (chanel != null)
  524. {
  525. chanel.Close();
  526. chanel.Dispose();
  527. }
  528. }
  529. }
  530. #endregion
  531. }
  532. }