From b9e07918f687f7b51e165d659c4a1595916406ff Mon Sep 17 00:00:00 2001
From: zrg <z1873@LAPTOP-EAVL132E>
Date: 星期二, 24 三月 2026 12:26:20 +0800
Subject: [PATCH] 斯末尔单品过站优化,增加自动开工功能,自动完工,华舟数采优化

---
 WFormSynchronizeData_SMR/WFormReadData_SMR/ReadyDataForm_New.cs |  279 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 250 insertions(+), 29 deletions(-)

diff --git a/WFormSynchronizeData_SMR/WFormReadData_SMR/ReadyDataForm_New.cs b/WFormSynchronizeData_SMR/WFormReadData_SMR/ReadyDataForm_New.cs
index 0d4834a..dc7a975 100644
--- a/WFormSynchronizeData_SMR/WFormReadData_SMR/ReadyDataForm_New.cs
+++ b/WFormSynchronizeData_SMR/WFormReadData_SMR/ReadyDataForm_New.cs
@@ -113,6 +113,21 @@
         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<string, double> RemainingQtyCache = new Dictionary<string, double>();  // 缂撳瓨姣忎釜娴佽浆鍗�+宸ュ簭鐨勫墿浣欐暟閲�
+        public static Dictionary<string, DateTime> CacheUpdateTime = new Dictionary<string, DateTime>(); // 璁板綍姣忔缂撳瓨鏇存柊鐨勬椂闂达紝鐢ㄤ簬鍒ゆ柇鏄惁闇�瑕佸埛鏂�
+        public string CurrentActiveKey = "";//缂撳瓨閿紝鐢ㄤ簬鍒ゆ柇娴佽浆鍗℃槸鍚﹀垏鎹�
+
+        // 鏂板闈欐�佸彉閲忥細璁板綍姣忎釜椤圭洰鍙风殑寮�宸ョ姸鎬�
+public static Dictionary<string, bool> ProjectBeginWorkStatus = new Dictionary<string, bool>();
+
         //闀洉鎷嶇収宸ュ簭鏂囨湰 杩囩珯璇诲彇 鏂扮殑
 
         //璇诲彇鏁版嵁
@@ -285,7 +300,7 @@
                 {
                     stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}gy.txt", Encoding.GetEncoding("gb2312"));                    
                     FileData = stream.ReadToEnd();
-                    //鑾峰彇绯荤粺鍙傛暟
+                    //鑾峰彇宸ヨ壓鍙傛暟
                     Xt_Gy_RoutBill(FileData);
                 }
             }
@@ -293,11 +308,8 @@
 
 
        
-        public static string AllBeginWork = "-1";  //鏄惁寮�宸� 
-        public static string AllProcessExchange = "-1";   //娴佽浆鍗¢」鐩彿
-        public static string AllProcessExchangeHProcExchBillNo = "";  //娴佽浆鍗″彿
-        public static string AllProcessExchangeHProcExchInterID = ""; //娴佽浆鍗′富鐮�
 
+        //璇诲彇鏂囨湰鏉$爜
         public void Xt_ReadText(string FileData)
         {
             //鑾峰彇鏂囨湰鎵�鏈夋暟鎹�
@@ -336,15 +348,14 @@
                     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 (true)
                     {
                         DataSet dataSet;
 
                         string HProjectNum = HBarCode.Substring(18, 8);
                         //閫氳繃鎴彇椤圭洰鍙� 濡傛灉绗竴鏉℃暟鎹殑椤圭洰鍙疯窡鍚庨潰鐨勪竴鏍� 灏变笉杩涜鍒ゆ柇
-                        if (HBarCode.Contains(AllProcessExchange) == false)
+                        if (HProjectNum != AllProcessExchange)
                         {
 
                             //鍒ゆ柇娴佽浆鍗℃槸鍚﹀瓨鍦�
@@ -360,17 +371,52 @@
                         if (AllProcessExchange == HProjectNum)
                         {
                             //閫氳繃鎴彇椤圭洰鍙� 濡傛灉绗竴鏉℃暟鎹殑椤圭洰鍙疯窡鍚庨潰鐨勪竴鏍� 灏变笉杩涜鍒ゆ柇
-                            if (HBarCode.Contains(AllBeginWork) == false)
+                            //鍙垽鏂涓�涓猄N鐮佹槸鍚﹁繘琛屽紑宸ワ紝娌℃湁寮�宸ヨ嚜鍔ㄥ紑宸�
+                            if (HProjectNum != AllBeginWork)
                             {
-                                //鍒ゆ柇褰撳墠娴佽浆鍗″搴旂殑宸ュ簭鏁版嵁鏄惁寮�宸�
-                                dataSet = oCN.RunProcReturn("select  HICMOStatus from Sc_ICMOBillStatus_Tmp where HSourceBillNo='" + AllProcessExchangeHProcExchBillNo + "' and HICMOStatus<>'0'", "Sc_ICMOBillStatus_Tmp");
-
+                                // 鏌ヨ宸ュ簭淇℃伅锛堜笉鍙橈級
+                                dataSet = oCN.RunProcReturn(@"SELECT  HItemID,HName FROM Gy_Process  WITH(NOLOCK) WHERE HNumber='" + HProcNumber + "'", "Gy_Process");
                                 if (dataSet.Tables[0].Rows.Count > 0)
                                 {
-                                    AllBeginWork = HProjectNum;
+                                    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)
                             {
@@ -424,7 +470,7 @@
             }
         }
 
-        //璇诲彇绯荤粺鍙傛暟
+        //璇诲彇宸ヨ壓鍙傛暟
         public void Xt_Gy_RoutBill(string FileData)
         {
             FileData = FileData.Substring(0, FileData.Length - 1);
@@ -467,25 +513,17 @@
                     {
                         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);
-                        //ListSelect.Items.Add("鏉$爜:" + HBarCode + ",鏃ユ湡:" + HCreateTime + ",缁撴灉:" + HResult);
+                        oCN.RunProc(sql);                        
                     }
                 }
                 else
                 {
-                    //CustomWriteLog("鏃堕棿涓嶅湪淇濆瓨鑼冨洿鍐�", DateTime.Now.ToString("yyyy-MM-dd"));
                 }
             }
 
         }
 
-        public static string AllSNBarcodeProcCtrl = "";  //娴佽浆鍗�+宸ュ簭鐨勬嫾鍐�
-        public static string AllHProcID = "0";           //宸ュ簭id
-        public static string AllHProName = "";           //宸ュ簭鍚嶅瓧
-        public static double AllHQty = 0;               //榛樿娴佽浆鍗℃暟閲� 濡傛灉鍑虹珯鍗曟湁鏁版嵁浼氭洿鏂版垚鍑虹珯鍗曟暟閲�
-        public static Dictionary<string, double> RemainingQtyCache = new Dictionary<string, double>();  // 缂撳瓨姣忎釜娴佽浆鍗�+宸ュ簭鐨勫墿浣欐暟閲�
-        public static Dictionary<string, DateTime> CacheUpdateTime = new Dictionary<string, DateTime>(); // 璁板綍姣忔缂撳瓨鏇存柊鐨勬椂闂达紝鐢ㄤ簬鍒ゆ柇鏄惁闇�瑕佸埛鏂�
-        public string CurrentActiveKey = "";//缂撳瓨閿紝鐢ㄤ簬鍒ゆ柇娴佽浆鍗℃槸鍚﹀垏鎹�
+        
         //鏍规嵁鏉$爜 鍒ゆ柇鏄惁淇濆瓨
         public bool Get_AllowLoadData(string HBarCode, string HProcNumber, bool flag_1, string HProcExchBillNo, string HProcExchInterID, out bool flag_2)
         {
@@ -549,9 +587,6 @@
                 AllSNBarcodeProcCtrl = HProcExchBillNo + HProcNumber;
             }
 
-            ////绗竴娆℃祦杞崱+宸ュ簭鍜屽悗闈㈢殑鍋氬姣� 濡傛灉鏄悓涓�涓祦杞崱灏变笉杩涜鍒ゆ柇
-            //if ((HProcExchBillNo + HProcNumber) == AllSNBarcodeProcCtrl)
-            //{
             //杩囩珯鎺у埗
             //鍒ゆ柇褰撳墠宸ュ簭瀵瑰簲鐨勬潯鐮佹槸鍚︿笂閬撳伐搴忚繃绔�
             ds1 = oCN.RunProcReturn("exec h_p_Sc_SNBarcodeProcCtrl_S_New '" + HBarCode + "'," + AllHProcID, "h_p_Sc_SNBarcodeProcCtrl_S_New");
@@ -582,7 +617,6 @@
                 flag_2 = true;
                 return false;
             }
-            //}
 
 
             // 鏌ヨ鍑虹珯鏁伴噺鏄惁瓒呰繃娴佽浆鍗℃暟閲�
@@ -680,7 +714,192 @@
             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 杈呭姪鏂规硶
+        //鍒ゆ柇鏄惁鏄疘NT64
+        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)
@@ -704,6 +923,8 @@
                 HistoryConfigHelper.SaveSources(sourceHistoryList, lastSrcCode);
         }
 
+       
+        //妫�绱㈣ˉ婕�
         private void btnSelectAll_Click(object sender, EventArgs e)
         {
             if (num == 0)

--
Gitblit v1.9.1