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_New : Form { public DBHelper oCN = new DBHelper(); private List empHistoryList = new List(); private List sourceHistoryList = new List(); public ReadyDataForm_New() { InitializeComponent(); this.Load += ReadyDataForm_New_Load; // 挂载加载事件 } private void ReadyDataForm_New_Load(object sender, EventArgs e) { // 加载员工 var empResult = HistoryConfigHelper.LoadEmployees(); empHistoryList = empResult.Items; BindComboBox(HEmpCode, empHistoryList, empResult.LastCode, HEmpCodeName); // 加载生产资源 var srcResult = HistoryConfigHelper.LoadSources(); sourceHistoryList = srcResult.Items; BindComboBox(HSouce, sourceHistoryList, srcResult.LastCode, HSouceName); } private void BindComboBox(ComboBox combo, List list, string lastCode, TextBox nameBox) { combo.DataSource = null; combo.DisplayMember = "Code"; // 只显示代码 combo.ValueMember = "Code"; combo.DataSource = list; if (!string.IsNullOrEmpty(lastCode)) { var item = list.FirstOrDefault(x => x.Code == lastCode); if (item != null) { combo.SelectedItem = item; nameBox.Text = item.Name; // 更新名称文本框 } } else if (list.Count > 0) { combo.SelectedIndex = combo.Items.Count - 1; // 默认最后一项 var lastItem = list[list.Count - 1]; nameBox.Text = lastItem.Name; // 更新名称文本框 } else { nameBox.Text = ""; // 清空名称 } } // 辅助方法:获取当前选中的代码(用于读取文本时) private string GetSelectedCode(ComboBox combo) { if (combo.SelectedValue != null && combo.SelectedValue is string code) return code; // 手动输入时,直接取 Text(假定输入的是纯代码) string text = combo.Text.Trim(); // 如果意外包含了“ - ”分隔符,则提取代码部分 int idx = text.IndexOf(" - "); if (idx > 0) return text.Substring(0, idx).Trim(); return text; } //添加 ComboBox 选中项改变事件 private void HEmpCode_SelectedIndexChanged(object sender, EventArgs e) { if (HEmpCode.SelectedIndex >= 0 && HEmpCode.SelectedIndex < empHistoryList.Count) { HEmpCodeName.Text = empHistoryList[HEmpCode.SelectedIndex].Name; } else { HEmpCodeName.Text = ""; } } private void HSouce_SelectedIndexChanged(object sender, EventArgs e) { if (HSouce.SelectedIndex >= 0 && HSouce.SelectedIndex < sourceHistoryList.Count) { HSouceName.Text = sourceHistoryList[HSouce.SelectedIndex].Name; } else { HSouceName.Text = ""; } } public static int num = 0; public static int SelectTime = 0; public static DateTime time = DateTime.Now.AddMinutes(-5); public static decimal hqty = 2; public static string AllBeginWork = "-1"; //是否开工 public static string AllProcessExchange = "-1"; //流转卡项目号 public static string AllProcessExchangeHProcExchBillNo = ""; //流转卡号 public static string AllProcessExchangeHProcExchInterID = ""; //流转卡主码 public static string AllSNBarcodeProcCtrl = ""; //流转卡+工序的拼写 public static string AllHProcID = "0"; //工序id public static string AllHProName = ""; //工序名字 public static double AllHQty = 0; //默认流转卡数量 如果出站单有数据会更新成出站单数量 public static Dictionary RemainingQtyCache = new Dictionary(); // 缓存每个流转卡+工序的剩余数量 public static Dictionary CacheUpdateTime = new Dictionary(); // 记录每次缓存更新的时间,用于判断是否需要刷新 public string CurrentActiveKey = "";//缓存键,用于判断流转卡是否切换 // 新增静态变量:记录每个项目号的开工状态 public static Dictionary ProjectBeginWorkStatus = new Dictionary(); //镭雕拍照工序文本 过站读取 新的 //读取数据 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; SelectTime = -4; } 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) { string inputCode = HEmpCode.Text.Trim(); if (string.IsNullOrEmpty(inputCode)) { MessageBox.Show("请输入员工编码"); return; } SelectHEmpCode(inputCode); } } //查询员工 public void SelectHEmpCode(string HNumber) { try { DataSet ds = oCN.RunProcReturn("select HName,HNumber from Gy_Employee WITH(NOLOCK) where HNumber='" + HNumber + "'", "Gy_Employee"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("查无数据!"); } else { string name = ds.Tables[0].Rows[0]["HName"].ToString(); string code = ds.Tables[0].Rows[0]["HNumber"].ToString(); // 更新历史列表 HistoryConfigHelper.AddOrUpdateItem(empHistoryList, code, name); // 重新绑定下拉框,并传入名称文本框 BindComboBox(HEmpCode, empHistoryList, code, HEmpCodeName); // 注意第四个参数 // 保存到配置文件 HistoryConfigHelper.SaveEmployees(empHistoryList, code); } } catch (Exception e) { MessageBox.Show(this, e.Message, "提示"); } } //更换员工 private void btnSelect_Click(object sender, EventArgs e) { // 清空员工 HEmpCode.SelectedIndex = -1; HEmpCode.Text = ""; HEmpCodeName.Text = ""; HEmpCode.Focus(); // 清空生产资源 HSouce.SelectedIndex = -1; HSouce.Text = ""; HSouceName.Text = ""; } //生产资源回车 private void HSouce_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { string inputCode = HSouce.Text.Trim(); if (string.IsNullOrEmpty(inputCode)) { MessageBox.Show("请输入生产资源编码"); return; } SelectHSouc(inputCode); } } public void SelectHSouc(string HNumber) { try { DataSet ds = oCN.RunProcReturn("select HName,HNumber from Gy_Source WITH(NOLOCK) where HNumber='" + HNumber + "'", "Gy_Source"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("查无数据!"); } else { string name = ds.Tables[0].Rows[0]["HName"].ToString(); string code = ds.Tables[0].Rows[0]["HNumber"].ToString(); HistoryConfigHelper.AddOrUpdateItem(sourceHistoryList, code, name); BindComboBox(HSouce, sourceHistoryList, code, HSouceName); // 传入名称文本框 HistoryConfigHelper.SaveSources(sourceHistoryList, code); } } catch (Exception e) { MessageBox.Show(this, e.Message, "提示"); } } private void timer1_Tick(object sender, EventArgs e) { if (this.HEmpCode.Text == "") { MessageBox.Show("请输入员工编码"); } else { Read_Txt(); } } public static List listData = new List(); //文本读取数据 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"); if (flag) { StreamReader stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}.txt", Encoding.GetEncoding("gb2312")); string FileData = stream.ReadToEnd(); stream.Close(); //获取条码信息 Xt_ReadText(FileData); bool flag2 = File.Exists($@"D:\{year}\{month}\{day}\{dataTime}gy.txt"); if (flag2) { stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}gy.txt", Encoding.GetEncoding("gb2312")); FileData = stream.ReadToEnd(); //获取工艺参数 Xt_Gy_RoutBill(FileData); } } } //读取文本条码 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(SelectTime).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++) { string HBarCode = list[i]["HBadCodeSN"].ToString(); DateTime NowTime = DateTime.Parse(list[i]["HDate"].ToString()); if (NowTime >= ActionTime && NowTime < EndTime && list[i]["HBadCodeSN"].ToString() != "" && listData.Contains(HBarCode) == false) { string HSourceCode = list[i]["HSouceNumber"].ToString(); string HEmpCode = GetSelectedCode(this.HEmpCode); string HType = list[i]["HBadReason"].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; if (true) { DataSet dataSet; string HProjectNum = HBarCode.Substring(18, 8); //通过截取项目号 如果第一条数据的项目号跟后面的一样 就不进行判断 if (HProjectNum != AllProcessExchange) { //判断流转卡是否存在 dataSet = oCN.RunProcReturn("select HInterID,HBillNo from Sc_ProcessExchangeBillMain where HProjectNum='" + HProjectNum + "-1'", "Sc_ProcessExchangeBillMain"); if (dataSet.Tables[0].Rows.Count > 0) { AllProcessExchangeHProcExchBillNo = dataSet.Tables[0].Rows[0]["HBillNo"].ToString(); AllProcessExchangeHProcExchInterID = dataSet.Tables[0].Rows[0]["HInterID"].ToString(); AllProcessExchange = HProjectNum; } } //判断截取的项目号 跟赋值的项目号是否一样 需要先满足上面的条件 if (AllProcessExchange == HProjectNum) { //通过截取项目号 如果第一条数据的项目号跟后面的一样 就不进行判断 //只判断第一个SN码是否进行开工,没有开工自动开工 if (HProjectNum != AllBeginWork) { // 查询工序信息(不变) dataSet = oCN.RunProcReturn(@"SELECT HItemID,HName FROM Gy_Process WITH(NOLOCK) WHERE HNumber='" + HProcNumber + "'", "Gy_Process"); if (dataSet.Tables[0].Rows.Count > 0) { AllHProcID = dataSet.Tables[0].Rows[0]["HItemID"].ToString(); AllHProName = dataSet.Tables[0].Rows[0]["HName"].ToString(); } // 查询开工状态表(不变) dataSet = oCN.RunProcReturn("select * from Sc_ICMOBillStatus_Tmp where HSourceBillNo='" + AllProcessExchangeHProcExchBillNo + "' and HProcID='" + AllHProcID + "'", "Sc_ICMOBillStatus_Tmp"); if (dataSet.Tables[0].Rows.Count > 0) { // 先读取状态,再判断赋值时机(核心修正) string hicmoStatus = dataSet.Tables[0].Rows[0]["HICMOStatus"].ToString(); // 情况1:状态=1(已开工)→ 直接标记 if (hicmoStatus == "1") { AllBeginWork = HProjectNum; } // 情况2:状态=0(未开工)→ 执行开工操作,成功才标记 else if (hicmoStatus == "0") { bool completeFlag = SaveMESBeginWorkFrom_ZD(dataSet.Tables[0].Rows[0]["HBillType"].ToString(), dataSet.Tables[0].Rows[0]["HInterID"].ToString(), dataSet.Tables[0].Rows[0]["HSourceEntryID"].ToString(), dataSet.Tables[0].Rows[0]["HSourceBillNo"].ToString(), this.HEmpCodeName.Text, dataSet.Tables[0].Rows[0]["HSourceBillType"].ToString()); if (completeFlag) { // 开工成功,才标记为已开工(你确认此时状态已更1) AllBeginWork = HProjectNum; } else { // 开工失败,重置为-1,避免误判 AllBeginWork = "-1"; MessageBox.Show("自动生成开工单失败!"); } } } else { // 没查到记录 → 重置为-1 AllBeginWork = "-1"; } } //满足上述条件 并且项目号 和赋值项目号一样 则进行新增 if (AllBeginWork == HProjectNum) { if (Get_AllowLoadData(HBarCode, HProcNumber, flag_1, AllProcessExchangeHProcExchBillNo, AllProcessExchangeHProcExchInterID, out flag_2)) { DataSet ds = oCN.RunProcReturn(@"select HItemID 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_EquipMentCollection_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag) values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount}','{HCreateTime}',GETDATE(),'{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); ListSelect.Items.Add("条码:" + HBarCode + ",当前日期;" + DateTime.Now.ToString() + ",日期:" + HCreateTime + ",结果:" + HResult); listData.Add(HBarCode); } } else { flag_1 = flag_2; } } else { if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; MessageBox.Show(new Form { TopMost = true }, "条码:" + HBarCode + ",对应的流转卡:" + AllProcessExchangeHProcExchBillNo + ",流转卡未开工!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",对应的流转卡:" + AllProcessExchangeHProcExchBillNo + ",流转卡未开工!", DateTime.Now.ToString("yyyy-MM-dd")); flag_1 = true; } } else { if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; 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 = GetSelectedCode(this.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 HItemID 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}',GETDATE(),'{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); } } else { } } } //根据条码 判断是否保存 public bool Get_AllowLoadData(string HBarCode, string HProcNumber, bool flag_1, string HProcExchBillNo, string HProcExchInterID, out bool flag_2) { //判断条码的长度 if (HBarCode.Length != 29 && HBarCode.Length != 50) { flag_2 = false; return false; } //判断长度是否为29位 无尘车间 if (HBarCode.Length == 29) { } //判断长度是否为50位 15车间 else if (HBarCode.Length == 50) { string str1 = HBarCode.Substring(42, 8); DataSet ds = oCN.RunProcReturn("select HBillNo from Sc_ProcessExchangeBillMain WITH(NOLOCK) where HProjectNum like'" + str1 + "-1%'", "Sc_ProcessExchangeBillMain"); //判断是否能找到对应的流转卡 if (ds.Tables[0].Rows.Count > 0) { HProcExchBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString(); } else { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; MessageBox.Show("条码:" + HBarCode + ",流转卡不存在!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + ",流转卡不存在!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } } DataSet ds1; //第一次流转卡+工序和后面的做对比 如果是同一个流转卡就不进行判断 if ((HProcExchBillNo + HProcNumber) != AllSNBarcodeProcCtrl) { //查询工序 ds1 = oCN.RunProcReturn(@"SELECT HItemID,HName FROM Gy_Process WITH(NOLOCK) WHERE HNumber='" + HProcNumber + "'", "Gy_Process"); if (ds1.Tables[0].Rows.Count > 0) { AllHProcID = ds1.Tables[0].Rows[0]["HItemID"].ToString(); AllHProName = ds1.Tables[0].Rows[0]["HName"].ToString(); } //查询流转卡数量 ds1 = oCN.RunProcReturn(@"SELECT HQty FROM Sc_ProcessExchangeBillSub WITH(NOLOCK) where HInterID=" + HProcExchInterID + " and HProcID=" + AllHProcID, "Sc_ProcessExchangeBillSub"); if (ds1.Tables[0].Rows.Count > 0) { AllHQty = double.Parse(ds1.Tables[0].Rows[0]["HQty"].ToString()); } AllSNBarcodeProcCtrl = HProcExchBillNo + HProcNumber; } //过站控制 //判断当前工序对应的条码是否上道工序过站 ds1 = oCN.RunProcReturn("exec h_p_Sc_SNBarcodeProcCtrl_S_New '" + HBarCode + "'," + AllHProcID, "h_p_Sc_SNBarcodeProcCtrl_S_New"); if (ds1.Tables[0].Rows.Count == 0) { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; MessageBox.Show("条码:" + HBarCode + "工序:" + AllHProName + ",工序控制查无数据!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + "工序:" + AllHProName + ",工序控制查无数据!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } else if (ds1.Tables[0].Rows[0]["HBack"].ToString() == "2") { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; MessageBox.Show("条码:" + HBarCode + "工序:" + AllHProcID + "," + ds1.Tables[0].Rows[0]["HBackRemark"].ToString() + "!"); } DBHelper.CustomWriteLog("条码:" + HBarCode + "工序:" + AllHProcID + "," + ds1.Tables[0].Rows[0]["HBackRemark"].ToString() + "!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } // 查询出站数量是否超过流转卡数量 string cacheKey = $"{HProcExchBillNo}_{AllHProcID}"; double hqtyOut; // 判断是否已经有缓存,并且缓存未过期(假设5分钟刷新一次) if (RemainingQtyCache.ContainsKey(cacheKey) && (DateTime.Now - CacheUpdateTime[cacheKey]).TotalMinutes <= 15 ) { // 使用缓存中的剩余数量 hqtyOut = RemainingQtyCache[cacheKey]; } else { if (CurrentActiveKey!= cacheKey) { if (RemainingQtyCache.ContainsKey(CurrentActiveKey)) { RemainingQtyCache.Remove(CurrentActiveKey); CacheUpdateTime.Remove(CurrentActiveKey); } // 更新当前缓存键 CurrentActiveKey = cacheKey; } // 缓存已过期,查询数据库获取当前剩余数量 ds1 = oCN.RunProcReturn($@"SELECT ({AllHQty} - SUM(ISNULL(ou.HQty, 0)) - SUM(ISNULL(ou.HBadCount, 0))) AS HQty FROM Sc_StationOutBillMain ou WITH(NOLOCK) WHERE ou.HProcExchInterID = {HProcExchInterID} AND ou.HProcID = {AllHProcID} GROUP BY ou.HProcExchInterID, ou.HProcExchEntryID", "Sc_StationOutBillMain"); if (ds1.Tables[0].Rows.Count > 0) { // 如果查到出站记录,计算剩余可用数量 hqtyOut = double.Parse(ds1.Tables[0].Rows[0]["HQty"].ToString()); } else { // 第一次运行,没有出站记录,使用流转卡总数量 hqtyOut = AllHQty; } // 更新缓存和更新时间 RemainingQtyCache[cacheKey] = hqtyOut; CacheUpdateTime[cacheKey] = DateTime.Now; } //流转卡数量-出站单数量大于0 if (hqtyOut <= 0) { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; MessageBox.Show("流转卡:" + HProcExchBillNo + ",出站数量超过流转卡数量!"); } DBHelper.CustomWriteLog("流转卡:" + HProcExchBillNo + ",出站数量超过流转卡数量!", DateTime.Now.ToString("yyyy-MM-dd")); flag_2 = true; return false; } // 每次成功校验一个条码后,剩余数量减1 hqtyOut -= 1; // 更新缓存值 RemainingQtyCache[cacheKey] = hqtyOut; //拍照工序除外 if (HProcNumber != "013") { //增加产线组装追溯单 //查询当前流转卡对应的工序有没有配件信息,如果有 则判断配件单的数量是否为0 DataSet dataSet = oCN.RunProcReturn("exec h_p_Gy_BarCodeBillBomList '" + HProcExchBillNo + "'," + AllHProcID, "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) { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; 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; } //自动开工 public bool SaveMESBeginWorkFrom_ZD(string HBillType, string HSourceInterID, string HSourceEntryID, string HSourceBillNo, string user, string HSourceBillType) { try { DataSet ds; ds = oCN.RunProcReturn("exec h_p_JIT_GetInfoByICMOStatusInterID @HSourceInterID='" + HSourceInterID + "',@HSourceEntryID='" + HSourceEntryID + "',@HSourceBillNo='" + HSourceBillNo + "',@HSourceBillType='" + HBillType + "'", "h_p_JIT_GetInfoByICMOStatusInterID"); string sExeReturnInfo = ""; long HProcID = int.Parse(ds.Tables[0].Rows[0]["HProcID"].ToString()); long HMaterID = int.Parse(ds.Tables[0].Rows[0]["HMaterID"].ToString()); long HSourceID = int.Parse(ds.Tables[0].Rows[0]["HSourceID"].ToString()); string HMainSourceBillNo = ds.Tables[0].Rows[0]["HSourceBillNo"].ToString(); long HMainSourceInterID = int.Parse(ds.Tables[0].Rows[0]["HSourceInterID"].ToString()); long HMainSourceEntryID = long.Parse(ds.Tables[0].Rows[0]["HSourceEntryID"].ToString()); string HMainSourceBillType = HSourceBillType == null ? "" : HSourceBillType; long HDeptID = int.Parse(ds.Tables[0].Rows[0]["HDeptID"].ToString()); long HICMOInterID = int.Parse(ds.Tables[0].Rows[0]["HICMOInterID"].ToString()); long HICMOEntryID = int.Parse(ds.Tables[0].Rows[0]["HICMOEntryID"].ToString()); string HICMOBillNo = ds.Tables[0].Rows[0]["HICMOBillNo"].ToString(); long HProcExchInterID = int.Parse(ds.Tables[0].Rows[0]["HProcExchInterID"].ToString()); long HProcExchEntryID = int.Parse(ds.Tables[0].Rows[0]["HProcExchEntryID"].ToString()); string HProcExchBillNo = ds.Tables[0].Rows[0]["HProcExchBillNo"].ToString(); long HWorkShiftID = 0; //获取班次 DataSet set = oCN.RunProcReturn("exec h_p_Gy_GetWorkShiftInfo '389505','77'", "h_p_Gy_GetWorkShiftInfo"); if (set.Tables[0].Rows.Count > 0) { HWorkShiftID = int.Parse(set.Tables[0].Rows[0]["HInterID"].ToString()); } ds = oCN.RunProcReturn("select * from Gy_Employee with(nolock) where HNumber='" + this.HEmpCode.Text + "'", "Gy_Employee"); long HGroupID = int.Parse(ds.Tables[0].Rows[0]["HGroupID"].ToString()); long HEmpID = int.Parse(ds.Tables[0].Rows[0]["HItemID"].ToString()); Int64 HInterID = CreateBillID("3787", ref sExeReturnInfo); string HBillNo = CreateBillCode_Prod("3787", ref sExeReturnInfo, true); //保存前 ds = oCN.RunProcReturn("exec h_p_Sc_MESBeginWorkBill_BeforeSaveCtrl " + HInterID.ToString() + "," + HICMOInterID.ToString() + "," + HICMOEntryID.ToString() + ",'" + HICMOBillNo + "'," + 1, "h_p_Sc_MESEndWorkBill_BeforeSaveCtrl"); if (ds == null || ds.Tables[0].Rows.Count == 0) { return false; } oCN.RunProc("Insert Into Sc_MESBeginWorkBillMain " + "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate" + ",HYear,HPeriod,HRemark" + ",HICMOInterID,HICMOEntryID,HICMOBillNo,HProcPlanInterID,HProcPlanEntryID,HProcPlanBillNo,HProcExchInterID,HProcExchEntryID" + ",HProcExchBillNo,HMaterID,HProcID,HICMOQty,HPlanQty,HBeginWorkTime,HSourceID" + ",HGroupID,HDeptID,HEmpID,HBarCode,HAddr,HBarCodeMaker,HBarCodeMakeDate" + ",HSourceInterID_Main,HSourceEntryID_Main,HSourceBillNo_Main,HSourceBillType_Main" + ",HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HMainSourceBillType" + ",HRunStatus,HSourceBeginQty,HWorkShiftID,HPeopleSum" + ") " + " values('3787','3787'," + HInterID + ",'" + HBillNo + "',1,getdate(),'" + user + "',getdate()" + ",Year(getdate()),Month(getdate()),'自动开工'" + "," + HICMOInterID + ",'" + HICMOEntryID + "','" + HICMOBillNo + "',0,0,''," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + ",0,0,getdate()," + HSourceID + "," + HGroupID + "," + HDeptID + "," + HEmpID + ",'','','',getdate()" + "," + HMainSourceInterID + "," + HMainSourceEntryID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'" + "," + HMainSourceInterID + "," + HMainSourceEntryID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'" + ",0,0,'" + HWorkShiftID + "',1) "); oCN.RunProc(" Update Sc_MESBeginWorkBillMain set HBillStatus='2',HChecker='" + user + "',HCheckDate=getdate() Where HInterID=" + HInterID.ToString()); //根据开工单内码 取到生产状态临时表主内码,然后更新生产状态临时表状态为:待生产 任务单状态(0待生产,1生产中,2挂起,3已完工) oCN.RunProc("exec h_p_JIT_MESBeginWorkBill_BeginWork @HInterId=" + HInterID + ",@flag='开工'"); //保存后 ds = oCN.RunProcReturn("exec h_p_Sc_MESBeginWorkBill_AfterSaveCtrl " + HInterID.ToString() + "," + HICMOInterID.ToString() + "," + HICMOEntryID.ToString() + ",'" + HICMOBillNo + "'," + 1, "h_p_Sc_MESBeginWorkBill_AfterSaveCtrl"); if (ds == null || ds.Tables[0].Rows.Count == 0) { return false; } return true; } catch (Exception e) { return false; } } #region 获取 内码 单据号 public static Int64 CreateBillID(string BillCode, ref string sReturn) { string BillType = ""; DataSet Ds; DBHelper oCn = new DBHelper(); Int64 lID; Ds = oCn.RunProcReturn("select * from Gy_BillNumber with (nolock) where BillCode='" + BillCode.Trim() + "'", "Gy_BillNumber"); if (Ds.Tables[0].Rows.Count != 0) { lID = isLong(Ds.Tables[0].Rows[0]["IDNow"].ToString()); BillType = Ds.Tables[0].Rows[0]["BillType"].ToString().Trim(); } else { lID = 0; } //同类型单据 自增1 if (BillType.Trim() != "") { oCn.RunProc("update Gy_BillNumber set IDNow=IDNow+1 where BillType='" + BillType.Trim() + "'"); } oCn.CnClose(); oCn.CnDispose(); oCn = null; Ds = null; return lID; } public static string CreateBillCode_Prod(string BillCode, ref string sReturn, bool Add) { try { string BillType = ""; DataSet Ds; DBHelper oCn = new DBHelper(); string sBIllNO; Ds = oCn.RunProcReturn("exec h_p_Xt_GetMaxBillNo '" + BillCode + "'", "h_p_Xt_GetMaxBillNo"); if (Ds == null || Ds.Tables[0].Rows.Count == 0) { sBIllNO = "ERROR"; } else { sBIllNO = isStrNull(Ds.Tables[0].Rows[0]["HBillNo"]); } oCn.CnClose(); oCn.CnDispose(); oCn = null; Ds = null; return sBIllNO; } catch (Exception e) { return "ERROR"; } } #endregion #region 辅助方法 //判断是否是INT64 public static Int64 isLong(object message) { try { return Convert.ToInt64(message); } catch (Exception e) { return 0; } } //判断是否NULL public static string isStrNull(object message) { try { if (message == null) { return ""; } else { return message.ToString().Trim(); } } catch (Exception e) { return ""; } } #endregion //关闭 private void ReadyDataForm_FormClosing(object sender, FormClosingEventArgs e) { if (num == 1) { MessageBox.Show("当前按钮未暂停,不允许关闭!"); e.Cancel = true; return; } else if (MessageBox.Show("确定要关闭吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.No) { e.Cancel = true; return; } // 保存最后选中的代码 string lastEmpCode = HEmpCode.SelectedValue?.ToString(); string lastSrcCode = HSouce.SelectedValue?.ToString(); if (!string.IsNullOrEmpty(lastEmpCode)) HistoryConfigHelper.SaveEmployees(empHistoryList, lastEmpCode); if (!string.IsNullOrEmpty(lastSrcCode)) HistoryConfigHelper.SaveSources(sourceHistoryList, lastSrcCode); } //检索补漏 private void btnSelectAll_Click(object sender, EventArgs e) { if (num == 0) { if (MessageBox.Show("是否启用检索补漏?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes) { SelectTime = -420; Read_Txt(); } } else if (num == 1) { MessageBox.Show("请先点击暂停按钮!"); } } } }