FrmAlarmHistory.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using GDNXFD.Data;
  2. using IntelligentControlForsx.Service;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace IntelligentControlForsx
  15. {
  16. public partial class FrmAlarmHistory : Form
  17. {
  18. [DllImport("user32.dll")]
  19. public static extern bool ReleaseCapture();
  20. [DllImport("user32.dll")]
  21. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
  22. public const int WM_SYSCOMMAND = 0x0112;
  23. public const int SC_MOVE = 0xF010;
  24. public const int HTCAPTION = 0x0002;
  25. private bool isReady = false;
  26. private bool isLoading = false;
  27. private AlertServiceClient asc = null;
  28. string station = string.Empty;
  29. string rank = string.Empty;
  30. string category = string.Empty;
  31. string category2 = string.Empty;
  32. string keyWords = string.Empty;
  33. DateTime endDt;
  34. DateTime startDt;
  35. //定义分页需要的变量
  36. int total =0;
  37. int pageNum = 1;
  38. int pageCurrent = 1;
  39. int pageSize = 20;
  40. int exportPageSize = 1000;
  41. private BindingList<AlertInfo> bindings;
  42. public FrmAlarmHistory()
  43. {
  44. InitializeComponent();
  45. this.dgvAlarm.AutoGenerateColumns = false;
  46. this.dgvAlarm.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  47. DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  48. dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleCenter;
  49. this.dgvAlarm.DefaultCellStyle = dataGridViewCellStyle1;
  50. endDt = DateTime.Now;
  51. startDt = endDt.AddDays(-1);
  52. dpEnd.Value = endDt;
  53. dpEnd.MinDate = startDt;
  54. dpStart.Value = startDt;
  55. dpStart.MaxDate = endDt;
  56. this.ts_PageSize.SelectedIndex = ts_PageSize.Items.IndexOf("20");
  57. //LoadData();
  58. //updatePageControls();
  59. isReady = true;
  60. }
  61. #region 事件处理
  62. private void cmbStation_SelectedValueChanged(object sender, EventArgs e)
  63. {
  64. if ("全部".Equals(cmbStation.SelectedItem) == false)
  65. {
  66. station = getStationId(cmbStation.SelectedItem.ToString());
  67. }
  68. else
  69. station = string.Empty;
  70. }
  71. private string getStationId(string stName)
  72. {
  73. if ("石板泉风电场".Equals(stName))
  74. return "SBQ_FDC";
  75. if ("麻黄山风电场".Equals(stName))
  76. return "MHS_FDC";
  77. if ("牛首山风电场".Equals(stName))
  78. return "NSS_FDC";
  79. if ("青山风电场".Equals(stName))
  80. return "QS_FDC";
  81. if ("香山风电场".Equals(stName))
  82. return "XS_FDC";
  83. return "";
  84. }
  85. private void cmbRank_SelectedValueChanged(object sender, EventArgs e)
  86. {
  87. if ("全部".Equals(cmbRank.SelectedItem) == false)
  88. {
  89. try
  90. {
  91. IList<DictItem> lst1 = ClientCache.Instance.GetDataDictByCategory("AlertLevel");
  92. rank = lst1.Where(q => q.Name == cmbRank.SelectedItem.ToString()).First().Code;
  93. }
  94. catch
  95. {
  96. rank = string.Empty;
  97. }
  98. }
  99. else
  100. rank = string.Empty;
  101. }
  102. private void cmbCategory_SelectedValueChanged(object sender, EventArgs e)
  103. {
  104. if ("全部".Equals(cmbCategory.SelectedItem) == false)
  105. {
  106. try
  107. {
  108. IList<DictItem> lst1 = ClientCache.Instance.GetDataDictByCategory("AlertCategory");
  109. category = lst1.Where(q => q.Name == cmbCategory.SelectedItem.ToString()).First().Code;
  110. }
  111. catch
  112. {
  113. category = string.Empty;
  114. }
  115. }
  116. else
  117. category = string.Empty;
  118. ResetCmbSubCategory();
  119. }
  120. private void ResetCmbSubCategory()
  121. {
  122. this.cmbSubCategory.Items.Clear();
  123. this.cmbSubCategory.Items.Add("全部");
  124. if (!string.IsNullOrWhiteSpace(category))
  125. {
  126. try
  127. {
  128. IList<DictItem> lst = ClientCache.Instance.GetDataDictByCategory(category);
  129. if (lst != null)
  130. {
  131. foreach (DictItem dItem in lst)
  132. {
  133. this.cmbSubCategory.Items.Add(dItem.Name);
  134. }
  135. }
  136. }
  137. catch (Exception ex) { }
  138. }
  139. this.cmbSubCategory.SelectedIndex = cmbSubCategory.Items.IndexOf("全部");
  140. }
  141. private void cmbSubCategory_SelectedValueChanged(object sender, EventArgs e)
  142. {
  143. if ("全部".Equals(cmbSubCategory.SelectedItem) == false)
  144. {
  145. try
  146. {
  147. IList<DictItem> lst1 = ClientCache.Instance.GetDataDictByCategory(category);
  148. category2 = lst1.Where(q => q.Name == cmbSubCategory.SelectedItem.ToString()).First().Code;
  149. }
  150. catch
  151. {
  152. category2 = string.Empty;
  153. }
  154. }
  155. else
  156. category2 = string.Empty;
  157. }
  158. private void txtKeyWord_TextChanged(object sender, EventArgs e)
  159. {
  160. keyWords = txtKeyWord.Text.Trim();
  161. }
  162. private void btnQuery_Click(object sender, EventArgs e)
  163. {
  164. pageCurrent = 1;
  165. LoadData();
  166. updatePageControls();
  167. }
  168. #endregion
  169. #region 分页查询
  170. public void LoadData()
  171. {
  172. updatePageControls();
  173. // 查询按钮点击后,按钮文本由"查询"改为"加载中",并把按钮状态改为不可点击
  174. //btnQuery.Text = " 加载中...";
  175. //pnlQuery.Enabled = false;
  176. //this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
  177. try
  178. {
  179. frmWaitingBox f = new frmWaitingBox((obj, args) =>
  180. {
  181. if (asc == null)
  182. asc = AlertServiceClientFactory.CreateAlertServiceClient();
  183. string filter = CreateFilter();
  184. var alerts = asc.GetAlertInfosPage(filter, pageCurrent, pageSize, ref total);
  185. if (alerts != null)
  186. {
  187. bindings = new BindingList<AlertInfo>(alerts);
  188. }
  189. }, 60, "正在加载数据,请稍后...", false, false);
  190. f.ShowDialog(this);
  191. this.dgvAlarm.DataSource = bindings;
  192. pageNum = (int)Math.Ceiling((double)(total / pageSize));
  193. ts_Pages.Text = pageNum + "";
  194. //this.btnQuery.Text = " 开始查询";
  195. //// 使用匿名方法定义一个委托事件,委托事件主要操作数据库查询,并返回查询的结果,不涉及任何UI操作
  196. //Func<IList<AlertInfo>> act = () =>
  197. //{
  198. // if (asc == null)
  199. // asc = AlertServiceClientFactory.CreateAlertServiceClient();
  200. // string filter = CreateFilter();
  201. // var alerts = asc.GetAlertInfosPage(filter, pageCurrent, pageSize, ref total);
  202. // return alerts;
  203. //};
  204. //// 异步调用委托事件,在委托事件回调方法中,使用任意控件的异步方法把查询结果绑定到数据表控件中
  205. //act.BeginInvoke((result) =>
  206. //{
  207. // IList<AlertInfo> dtResult = act.EndInvoke(result);
  208. // // this是当前WinForm窗口的实例,也可以替换为界面中的任意控件示例,如dgvMain.BeginInvoke
  209. // this.BeginInvoke(new Action<IList<AlertInfo>>((IList<AlertInfo> dtList) =>
  210. // {
  211. // if (dtList != null)
  212. // {
  213. // bindings = new BindingList<AlertInfo>(dtList);
  214. // this.dgvAlarm.DataSource = bindings;
  215. // }
  216. // pageNum = (int)Math.Ceiling((double)(total / pageSize));
  217. // ts_Pages.Text = pageNum + "";
  218. // this.btnQuery.Text = " 开始查询";
  219. // isLoading = false;
  220. // this.Cursor = System.Windows.Forms.Cursors.Arrow;
  221. // }), dtResult);
  222. //}, null);
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageBox.Show(ex.Message);
  227. //logger.Error("查询历史报警时发生异常:" + ex.Message);
  228. }finally
  229. {
  230. //isLoading = false;
  231. }
  232. }
  233. private void updatePageControls()
  234. {
  235. if (pageCurrent == 1)
  236. {
  237. ts_PagePrevious.Enabled = false;
  238. ts_PageFirst.Enabled = false;
  239. }
  240. else
  241. {
  242. ts_PagePrevious.Enabled = true;
  243. ts_PageFirst.Enabled = true;
  244. }
  245. if (pageNum == pageCurrent)
  246. {
  247. ts_PageNext.Enabled = false;
  248. ts_PageLast.Enabled = false;
  249. }
  250. else
  251. {
  252. ts_PageNext.Enabled = true;
  253. ts_PageLast.Enabled = true;
  254. }
  255. ts_PagePostion.Text = pageCurrent.ToString();
  256. ts_PagePostion.Enabled = true;
  257. ts_PagePostion.ReadOnly = false;
  258. ts_Pages.Text = pageNum.ToString();
  259. }
  260. private string CreateFilter()
  261. {
  262. return String.Format("{0},{1},{2},{3},{4},{5},{6}",
  263. startDt.ToString("yyyy-MM-dd HH:mm:ss"),
  264. endDt.ToString("yyyy-MM-dd HH:mm:ss"),
  265. station, rank, category, category2, keyWords);
  266. }
  267. private void ts_PagePrevious_Click(object sender, EventArgs e)
  268. {
  269. //ts_PageNext.Enabled = true;
  270. //if (pageCurrent == 1)
  271. //{
  272. // ts_PagePrevious.Enabled = false;
  273. // MessageBox.Show("已经是第一页了");
  274. // return;
  275. //}
  276. pageCurrent--;
  277. LoadData();
  278. }
  279. private void ts_PageNext_Click(object sender, EventArgs e)
  280. {
  281. //ts_PagePrevious.Enabled = true;
  282. //if (pageCurrent == pageSize)
  283. //{
  284. // ts_PageNext.Enabled = false;
  285. // MessageBox.Show("已经是最后一页了");
  286. // return;
  287. //}
  288. pageCurrent++;
  289. LoadData();
  290. }
  291. private void ts_PageFirst_Click(object sender, EventArgs e)
  292. {
  293. pageCurrent = 1;
  294. //ts_PageFirst.Enabled = false;
  295. //ts_PageLast.Enabled = true;
  296. //ts_PagePrevious.Enabled = false;
  297. //ts_PageNext.Enabled = true;
  298. LoadData();
  299. }
  300. private void ts_PageLast_Click(object sender, EventArgs e)
  301. {
  302. pageCurrent = pageNum;
  303. //ts_PageFirst.Enabled = true;
  304. //ts_PageLast.Enabled = false;
  305. //ts_PagePrevious.Enabled = true;
  306. //ts_PageNext.Enabled = false;
  307. LoadData();
  308. }
  309. private void ts_PagePostion_TextChanged(object sender, EventArgs e)
  310. {
  311. if (isReady == false)
  312. return;
  313. if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= pageNum)
  314. {
  315. pageCurrent = Convert.ToInt32(ts_PagePostion.Text);
  316. }
  317. else
  318. {
  319. pageCurrent = 1;
  320. }
  321. LoadData();
  322. }
  323. private void ts_PageSize_SelectedIndexChanged(object sender, EventArgs e)
  324. {
  325. int.TryParse(ts_PageSize.SelectedItem.ToString(), out pageSize);
  326. }
  327. #endregion
  328. private void btnExportCurrent_Click(object sender, EventArgs e)
  329. {
  330. SaveFileDialog dialog = new SaveFileDialog
  331. {
  332. Title = "保存csv文件",
  333. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  334. FilterIndex = 1
  335. };
  336. if (dialog.ShowDialog() == DialogResult.OK)
  337. {
  338. string fileName = dialog.FileName;
  339. if (File.Exists(fileName))
  340. {
  341. File.Delete(fileName);
  342. }
  343. using (StreamWriter sw = new StreamWriter(fileName,false, Encoding.UTF8))
  344. {
  345. sw.WriteLine("时间,设备,报警信息,级别,报警类型");
  346. if (bindings != null && bindings.Count > 0)
  347. {
  348. foreach(AlertInfo info in bindings)
  349. {
  350. sw.WriteLine(String.Format("{0},{1},{2},{3},{4}", info.AlertTime.ToString("yyyy-MM-dd HH:mm:ss"),
  351. info.ObjectName, info.AlertText, info.RankName, info.CategoryName));
  352. }
  353. }
  354. }
  355. }
  356. else
  357. {
  358. MessageBox.Show("未保存", "提示", MessageBoxButtons.OK);
  359. }
  360. }
  361. private void btnExportAll_Click(object sender, EventArgs e)
  362. {
  363. SaveFileDialog dialog = new SaveFileDialog
  364. {
  365. Title = "保存csv文件",
  366. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  367. FilterIndex = 1
  368. };
  369. if (dialog.ShowDialog() == DialogResult.OK)
  370. {
  371. string fileName = dialog.FileName;
  372. if (File.Exists(fileName))
  373. {
  374. File.Delete(fileName);
  375. }
  376. frmWaitingBox f = new frmWaitingBox((obj, args) =>
  377. {
  378. using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
  379. {
  380. sw.WriteLine("时间,设备,报警信息,级别,报警类型");
  381. if (total <= 0) return;
  382. #region 分页查询并导出
  383. int exportPageNum = (int)Math.Ceiling((double)(total / exportPageSize));
  384. string filter = CreateFilter();
  385. for (int i = 0; i < exportPageNum;)
  386. {
  387. var alerts = asc.GetAlertInfosPage(filter, ++i, exportPageSize, ref total);
  388. foreach (AlertInfo info in alerts)
  389. {
  390. sw.WriteLine(String.Format("{0},{1},{2},{3},{4}", info.AlertTime.ToString("yyyy-MM-dd HH:mm:ss"),
  391. info.ObjectName, info.AlertText, info.RankName, info.CategoryName));
  392. }
  393. }
  394. #endregion
  395. }
  396. }, 120, "正在加载数据,请稍后...", false, false);
  397. f.ShowDialog(this);
  398. }
  399. else
  400. {
  401. MessageBox.Show("未保存", "提示", MessageBoxButtons.OK);
  402. }
  403. }
  404. private void btnClose_Click(object sender, EventArgs e)
  405. {
  406. this.Hide();
  407. }
  408. private void btnMax_Click(object sender, EventArgs e)
  409. {
  410. if (this.WindowState == FormWindowState.Maximized)
  411. this.WindowState = FormWindowState.Normal;
  412. else
  413. this.WindowState = FormWindowState.Maximized;
  414. }
  415. private void panel1_MouseDown(object sender, MouseEventArgs e)
  416. {
  417. ReleaseCapture();
  418. SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  419. }
  420. private void FrmAlarmHistory_Load(object sender, EventArgs e)
  421. {
  422. pageCurrent = 1;
  423. LoadData();
  424. updatePageControls();
  425. }
  426. private void dgvAlarm_MouseEnter(object sender, EventArgs e)
  427. {
  428. if (isLoading == false)
  429. this.Cursor = System.Windows.Forms.Cursors.Arrow;
  430. }
  431. private void dgvAlarm_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
  432. {
  433. if (e.RowIndex > -1)
  434. {
  435. try
  436. {
  437. string objectId = dgvAlarm.Rows[e.RowIndex].Cells["ObjectId"].Value.ToString();
  438. if (objectId.IndexOf("01") > 0)
  439. {
  440. NavService.Instance.NavForWindturbine("pictureBoxParamters", objectId, getStationId2(objectId));
  441. }
  442. }
  443. catch { }
  444. }
  445. }
  446. private string getStationId2(string wtId)
  447. {
  448. if (wtId.StartsWith("MG"))
  449. return "MHS_FDC";
  450. if (wtId.StartsWith("NG"))
  451. return "NSS_FDC";
  452. if (wtId.StartsWith("SG"))
  453. return "SBQ_FDC";
  454. if (wtId.StartsWith("XG"))
  455. return "XS_FDC";
  456. if (wtId.StartsWith("QG"))
  457. return "QS_FDC";
  458. return "SBQ_FDC";
  459. }
  460. }
  461. }