using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; using DevExpress.XtraEditors; using ProjectBase.Controls; using SIMDP.BLL; using SIMDP.Model; using DevExpress.XtraNavBar; using ProjectBase.Data.BaseDAL; using ProjectBase.Data.Logs; using ProjectBase.Controls.DocViewer; using System.IO; using System.Timers; using SIMDP.Project; namespace SIMDP { public partial class FormDataTraceability : BaseDock { private DataTable table = null; // 数据规则解析表 private string condition = ""; // 查询条件 private string selectRuleName = ""; // 选中的规则名称 private string batchid = null; private delegate void winFormDelegate(DataTable table, string condition); private List> listModel = new List>(); public FormDataTraceability() { InitializeComponent(); InitModelList(); } private void FormDataManage_Load(object sender, EventArgs e) { //初始化分页表格控件 table = new DataTable(); InitNavBarControl(); this.panelControl1.Visible = false; InitData(); this.splitContainerControl1.SplitterPosition = 1; } private void InitModelList() { List listUserPara = BLLFactory.Instance.GetAll(); foreach (MoUserParameter mo in listUserPara) { if (mo.UserParaUnit != "model") continue; listModel.Add(new KeyValuePair(mo.UserParaValue, mo.UserParaName)); } } /// /// 初始化导航栏 /// private void InitNavBarControl() { List ruleList = new List(); ruleList = BLLFactory.Instance.GetAll(); if (ruleList == null || ruleList.Count < 1) { MessageBox.Show("请先添加数据规则!"); } foreach (MoRule mo in ruleList) { NavBarItem item = new NavBarItem(); item.Visible = true; item.SmallImage = Image.FromFile(Application.StartupPath + "/Images/101.png"); item.Name = mo.RuleId.ToString(); item.Caption = mo.RuleName; item.LinkClicked += Item_Click; this.navBarControl.Items.AddRange(new DevExpress.XtraNavBar.NavBarItem[] { item }); this.nbDataHistory.ItemLinks.AddRange(new DevExpress.XtraNavBar.NavBarItemLink[] { new DevExpress.XtraNavBar.NavBarItemLink(item) }); } if (this.nbDataHistory.ItemLinks.Count > 0) { this.navBarControl.SelectedLink = this.nbDataHistory.ItemLinks[0]; } } /// /// 初始化右侧控件 /// private void InitData() { MoRule mo = new MoRule(); mo = BLLFactory.Instance.FindByID(this.navBarControl.SelectedLink.ItemName); table.Clear(); table = BLLFactory.Instance.AnalyzeRuleValue(mo); InitPanel(); selectRuleName = this.navBarControl.SelectedLink.ItemName; condition = string.Format(" rule_id = '{0}'", selectRuleName); string startTime = string.Format("{0} {1}", this.dateEditStart.Text, this.timeEditStart.Text); string endTime = string.Format("{0} {1}", this.dateEditEnd.Text, this.timeEditEnd.Text); condition += string.Format(" and rule_time >= '{0}' and rule_time <= '{1}'", startTime, endTime); if (!string.IsNullOrEmpty(txt_batchid.Text)) { condition += string.Format(" and batchid like '%{0}%'", txt_batchid.Text); } BindData(table, condition); } /// /// 导航栏子项目的点击事件 /// private void Item_Click(object sender, NavBarLinkEventArgs e) { MoRule mo = new MoRule(); mo = BLLFactory.Instance.FindByID(e.Link.ItemName); table.Clear(); table = BLLFactory.Instance.AnalyzeRuleValue(mo); InitPanel(); selectRuleName = e.Link.ItemName; condition = string.Format(" rule_id = '{0}'", selectRuleName); string startTime = string.Format("{0} {1}", this.dateEditStart.Text, this.timeEditStart.Text); string endTime = string.Format("{0} {1}", this.dateEditEnd.Text, this.timeEditEnd.Text); condition += string.Format(" and rule_time >= '{0}' and rule_time <= '{1}'", startTime, endTime); if (!string.IsNullOrEmpty(txt_batchid.Text)) { condition += string.Format(" and batchid like '%{0}%'", txt_batchid.Text); } BindData(table, condition); } /// /// 初始化panel /// private void InitPanel() { this.panelControl1.Visible = true; this.dateEditStart.EditValue = DateTime.Today; this.dateEditEnd.EditValue = DateTime.Today; this.timeEditStart.EditValue = DateTime.Parse("00:00:00"); this.timeEditEnd.EditValue = DateTime.Parse("23:59:59"); } /// /// 按条件查询内容 /// /// /// private void SbtnSelect_Click(object sender, EventArgs e) { condition = string.Format(" rule_id = '{0}'", selectRuleName); string startTime = string.Format("{0} {1}", this.dateEditStart.Text, this.timeEditStart.Text); string endTime = string.Format("{0} {1}", this.dateEditEnd.Text, this.timeEditEnd.Text); condition += string.Format(" and rule_time >= '{0}' and rule_time <= '{1}'", startTime, endTime); if (!string.IsNullOrEmpty(txt_batchid.Text)) { condition += string.Format(" and batchid like '%{0}%'", txt_batchid.Text); } BindData(table, condition); } /// /// 页面切换 /// /// /// private void winGridViewPager1_OnPageChanged(object sender, EventArgs e) { BindData(table, condition); } /// /// 绑定产品数据 /// /// 需要查询的某一规则列表 /// 输入的查询语句 private void BindData(DataTable table, string condition) { try { this.gridView1.Columns.Clear(); List productList = new List(); productList = BLLFactory.Instance.Find(condition); DataTable dtProduct = new DataTable(); int columns = 0; dtProduct.Columns.Add("更新时间", typeof(DateTime)); dtProduct.Columns.Add("VIN码", typeof(string)); foreach (DataRow item in table.Rows) { dtProduct.Columns.Add(item.Field("Name"), typeof(string)); } for (int i = 0; i < productList.Count; i++) { string time = productList[i].RuleTime.ToString("yyyy-MM-dd HH:mm:ss"); batchid = productList[i].Batchid; string[] values = productList[i].DataValue.Split(','); if (table.Rows.Count != values.Count()) { continue; } List tempList = new List(); tempList = values.ToList(); tempList.Insert(0, batchid); tempList.Insert(0, time); dtProduct.Rows.Add(tempList.ToArray()); } //按时间降序排列 dtProduct.DefaultView.Sort = "更新时间 DESC"; dtProduct = dtProduct.DefaultView.ToTable(); this.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; this.gridControl1.DataSource = dtProduct; for (int i = 0; i < this.gridView1.Columns.Count; i++) //设置每列内容居中显示 { this.gridView1.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //设置时间列显示格式 this.gridView1.Columns[0].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; this.gridView1.Columns[0].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime; } foreach (DataRow item in table.Rows) { if (item.Field("Query").Equals("是")) { gridView1.Columns[columns].OptionsFilter.AllowAutoFilter = true; gridView1.Columns[columns].OptionsFilter.AllowFilter = true; gridView1.Columns[columns].OptionsFilter.ImmediateUpdateAutoFilter = true; } else { gridView1.Columns[columns].OptionsFilter.AllowAutoFilter = false; gridView1.Columns[columns].OptionsFilter.AllowFilter = false; gridView1.Columns[columns].OptionsFilter.ImmediateUpdateAutoFilter = false; } columns++; } //1129 this.gridView1.Columns[0].MinWidth = 165; this.gridView1.Columns[1].MinWidth = 155; this.gridView1.Columns[0].BestFit();//自动列宽 this.gridView1.Columns[1].BestFit();//自动列宽 this.gridControl1.RefreshDataSource(); } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("绑定产品数据出现错误。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("查询数据库product_data出现错误:{0}", ex)); } } /// /// 过滤界面数据 /// /// /// /// /// private bool FilterData(DataTable table, string[] values, out string[] result) { result = null; foreach (DataRow item in table.Rows) { if (item.Field("Name").Equals("日期")) { int pos = Convert.ToInt32(item.Field("OrderNum")); DateTime dateValue = Convert.ToDateTime(values[pos]); DateTime dateStart = Convert.ToDateTime(this.dateEditStart.EditValue.ToString()); DateTime dateEnd = Convert.ToDateTime(this.dateEditEnd.EditValue.ToString()); if (!((0 >= dateValue.CompareTo(dateStart)) && (0 <= dateValue.CompareTo(dateEnd)))) { return false; } } if (item.Field("Name").Equals("时间")) { int pos = item.Field("OrderNum"); DateTime timeValue = Convert.ToDateTime(values[pos]); DateTime timeStart = Convert.ToDateTime(this.timeEditStart.EditValue.ToString()); DateTime timeEnd = Convert.ToDateTime(this.timeEditEnd.EditValue.ToString()); if (!((0 >= timeValue.CompareTo(timeStart)) && (0 < timeValue.CompareTo(timeEnd)))) { return false; } } } result = values; return true; } private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle > -1) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } private void btn_ExportData_Click(object sender, EventArgs e) { string dir = System.Environment.CurrentDirectory; SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Excel(*.xls)|*.xls|All File(*.*)|*.*"; dialog.Title = "保存Excel"; dialog.FileName = ""; dialog.RestoreDirectory = true; if (!string.IsNullOrEmpty(dir)) { dialog.InitialDirectory = dir; } if (dialog.ShowDialog() == DialogResult.OK) { try { DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(); this.gridControl1.ExportToXls(dialog.FileName); DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show("保存Excel文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); LogHelper.log.Error(string.Format("保存Excel文件失败,错误为{0}", ex.ToString())); } } } /// /// 定时刷新 /// private void Refresh_Elapsed(object sender, EventArgs e) { if (!this.IsActive) return; if (!this.chk_Refresh.Checked) return; if (this.InvokeRequired) { winFormDelegate mes = new winFormDelegate(BindData); this.Invoke(mes, new object[] { table, condition }); } else { BindData(table, condition); } } private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { } private void dateEditStart_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) SbtnSelect_Click(sender, e); } private void dateEditEnd_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) SbtnSelect_Click(sender, e); } private void gridView_PLC_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.FieldName == "产品型号") { e.DisplayText = (listModel.FirstOrDefault(p => p.Key == e.Value.ToString().Trim()).Value == null) ? e.Value.ToString() : listModel.FirstOrDefault(p => p.Key == e.Value.ToString().Trim()).Value; } } } }