using HslCommunication; using HslCommunication.Profinet.Melsec; using Microsoft.VisualBasic.FileIO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WFormReadData_SMR { public partial class ReadyDataForm : Form { public DBHelper oCN = new DBHelper(); public ReadyDataForm() { InitializeComponent(); } public static int num = 0; //读取数据 private void btnReadData_Click(object sender, EventArgs e) { if (this.HEmpCode.Text == "") { MessageBox.Show("请输入员工编码"); } else { if (num == 0) { this.btnReadData.Text = "暂停"; this.timer1.Enabled = true; num = 1; } else if (num == 1) { this.btnReadData.Text = "启动"; this.timer1.Enabled = false; num = 0; } } } //员工编码回车 private void HEmpCode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { SelectHEmpCode(this.HEmpCode.Text); } } //查询员工 public void SelectHEmpCode(string HNumber) { try { DataSet ds = oCN.RunProcReturn("select * from Gy_Employee where HNumber='" + HNumber + "' ", "Gy_Employee"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("查无数据!"); } else { string HEmpCodeName = ds.Tables[0].Rows[0]["HName"].ToString(); string HEmpCodeNumber = ds.Tables[0].Rows[0]["HNumber"].ToString(); this.HEmpCodeName.Text = HEmpCodeName; this.HEmpCode.Text = HEmpCodeNumber; this.HEmpCode.ReadOnly = true; } } catch (Exception e) { MessageBox.Show(this, e.Message, "提示"); } } //更换员工 private void btnSelect_Click(object sender, EventArgs e) { this.HEmpCode.ReadOnly = false; this.HEmpCode.Focus(); this.HEmpCode.Text = ""; this.HEmpCodeName.Text = ""; this.HSouce.ReadOnly = false; this.HSouce.Focus(); this.HSouce.Text = ""; this.HSouceName.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { if (this.HEmpCode.Text == "") { MessageBox.Show("请输入员工编码"); } else { Read_Txt(); } } //文本读取数据 public void Read_Txt() { int year = DateTime.Now.Year; string month = DateTime.Now.Month.ToString(); string day = DateTime.Now.Day.ToString(); string dataTime = DateTime.Now.ToString("yyyyMMdd"); ////判断是否有这个文件 bool flag = File.Exists($@"D:\{year}\{month}\{day}\{dataTime}.txt"); //bool flag = File.Exists($@"C:\Users\admin\Desktop\新建文件夹 (2)\20241128.txt"); if (flag) { StreamReader stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}.txt", Encoding.GetEncoding("gb2312")); //StreamReader stream = new StreamReader($@"C:\Users\admin\Desktop\新建文件夹 (2)\20241128.txt", Encoding.GetEncoding("gb2312")); string FileData = stream.ReadToEnd(); stream.Close(); //获取条码信息 Xt_ReadText(FileData); bool flag2 = File.Exists($@"D:\{year}\{month}\{day}\{dataTime}gy.txt"); //bool flag2 = File.Exists($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123gy.txt"); if (flag2) { stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}gy.txt", Encoding.GetEncoding("gb2312")); //stream = new StreamReader($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123gy.txt", Encoding.GetEncoding("gb2312")); FileData = stream.ReadToEnd(); //获取系统参数 Xt_Gy_RoutBill(FileData); } } } //生产资源回车 private void HSouce_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { SelectHSouc(this.HSouce.Text); } } //查询生产资源 public void SelectHSouc(string HNumber) { try { DataSet ds = oCN.RunProcReturn("select * from Gy_Source where HNumber='" + HNumber + "' ", "Gy_Source"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("查无数据!"); } else { string HSouceName = ds.Tables[0].Rows[0]["HName"].ToString(); string HSouceNumber = ds.Tables[0].Rows[0]["HNumber"].ToString(); this.HSouceName.Text = HSouceName; this.HSouce.Text = HSouceNumber; this.HSouce.ReadOnly = true; } } catch (Exception e) { MessageBox.Show(this, e.Message, "提示"); } } public void Xt_ReadText(string FileData) { FileData = FileData.Substring(0, FileData.Length - 1); FileData = "[" + FileData + "]"; //JSON序列化转换字典集合 List> list = new List>(); List DataList = JsonConvert.DeserializeObject>(FileData); foreach (JObject item in DataList) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } //获取当前时间 DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-4).ToString("yyyy-MM-dd HH:mm:ss")); DateTime EndTime = DateTime.Parse(DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:mm:00")); bool flag_1 = false; bool flag_2 = false; //循环集合 for (int i = 0; i < list.Count; i++) { DateTime NowTime = DateTime.Parse(list[i]["HDate"].ToString()); if (NowTime >= ActionTime && NowTime < EndTime && list[i]["HBadCodeSN"].ToString() != "") { string HSourceCode = list[i]["HSouceNumber"].ToString(); string HEmpCode = this.HEmpCode.Text; //string HEmpCode = ""; string HType = list[i]["HBadReason"].ToString(); string HBarCode = list[i]["HBadCodeSN"].ToString(); int HCount = 1; string HCreateTime = list[i]["HDate"].ToString(); string HDate = DateTime.Parse(list[i]["HDate"].ToString()).ToString("yyyy-MM-dd"); string HResult = list[i]["HResult"].ToString(); string HProcNumber = list[i]["HProcNumber"].ToString(); int HFlag = 0; DataSet ds = oCN.RunProcReturn(@"select * from Sb_EquipMentCollection_SN where HBarCode='" + HBarCode + "' and HCreateTime='" + HCreateTime + "'", "Sb_EquipMentCollection_SN"); if (ds.Tables[0].Rows.Count == 0) { string HProjectNum = HBarCode.Substring(18, 8); DataSet dataSet = oCN.RunProcReturn("select HBillNo from Sc_ProcessExchangeBillMain where HProjectNum='" + HProjectNum + "-1'", "Sc_ProcessExchangeBillMain"); if (dataSet.Tables[0].Rows.Count > 0) { string HBillNo = dataSet.Tables[0].Rows[0]["HBillNo"].ToString(); dataSet = oCN.RunProcReturn("select * from Sc_ICMOBillStatus_Tmp where HSourceBillNo='" + HBillNo + "' and HICMOStatus<>'0'", "Sc_ICMOBillStatus_Tmp"); if (dataSet.Tables[0].Rows.Count > 0) { if (Get_AllowLoadData(HBarCode, HProcNumber, flag_1, out flag_2)) { string sql = $@"insert into Sb_EquipMentCollection_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag) values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount}','{HCreateTime}','{HDate}','{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); ListSelect.Items.Add("条码:" + HBarCode + ",当前日期;"+DateTime.Now.ToString()+",日期:" + HCreateTime + ",结果:" + HResult); } else { flag_1 = flag_2; } } else { if (!flag_1) { MessageBox.Show(new Form { TopMost = true }, "条码:" + HBarCode + ",对应的流转卡:" + HBillNo + ",流转卡未开工!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",对应的流转卡:" + HBillNo + ",流转卡未开工!", DateTime.Now.ToString("yyyy-MM-dd")); flag_1 = true; } } else { if (!flag_1) { MessageBox.Show(new Form { TopMost = true }, "条码:" + HBarCode + ",流转卡不存在!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",流转卡不存在!", DateTime.Now.ToString("yyyy-MM-dd")); flag_1 = true; } } } else { //CustomWriteLog("时间不在保存范围内", DateTime.Now.ToString("yyyy-MM-dd")); } } } //读取系统参数 public void Xt_Gy_RoutBill(string FileData) { FileData = FileData.Substring(0, FileData.Length - 1); FileData = "[" + FileData + "]"; //JSON序列化转换字典集合 List> list = new List>(); List DataList = JsonConvert.DeserializeObject>(FileData); foreach (JObject item in DataList) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } //获取当前时间 DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")); DateTime EndTime = DateTime.Parse(DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:mm:00")); //循环集合 for (int i = 0; i < list.Count; i++) { DateTime NowTime = DateTime.Parse(list[i]["HDate"].ToString()); if (NowTime >= ActionTime && NowTime < EndTime) { string HSourceCode = list[i]["HSouceNumber"].ToString(); string HEmpCode = this.HEmpCode.Text; //string HEmpCode = ""; string HType = list[i]["HType"].ToString(); string HBarCode = list[i]["HBadCodeSN"].ToString(); double HCount = double.Parse(list[i]["HCount"].ToString()); string HCreateTime = list[i]["HDate"].ToString(); string HDate = DateTime.Parse(list[i]["HDate"].ToString()).ToString("yyyy-MM-dd"); string HResult = list[i]["HResult"].ToString(); string HProcNumber = list[i]["HProcNumber"].ToString(); int HFlag = 0; DataSet ds = oCN.RunProcReturn(@"select * from Sb_EquipMentCollectionTechParam_SN where HBarCode='" + HBarCode + "' and HCreateTime='" + HCreateTime + "'", "Sb_EquipMentCollectionTechParam_SN"); if (ds.Tables[0].Rows.Count == 0) { string sql = $@"insert into Sb_EquipMentCollectionTechParam_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag) values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount}','{HCreateTime}','{HDate}','{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); //ListSelect.Items.Add("条码:" + HBarCode + ",日期:" + HCreateTime + ",结果:" + HResult); } } else { //CustomWriteLog("时间不在保存范围内", DateTime.Now.ToString("yyyy-MM-dd")); } } } //根据条码 判断是否保存 public bool Get_AllowLoadData(string HBarCode, string HProcNumber, bool flag_1, out bool flag_2) { if (HBarCode.Length != 29 && HBarCode.Length != 50) { flag_2 = false; return false; } //判断长度是否为29位 无尘车间 string HProcExchBillNo = ""; string HProcExchInterID = ""; if (HBarCode.Length == 29) { string str1 = HBarCode.Substring(18, 8); string str2 = HBarCode.Substring(23, 3); DataSet ds = oCN.RunProcReturn("select HInterID,HBillNo from Sc_ProcessExchangeBillMain where HProjectNum like'" + str1 + "-1%' or HProjectNum like'" + str2 + "-1%' order by HMakeDate desc", "Sc_ProcessExchangeBillMain"); //判断是否能找到对应的流转卡 if (ds.Tables[0].Rows.Count > 0) { HProcExchBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString(); HProcExchInterID = ds.Tables[0].Rows[0]["HInterID"].ToString(); } else { if (!flag_1) { MessageBox.Show(new Form { TopMost = true }, "条码:" + HBarCode + ",流转卡不存在!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",流转卡不存在!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } } //判断长度是否为50位 15车间 else if (HBarCode.Length == 50) { string str1 = HBarCode.Substring(42, 8); DataSet ds = oCN.RunProcReturn("select HBillNo from Sc_ProcessExchangeBillMain where HProjectNum like'" + str1 + "-1%'", "Sc_ProcessExchangeBillMain"); //判断是否能找到对应的流转卡 if (ds.Tables[0].Rows.Count > 0) { HProcExchBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString(); } else { if (!flag_1) { MessageBox.Show("条码:" + HBarCode + ",流转卡不存在!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",流转卡不存在!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } } //过站控制 DataSet ds1 = oCN.RunProcReturn(@"select b.HProcNo,b.HProcID,p.HName from Sc_ProcessExchangeBillMain a WITH(NOLOCK) inner join Sc_ProcessExchangeBillSub b WITH(NOLOCK) on a.HInterID=b.HInterID left join Gy_Process p WITH(NOLOCK) on b.HProcID=p.HItemID where a.HBillNo='" + HProcExchBillNo + "' and p.HNumber='" + HProcNumber + "'", "Sc_ProcessExchangeBillMain"); string HProcID = "0"; if (ds1.Tables[0].Rows.Count > 0) { HProcID = ds1.Tables[0].Rows[0]["HProcID"].ToString(); string HProName = ds1.Tables[0].Rows[0]["HName"].ToString(); ds1 = oCN.RunProcReturn("exec h_p_Sc_SNBarcodeProcCtrl_S '" + HBarCode + "'," + HProcID, "h_v_Gy_QualifiedRecordsList"); if (ds1.Tables[0].Rows.Count == 0) { if (!flag_1) { MessageBox.Show("条码:" + HBarCode + "工序:" + HProName + ",工序控制查无数据!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + "工序:" + HProName + ",工序控制查无数据!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } else if (ds1.Tables[0].Rows[0]["HBack"].ToString() == "2") { if (!flag_1) { MessageBox.Show("条码:" + HBarCode + "工序:" + HProcID + "," + ds1.Tables[0].Rows[0]["HBackRemark"].ToString() + "!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + "工序:" + HProcID + "," + ds1.Tables[0].Rows[0]["HBackRemark"].ToString() + "!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } } //查询出站数量是否超过流转卡数量 ds1 = oCN.RunProcReturn($@"select (b.HQty -sum(isnull(ou.HQty,0))-sum(isnull(ou.HBadCount,0))) HQty from Sc_ProcessExchangeBillSub b WITH(NOLOCK) left join Sc_StationOutBillMain ou WITH(NOLOCK) on b.HInterID=ou.HProcExchInterID and b.HEntryID=ou.HProcExchEntryID where b.HInterID={HProcExchInterID} and b.HProcID={HProcID} group by b.HInterID,b.HEntryID,b.HQty", "Sc_ProcessExchangeBill_Out"); if (double.Parse(ds1.Tables[0].Rows[0][0].ToString()) < 0) { if (!flag_1) { MessageBox.Show("流转卡:" + HProcExchBillNo + ",出站数量超过流转卡数量!"); } DBHelper.CustomWriteLog("流转卡:" + HProcExchBillNo + ",出站数量超过流转卡数量!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } //增加产线组装追溯单 DataSet dataSet = oCN.RunProcReturn("exec h_p_Gy_BarCodeBillBomList '" + HProcExchBillNo + "'," + HProcID, "h_p_Gy_BarCodeBillBomList"); if (dataSet.Tables[0].Rows.Count > 0) { //判配件数量是否等于0 for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++) { double SYHQty = double.Parse(dataSet.Tables[0].Rows[i]["配件数量"].ToString()); string HMaterNamePJ = dataSet.Tables[0].Rows[i]["配件代码"].ToString(); string HMaterBarCode = dataSet.Tables[0].Rows[i]["HBarCode"].ToString(); if (SYHQty == 0) { if (!flag_1) { MessageBox.Show("流转卡:" + HProcExchBillNo + ",配件条码:" + HMaterBarCode + ",配件代码:" + HMaterNamePJ + ",数量为0!"); } DBHelper.CustomWriteLog("流转卡:" + HProcExchBillNo + ",配件条码:" + HMaterBarCode + ",配件代码:" + HMaterNamePJ + ",数量为0!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } } } flag_2 = false; return true; } private void ReadyDataForm_FormClosing(object sender, FormClosingEventArgs e) { if (num == 1) { MessageBox.Show("当前按钮未暂停,不允许关闭!"); e.Cancel = true; } else if (MessageBox.Show("确定要关闭吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.No) { e.Cancel = true; } } } }