From b056c584935b19725086599d5a1ee190de600bfb Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期三, 22 十月 2025 13:23:10 +0800
Subject: [PATCH] 添加行级锁防并发:锁工单行,防止出现两张时间一样的首检单

---
 Services/MesOrderStaManager.cs |   60 ++++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/Services/MesOrderStaManager.cs b/Services/MesOrderStaManager.cs
index 17923ba..7e4a650 100644
--- a/Services/MesOrderStaManager.cs
+++ b/Services/MesOrderStaManager.cs
@@ -97,38 +97,54 @@
     /// <returns>鏇存柊鏄惁鎴愬姛</returns>
     public bool ChangeMachineTime(MesOrderSta entity)
     {
-        // 鏌ヨ宸ュ崟涓昏〃淇℃伅
         var womdaa = Db.Queryable<Womdaa>()
             .Where(s => s.Id == entity.OrderId).First();
+        if (womdaa == null) throw new Exception("宸ュ崟涓嶅瓨鍦�");
 
-        // 濡傛灉鏍囪涓�1,闇�瑕佸鐞嗛妫�鐩稿叧閫昏緫
         if (entity.Flag == 1)
         {
-            //璋冪敤瀛樺偍杩囩▼鎵ц鑷姩棣栨
-             Db.Ado.ExecuteCommand(
-                 "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
-                 new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String));
-            var automaticIpqcFirstCheck =
-                mesQaItemsDetect02Manager
-                    .AutomaticIpqcFirstCheck(womdaa.Daa001);
-            if (!automaticIpqcFirstCheck)
+            // 骞傜瓑锛氬厛鏌ユ槸鍚﹀凡鏈夐妫�
+            var existsFirst = Db.Queryable<MesQaItemsDetect02>()
+                .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "棣栨" && (s.Fcancel == null || s.Fcancel != "Y"))
+                .Any();
+
+            if (!existsFirst)
             {
-                Console.WriteLine($"鑷姩棣栨澶辫触锛屽伐鍗曞彿锛歿womdaa.Daa001}");
-                throw new Exception("棣栨鐢熸垚澶辫触");
+                // 琛岀骇閿侀槻骞跺彂锛氶攣宸ュ崟琛�
+                Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE",
+                    new SugarParameter("BILL_NO", womdaa.Daa001));
+
+                // 鍐嶆纭
+                existsFirst = Db.Queryable<MesQaItemsDetect02>()
+                    .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "棣栨" && (s.Fcancel == null || s.Fcancel != "Y"))
+                    .Any();
+
+                if (!existsFirst)
+                {
+                    // 璋冪敤瀛樺偍杩囩▼锛堝崟璺緞锛�
+                    Db.Ado.ExecuteCommand(
+                        "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
+                        new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String));
+                }
             }
 
-            // 鑾峰彇褰撳墠鏃堕棿
-            var s1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-            // 鐢熸垚棣栨澶囨敞淇℃伅
-            var remeke = "宸ユ帶鏈轰簬" + s1 + "鑷姩鍒涘缓鐨勯妫�鍗�";
-
-            // 鏇存柊棣栨鍗曞娉ㄤ俊鎭�
-            Db.Updateable<MesQaItemsDetect02>()
-                .SetColumns(s => s.Remeke == remeke)
-                .Where(s => s.Ftype == "棣栨" && s.Aufnr == womdaa.Daa001)
-                .ExecuteCommand();
+            // 缁熶竴鏇存柊鏈�鏂伴妫�澶囨敞
+            var latestFirst = Db.Queryable<MesQaItemsDetect02>()
+                .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "棣栨" && (s.Fcancel == null || s.Fcancel != "Y"))
+                .OrderBy(s => s.CreateDate, OrderByType.Desc)
+                .First();
+            if (latestFirst != null)
+            {
+                var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+                var remark = $"宸ユ帶鏈轰簬{ts}鑷姩鍒涘缓鐨勯妫�鍗�";
+                Db.Updateable<MesQaItemsDetect02>()
+                    .SetColumns(s => s.Remeke == remark)
+                    .Where(s => s.Id == latestFirst.Id)
+                    .ExecuteCommand();
+            }
         }
 
+        // 鍘熼�昏緫淇濇寔...
         var mesReporting = Db.Queryable<MesReporting>()
             .Where(s => s.BillNo == womdaa.Daa001)
             .OrderByDescending(s => s.Id)

--
Gitblit v1.9.3