using JY.DAL; using JY.Inspection.Enums; using JY.Inspection.Mes; using JY.MES; using JY.MES.Entity; using JY.MES.Enum; using JY.MES.MES; using JY.Model; using JY.Model.Enums; using JY.Utility; using JYControl; using MiniExcelLibs; using Newtonsoft.Json; using OfficeOpenXml; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Threading; using System.Windows.Forms; namespace JY.Inspection.Frm { public partial class FrmHistoricalDataQuery : MetroFramework.Forms.MetroForm { public delegate void myDelegate(DataTable t); public delegate void PDelegate(); Thread tSo; //int type = 0;//查询类型 //int resulttype = 0;//结果类型 DataTable dts = null; /// /// 用于电芯进站时设备与FMS校验电芯合法性 /// string CheckBarCodeInUrl = ""; /// /// 用于电芯出站时向FMS上报出站及生产数据 /// string BarCodeOutUrl = ""; /// /// MES上传验证码 /// string authorization = ""; /// /// 数据库访问接口 /// private IDbHelper dbHelper = new OpSqlDataBase(); public FrmHistoricalDataQuery() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; InitCombobox(); } private void InitCombobox() { #region 初始化查询类型列表 var selectItems = new List() { new ComboItem() { Value = (int)EnumSearchType.ArrivalStation, Text = EnumSearchType.ArrivalStation.GetDescription() }, new ComboItem() { Value = (int)EnumSearchType.ExitStation, Text = EnumSearchType.ExitStation.GetDescription() }, //new ComboItem() { // Value = (int)EnumSearchType.ProductResult, // Text = EnumSearchType.ProductResult.GetDescription() //} }; cbSelectType.DataSource = selectItems; cbSelectType.DisplayMember = "Text"; cbSelectType.ValueMember = "Value"; #endregion #region 初始化结果类型列表 var resultItems = new List() { new ComboItem() { Value = (int)EnumResultType.All, Text = EnumResultType.All.GetDescription() }, new ComboItem() { Value = (int)EnumResultType.OK, Text = EnumResultType.OK.GetDescription() }, new ComboItem() { Value = (int)EnumResultType.NG, Text = EnumResultType.NG.GetDescription() } }; resultSelectType.DataSource = resultItems; resultSelectType.DisplayMember = "Text"; resultSelectType.ValueMember = "Value"; #endregion #region 初始化上传类型列表 var upMesStatusItems = new List() { new ComboItem() { Value = (int)EnumUpMesStatus.All, Text = EnumUpMesStatus.All.GetDescription() }, new ComboItem() { Value = (int)EnumUpMesStatus.Auto, Text = EnumUpMesStatus.Auto.GetDescription() }, new ComboItem() { Value = (int)EnumUpMesStatus.Manual, Text = EnumUpMesStatus.Manual.GetDescription() }, new ComboItem() { Value = (int)EnumUpMesStatus.None, Text = EnumUpMesStatus.None.GetDescription() } }; cmbFlag.DataSource = upMesStatusItems; cmbFlag.DisplayMember = "Text"; cmbFlag.ValueMember = "Value"; #endregion } #region 属性 public EnumSearchType CurSearchType { get { EnumSearchType curEnum = EnumSearchType.ArrivalStation; if (this.InvokeRequired) { this.Invoke(new Action(() => { var curItem = cbSelectType.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); })); } else { var curItem = cbSelectType.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); } return curEnum; } } public EnumResultType CurResultType { get { EnumResultType curEnum = EnumResultType.All; if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { var curItem = resultSelectType.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); })); } else { var curItem = resultSelectType.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); } return curEnum; } } public EnumUpMesStatus CurUpMesStatus { get { EnumUpMesStatus curEnum = EnumUpMesStatus.All; if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { var curItem = cmbFlag.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); })); } else { var curItem = cmbFlag.SelectedItem as ComboItem; curEnum = curItem.Value.ToEnum(); } return curEnum; } } #endregion /// /// 窗体加载事件 /// /// /// private void FrmHistoricalDataQuery_Load(object sender, EventArgs e) { cbSelectType.SelectedIndex = 0; resultSelectType.SelectedIndex = 0; cmbFlag.SelectedIndex = 0; dtStartTime.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm")); dtEndTime.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm")); //为dgv添加复选框列 DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn(); //列显示名称 checkbox.HeaderText = "选择"; checkbox.Name = "IsChecked"; checkbox.TrueValue = true; checkbox.FalseValue = false; checkbox.DataPropertyName = "IsChecked"; //列宽 checkbox.Width = 30; //列大小不改变 checkbox.Resizable = DataGridViewTriState.False; //添加的checkbox在dgv的第一列 this.dgvData.Columns.Insert(0, checkbox); } private void btnSelect_Click(object sender, EventArgs e) { try { tSo = new Thread(new ThreadStart(ThreadWork)); tSo.IsBackground = true; tSo.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString() + ",数据查询失败", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } this.dgvData.DataSource = null; //type = cbSelectType.SelectedIndex; //resulttype = resultSelectType.SelectedIndex; } private void ThreadWork() { lblSelectStures.BeginInvoke(new PDelegate(SearchBegin)); string strDate1 = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm") + ":01"; string strDate2 = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm") + ":01"; if (dtEndTime.Value.Year != dtStartTime.Value.Year) { MessageBox.Show("请选择日期必须在同一年份内!"); lblSelectStures.BeginInvoke(new PDelegate(SearchEnd)); return; } string flag = cmbFlag.Text; string strBarcode = txtBarCode.Text.Trim(); // var dt = dbHelper.GetTestData(type, strBarcode, strDate1, strDate2, flag); int type = (int)CurSearchType; int resultType = (int)CurResultType; var dt = dbHelper.GetTestData2(type, resultType, strBarcode, strDate1, strDate2, flag); if (dt == null || dt.Rows.Count == 0) { MessageBox.Show("此时间段无数据或无此条码数据", "系统提示"); lblSelectStures.BeginInvoke(new PDelegate(SearchEnd)); return; } this.dgvData.BeginInvoke(new myDelegate(FillData), new object[] { dt });//异步调用(来填充) lblSelectStures.BeginInvoke(new PDelegate(SearchEnd)); } private void FillData(DataTable dt) { dts = dt; this.dgvData.DataSource = dt.DefaultView; } private void SearchBegin() { this.lblSelectStures.Text = "正在查询数据..."; } private void SearchEnd() { this.lblSelectStures.Text = "查询结束"; } private void btnExcel_Click(object sender, EventArgs e) { if (dts != null && dgvData.Columns.Count <= 0) { MessageBox.Show("请先查询数据,再进行导出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx"; saveFileDialog.FilterIndex = 1; //保存对话框是否记忆上次打开的目录 saveFileDialog.RestoreDirectory = true; //设置默认的文件名 saveFileDialog.DefaultExt = "xlsx"; //saveFileDialog.DefaultFileName = "查询结果" + DateTime.Now.ToString("yyyyMMddHHmmss"); var dialogResult = saveFileDialog.ShowDialog(this); if (dialogResult == DialogResult.OK) { //string filter = saveFileDialog.FileName.Substring(saveFileDialog.FileName.LastIndexOf(".") + 1); try { MiniExcel.SaveAs(saveFileDialog.FileName, dts); //ExcelPackage.License.SetNonCommercialOrganization("My Noncommercial organization"); //EPPlusExcelHelper excelHelper = new EPPlusExcelHelper(saveFileDialog.FileName); //excelHelper.ExportDataTable("sheet1", dts); Thread.Sleep(50); MessageBox.Show("数据导出成功!"); } catch (Exception ex) { MessageBox.Show("保存的EXCEL已有数据,无法覆盖,重新创建" + ex.Message); } //if (MessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) //{ // System.Diagnostics.Process.Start(saveFileDialog.FileName); //} } } private void dgvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //自动编号,与数据无关 Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvData.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvData.RowHeadersDefaultCellStyle.Font, rectangle, dgvData.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } /// /// 上传MES按钮设置 /// /// /// private void btnUpMES_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); if (dgvData.Columns.Count <= 1) { MessageBox.Show("请先查询数据,再进行上传MES操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { if (cbSelectType.Text == "查询进站数据")//上料 { foreach (DataGridViewRow row in dgvData.Rows) { if (Convert.ToBoolean(row.Cells[0].Value)) { if (Convert.ToInt32(row.Cells["状态"].Value) == 1 || Convert.ToInt32(row.Cells["状态"].Value) == 2) { MessageBox.Show("勾选电芯数据已上传", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { JY.MES.FMS.FMS_In fMS_In = new MES.FMS.FMS_In(); //fMS_In.systemCode = Global.systemConfig.systemCode; //fMS_In.houseCode = Global.systemConfig.houseCode; //fMS_In.skuCode = Convert.ToString(row.Cells["条码"].Value); //fMS_In.deviceCode = Global.systemConfig.deviceCode; //fMS_In.processCode = Global.systemConfig.processCode; string strJosn = JsonConvert.SerializeObject(fMS_In); LogManagerControl.AddLog($"PC->FMS[电芯出站]:{strJosn}", LogAddtype.MES); var result = JY.MES.FMS.MesHelper_EVE.MES_Inbound(fMS_In); string strRes = "statusCode:" + "[" + result.statusCode + "]" + "statusMessage" + "[" + result.statusMessage + "]"; LogManagerControl.AddLog($"FMS->PC[电芯出站]:{strRes}", LogAddtype.MES); string NowTime = DateTime.Now.ToString("yyyy - MM - dd HH: mm: ss"); var result1 = @" UPDATE FeedingData set Flag=2,UploadMESTime='" + NowTime + "' where[ID]=" + Convert.ToString(row.Cells["唯一ID"].Value); //var result2 = @" UPDATE FeedingData set UploadMESTime='" + NowTime + "' where[ID]='" + Convert.ToString(row.Cells["唯一ID"].Value) + "'"; DataTable dt = SqlHelper.QueryTable(result1); } } } } else//下料 { // TODO 结果数据上传 sb.Clear(); int i = 0; foreach (DataGridViewRow row in dgvData.Rows) { i++; if (Convert.ToBoolean(row.Cells[0].Value)) { if (Convert.ToInt32(row.Cells["状态"].Value) == 1 || Convert.ToInt32(row.Cells["状态"].Value) == 2) { sb.AppendLine($"第{i}行数据: 勾选电芯数据已上传"); continue; } else { // 获取出站数据上传MES BlankingData data = GetBlankingData(row); if (data == null) { sb.AppendLine($"第{i}行数据: 获取出站数据失败, 跳过该数据"); continue; } MesCallResult resMes = new MESDataCombin().ProductResultParameters(data); if (!resMes.IsSuccess) { if (resMes.ErrorType == MesErrorType.NetworkError || resMes.ErrorType == MesErrorType.NetworkTimeout) { sb.AppendLine($"第{i}行数据: 结果数据上传MES超时或无法访问MES服务器"); LogManagerControl.AddLog("结果数据上传MES超时或无法访问MES服务器", LogAddtype.local, Logtype.Error); } else { string errMsg = resMes.OriResponse == null ? resMes.ErrorMessage : resMes.OriResponse.ToString(); sb.AppendLine($"第{i}行数据: 结果数据上传MES失败NG,{errMsg}"); LogManagerControl.AddLog("结果数据上传MES失败NG," + errMsg, LogAddtype.local, Logtype.Error); } } } } } } } catch(Exception ex) { MessageBox.Show($"手动上传MES失败: {ex.ToString()}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (sb.Length > 0) { // 弹出错误信息 MessageBox.Show(sb.ToString(), "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("手动上传MES成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private BlankingData GetBlankingData(DataGridViewRow row) { // 获取BlankingData BlankingData data = new BlankingData(); data.ID = Convert.ToInt32(row.Cells["ID"].Value); data.TD = Convert.ToInt32(row.Cells["序号"].Value); data.WorkShift = row.Cells["班次"].Value?.ToString(); data.TDGroup = Convert.ToInt32(row.Cells["主道"].Value); data.ArrivalBarCode = row.Cells["入站条码"].Value?.ToString(); data.DepartureBarCode = row.Cells["出站条码"].Value?.ToString(); data.TMDB = row.Cells["条码对比"].Value?.ToString(); data.OutTime = row.Cells["出站时间"].Value?.ToString(); data.CCD1 = row.Cells["正面(2D/3D)"].Value?.ToString(); data.CCD2 = row.Cells["反面(2D/3D)"].Value?.ToString(); data.CCD3 = row.Cells["左侧面(2D/3D)"].Value?.ToString(); data.CCD4 = row.Cells["右侧面(2D/3D)"].Value?.ToString(); data.CCD5 = row.Cells["顶面(2D/3D)"].Value?.ToString(); data.CCD6 = row.Cells["底面(2D/3D)"].Value?.ToString(); data.CCD7 = row.Cells["底WE1"].Value?.ToString(); data.CCD8 = row.Cells["底WE2"].Value?.ToString(); data.CCD9 = row.Cells["底WE3"].Value?.ToString(); data.CCD10 = row.Cells["底WE4"].Value?.ToString(); data.CCD11 = row.Cells["中ME1"].Value?.ToString(); data.CCD12 = row.Cells["中ME2"].Value?.ToString(); data.CCD13 = row.Cells["中ME3"].Value?.ToString(); data.CCD14 = row.Cells["中ME4"].Value?.ToString(); data.CCD15 = row.Cells["极柱(POS/NEG)"].Value?.ToString(); data.CCD16 = row.Cells["防爆阀(PRO)"].Value?.ToString(); data.Result = row.Cells["综合结果"].Value?.ToString(); data.Remark = row.Cells["备注"].Value?.ToString(); data.Flag = Convert.ToInt32(row.Cells["状态"].Value); return data; } /// /// dgvCellMouseClick鼠标点击列事件 /// /// /// private void dgvData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { //不是序号列和标题列时才执行 if (e.RowIndex != -1 && e.ColumnIndex != -1) { //checkbox勾上 if ((bool)dgvData.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true) { //选中改为不选中 this.dgvData.Rows[e.RowIndex].Cells[0].Value = false; } else { //不选中改为选中 this.dgvData.Rows[e.RowIndex].Cells[0].Value = true; } } } private class ComboItem { /// /// 实际值 /// public int Value { get; set; } /// /// 显示值 /// public string Text { get; set; } } private void cbSelectType_SelectedIndexChanged(object sender, EventArgs e) { if (CurSearchType == EnumSearchType.ExitStation) { btnUpMES.Visible = true; } else { btnUpMES.Visible = false; } } } }