AlertSnapRepository.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace GDNXFD.Data.Repositories
  7. {
  8. public class AlertSnapRepository
  9. {
  10. #region 报警历史
  11. public static IList<AlertInfo> GetRealTimeAlertInfos()
  12. {
  13. try
  14. {
  15. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  16. {
  17. DateTime startTime = DateTime.Now.AddDays(-1);
  18. var results = ctx.AlertInfo
  19. .Where(q => q.IsConfirmed == false)
  20. .Where(q => q.IsOpened == true)
  21. .Where(q => q.AlertTime > startTime)
  22. .Where(q => q.MessageType == "1")
  23. .OrderByDescending(q => q.Rank)
  24. .OrderByDescending(q => q.AlertTime)
  25. //.Take(1000)
  26. .ToList();
  27. return results;
  28. }
  29. }
  30. catch (Exception ex)
  31. {
  32. var o = ex;
  33. return null;
  34. }
  35. }
  36. public static IList<AlertSnap> GetRealTimeAlertSnaps()
  37. {
  38. try
  39. {
  40. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  41. {
  42. // DateTime startTime = DateTime.Now.AddHours(-8);
  43. var results = ctx.AlertSnap
  44. // .Where(q => q.IsConfirmed == false)
  45. .Where(q => q.IsOpened == true)
  46. // .Where(q => q.LastUpdateTime > startTime)
  47. .OrderByDescending(q => q.Rank)
  48. .OrderByDescending(q => q.LastUpdateTime)
  49. .ToList();
  50. return results;
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. var o = ex;
  56. return null;
  57. }
  58. }
  59. public static IList<AlertSnap> GetLatestAlertSnaps(int count)
  60. {
  61. try
  62. {
  63. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  64. {
  65. DateTime startTime = DateTime.Now.AddHours(-8);
  66. var results = ctx.AlertSnap
  67. .Where(q => q.IsOpened == true)
  68. .Where(q => q.LastUpdateTime > startTime)
  69. .OrderByDescending(q => q.LastUpdateTime)
  70. .Take(count)
  71. .ToList();
  72. return results;
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. var o = ex;
  78. return null;
  79. }
  80. }
  81. public static IList<ShutdownEvent> GetLatestShutdownEvents()
  82. {
  83. try
  84. {
  85. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  86. {
  87. DateTime startTime = DateTime.Now.AddDays(-1);
  88. var results = ctx.ShutdownEvent
  89. .Where(q => q.StopHours > 0)
  90. .Where(q => q.StopTime > startTime)
  91. .OrderByDescending(q => q.StopTime)
  92. .Take(1000)
  93. .ToList();
  94. return results;
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. var o = ex;
  100. return null;
  101. }
  102. }
  103. public static IList<AlertInfo> GetAlertInfosPage(string filter, int pageIndex, int pageSize, ref int total)
  104. {
  105. try
  106. {
  107. string[] cri = filter.Split(',');
  108. DateTime startDt = DateTime.Parse(cri[0]);
  109. DateTime endDt = DateTime.Parse(cri[1]);
  110. string stationId = cri[2];
  111. string rank = cri[3];
  112. string category1 = cri[4];
  113. string category2 = cri[5];
  114. string keyWords = cri[6];
  115. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  116. {
  117. var r1 = ctx.AlertInfo.AsQueryable();
  118. r1 = r1.Where(q => q.AlertTime > startDt && q.AlertTime < endDt);
  119. if (!string.IsNullOrWhiteSpace(stationId))
  120. {
  121. r1 = r1.Where(q => q.StationId == stationId);
  122. }
  123. else
  124. {
  125. r1 = r1.Where(q => q.StationId != null);
  126. }
  127. if (!string.IsNullOrWhiteSpace(rank))
  128. {
  129. r1 = r1.Where(q => q.Rank == rank);
  130. }
  131. if (!string.IsNullOrWhiteSpace(category1))
  132. {
  133. r1 = r1.Where(q => q.Category1 == category1);
  134. }
  135. if (!string.IsNullOrWhiteSpace(category2))
  136. {
  137. r1 = r1.Where(q => q.Category2 == category2);
  138. }
  139. if (!string.IsNullOrWhiteSpace(keyWords))
  140. {
  141. r1 = r1.Where(q => q.AlertText.Contains(keyWords));
  142. }
  143. r1 = r1.Where(q => q.MessageType == "1");
  144. r1 = r1.Where(q => q.AlertValue > 0);
  145. r1 = r1.Where(q => q.SnapID > 0);
  146. total = r1.Count();
  147. var results = r1.OrderByDescending(q => q.AlertTime)
  148. .Skip(--pageIndex * pageSize)
  149. .Take(pageSize)
  150. .ToList();
  151. return results;
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. var o = ex;
  157. return null;
  158. }
  159. }
  160. public static IList<IFixBJTestingPointDI> GetIFixBjPage(string filter, int pageIndex, int pageSize, ref int total)
  161. {
  162. try
  163. {
  164. string[] cri = filter.Split(',');
  165. string stationId = cri[0];
  166. string rank = cri[1];
  167. string category1 = cri[2];
  168. string status = cri[3];
  169. string tagDesc = cri[4];
  170. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  171. {
  172. var r1 = ctx.IFixBJTestingPointDI.AsQueryable();
  173. if (!string.IsNullOrWhiteSpace(stationId))
  174. {
  175. r1 = r1.Where(q => q.StationId == stationId);
  176. }
  177. if (!string.IsNullOrWhiteSpace(rank))
  178. {
  179. r1 = r1.Where(q => q.LevelId == rank);
  180. }
  181. if (!string.IsNullOrWhiteSpace(category1))
  182. {
  183. r1 = r1.Where(q => q.Area1 == category1);
  184. }
  185. if (!string.IsNullOrWhiteSpace(status))
  186. {
  187. bool isEnabled = status == "0" ? false : true;
  188. r1 = r1.Where(q => q.Enabled == isEnabled);
  189. }
  190. if (!string.IsNullOrWhiteSpace(tagDesc))
  191. {
  192. r1 = r1.Where(q => q.PointKey.Contains(tagDesc) || q.Description.Contains(tagDesc));
  193. }
  194. total = r1.Count();
  195. var results = r1.OrderBy(q => q.PointKey)
  196. .Skip(--pageIndex * pageSize)
  197. .Take(pageSize)
  198. .ToList();
  199. return results;
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. var o = ex;
  205. return null;
  206. }
  207. }
  208. public static IFixBJTestingPointDI GetIFixBjModel(string pointKey)
  209. {
  210. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  211. {
  212. return ctx.IFixBJTestingPointDI.Where(q => q.PointKey == pointKey).FirstOrDefault();
  213. }
  214. }
  215. public static void UpdateIFixBj(IFixBJTestingPointDI model)
  216. {
  217. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  218. {
  219. var obj = ctx.IFixBJTestingPointDI.Where(q => q.Id == model.Id).FirstOrDefault();
  220. if (obj != null)
  221. {
  222. MergeIFixBj(obj, model);
  223. ctx.SaveChanges();
  224. }
  225. }
  226. }
  227. public static void AddIFixBj(IFixBJTestingPointDI model)
  228. {
  229. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  230. {
  231. var obj = ctx.IFixBJTestingPointDI.Where(q => q.PointKey == model.PointKey).FirstOrDefault();
  232. if (obj == null)
  233. {
  234. ctx.IFixBJTestingPointDI.Add(model);
  235. ctx.SaveChanges();
  236. }
  237. }
  238. }
  239. public static IList<AlertInfo> GetWindturbineAlertInfoByPage(string filter, int pageIndex, int pageSize, ref int total)
  240. {
  241. try
  242. {
  243. string[] cri = filter.Split(',');
  244. DateTime startDt = DateTime.Parse(cri[0]);
  245. DateTime endDt = DateTime.Parse(cri[1]);
  246. string windturbineId = cri[2];
  247. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  248. {
  249. var r1 = ctx.AlertInfo.AsQueryable();
  250. r1 = r1.Where(q => q.AlertTime > startDt && q.AlertTime < endDt);
  251. r1 = r1.Where(q => q.WindturbineId == windturbineId);
  252. r1 = r1.Where(q => q.MessageType == "1");
  253. r1 = r1.Where(q => q.AlertValue > 0);
  254. r1 = r1.Where(q => q.SnapID > 0);
  255. total = r1.Count();
  256. var results = r1.OrderByDescending(q => q.AlertTime)
  257. .Skip(--pageIndex * pageSize)
  258. .Take(pageSize)
  259. .ToList();
  260. return results;
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. var o = ex;
  266. return null;
  267. }
  268. }
  269. #endregion
  270. public static IList<AlertSnap> GetTop1000Snaps()
  271. {
  272. try
  273. {
  274. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  275. {
  276. DateTime startTime = DateTime.Now.AddDays(-3);
  277. var results = ctx.AlertSnap
  278. .Where(q => q.IsOpened == true)
  279. .Where(q => q.LastUpdateTime > startTime)
  280. .OrderByDescending(q => q.LastUpdateTime)
  281. .Take(5000)
  282. .ToList();
  283. return results;
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. var o = ex;
  289. return null;
  290. }
  291. }
  292. public static IList<AlertSnap> GetTop1000ClosedSnaps()
  293. {
  294. try
  295. {
  296. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  297. {
  298. DateTime startTime = DateTime.Now.AddDays(-3);
  299. var results = ctx.AlertSnap
  300. .Where(q => q.IsOpened == false)
  301. .Where(q => q.LastCloseTime > startTime)
  302. .OrderByDescending(q => q.LastCloseTime)
  303. .Take(1000)
  304. .ToList();
  305. return results;
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. var o = ex;
  311. return null;
  312. }
  313. }
  314. public static IList<AlertSnap> GetAlertSnaps(string stationId, string rank, string category, string category2, string filter, string status)
  315. {
  316. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  317. {
  318. DateTime startTime = DateTime.Now.AddDays(-3);
  319. var r1 = ctx.AlertSnap.Where(q => q.IsOpened == true).Where(q => q.LastUpdateTime > startTime);
  320. if (!string.IsNullOrWhiteSpace(stationId))
  321. {
  322. r1 = r1.Where(q => q.StationId == stationId);
  323. }
  324. if (!string.IsNullOrWhiteSpace(rank))
  325. {
  326. r1 = r1.Where(q => q.Rank == rank);
  327. }
  328. if (!string.IsNullOrWhiteSpace(category))
  329. {
  330. r1 = r1.Where(q => q.Category1 == category);
  331. }
  332. if (!string.IsNullOrWhiteSpace(category2))
  333. {
  334. r1 = r1.Where(q => q.Category2 == category2);
  335. }
  336. if (!string.IsNullOrWhiteSpace(status))
  337. {
  338. bool isConfirmed = status == "0" ? false : true;
  339. r1 = r1.Where(q => q.IsConfirmed == isConfirmed);
  340. }
  341. if (!string.IsNullOrWhiteSpace(filter))
  342. {
  343. r1 = r1.Where(q => q.AlertText.Contains(filter) ||
  344. q.StationName.Contains(filter) ||
  345. q.ProjectName.Contains(filter) ||
  346. q.LineName.Contains(filter) ||
  347. q.WindturbineName.Contains(filter));
  348. }
  349. var results = r1.OrderByDescending(q => q.LastUpdateTime)
  350. .Take(1000)
  351. .ToList();
  352. return results;
  353. }
  354. }
  355. //
  356. public static void ConfirmAlert(long id)
  357. {
  358. try
  359. {
  360. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  361. {
  362. string sql = "update alertsnap set isconfirmed='1', confirmtime=sysdate where id='" + id + "'";
  363. ctx.Database.ExecuteSqlCommand(sql, new object[] { });
  364. }
  365. }
  366. catch (Exception ex)
  367. {
  368. var x = ex.Message;
  369. }
  370. }
  371. public static void ConfirmAlertPage(string[] ids)
  372. {
  373. try
  374. {
  375. //ids = '2016-10-13 15:38:30','MHS_FDC','3','custom','1','96'
  376. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  377. {
  378. StringBuilder sb = new StringBuilder("update view_alerthistory set isconfirmed='1', confirmtime=sysdate where isconfirmed='0' ");
  379. sb.AppendFormat("and alerttime <= to_date('{0}','yyyy-mm-dd hh24:mi:ss') and alerttime > sysdate-4 ", ids[0]);
  380. if (string.IsNullOrWhiteSpace(ids[1]) == false)
  381. {
  382. sb.AppendFormat("and stationid='{0}' ", ids[1]);
  383. }
  384. if (string.IsNullOrWhiteSpace(ids[2]) == false)
  385. {
  386. sb.AppendFormat("and rank='{0}' ", ids[2]);
  387. }
  388. if (string.IsNullOrWhiteSpace(ids[3]) == false)
  389. {
  390. sb.AppendFormat("and category1='{0}' ", ids[3]);
  391. }
  392. if (string.IsNullOrWhiteSpace(ids[4]) == false)
  393. {
  394. sb.AppendFormat("and category2='{0}' ", ids[4]);
  395. }
  396. if (string.IsNullOrWhiteSpace(ids[5]) == false)
  397. {
  398. sb.AppendFormat("and ( stationname like '%{0}%' or projectname like '%{0}%' or linename like '%{0}%' or windturbinename like '%{0}%' or alerttext like '%{0}%') ", ids[5]);
  399. }
  400. ctx.Database.ExecuteSqlCommand(sb.ToString(), new object[] { });
  401. }
  402. }
  403. catch (Exception ex)
  404. { var tmp = ex.Message; }
  405. }
  406. public static void ConfirmAll()
  407. {
  408. try
  409. {
  410. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  411. {
  412. string sql = "update alerthistory set isconfirmed='1', confirmtime=sysdate where alerttime > sysdate-3 and isconfirmed='0'";
  413. ctx.Database.ExecuteSqlCommand(sql, new object[] { });
  414. }
  415. }
  416. catch { }
  417. }
  418. public static AlertSnap GetAlertSnap(AlertObjectType oType, string objectId, long eDnaValue)
  419. {
  420. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  421. {
  422. string category = "custom";
  423. switch (oType)
  424. {
  425. case AlertObjectType.WindTurbine:
  426. return ctx.AlertSnap.Where(q => q.Category1 == category && q.Category2 == "1" && q.WindturbineId == objectId && q.AlertValue == eDnaValue).FirstOrDefault();
  427. case AlertObjectType.WindPowerStation:
  428. return ctx.AlertSnap.Where(q => q.Category1 == category && q.Category2 == "2" && q.StationId == objectId && q.AlertValue == eDnaValue).FirstOrDefault();
  429. case AlertObjectType.Project:
  430. return ctx.AlertSnap.Where(q => q.Category1 == category && q.Category2 == "3" && q.ProjectId == objectId && q.AlertValue == eDnaValue).FirstOrDefault();
  431. case AlertObjectType.Line:
  432. return ctx.AlertSnap.Where(q => q.Category1 == category && q.Category2 == "4" && q.LineId == objectId && q.AlertValue == eDnaValue).FirstOrDefault();
  433. case AlertObjectType.Electrical:
  434. return ctx.AlertSnap.Where(q => q.Category1 == category && q.Category2 == "5" && q.StationId == objectId && q.AlertValue == eDnaValue).FirstOrDefault();
  435. }
  436. }
  437. return null;
  438. }
  439. public static AlertSnap GetWindturbineAlertSnap(string windturbineId, long eDnaValue)
  440. {
  441. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  442. {
  443. return ctx.AlertSnap.Where(q => q.Category1 == "windturbine" &&
  444. q.WindturbineId == windturbineId
  445. && q.AlertValue == eDnaValue).FirstOrDefault();
  446. }
  447. }
  448. public static AlertSnap GetIFixAlertSnap(string pointKey)
  449. {
  450. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  451. {
  452. return ctx.AlertSnap.Where(q => (q.Category1 == "SYZ" || q.Category1 == "GF") &&
  453. q.TestingPointKey == pointKey).FirstOrDefault();
  454. }
  455. }
  456. public static IList<AlertSnap> GetTestingPointAlertSnap()
  457. {
  458. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  459. {
  460. return ctx.AlertSnap.Where(q => string.IsNullOrEmpty(q.TestingPointKey) == false).ToList();
  461. }
  462. }
  463. public static void SaveAlertSnap(AlertSnap snap)
  464. {
  465. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  466. {
  467. var obj = ctx.AlertSnap.Where(q => q.Id == snap.Id).FirstOrDefault();
  468. if (obj != null)
  469. {
  470. MergeRule(obj, snap);
  471. }
  472. else
  473. {
  474. ctx.AlertSnap.Add(snap);
  475. }
  476. AlertHistory his = AlertSnapFactory.CreateAlertHistory(snap);
  477. ctx.AlertHistory.Add(his);
  478. ctx.SaveChanges();
  479. }
  480. }
  481. private static void MergeRule(AlertSnap pr, AlertSnap ur)
  482. {
  483. pr.AlertText = ur.AlertText;
  484. pr.AlertValue = ur.AlertValue;
  485. pr.Category1 = ur.Category1;
  486. pr.Category2 = ur.Category2;
  487. pr.Category3 = ur.Category3;
  488. pr.ConfirmPerson = ur.ConfirmPerson;
  489. pr.ConfirmTime = ur.ConfirmTime;
  490. pr.IsConfirmed = ur.IsConfirmed;
  491. pr.IsOpened = ur.IsOpened;
  492. pr.LastUpdatePerson = ur.LastUpdatePerson;
  493. pr.LastUpdateTime = ur.LastUpdateTime;
  494. pr.LastClosePerson = ur.LastClosePerson;
  495. pr.LastCloseTime = ur.LastCloseTime;
  496. pr.LineId = ur.LineId;
  497. pr.LineName = ur.LineName;
  498. pr.ModelId = ur.ModelId;
  499. pr.ProjectId = ur.ProjectId;
  500. pr.ProjectName = ur.ProjectName;
  501. pr.Rank = ur.Rank;
  502. pr.StationId = ur.StationId;
  503. pr.StationName = ur.StationName;
  504. pr.WindturbineId = ur.WindturbineId;
  505. pr.WindturbineName = ur.WindturbineName;
  506. pr.TestingPointKey = ur.TestingPointKey;
  507. pr.DataInfo = ur.DataInfo;
  508. }
  509. private static void MergeIFixBj(IFixBJTestingPointDI pr, IFixBJTestingPointDI ur)
  510. {
  511. pr.PointKey = ur.PointKey;
  512. pr.Area1 = ur.Area1;
  513. pr.Description = ur.Description;
  514. pr.Enabled = ur.Enabled;
  515. //pr.Id = ur.Id;
  516. pr.LevelId = ur.LevelId;
  517. pr.StationId = ur.StationId;
  518. }
  519. }
  520. }