Files
1440WGJ/JY.Inspection/Frm/FrmHistoricalDataQuery.cs
T

536 lines
22 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
using System.Collections.Generic;
using JY.Inspection.Enums;
using JY.Model.Enums;
using JY.Model;
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;
/// <summary>
/// 用于电芯进站时设备与FMS校验电芯合法性
/// </summary>
string CheckBarCodeInUrl = "";
/// <summary>
/// 用于电芯出站时向FMS上报出站及生产数据
/// </summary>
string BarCodeOutUrl = "";
/// <summary>
/// MES上传验证码
/// </summary>
string authorization = "";
/// <summary>
/// 数据库访问接口
/// </summary>
private IDbHelper dbHelper = new OpSqlDataBase();
public FrmHistoricalDataQuery()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
InitCombobox();
}
private void InitCombobox()
{
#region 初始化查询类型列表
var selectItems = new List<ComboItem>()
{
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<ComboItem>()
{
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<ComboItem>()
{
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.BeginInvoke(new Action(() => {
var curItem = cbSelectType.SelectedItem as ComboItem;
curEnum = curItem.Value.ToEnum<EnumSearchType>();
}));
}
else
{
var curItem = cbSelectType.SelectedItem as ComboItem;
curEnum = curItem.Value.ToEnum<EnumSearchType>();
}
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<EnumResultType>();
}));
}
else
{
var curItem = resultSelectType.SelectedItem as ComboItem;
curEnum = curItem.Value.ToEnum<EnumResultType>();
}
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<EnumUpMesStatus>();
}));
}
else
{
var curItem = cmbFlag.SelectedItem as ComboItem;
curEnum = curItem.Value.ToEnum<EnumUpMesStatus>();
}
return curEnum;
}
}
#endregion
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
/// <summary>
/// 上传MES按钮设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<object>.QueryTable(result1);
}
}
}
}
else//下料
{
// TODO 结果数据上传
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
{
// TODO 获取出站数据上传MES
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<object>.QueryTable(result1);
}
}
}
}
}
catch
{
MessageBox.Show("手动上传MES失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MessageBox.Show("手动上传MES成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private BlankingData GetBlankingData(DataGridViewRow row)
{
// TODO 获取BlankingData
return null;
}
/// <summary>
/// dgvCellMouseClick鼠标点击列事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
{
/// <summary>
/// 实际值
/// </summary>
public int Value { get; set; }
/// <summary>
/// 显示值
/// </summary>
public string Text { get; set; }
}
private void cbSelectType_SelectedIndexChanged(object sender, EventArgs e)
{
if (CurSearchType == EnumSearchType.ExitStation)
{
btnUpMES.Visible = true;
}
else
{
btnUpMES.Visible = false;
}
}
}
}