using System; using System.Data; using System.IO; using System.Threading; using System.Windows.Forms; using JY.DAL; using JY.Utility; using System.Drawing; using Newtonsoft.Json; using JYControl; using JY.MES.Entity; using JY.MES; using OfficeOpenXml; using MiniExcelLibs; 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; } /// /// 窗体加载事件 /// /// /// 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(aa)); 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(bb)); return; } string flag = cmbFlag.Text; string strBarcode = txtBarCode.Text.Trim(); // var dt = dbHelper.GetTestData(type, strBarcode, strDate1, strDate2, flag); var dt = dbHelper.GetTestData2(type,resulttype, strBarcode, strDate1, strDate2, flag); if (dt == null || dt.Rows.Count == 0) { MessageBox.Show("此时间段无数据或无此条码数据", "系统提示"); lblSelectStures.BeginInvoke(new PDelegate(bb)); return; } this.dgvData.BeginInvoke(new myDelegate(FillData), new object[] { dt });//异步调用(来填充) lblSelectStures.BeginInvoke(new PDelegate(bb)); } private void FillData(DataTable dt) { dts = dt; this.dgvData.DataSource = dt.DefaultView; } private void aa() { this.lblSelectStures.Text = "正在查询数据..."; } private void bb() { 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) { 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//下料 { 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_Out fMS_Out = new MES.FMS.FMS_Out(); //fMS_In.deviceCode = Global.systemConfig.eqp_code; //fMS_In.houseCode = "test11"; //fMS_In.skuCode = m.BarCode; //fMS_In.systemCode = "win10"; //fMS_In.processCode = Global.systemConfig.seq_code; //fMS_In.ngCode = ""; //fMS_Out.systemCode = Global.systemConfig.systemCode; //fMS_Out.houseCode = Global.systemConfig.houseCode; //fMS_Out.skuCode = Convert.ToString(row.Cells["条码"].Value); //fMS_Out.deviceCode = Global.systemConfig.deviceCode; //fMS_Out.processCode = Global.systemConfig.processCode; fMS_Out.testResult = "NG"; fMS_Out.ngCode = "0"; JY.MES.FMS.processData ProData = new JY.MES.FMS.processData(); ProData.inTime = Convert.ToString(row.Cells["进站时间"].Value); ProData.outTime = Convert.ToString(row.Cells["进站时间"].Value); ProData.cspdjg = Convert.ToString(row.Cells["综合结果"].Value); ProData.cssj = ""; ProData.cspc = ""; ProData.csjg = Convert.ToString(row.Cells["综合结果"].Value); ProData.fcbj = ""; ProData.fccs = ""; ProData.fcsj = ""; ProData.fcyy = ""; ProData.ngyy = Convert.ToString(row.Cells["备注"].Value); ProData.ngsj = ""; ProData.ngwz = ""; ProData.hjwd = ""; ProData.hjsd = ""; ProData.kqjjd = ""; ProData.workShift = Convert.ToString(row.Cells["班次"].Value); fMS_Out.processData = ProData; string strJosn = JsonConvert.SerializeObject(fMS_Out); LogManagerControl.AddLog($"PC->FMS[电芯出站]:{strJosn}", LogAddtype.MES); var result = JY.MES.FMS.MesHelper_EVE.MES_Outbound(fMS_Out); 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 BlankingData set Flag=2,strUploadMESTime='" + NowTime + "' where[ID]=" + Convert.ToInt32(row.Cells["唯一ID"].Value); //var result2 = @" UPDATE BlankingData set UploadMESTime='" + NowTime + "' where[ID]='" + Convert.ToString(row.Cells["唯一ID"].Value) + "'"; DataTable dt = SqlHelper.QueryTable(result1); } } } } } catch { MessageBox.Show("手动上传MES失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MessageBox.Show("手动上传MES成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } /// /// 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; } } } } }