using GDNXFD.Data; using IntelligentControlForsx.Service; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IntelligentControlForsx { public partial class FrmAlarmHistory : Form { [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private bool isReady = false; private bool isLoading = false; private AlertServiceClient asc = null; string station = string.Empty; string rank = string.Empty; string category = string.Empty; string category2 = string.Empty; string keyWords = string.Empty; DateTime endDt; DateTime startDt; //定义分页需要的变量 int total =0; int pageNum = 1; int pageCurrent = 1; int pageSize = 20; int exportPageSize = 1000; private BindingList bindings; public FrmAlarmHistory() { InitializeComponent(); this.dgvAlarm.AutoGenerateColumns = false; this.dgvAlarm.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleCenter; this.dgvAlarm.DefaultCellStyle = dataGridViewCellStyle1; endDt = DateTime.Now; startDt = endDt.AddDays(-1); dpEnd.Value = endDt; dpEnd.MinDate = startDt; dpStart.Value = startDt; dpStart.MaxDate = endDt; this.ts_PageSize.SelectedIndex = ts_PageSize.Items.IndexOf("20"); //LoadData(); //updatePageControls(); isReady = true; } #region 事件处理 private void cmbStation_SelectedValueChanged(object sender, EventArgs e) { if ("全部".Equals(cmbStation.SelectedItem) == false) { station = getStationId(cmbStation.SelectedItem.ToString()); } else station = string.Empty; } private string getStationId(string stName) { if ("石板泉风电场".Equals(stName)) return "SBQ_FDC"; if ("麻黄山风电场".Equals(stName)) return "MHS_FDC"; if ("牛首山风电场".Equals(stName)) return "NSS_FDC"; if ("青山风电场".Equals(stName)) return "QS_FDC"; if ("香山风电场".Equals(stName)) return "XS_FDC"; return ""; } private void cmbRank_SelectedValueChanged(object sender, EventArgs e) { if ("全部".Equals(cmbRank.SelectedItem) == false) { try { IList lst1 = ClientCache.Instance.GetDataDictByCategory("AlertLevel"); rank = lst1.Where(q => q.Name == cmbRank.SelectedItem.ToString()).First().Code; } catch { rank = string.Empty; } } else rank = string.Empty; } private void cmbCategory_SelectedValueChanged(object sender, EventArgs e) { if ("全部".Equals(cmbCategory.SelectedItem) == false) { try { IList lst1 = ClientCache.Instance.GetDataDictByCategory("AlertCategory"); category = lst1.Where(q => q.Name == cmbCategory.SelectedItem.ToString()).First().Code; } catch { category = string.Empty; } } else category = string.Empty; ResetCmbSubCategory(); } private void ResetCmbSubCategory() { this.cmbSubCategory.Items.Clear(); this.cmbSubCategory.Items.Add("全部"); if (!string.IsNullOrWhiteSpace(category)) { try { IList lst = ClientCache.Instance.GetDataDictByCategory(category); if (lst != null) { foreach (DictItem dItem in lst) { this.cmbSubCategory.Items.Add(dItem.Name); } } } catch (Exception ex) { } } this.cmbSubCategory.SelectedIndex = cmbSubCategory.Items.IndexOf("全部"); } private void cmbSubCategory_SelectedValueChanged(object sender, EventArgs e) { if ("全部".Equals(cmbSubCategory.SelectedItem) == false) { try { IList lst1 = ClientCache.Instance.GetDataDictByCategory(category); category2 = lst1.Where(q => q.Name == cmbSubCategory.SelectedItem.ToString()).First().Code; } catch { category2 = string.Empty; } } else category2 = string.Empty; } private void txtKeyWord_TextChanged(object sender, EventArgs e) { keyWords = txtKeyWord.Text.Trim(); } private void btnQuery_Click(object sender, EventArgs e) { pageCurrent = 1; LoadData(); updatePageControls(); } #endregion #region 分页查询 public void LoadData() { updatePageControls(); // 查询按钮点击后,按钮文本由"查询"改为"加载中",并把按钮状态改为不可点击 //btnQuery.Text = " 加载中..."; //pnlQuery.Enabled = false; //this.Cursor = System.Windows.Forms.Cursors.WaitCursor; try { frmWaitingBox f = new frmWaitingBox((obj, args) => { if (asc == null) asc = AlertServiceClientFactory.CreateAlertServiceClient(); string filter = CreateFilter(); var alerts = asc.GetAlertInfosPage(filter, pageCurrent, pageSize, ref total); if (alerts != null) { bindings = new BindingList(alerts); } }, 60, "正在加载数据,请稍后...", false, false); f.ShowDialog(this); this.dgvAlarm.DataSource = bindings; pageNum = (int)Math.Ceiling((double)(total / pageSize)); ts_Pages.Text = pageNum + ""; //this.btnQuery.Text = " 开始查询"; //// 使用匿名方法定义一个委托事件,委托事件主要操作数据库查询,并返回查询的结果,不涉及任何UI操作 //Func> act = () => //{ // if (asc == null) // asc = AlertServiceClientFactory.CreateAlertServiceClient(); // string filter = CreateFilter(); // var alerts = asc.GetAlertInfosPage(filter, pageCurrent, pageSize, ref total); // return alerts; //}; //// 异步调用委托事件,在委托事件回调方法中,使用任意控件的异步方法把查询结果绑定到数据表控件中 //act.BeginInvoke((result) => //{ // IList dtResult = act.EndInvoke(result); // // this是当前WinForm窗口的实例,也可以替换为界面中的任意控件示例,如dgvMain.BeginInvoke // this.BeginInvoke(new Action>((IList dtList) => // { // if (dtList != null) // { // bindings = new BindingList(dtList); // this.dgvAlarm.DataSource = bindings; // } // pageNum = (int)Math.Ceiling((double)(total / pageSize)); // ts_Pages.Text = pageNum + ""; // this.btnQuery.Text = " 开始查询"; // isLoading = false; // this.Cursor = System.Windows.Forms.Cursors.Arrow; // }), dtResult); //}, null); } catch (Exception ex) { MessageBox.Show(ex.Message); //logger.Error("查询历史报警时发生异常:" + ex.Message); }finally { //isLoading = false; } } private void updatePageControls() { if (pageCurrent == 1) { ts_PagePrevious.Enabled = false; ts_PageFirst.Enabled = false; } else { ts_PagePrevious.Enabled = true; ts_PageFirst.Enabled = true; } if (pageNum == pageCurrent) { ts_PageNext.Enabled = false; ts_PageLast.Enabled = false; } else { ts_PageNext.Enabled = true; ts_PageLast.Enabled = true; } ts_PagePostion.Text = pageCurrent.ToString(); ts_PagePostion.Enabled = true; ts_PagePostion.ReadOnly = false; ts_Pages.Text = pageNum.ToString(); } private string CreateFilter() { return String.Format("{0},{1},{2},{3},{4},{5},{6}", startDt.ToString("yyyy-MM-dd HH:mm:ss"), endDt.ToString("yyyy-MM-dd HH:mm:ss"), station, rank, category, category2, keyWords); } private void ts_PagePrevious_Click(object sender, EventArgs e) { //ts_PageNext.Enabled = true; //if (pageCurrent == 1) //{ // ts_PagePrevious.Enabled = false; // MessageBox.Show("已经是第一页了"); // return; //} pageCurrent--; LoadData(); } private void ts_PageNext_Click(object sender, EventArgs e) { //ts_PagePrevious.Enabled = true; //if (pageCurrent == pageSize) //{ // ts_PageNext.Enabled = false; // MessageBox.Show("已经是最后一页了"); // return; //} pageCurrent++; LoadData(); } private void ts_PageFirst_Click(object sender, EventArgs e) { pageCurrent = 1; //ts_PageFirst.Enabled = false; //ts_PageLast.Enabled = true; //ts_PagePrevious.Enabled = false; //ts_PageNext.Enabled = true; LoadData(); } private void ts_PageLast_Click(object sender, EventArgs e) { pageCurrent = pageNum; //ts_PageFirst.Enabled = true; //ts_PageLast.Enabled = false; //ts_PagePrevious.Enabled = true; //ts_PageNext.Enabled = false; LoadData(); } private void ts_PagePostion_TextChanged(object sender, EventArgs e) { if (isReady == false) return; if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= pageNum) { pageCurrent = Convert.ToInt32(ts_PagePostion.Text); } else { pageCurrent = 1; } LoadData(); } private void ts_PageSize_SelectedIndexChanged(object sender, EventArgs e) { int.TryParse(ts_PageSize.SelectedItem.ToString(), out pageSize); } #endregion private void btnExportCurrent_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog { Title = "保存csv文件", Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*", FilterIndex = 1 }; if (dialog.ShowDialog() == DialogResult.OK) { string fileName = dialog.FileName; if (File.Exists(fileName)) { File.Delete(fileName); } using (StreamWriter sw = new StreamWriter(fileName,false, Encoding.UTF8)) { sw.WriteLine("时间,设备,报警信息,级别,报警类型"); if (bindings != null && bindings.Count > 0) { foreach(AlertInfo info in bindings) { sw.WriteLine(String.Format("{0},{1},{2},{3},{4}", info.AlertTime.ToString("yyyy-MM-dd HH:mm:ss"), info.ObjectName, info.AlertText, info.RankName, info.CategoryName)); } } } } else { MessageBox.Show("未保存", "提示", MessageBoxButtons.OK); } } private void btnExportAll_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog { Title = "保存csv文件", Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*", FilterIndex = 1 }; if (dialog.ShowDialog() == DialogResult.OK) { string fileName = dialog.FileName; if (File.Exists(fileName)) { File.Delete(fileName); } frmWaitingBox f = new frmWaitingBox((obj, args) => { using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8)) { sw.WriteLine("时间,设备,报警信息,级别,报警类型"); if (total <= 0) return; #region 分页查询并导出 int exportPageNum = (int)Math.Ceiling((double)(total / exportPageSize)); string filter = CreateFilter(); for (int i = 0; i < exportPageNum;) { var alerts = asc.GetAlertInfosPage(filter, ++i, exportPageSize, ref total); foreach (AlertInfo info in alerts) { sw.WriteLine(String.Format("{0},{1},{2},{3},{4}", info.AlertTime.ToString("yyyy-MM-dd HH:mm:ss"), info.ObjectName, info.AlertText, info.RankName, info.CategoryName)); } } #endregion } }, 120, "正在加载数据,请稍后...", false, false); f.ShowDialog(this); } else { MessageBox.Show("未保存", "提示", MessageBoxButtons.OK); } } private void btnClose_Click(object sender, EventArgs e) { this.Hide(); } private void btnMax_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal; else this.WindowState = FormWindowState.Maximized; } private void panel1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } private void FrmAlarmHistory_Load(object sender, EventArgs e) { pageCurrent = 1; LoadData(); updatePageControls(); } private void dgvAlarm_MouseEnter(object sender, EventArgs e) { if (isLoading == false) this.Cursor = System.Windows.Forms.Cursors.Arrow; } private void dgvAlarm_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { try { string objectId = dgvAlarm.Rows[e.RowIndex].Cells["ObjectId"].Value.ToString(); if (objectId.IndexOf("01") > 0) { NavService.Instance.NavForWindturbine("pictureBoxParamters", objectId, getStationId2(objectId)); } } catch { } } } private string getStationId2(string wtId) { if (wtId.StartsWith("MG")) return "MHS_FDC"; if (wtId.StartsWith("NG")) return "NSS_FDC"; if (wtId.StartsWith("SG")) return "SBQ_FDC"; if (wtId.StartsWith("XG")) return "XS_FDC"; if (wtId.StartsWith("QG")) return "QS_FDC"; return "SBQ_FDC"; } } }