using Microsoft.VisualBasic.FileIO; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WFormReadData_SMR { public partial class ReadCSV_New : Form { public DBHelper oCN = new DBHelper(); private List empHistoryList = new List(); private List sourceHistoryList = new List(); public static int num = 0; public static DateTime time = DateTime.Now.AddMinutes(-5); public static string AllProcessExchange = "-1"; public static string AllProcessExchangeHProcExchBillNo = ""; public static string AllProcessExchangeHProcExchInterID = ""; public static string AllSNBarcodeProcCtrl = ""; public static string AllHProcID = "0"; public static string AllHProName = ""; public static double AllHQty = 0; public static string AllBeginWork = "-1"; public static Dictionary RemainingQtyCache = new Dictionary(); // 缓存每个流转卡+工序的剩余数量 public static Dictionary CacheUpdateTime = new Dictionary(); // 记录每次缓存更新的时间,用于判断是否需要刷新 public string CurrentActiveKey = "";//缓存键,用于判断流转卡是否切换 public ReadCSV_New() { InitializeComponent(); this.Load += ReadCSV_New_Load; // 挂载加载事件 } private void ReadCSV_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 = ""; } } //组装工序CSV 过站读取 新的 //更换条形码 private void btnGH_Click(object sender, EventArgs e) { this.txtHBardCode.Text = ""; this.txtHBath.Text = ""; this.txtHBardCode.ReadOnly = false; } //查询条形码 public void SelectHBardCode(string HBillNo) { try { DataSet ds = oCN.RunProcReturn("select HProjectNum from Sc_ProcessExchangeBillMain WITH(NOLOCK) where HBillNo='" + HBillNo + "' ", "Sc_ProcessExchangeBillMain"); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("查无数据!"); } else { DataSet dataSet = oCN.RunProcReturn("select HICMOStatus HICMOStatus from Sc_ICMOBillStatus_Tmp WITH(NOLOCK) where HSourceBillNo='" + HBillNo + "' and HICMOStatus<>'0'", "Sc_ICMOBillStatus_Tmp"); if (dataSet.Tables[0].Rows.Count > 0) { string HProjectNum = ds.Tables[0].Rows[0]["HProjectNum"].ToString(); this.txtHBath.Text = HProjectNum.Split('-')[0]; this.txtHBardCode.ReadOnly = true; list.Clear(); } else { MessageBox.Show(HBillNo + ",流转卡未开工!"); } } } catch (Exception e) { MessageBox.Show(this, e.Message, "提示"); } } private void txtHBardCode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { SelectHBardCode(this.txtHBardCode.Text); } } //员工编码回车 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 btnReadData_Click(object sender, EventArgs e) { if (this.HEmpCode.Text == "" || this.txtHBath.Text == "" || this.HSouceName.Text == "") { MessageBox.Show("请输入生产资源,员工编码,者条形码!"); } else { if (num == 0) { this.btnReadData.Text = "暂停"; this.timer1.Enabled = true; this.btnGH.Enabled = false; this.btnSelect.Enabled = false; num = 1; //ReadCsv(); } else if (num == 1) { this.btnReadData.Text = "启动"; this.timer1.Enabled = false; this.btnGH.Enabled = true; this.btnSelect.Enabled = true; num = 0; } } } private void timer1_Tick(object sender, EventArgs e) { if (this.HEmpCode.Text == "" || this.txtHBath.Text == "") { MessageBox.Show("请输入员工编码或者流转卡!"); } else { ReadCsv(); } } public static List list = new List(); //读取csv格式文件 private void ReadCsv() { string Date = DateTime.Now.ToString("yyMMdd"); string HBath = this.txtHBath.Text; string Year = DateTime.Now.Year.ToString(); Year = Year.Substring(Year.Length - 2, 2); string Month = DateTime.Now.Month.ToString(); Month = Month.Length > 1 ? Month : "0" + Month; string Day = DateTime.Now.Day.ToString(); Day = Day.Length > 1 ? Day : "0" + Day; //注:斯莫尔组装3 在E盘 _332009 //其余在D盘 _C332001_2M string csv_file_path = $@"D:\THLCR_Data\{HBath}\{HBath}_{Date}_C332001_2M.csv"; //组装1 组装2路径 //string csv_file_path = $@"E:\THLCR_Data\{HBath}\{HBath}_{Date}_332009.csv"; //组装3路径 //15车间缺少路径 D:\DATA SAVE\年\年月\年月日\批次号\数据保存.csv //string csv_file_path = $@"D:\DATA SAVE\{Year}\{Year + Month}\{Year + Month + Day}\{HBath}\数据保存.csv"; bool flag = File.Exists(csv_file_path); if (flag) { DataTable dt = new DataTable(); string HTypeName = "无尘"; //string csv_file_path_Read = @"C:\Users\admin\Desktop\新建文件夹 (2)\250933P9_250403_C332001_2M_1.csv"; string csv_file_path_Read = $@"D:\THLCR_Data\{HBath}\{HBath}_{Date}_C332001_2M_Read.csv"; //组装1 组装2路径 //string csv_file_path_Read = $@"E:\THLCR_Data\{HBath}\{HBath}_{Date}_332009_Read.csv"; //组装3路径 //string HTypeName = "15"; ////15车间缺少路径 D:\DATA SAVE\年\年月\年月日\批次号\数据保存.csv ////string csv_file_path_Read = $@"C:\Users\admin\Desktop\新建文件夹 (2)\DATA SAVE\数据保存_MES读取.csv"; //string csv_file_path_Read = $@"D:\DATA SAVE\{Year}\{Year + Month}\{Year + Month + Day}\{HBath}\数据保存_MES读取.csv"; //复制一份文件 File.Copy(csv_file_path, csv_file_path_Read, true); string contents = File.ReadAllText(csv_file_path_Read, Encoding.GetEncoding("gb2312")); TextFieldParser parser = new TextFieldParser(new StringReader(contents)); parser.HasFieldsEnclosedInQuotes = true; parser.SetDelimiters(","); string[] fields; while (!parser.EndOfData) { fields = parser.ReadFields(); if (dt.Columns.Count == 0) { foreach (string field in fields) { dt.Columns.Add(new DataColumn(string.IsNullOrWhiteSpace(field.Trim('\"')) ? null : field.Trim('\"'), typeof(string))); } } else { dt.Rows.Add(fields.Select(item => string.IsNullOrWhiteSpace(item.Trim('\"')) ? null : item.Trim('\"')).ToArray()); } } parser.Close(); //获取当前时间 DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-20).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; foreach (DataRow item in dt.Rows) { if (HTypeName == "无尘") { DateTime NowTime = DateTime.Parse(item["测试时间"].ToString()); string HBadCodeSN = item["序号"].ToString(); if (((NowTime >= ActionTime && NowTime < EndTime) || dt.Rows.Count <= 20) && HBadCodeSN != "" && list.Contains(HBadCodeSN) == false) { string HSourceCode = this.HSouce.Text; //设备编号 组装1 9994 组装2 9995 组装3 9996 string HEmpCode = GetSelectedCode(this.HEmpCode); ; string HBarCode = item["序号"].ToString(); string HCreateTime = item["测试时间"].ToString(); string HDate = DateTime.Parse(item["测试时间"].ToString()).ToString("yyyy-MM-dd"); string HResult = item["分选"].ToString() == "PASS" ? "OK" : "NG"; string HProcNumber = "005"; //005 int HFlag = 0; //判断条码不为空 if (HBadCodeSN != "") { string HType = ""; int HCount = 1; DataSet ds = oCN.RunProcReturn(@"select * from Sb_EquipMentCollection_SN with(nolock) where HBarCode='" + HBarCode + "' and HProcNumber='" + HProcNumber + "'", "Sb_EquipMentCollection_SN"); if (ds.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}',GETDATE(),'{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); ListSelect.Items.Add("条码:" + HBarCode + ",当前日期:" + DateTime.Now.ToString() + ",日期:" + HCreateTime + ",结果:" + HResult); list.Add(HBarCode); //新增工艺参数 for (int i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].ColumnName.Contains("Hz")) { HType = dt.Columns[i].ColumnName; string HCount_1 = item[HType].ToString(); //查询当天条码 对应的工艺参数有没有插入到里面 如果没有则新增 DataSet ds1 = oCN.RunProcReturn(@"select HBarCode from Sb_EquipMentCollectionTechParam_SN WITH(NOLOCK) where HBarCode='" + HBarCode + "' and HType='" + HType + "'", "Sb_EquipMentCollectionTechParam_SN"); if (ds1.Tables[0].Rows.Count == 0) { string sql1 = $@"insert into Sb_EquipMentCollectionTechParam_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag) values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount_1}','{HCreateTime}',GETDATE(),'{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql1); } } } } else { flag_1 = flag_2; } } } } } if (HTypeName == "15") { DateTime NowTime = DateTime.Parse(item["CS_时间"].ToString()); string HBadCodeSN = item["CS_DM码"].ToString(); if (((NowTime >= ActionTime && NowTime < EndTime) || dt.Rows.Count <= 20) && HBadCodeSN != "") { string HSourceCode = this.HSouce.Text; //设备编号 组装1 9994 组装2 9995 组装3 9996 string HEmpCode = GetSelectedCode(this.HEmpCode); ; string HBarCode = item["CS_DM码"].ToString(); string HCreateTime = item["CS_时间"].ToString(); string HDate = DateTime.Parse(item["CS_时间"].ToString()).ToString("yyyy-MM-dd"); string HResult = item["CS_总结果"].ToString(); string HProcNumber = "005"; int HFlag = 0; if (HResult != "") { if (HBadCodeSN != "") { string HType = ""; int HCount = 1; DataSet ds = oCN.RunProcReturn(@"select HBarCode from Sb_EquipMentCollectionTechParam_SN WITH(NOLOCK) where HBarCode='" + HBarCode + "' and HCreateTime='" + HCreateTime + "'", "Sb_EquipMentCollection_SN"); if (ds.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}',GETDATE(),'{HResult}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql); ListSelect.Items.Add("条码:" + HBarCode + ",日期:" + HCreateTime + ",结果:" + HResult); for (int i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].ColumnName.Contains("CS_")) { HType = dt.Columns[i].ColumnName; string HCount_1 = item[HType].ToString(); string HResult_TechParam = ""; if (HType != "CS_时间" && HType != "CS_DM码" && HType != "CS_总结果") { HResult_TechParam = HCount_1; double number = 0; if (!Double.TryParse(HCount_1, out number)) { HCount_1 = "0"; } DataSet ds1 = oCN.RunProcReturn(@"select HBarCode from Sb_EquipMentCollectionTechParam_SN WITH(NOLOCK) where HBarCode='" + HBarCode + "' and HType='" + HType + "'", "Sb_EquipMentCollectionTechParam_SN"); if (ds1.Tables[0].Rows.Count == 0) { string sql1 = $@"insert into Sb_EquipMentCollectionTechParam_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag) values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{(HCount_1 == "" ? " 0" : HCount_1)}','{HCreateTime}',GETDATE(),'{HResult_TechParam}','{HProcNumber}','{HFlag}')"; oCN.RunProc(sql1); //ListSelect.Items.Add("条码:" + HBarCode + ",日期:" + HCreateTime + ",结果:" + HResult); } } } } } else { flag_1 = flag_2; } } } } } } } } } //根据条码 判断是否保存 public bool Get_AllowLoadData(string HBarCode, string HProcNumber, bool flag_1, out bool flag_2) { decimal hqty = 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); if (AllProcessExchange != str1) { DataSet ds = oCN.RunProcReturn("select HInterID,HBillNo from Sc_ProcessExchangeBillMain WITH(NOLOCK) where HProjectNum like'" + str1 + "-1%' order by HMakeDate desc", "Sc_ProcessExchangeBillMain"); //判断是否能找到对应的流转卡 if (ds.Tables[0].Rows.Count > 0) { AllProcessExchangeHProcExchBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString(); AllProcessExchangeHProcExchInterID = ds.Tables[0].Rows[0]["HInterID"].ToString(); } else { //错误信息弹出框 定时超过2分钟弹一次 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_2 = true; return false; } AllProcessExchange = str1; } } //判断长度是否为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) { AllProcessExchangeHProcExchBillNo = 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; } } //赋值 HProcExchBillNo = AllProcessExchangeHProcExchBillNo; HProcExchInterID = AllProcessExchangeHProcExchInterID; 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 * from Sc_ICMOBillStatus_Tmp where HSourceBillNo='" + AllProcessExchangeHProcExchBillNo + "' and HProcID='" + AllHProcID + "'", "Sc_ICMOBillStatus_Tmp"); if (ds1.Tables[0].Rows.Count > 0) { //生产状态临时表 状态如果为0 那就是没开工 进入开工单 if (ds1.Tables[0].Rows[0]["HICMOStatus"].ToString() == "0") { bool completeFlag = SaveMESBeginWorkFrom_ZD(ds1.Tables[0].Rows[0]["HBillType"].ToString(), ds1.Tables[0].Rows[0]["HInterID"].ToString(), ds1.Tables[0].Rows[0]["HSourceEntryID"].ToString(), ds1.Tables[0].Rows[0]["HSourceBillNo"].ToString(), this.HEmpCodeName.Text, ds1.Tables[0].Rows[0]["HSourceBillType"].ToString()); if (!completeFlag) { MessageBox.Show(new Form { TopMost = true }, "流转卡:" + AllProcessExchangeHProcExchBillNo + ",自动生成开工单失败!"); } } } //查询流转卡数量 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; } string HProcID = AllHProcID; string HProName = AllHProName; //判断 当前工序 条码 的上一道工序有没有过站 ds1 = oCN.RunProcReturn("exec h_p_Sc_SNBarcodeProcCtrl_S_New '" + HBarCode + "'," + HProcID, "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 + "工序:" + 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") { //flag_1=只有第一次进来才会弹出 错误信息弹出框 定时超过2分钟弹一次 if (!flag_1 && (DateTime.Now - time).Minutes > hqty) { time = DateTime.Now; 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; } // 查询出站数量是否超过流转卡数量 string cacheKey = $"{HProcExchBillNo}_{AllHProcID}"; double hqtyOut; // 判断是否已经有缓存,并且缓存未过期(假设5分钟刷新一次) if (RemainingQtyCache.ContainsKey(cacheKey) && (DateTime.Now - CacheUpdateTime[cacheKey]).TotalMinutes <= 5) { // 使用缓存中的剩余数量 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 + "'," + 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) { //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 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 ReadCSV_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); } } }