using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Drawing; using System.IO; using System.Web.UI.WebControls; using System.Windows.Forms; using ZJ_BYD.Common; using ZJ_BYD.DB; using ZJ_BYD.Untils; using ZJ_BYD.ViewModel; namespace ZJ_BYD { public partial class SearchResult : XtraForm { private int pagesize = 30; //每页显示行数 private int totalCount = 0; //总记录数 private int pageCurrent = 1; //当前页号 public SearchResult() { InitializeComponent(); Top = 0; Left = 0; Width = Screen.PrimaryScreen.WorkingArea.Width; Height = Screen.PrimaryScreen.WorkingArea.Height; gridView1.CustomDrawRowIndicator += GridView1_CustomDrawRowIndicator; gridPage1.williamPagerEvent += GridPage1_williamPagerEvent; gridView1.IndicatorWidth = 35; dtBegin.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); dtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd"); } private void SearchResult_Load(object sender, EventArgs e) { var logoFileName = ConfigurationManager.AppSettings["logofilename"]; var imgPath = Path.Combine(Application.StartupPath, "image/" + logoFileName); loginpic.Image = System.Drawing.Image.FromFile(imgPath); BindCombox.BindSystemMode(cmbMode); BindCombox.BindIsOk(cmbIsUpload); } private void GetData(int pageCurrent = 1) { var list = DataSource(); this.gridResult.DataSource = list; gridPage1.RefreshPager(pagesize, totalCount, pageCurrent); } /// /// 数据源 /// /// /// private List DataSource(bool isPage = true) { try { var selectedItem = cmbMode.SelectedItem as ListItem; ListItem isOkSelectedItem = cmbIsUpload.SelectedItem as ListItem; string sqlCol = ""; var beginParseResult = DateTime.TryParse(this.dtBegin.Text, out var dtBeginDate); var endParseResult = DateTime.TryParse(string.Format("{0} 23:59:59", this.dtEnd.Text), out var dtEndDate); if (!beginParseResult || !endParseResult) { MessageBox.Show("时间格式有误,请重新设置"); return null; } else if (dtBeginDate > dtEndDate) { MessageBox.Show("开始时间不能大于结束时间,请重新设置"); return null; } #region 查询条件 var strWhere = @" WHERE t_result.linecode=@linecode and t_result.stationcode=@stationcode and t_result.createdtime>=@begindate AND t_result.createdtime<=@enddate"; var paras = new List { new SugarParameter("@linecode", Program.CurrentLineCode), new SugarParameter("@stationcode", Program.ActiveStatinCode), new SugarParameter("@begindate", dtBeginDate), new SugarParameter("@enddate", dtEndDate) }; if (!string.IsNullOrWhiteSpace(txtkeyword.Text.Trim())) { strWhere += @" and (t_result.stationcode like @keyword or stationname like @keyword or productsfccode like @keyword or barcode like @keyword)"; paras.Add(new SugarParameter("@keyword", string.Format("%{0}%", txtkeyword.Text.Trim()))); } if (selectedItem != null) { var selectedVal = selectedItem.Value != "0"; strWhere += @" and t_result.sysmode=@selectedval"; paras.Add(new SugarParameter("@selectedval", selectedVal)); } if (isOkSelectedItem != null) { var selectedVal = selectedItem.Value != "0"; strWhere += @" and t_result.isuploaded=@isokselectedval"; paras.Add(new SugarParameter("@isokselectedval", selectedVal)); } #endregion //查询点位信息 var plcPoints = PlcPointHelper.QueryActiveCustomPlcPoints(Program.CurrentIpcId, Program.CurrentLineCode, Program.ActiveStatinCode).ToList(); string sql; if (plcPoints.Count > 0) { int visibleIndex = 14; foreach (var item in plcPoints) { #region 动态添加列 GridColumn column = new GridColumn(); column.AppearanceCell.Font = new Font("微软雅黑", 12F); column.AppearanceCell.Options.UseFont = true; column.AppearanceCell.ForeColor = Color.FromArgb(((int)(((byte)(82)))), ((int)(((byte)(215)))), ((int)(((byte)(246))))); column.AppearanceCell.Options.UseForeColor = true; column.AppearanceCell.BackColor = Color.FromArgb(((int)(((byte)(9)))), ((int)(((byte)(28)))), ((int)(((byte)(77))))); column.AppearanceCell.Options.UseBackColor = true; column.AppearanceHeader.BackColor = Color.FromArgb(((int)(((byte)(9)))), ((int)(((byte)(28)))), ((int)(((byte)(77))))); column.AppearanceHeader.Font = new Font("微软雅黑", 12F); column.AppearanceHeader.Options.UseBackColor = true; column.AppearanceHeader.ForeColor = Color.White; column.AppearanceHeader.Options.UseFont = true; column.Caption = item.PointName; column.FieldName = item.ResultField; column.MinWidth = 120; column.Name = item.ResultField; column.Visible = true; column.VisibleIndex = visibleIndex; column.Width = 120; column.OptionsColumn.AllowEdit = false; column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False; column.OptionsColumn.AllowMove = false; column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False; column.OptionsFilter.AllowFilter = false; sqlCol += $"coalesce({item.ResultField},'') as \"{item.ResultField}\","; var col = gridView1.Columns.ColumnByName(item.ResultField); if (col == null) { gridView1.Columns.Add(column); } #endregion visibleIndex++; } sql = string.Format(@"select {0} t_result.createdtime as CreatedTime,t_result.stationcode as StationCode ,stationname as StationName,loginuser as LoginUser,sysmode as SysMode ,case sysmode when true then '在线' else '离线' end as StrSysMode ,productsfccode as ProductSfcCode,totalstatus as TotalStatus,bulkparts as BulkParts ,isuploaded as IsUploaded ,case isuploaded when true then '是' else '否' end as StrIsUpload ,uploadmsg as UploadMsg,barcode as BarCode,t_result.category as Category from t_result left join t_station on t_result.linecode=t_station.linecode and t_result.stationcode=t_station.stationcode and t_result.category=t_station.category {1} ORDER BY t_result.createdtime DESC", sqlCol, strWhere); } else { sql = string.Format(@"select t_result.createdtime as CreatedTime,t_result.stationcode as StationCode ,stationname as StationName,loginuser as LoginUser,sysmode as SysMode ,case sysmode when true then '在线' else '离线' end as StrSysMode ,productsfccode as ProductSfcCode,totalstatus as TotalStatus,bulkparts as BulkParts ,isuploaded as IsUploaded ,case isuploaded when true then '是' else '否' end as StrIsUpload ,uploadmsg as UploadMsg,barcode as BarCode,t_result.category as Category from t_result left join t_station on t_result.linecode=t_station.linecode and t_result.stationcode=t_station.stationcode and t_result.category=t_station.category {0} ORDER BY t_result.createdtime DESC", strWhere); } if (isPage) { var data = ResultHelper.QueryBySql(sql, paras.ToArray()).ToPageList(pageCurrent, pagesize, ref totalCount); return data; } else { var data = ResultHelper.QueryBySql(sql, paras.ToArray()).ToList(); return data; } } catch (Exception ex) { var msg = ex == null ? "执行SearchResult中DataSource方法时异常" : ex.Message; LogHelper.WriteLog($"执行SearchResult中DataSource方法时异常:{msg}"); return null; } } /// /// 点击查询按钮 /// /// /// private void btnsearch_Click(object sender, EventArgs e) { SplashScreenManager.ShowWaitForm(); GetData(); SplashScreenManager.CloseWaitForm(); } /// /// 点击导出按钮 /// /// /// private void btnexport_Click(object sender, EventArgs e) { try { FolderBrowserDialog path = new FolderBrowserDialog(); if (path.ShowDialog() == DialogResult.OK) { SplashScreenManager.ShowWaitForm(); var list = DataSource(false); var filePath = string.Format("{0}\\{1}.xlsx", path.SelectedPath, DateTime.Now.ToString("yyyyMMddHHmmss")); #region 设置列名称 var mappingColumns = new List(); var co1 = new ExcelColumns { field = "CreatedTime", title = "创建时间" }; var co2 = new ExcelColumns { field = "StrIsUpload", title = "是否已上传" }; var co3 = new ExcelColumns { field = "UploadMsg", title = "上传结果" }; var co4 = new ExcelColumns { field = "StrSysMode", title = "模式" }; var co5 = new ExcelColumns { field = "LoginUser", title = "用户" }; var co6 = new ExcelColumns { field = "StationCode", title = "工位编码" }; var co7 = new ExcelColumns { field = "StationName", title = "工位名称" }; var co8 = new ExcelColumns { field = "ProductSfcCode", title = "主条码" }; var co9 = new ExcelColumns { field = "TotalStatus", title = "总状态" }; var co10 = new ExcelColumns { field = "BulkParts", title = "散件条码" }; var co11 = new ExcelColumns { field = "Category", title = "分类" }; var co12 = new ExcelColumns { field = "BarCode", title = "打印条码" }; mappingColumns.Add(co1); mappingColumns.Add(co2); mappingColumns.Add(co3); mappingColumns.Add(co4); mappingColumns.Add(co5); mappingColumns.Add(co6); mappingColumns.Add(co7); mappingColumns.Add(co8); mappingColumns.Add(co9); mappingColumns.Add(co10); mappingColumns.Add(co11); mappingColumns.Add(co12); //查询点位信息 var plcPoints = PlcPointHelper.QueryActiveCustomPlcPoints(Program.CurrentIpcId, Program.CurrentLineCode, Program.ActiveStatinCode).ToList(); foreach (var item in plcPoints) { var column = new ExcelColumns { field = item.ResultField, title = item.PointName }; mappingColumns.Add(column); } #endregion if (list == null || list.Count <= 0) { SplashScreenManager.CloseWaitForm(); XtraMessageBox.Show("无数据!", "提示"); return; } ExcelHelper.ToExcel(list, filePath, mappingColumns, true); SplashScreenManager.CloseWaitForm(); XtraMessageBox.Show("导出成功!", "提示"); } } catch (Exception ex) { SplashScreenManager.CloseWaitForm(); XtraMessageBox.Show("导出数据异常!", "提示"); LogHelper.WriteErrorLog("导出数据异常!", ex); } } private void lblClose_Click(object sender, EventArgs e) { this.Close(); } private void picminimize_Click(object sender, EventArgs e) { //this.WindowState = FormWindowState.Minimized; } /// /// 分页事件 /// /// /// private void GridPage1_williamPagerEvent(int curPage, int pageSize) { pageCurrent = curPage; pagesize = pageSize; GetData(curPage); } /// /// 行号 /// /// /// 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(); } } } }