From 45a69e109d3ed2f3ba6d03f722021d019cb8792a Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期二, 18 十一月 2025 10:40:28 +0800
Subject: [PATCH] 查询报工记录,获取最后一条报工记录,改为实际的工单ID

---
 Services/MesOrderStaManager.cs |  626 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 400 insertions(+), 226 deletions(-)

diff --git a/Services/MesOrderStaManager.cs b/Services/MesOrderStaManager.cs
index 8a4e5bc..abd4faa 100644
--- a/Services/MesOrderStaManager.cs
+++ b/Services/MesOrderStaManager.cs
@@ -91,144 +91,318 @@
     }
 
     /// <summary>
-    /// 璋冩満閫佹閫昏緫閲嶅啓鐗�
-    /// 娴佺▼锛�
-    /// 1. 璋冩満寮�濮嬶細鍙啓鍏ヨ皟鏈哄紑濮嬫椂闂�(MA_START_TIME)
-    /// 2. 閫佹鍛煎彨锛氬啓鍏ヨ皟鏈哄紑濮嬫椂闂�(鑻ユ湭鍐�)涓庨�佹鏃堕棿(MA_SHOUT_TIME)锛屽苟鍒涘缓棣栨鍗�
-    /// 3. 璋冩満瀹屾垚锛氫繚鎸佸師閫昏緫锛岄妫�鍚堟牸鍚庡啓鍏ュ紑宸�(START_TIME)涓庤皟鏈哄畬鎴�(MA_END_TIME)
-    /// 4. 鑻ユ渶鏂伴妫�缁撴灉涓衡�滀笉鍚堟牸鈥濓紝娓呯┖閫佹鏃堕棿(MA_SHOUT_TIME)锛屾彁绀洪噸鏂伴�佹锛屼笉绔嬪嵆閲嶅缓
-    ///    鐢ㄦ埛鍐嶆鐐瑰嚮閫佹鍛煎彨鎵嶅垱寤烘柊鐨勯妫�鍗�
+    ///     鏇存柊鏈哄櫒鏃堕棿骞跺鐞嗛妫�/閲嶉�佹
     /// </summary>
+    /// <param name="entity">
+    ///     宸ュ崟鐘舵�佸疄浣�,鍖呭惈鏈哄櫒鏃堕棿淇℃伅
+    ///     Flag锛�1 = 棣栨閫佹鍛煎彨锛�2 = 棣栨涓嶅悎鏍煎悗閲嶆柊閫佹锛涘叾瀹冨�间粎鏇存柊鏃堕棿涓嶈Е鍙戦妫�鍗曞垱寤�
+    /// </param>
+    /// <returns>鏇存柊鏄惁鎴愬姛</returns>
     public bool ChangeMachineTime(MesOrderSta entity)
     {
-        const string FirstCheckType = "棣栨";
-        const string FirstCheckResultOK = "鍚堟牸";
-        const string FirstCheckResultNG = "涓嶅悎鏍�";
-
-        if (entity == null || entity.Id <= 0) throw new ArgumentException("鍙傛暟閿欒");
-        var womdaa = Db.Queryable<Womdaa>().Where(s => s.Id == entity.OrderId).First();
+        var womdaa = Db.Queryable<Womdaa>()
+            .Where(s => s.Id == entity.OrderId).First();
         if (womdaa == null) throw new Exception("宸ュ崟涓嶅瓨鍦�");
 
-        // 褰撳墠鏁版嵁搴撶姸鎬佸揩鐓�
-        var dbSta = Db.Queryable<MesOrderSta>().Where(s => s.Id == entity.Id).First();
-        if (dbSta == null) throw new Exception("宸ュ崟鐘舵�佷笉瀛樺湪");
-
-        // 琛岀骇閿侊紝淇濊瘉鍒涘缓棣栨鍗曠殑骞跺彂瀹夊叏
-        Db.Ado.ExecuteCommand("SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE",
-            new SugarParameter("BILL_NO", womdaa.Daa001));
-
-        // 鏈�鏂伴妫�鍗曪紙鏈綔搴燂級
-        var latestFirst = Db.Queryable<MesQaItemsDetect02>()
-            .Where(s => s.Aufnr == womdaa.Daa001
-                        && s.Ftype == FirstCheckType
-                        && (s.Fcancel == null || s.Fcancel != "Y"))
-            .OrderBy(s => s.CreateDate, OrderByType.Desc)
-            .First();
-        var latestResult = latestFirst?.FcheckResu?.Trim();
-
-        // 鍒ゅ畾鎿嶄綔绫诲瀷
-        var hasStartTimeInput = !string.IsNullOrWhiteSpace(entity.MaStartTime);
-        var hasShoutTimeInput = !string.IsNullOrWhiteSpace(entity.MaShoutTime);
-        // 浠呭紑濮嬶細鏈夊紑濮嬫椂闂达紝閫佹鏃堕棿涓虹┖
-        var isStartOnly = hasStartTimeInput && !hasShoutTimeInput && dbSta.MaStartTime != entity.MaStartTime;
-        // 閫佹鍛煎彨锛氭湁閫佹鏃堕棿锛堝彲鑳藉悓鏃剁涓�娆″啓鍏ュ紑濮嬫椂闂达級
-        var isShoutCall = hasShoutTimeInput && dbSta.MaShoutTime != entity.MaShoutTime;
-        // 瀹屾垚閫昏緫淇濇寔锛氬悗闈㈤�氳繃棣栨鍚堟牸鏉′欢鍒ゆ柇
-
-        // 4) 鑻ユ渶鏂板垽瀹氱粨鏋溾�滀笉鍚堟牸鈥濓紝娓呯┖閫佹鏃堕棿锛屾彁绀洪渶閲嶆柊鍛煎彨锛屼笉鍒涘缓鏂板崟
-        if (string.Equals(latestResult, FirstCheckResultNG, StringComparison.OrdinalIgnoreCase))
+        // ================== 棣栨閫佹澶勭悊锛團lag == 1锛�==================
+        if (entity.Flag == 1)
         {
-            // 娓呯┖閫佹鏃堕棿锛堟暟鎹簱涓庤繑鍥炲疄浣擄級
-            entity.MaShoutTime = null;
-            entity.remark = "棣栨涓嶅悎鏍硷紝閫佹鏃堕棿宸叉竻绌猴紝璇烽噸鏂伴�佹鍛煎彨鐢熸垚鏂扮殑棣栨鍗�";
-            return Db.Updateable<MesOrderSta>()
-                .SetColumns(s => s.MaShoutTime == null)
-                .SetColumnsIF(hasStartTimeInput && dbSta.MaStartTime != entity.MaStartTime,
-                    s => s.MaStartTime == entity.MaStartTime) // 鍏佽鍚屾椂鍐欏叆鏂扮殑寮�濮嬫椂闂达紙濡傛灉鍒氫慨鏀癸級
-                .SetColumnsIF(entity.remark != null, s => s.remark == entity.remark)
-                .Where(s => s.Id == entity.Id)
-                .ExecuteCommand() > 0;
+            // 骞傜瓑锛氬厛鏌ユ槸鍚﹀凡鏈夋湭浣滃簾鐨勯妫�鍗曪紙閬垮厤閲嶅鐢熸垚锛�
+            var existsFirst = Db.Queryable<MesQaItemsDetect02>()
+                .Where(s => s.Aufnr == womdaa.Daa001 && s.Ftype == "棣栨" && (s.Fcancel == null || s.Fcancel != "Y"))
+                .Any();
+
+            if (!existsFirst)
+            {
+                // 琛岀骇閿侀槻骞跺彂锛圤racle FOR UPDATE锛�
+                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 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();
+            }
         }
-
-        // 1) 璋冩満寮�濮嬶細鍙啓寮�濮嬫椂闂�
-        if (isStartOnly)
+        // ================== 涓嶅悎鏍煎悗閲嶆柊閫佹锛團lag == 2锛�==================
+        else if (entity.Flag == 2)
         {
-            return Db.Updateable<MesOrderSta>()
-                .SetColumns(s => s.MaStartTime == entity.MaStartTime)
-                .Where(s => s.Id == entity.Id)
-                .ExecuteCommand() > 0;
-        }
-
-        // 2) 閫佹鍛煎彨锛氬啓鍏ュ紑濮嬫椂闂�(鑻ユ湭鍐�) + 閫佹鏃堕棿锛屽苟鍒涘缓棣栨鍗�
-        if (isShoutCall)
-        {
-            // 鑻ユ暟鎹簱灏氭棤寮�濮嬫椂闂达紝琛ュ啓
-            var needWriteStart = string.IsNullOrWhiteSpace(dbSta.MaStartTime) && hasStartTimeInput;
-
-            // 鍒涘缓棣栨鍗曪紙濮嬬粓鎸夐�佹鍛煎彨鐢熸垚涓�寮犳柊鐨勶紝濡傛灉闇�瑕佷綔搴熸棫鍗曞彲鍔犲彇娑堥�昏緫锛�
+            // 鍔犻攣闃插苟鍙戯紙琛岀骇閿侊級
             Db.Ado.ExecuteCommand(
-                "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
-                new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String));
+                "SELECT ID FROM WOMDAA WHERE DAA001 = :BILL_NO FOR UPDATE",
+                new SugarParameter("BILL_NO", womdaa.Daa001));
 
-            // 鑾峰彇鏂板缓棣栨鍗�
-            latestFirst = Db.Queryable<MesQaItemsDetect02>()
+            // 褰撳墠鏈�鏂版湭浣滃簾棣栨鍗�
+            var latestFirst = Db.Queryable<MesQaItemsDetect02>()
                 .Where(s => s.Aufnr == womdaa.Daa001
-                            && s.Ftype == FirstCheckType
+                            && s.Ftype == "棣栨"
                             && (s.Fcancel == null || s.Fcancel != "Y"))
                 .OrderBy(s => s.CreateDate, OrderByType.Desc)
                 .First();
 
+            bool needReCreate = false;
+
             if (latestFirst != null)
             {
-                var ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-                Db.Updateable<MesQaItemsDetect02>()
-                    .SetColumns(s => s.Remeke == $"宸ユ帶鏈轰簬{ts}璋冩満閫佹鐢熸垚棣栨鍗�")
-                    .Where(s => s.Id == latestFirst.Id)
-                    .ExecuteCommand();
+                var resu = latestFirst.FcheckResu?.Trim();
+                if (!string.IsNullOrEmpty(resu) &&
+                    (resu == "涓嶅悎鏍�" || resu.Equals("NG", StringComparison.OrdinalIgnoreCase)))
+                {
+                    needReCreate = true;
+                }
+            }
+            else
+            {
+                // 娌℃湁娲诲姩棣栨鍗曪紝鍒ゆ柇鏄惁瀛樺湪宸蹭綔搴熺殑涓嶅悎鏍奸妫�璁板綍
+                var existsCanceledNg = Db.Queryable<MesQaItemsDetect02>()
+                    .Where(s => s.Aufnr == womdaa.Daa001
+                                && s.Ftype == "棣栨"
+                                && s.Fcancel == "Y"
+                                && (s.FcheckResu == "涓嶅悎鏍�" || s.FcheckResu == "NG"))
+                    .Any();
+                if (existsCanceledNg)
+                {
+                    needReCreate = true;
+                    Console.WriteLine("[閲嶉�佹] 鏃犳椿鍔ㄩ妫�鍗曚絾瀛樺湪浣滃簾鐨勪笉鍚堟牸璁板綍锛屽厑璁搁噸寤�");
+                }
             }
 
-            entity.remark = "宸茬敓鎴愰妫�鍗曪紝绛夊緟妫�楠�";
-            var updateCount = Db.Updateable<MesOrderSta>()
-                .SetColumns(s => s.MaShoutTime == entity.MaShoutTime)
-                .SetColumnsIF(needWriteStart, s => s.MaStartTime == entity.MaStartTime)
-                .SetColumns(s => s.remark == entity.remark)
-                .Where(s => s.Id == entity.Id)
-                .ExecuteCommand();
-
-            return updateCount > 0;
-        }
-
-        // 3) 淇濈暀璋冩満瀹屾垚閫昏緫锛氶妫�鍚堟牸涓旈�佹鏃堕棿 >= 璋冩満寮�濮嬫椂闂存椂鑷姩鍐欏紑宸ヤ笌璋冩満瀹屾垚
-        // 锛堟澶勪笌鍘熼�昏緫涓�鑷达細鍓嶇涓嶇洿鎺ョ偣鈥滆皟鏈哄畬鎴愨�濓紝鑰屾槸鐢卞悎鏍艰Е鍙戯級
-        if (!string.IsNullOrEmpty(entity.MaShoutTime)
-            && DateTime.TryParse(entity.MaShoutTime, out var shoutTime)
-            && DateTime.TryParse(dbSta.MaStartTime ?? entity.MaStartTime, out var startTime)
-            && shoutTime >= startTime
-            && latestFirst != null
-            && string.Equals(latestFirst.FcheckResu?.Trim(), FirstCheckResultOK, StringComparison.OrdinalIgnoreCase))
-        {
-            // 鍐欏紑宸ュ強璋冩満瀹屾垚
-            QualifiedInspection(new OrderMachineDto
+            if (needReCreate)
             {
-                OrderId = entity.OrderId,
-                orderNo = entity.OrderNo,
-                machineNo = entity.MachineNo
-            });
+                // 浣滃簾褰撳墠娲诲姩涓嶅悎鏍煎崟
+                if (latestFirst != null)
+                {
+                    Db.Updateable<MesQaItemsDetect02>()
+                        .SetColumns(s => s.Fcancel == "Y")
+                        .Where(s => s.Id == latestFirst.Id
+                                    && (s.Fcancel == null || s.Fcancel != "Y"))
+                        .ExecuteCommand();
+                }
 
-            entity.StartTime = entity.MaShoutTime;
-            entity.MaEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+                bool procOk = true;
+                string procErr = "";
+                try
+                {
+                    // 璋冪敤瀛樺偍杩囩▼鐢熸垚鏂伴妫�鍗�
+                    Db.Ado.ExecuteCommand(
+                        "BEGIN AUTOMATIC_IPQC_FIRST_CHECK(:BILL_NO); END;",
+                        new SugarParameter("BILL_NO", womdaa.Daa001, System.Data.DbType.String));
+                }
+                catch (Exception ex)
+                {
+                    procOk = false;
+                    procErr = ex.Message;
+                    Console.WriteLine($"[閲嶉�佹] 瀛樺偍杩囩▼寮傚父: {procErr}");
+                }
 
-            return Db.Updateable<MesOrderSta>()
-                .SetColumnsIF(entity.MaEndTime != null, s => s.MaEndTime == entity.MaEndTime)
-                .SetColumnsIF(entity.StartTime != null, s => s.StartTime == entity.StartTime)
-                .Where(s => s.Id == entity.Id)
-                .ExecuteCommand() > 0;
+                // 鍐嶆鏌ヨ鏂扮敓鎴愮殑棣栨鍗�
+                var newLatest = Db.Queryable<MesQaItemsDetect02>()
+                    .Where(s => s.Aufnr == womdaa.Daa001
+                                && s.Ftype == "棣栨"
+                                && (s.Fcancel == null || s.Fcancel != "Y"))
+                    .OrderBy(s => s.CreateDate, OrderByType.Desc)
+                    .First();
+
+                // 瀛樺偍杩囩▼鏈敓鎴� 鈫� 鍏滃簳 C# 鑷姩鐢熸垚
+                if (newLatest == null)
+                {
+                    Console.WriteLine("[閲嶉�佹] 瀛樺偍杩囩▼鏈敓鎴愶紝鎵ц C# 鍏滃簳閫昏緫");
+                    var fallback = mesQaItemsDetect02Manager.AutomaticIpqcFirstCheck(womdaa.Daa001);
+                    if (fallback)
+                    {
+                        newLatest = 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 (newLatest != 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 == newLatest.Id)
+                        .ExecuteCommand();
+                }
+                else
+                {
+                    Console.WriteLine("[閲嶉�佹] 浠嶆湭鐢熸垚鏂伴妫�鍗曪紝璇锋鏌ュ瓨鍌ㄨ繃绋嬭鏁版槸鍚︽帓闄や綔搴熷崟銆�");
+                }
+            }
         }
 
-        // 鑻ユ湰娆¤皟鐢ㄦ湭鍖归厤浠讳綍鎿嶄綔锛堝彲鑳藉彧鏄噸澶嶆彁浜わ級锛屼繚鎸佷笉鍙�
-        return true;
-    }
+        // ========= 棣栨涓嶅悎鏍兼竻绌洪�佹鏃堕棿澶勭悊锛團lag != 2 鏃舵墠娓呯┖锛涘苟鐢熸垚缁熶竴 remark锛� =========
+        string remarkToSet = null;      // 鏈�缁堣鍐欏叆 MES_ORDER_STA.remark 鐨勫唴瀹�
+        var clearMaShoutTime = false;   // 鏄惁闇�瑕佺湡姝e啓 null 娓呯┖閫佹鏃堕棿
 
+        if (entity.Flag == 1)
+        {
+            // 棣栨閫佹鎻愪氦
+            if (!string.IsNullOrEmpty(entity.MaShoutTime))
+                remarkToSet = $"浜巤entity.MaShoutTime}鏃堕棿鏈変竴娆¢�佹";
+        }
+        else if (entity.Flag == 2)
+        {
+            // 涓嶅悎鏍奸噸鏂伴�佹锛堜笉娓呯┖閫佹鏃堕棿锛岀敱鍓嶇鍙兘瑕嗙洊涓烘柊鏃堕棿锛�
+            if (!string.IsNullOrEmpty(entity.MaShoutTime))
+                remarkToSet = $"浜巤entity.MaShoutTime}鏃堕棿涓嶅悎鏍奸噸鏂伴�佹";
+        }
+
+        // 闈為噸鏂伴�佹鍦烘櫙锛團lag!=2锛夋墠妫�鏌ユ槸鍚﹂渶瑕佸洜鈥滀笉鍚堟牸鈥濇竻绌洪�佹鏃堕棿
+        if (entity.Flag != 2)
+        {
+            var latestFirstCheck = 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 (latestFirstCheck != null && latestFirstCheck.FcheckResu == "涓嶅悎鏍�")
+            {
+                entity.MaShoutTime = null;  // 缃┖瀹炰綋锛堝悗闈㈡洿鏂版椂浣跨敤锛�
+                clearMaShoutTime = true;
+                remarkToSet = "棣栨涓嶅悎鏍硷紝閫佹鏃堕棿宸叉竻绌猴紝璇烽噸鏂伴�佹鍛煎彨鐢熸垚鏂扮殑棣栨鍗�";
+            }
+        }
+        // ========= 棣栨涓嶅悎鏍兼竻绌洪�佹鏃堕棿澶勭悊缁撴潫 =========
+
+        // ================== 閲囬泦鏁�/閿氱偣 Anchors 澶勭悊 ==================
+        var mesReporting = Db.Queryable<MesReporting>()
+            .Where(s => s.BillNo == womdaa.Daa001)
+            .OrderByDescending(s => s.Id)
+            .First();
+
+        var editDate = DateTime.Now.ToString("yyyy-MM-dd");
+
+        // 鍙戦�丠TTP璇锋眰鍒锋柊璁惧閲囬泦鏁版嵁锛堝け璐ヤ笉闃绘柇鍚庣画锛�
+        MesNumericalBycl mesNumerical = null;
+        try
+        {
+            using (var httpClient = new HttpClient())
+            {
+                httpClient.Timeout = TimeSpan.FromSeconds(30);
+                var content = new StringContent(
+                    JsonConvert.SerializeObject(new { machineNo = entity.MachineNo }),
+                    Encoding.UTF8,
+                    "application/json");
+                var response = httpClient
+                    .PostAsync("http://192.168.0.94:9095/Numerical/RefreshDevBycl", content)
+                    .GetAwaiter().GetResult();
+
+                if (response.IsSuccessStatusCode)
+                {
+                    var responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
+                    var responseObj = JsonConvert.DeserializeObject<dynamic>(responseString);
+                    if (responseObj != null && responseObj.code == 200)
+                    {
+                        mesNumerical = Db.Queryable<MesNumericalBycl>()
+                            .Where(s => s.EditDate == editDate && s.MachineNo == entity.MachineNo)
+                            .OrderByDescending(s => s.Id)
+                            .First();
+                    }
+                }
+            }
+        }
+        catch (Exception ex)
+        {
+            Console.WriteLine($"鍙戦�佹暟鎹埛鏂拌姹傛椂鍑洪敊: {ex.Message}");
+        }
+
+        // 鍒犻櫎骞堕噸寤哄綋鏃ラ敋鐐硅褰�
+        Db.Deleteable<MesAnchors>()
+            .Where(a => a.EditDate == editDate && a.OrderId == womdaa.Id)
+            .ExecuteCommand();
+
+        MesAnchors eAnchors = new MesAnchors
+        {
+            OrderId = womdaa.Id,
+            OrderNo = womdaa.Daa001,
+            EditDate = editDate,
+            Qty = mesReporting == null ? 0 : (long?)(mesReporting.DyQty ?? 0),
+            InitialValue = mesNumerical == null ? 0 : mesNumerical.CjNum
+        };
+        Db.Insertable<MesAnchors>(eAnchors).ExecuteCommand();
+
+        // ================== 棣栨鍚堟牸 -> 鑷姩璋冩満瀹屾垚 + 鍐欏叆寮�宸ユ椂闂� ==================
+        if (!clearMaShoutTime && !string.IsNullOrEmpty(entity.MaShoutTime)) // 鏈娓呯┖鐨勫墠鎻愪笅鎵嶅垽鏂悎鏍奸�昏緫
+        {
+            if (DateTime.TryParse(entity.MaShoutTime, out var sjTime) &&
+                DateTime.TryParse(entity.MaStartTime, out var startTime))
+            {
+                if (sjTime >= startTime)
+                {
+                    var sjRecord = Db.Queryable<MesQaItemsDetect02>()
+                        .Where(x => x.Aufnr == womdaa.Daa001 && x.Ftype == "棣栨" && (x.Fcancel == null || x.Fcancel != "Y"))
+                        .OrderBy(x => x.CreateDate, OrderByType.Desc)
+                        .First();
+
+                    if (sjRecord != null && sjRecord.FcheckResu == "鍚堟牸")
+                    {
+                        QualifiedInspection(new OrderMachineDto
+                        {
+                            OrderId = entity.OrderId,
+                            orderNo = entity.OrderNo,
+                            machineNo = entity.MachineNo
+                        });
+                        // 灏嗛�佹鏃堕棿鍐欏叆寮�宸ユ椂闂�
+                        entity.StartTime = entity.MaShoutTime;
+                        entity.MaEndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+                        // 鑻ヤ箣鍓� remark 鏄�滈�佹鈥濆彲缁х画娌跨敤锛涗笉寮哄埗瑕嗙洊
+                    }
+                }
+            }
+        }
+
+        // ================== 鏈�缁堟洿鏂� MES_ORDER_STA ==================
+        return Db.Updateable<MesOrderSta>()
+            // 閫佹鏃堕棿鏇存柊锛氳嫢闇�瑕佹竻绌哄垯鏄庣‘璧� null锛涘惁鍒欏綋鏈夊�兼椂鏇存柊
+            .SetColumnsIF(clearMaShoutTime, s => s.MaShoutTime == null)
+            .SetColumnsIF(!clearMaShoutTime && entity.MaShoutTime != null,
+                s => s.MaShoutTime == entity.MaShoutTime)
+            // 璋冩満寮�濮嬫椂闂�
+            .SetColumnsIF(entity.MaStartTime != null,
+                s => s.MaStartTime == entity.MaStartTime)
+            // 璋冩満瀹屾垚鏃堕棿
+            .SetColumnsIF(entity.MaEndTime != null,
+                s => s.MaEndTime == entity.MaEndTime)
+            // 寮�宸ユ椂闂�
+            .SetColumnsIF(entity.StartTime != null,
+                s => s.StartTime == entity.StartTime)
+            // 缁熶竴澶囨敞锛堢敱鍓嶉潰閫昏緫纭畾锛�
+            .SetColumnsIF(!string.IsNullOrEmpty(remarkToSet),
+                s => s.remark == remarkToSet)
+            // 杩囨护褰撳墠璁板綍
+            .Where(s => s.Id == entity.Id)
+            .ExecuteCommand() > 0;
+    }
 
     /// <summary>
     ///     鍒濆鍖栧伐鍗曠姸鎬�
@@ -380,125 +554,125 @@
 
                     break;
                 case "瀹屽伐":
-                {
-                    // 鏇存柊宸ュ崟閫夋嫨琛ㄦ樉绀虹姸鎬�
-                    // Db.Updateable<MesOrderSelect>()
-                    //     .SetColumns(a => a.IsShow == 1)
-                    //     .Where(a => orderIds.Contains(a.OrderId))
-                    //     .ExecuteCommand();
-
-                    // 鏇存柊宸ュ崟鐘舵�佽〃
-                    // Db.Updateable<MesOrderSta>()
-                    //     .SetColumns(a => a.IsShow == 0)
-                    //     .SetColumns(a => a.EndTime == endDate)
-                    //     .Where(a => orderIds.Contains(a.OrderId))
-                    //     .ExecuteCommand();
-
-                    // 澶勭悊姣忎釜宸ュ崟鐨勬姤宸ユ暟鎹�
-                    foreach (var orderId in orderIds)
                     {
-                        // 鏌ヨ宸ュ崟涓昏〃淇℃伅
-                        var womdaa = Db.Queryable<Womdaa>()
-                            .Where(s => s.Id == orderId).First();
+                        // 鏇存柊宸ュ崟閫夋嫨琛ㄦ樉绀虹姸鎬�
+                        // Db.Updateable<MesOrderSelect>()
+                        //     .SetColumns(a => a.IsShow == 1)
+                        //     .Where(a => orderIds.Contains(a.OrderId))
+                        //     .ExecuteCommand();
 
-                        //褰撳凡鐢熸垚鏁颁笉绛変簬瀹屽伐鏁版槸灏辫烦杩�
-                        if (womdaa.Daa008 > womdaa.Daa011)
+                        // 鏇存柊宸ュ崟鐘舵�佽〃
+                        // Db.Updateable<MesOrderSta>()
+                        //     .SetColumns(a => a.IsShow == 0)
+                        //     .SetColumns(a => a.EndTime == endDate)
+                        //     .Where(a => orderIds.Contains(a.OrderId))
+                        //     .ExecuteCommand();
+
+                        // 澶勭悊姣忎釜宸ュ崟鐨勬姤宸ユ暟鎹�
+                        foreach (var orderId in orderIds)
                         {
-                            updateable += 1;
-                            continue;
+                            // 鏌ヨ宸ュ崟涓昏〃淇℃伅
+                            var womdaa = Db.Queryable<Womdaa>()
+                                .Where(s => s.Id == orderId).First();
+
+                            //褰撳凡鐢熸垚鏁颁笉绛変簬瀹屽伐鏁版槸灏辫烦杩�
+                            if (womdaa.Daa008 > womdaa.Daa011)
+                            {
+                                updateable += 1;
+                                continue;
+                            }
+
+                            var btnLog = new MesWorkorderBtnLog
+                            {
+                                WorkNo = womdaa.Daa001,
+                                BtnType = "宸ユ帶鑷姩瀹屽伐",
+                                BtnDate = DateTime.Now
+                            };
+                            db.Insertable<MesWorkorderBtnLog>(btnLog)
+                                .ExecuteCommand();
+
+                            // 鏇存柊宸ュ崟涓昏〃鐘舵�� - 杩欓噷灏哤omdaa琛ㄧ殑daa018鏇存柊涓哄畬宸ョ姸鎬�
+                            updateable = db.Updateable<Womdaa>()
+                                .SetColumns(s => s.Daa018 == daa018)
+                                .SetColumnsIF("瀹屽伐".Equals(daa018),
+                                    s => s.Daa017 == DateTime.Now)
+                                .SetColumnsIF("鏆傚仠".Equals(daa018),
+                                    s => s.Daa034 == DateTime.Now)
+                                .Where(s => s.Id == orderId).ExecuteCommand();
+
+                            // Db.Deleteable<MesOrderSelect>()
+                            //     .Where(a =>  a.OrderId == orderId)
+                            //     .ExecuteCommand();
+                            //
+                            // Db.Deleteable<MesOrderSta>()
+                            //     .Where(a => a.OrderId == orderId)
+                            //     .ExecuteCommand();
+                            //鏇存柊宸ュ崟閫夋嫨琛ㄦ樉绀虹姸鎬�
+                            db.Updateable<MesOrderSelect>()
+                                .SetColumns(a => a.IsShow == 1)
+                                .Where(a => a.OrderId == orderId)
+                                .ExecuteCommand();
+
+                            //鏇存柊宸ュ崟鐘舵�佽〃
+                            db.Updateable<MesOrderSta>()
+                                .SetColumns(a => a.IsShow == 0)
+                                .SetColumns(a => a.EndTime == endDate)
+                                .Where(a => a.OrderId == orderId)
+                                .ExecuteCommand();
+
+                            // 鏌ヨ宸叉姤宸ユ暟閲�
+                            var reporting = db.Queryable<MesReporting>()
+                                .Where(s => s.BillNo == womdaa.Daa001)
+                                .Select(s => new MesReporting
+                                {
+                                    OkQty = SqlFunc.AggregateSum(s.OkQty),
+                                    BfQty = SqlFunc.AggregateSum(s.BfQty)
+                                }).First();
+
+                            reporting.OkQty ??= 0;
+                            reporting.BfQty ??= 0;
+
+                            // 鏌ヨ宸ュ崟瑙嗗浘淇℃伅
+                            // var vOrder = db.Queryable<VOrder>()
+                            //     .Where(s => s.ID == orderId).First();
+
+                            // 璁$畻寰呮姤宸ユ暟閲�
+                            // var reportingOkQty =
+                            //     Convert.ToDecimal(vOrder.todayOutput) -
+                            //     reporting.OkQty;
+                            var CjQty = Db.Queryable<MesNumericalBycl>()
+                                .Where(s => s.MachineNo == entity.MachineNo && s.EditDate == date)
+                                .OrderByDescending(s => s.CjTiem)
+                                .Select<long?>(s => s.CjNum).First();
+
+                            // 璁$畻寰呮姤宸ユ暟閲�
+                            var reportingOkQty = CjQty - reporting.OkQty;
+
+                            // 濡傛灉鏈夊緟鎶ュ伐鏁伴噺鍒欏垱寤烘姤宸ヨ褰�
+                            if (!(reportingOkQty > 0)) continue;
+                            var mesReporting = new MesReporting
+                            {
+                                CheckType = 1,
+                                BgDate = DateTime.Now,
+                                AddressCode = womdaa.AddressCode,
+                                MachineNo = womdaa.MachineNo,
+                                // BfQty = reportingOkQty,
+                                BfQty = 0,
+                                BlQty = reportingOkQty,
+                                OkQty = 0,
+                                ItemNo = womdaa.Daa002,
+                                BillNo = womdaa.Daa001,
+                                CjQty = CjQty,
+                                DyQty = reporting.OkQty
+                            };
+
+                            // 鎻掑叆鎶ュ伐璁板綍
+                            db.Insertable(mesReporting)
+                                .IgnoreColumns(true).ExecuteCommand();
                         }
 
-                        var btnLog = new MesWorkorderBtnLog
-                        {
-                            WorkNo = womdaa.Daa001,
-                            BtnType = "宸ユ帶鑷姩瀹屽伐",
-                            BtnDate = DateTime.Now
-                        };
-                        db.Insertable<MesWorkorderBtnLog>(btnLog)
-                            .ExecuteCommand();
-
-                        // 鏇存柊宸ュ崟涓昏〃鐘舵�� - 杩欓噷灏哤omdaa琛ㄧ殑daa018鏇存柊涓哄畬宸ョ姸鎬�
-                        updateable = db.Updateable<Womdaa>()
-                            .SetColumns(s => s.Daa018 == daa018)
-                            .SetColumnsIF("瀹屽伐".Equals(daa018),
-                                s => s.Daa017 == DateTime.Now)
-                            .SetColumnsIF("鏆傚仠".Equals(daa018),
-                                s => s.Daa034 == DateTime.Now)
-                            .Where(s => s.Id == orderId).ExecuteCommand();
-
-                        // Db.Deleteable<MesOrderSelect>()
-                        //     .Where(a =>  a.OrderId == orderId)
-                        //     .ExecuteCommand();
-                        //
-                        // Db.Deleteable<MesOrderSta>()
-                        //     .Where(a => a.OrderId == orderId)
-                        //     .ExecuteCommand();
-                        //鏇存柊宸ュ崟閫夋嫨琛ㄦ樉绀虹姸鎬�
-                        db.Updateable<MesOrderSelect>()
-                            .SetColumns(a => a.IsShow == 1)
-                            .Where(a => a.OrderId == orderId)
-                            .ExecuteCommand();
-
-                        //鏇存柊宸ュ崟鐘舵�佽〃
-                        db.Updateable<MesOrderSta>()
-                            .SetColumns(a => a.IsShow == 0)
-                            .SetColumns(a => a.EndTime == endDate)
-                            .Where(a => a.OrderId == orderId)
-                            .ExecuteCommand();
-
-                        // 鏌ヨ宸叉姤宸ユ暟閲�
-                        var reporting = db.Queryable<MesReporting>()
-                            .Where(s => s.BillNo == womdaa.Daa001)
-                            .Select(s => new MesReporting
-                            {
-                                OkQty = SqlFunc.AggregateSum(s.OkQty),
-                                BfQty = SqlFunc.AggregateSum(s.BfQty)
-                            }).First();
-                        
-                        reporting.OkQty ??= 0;
-                        reporting.BfQty ??= 0;
-                        
-                        // 鏌ヨ宸ュ崟瑙嗗浘淇℃伅
-                        // var vOrder = db.Queryable<VOrder>()
-                        //     .Where(s => s.ID == orderId).First();
-                        
-                        // 璁$畻寰呮姤宸ユ暟閲�
-                        // var reportingOkQty =
-                        //     Convert.ToDecimal(vOrder.todayOutput) -
-                        //     reporting.OkQty;
-                        var CjQty = Db.Queryable<MesNumericalBycl>()
-                            .Where(s => s.MachineNo == entity.MachineNo && s.EditDate == date)
-                            .OrderByDescending(s=>s.CjTiem)
-                            .Select<long?>(s=>s.CjNum).First();
-                        
-                        // 璁$畻寰呮姤宸ユ暟閲�
-                        var reportingOkQty = CjQty - reporting.OkQty;
-                        
-                        // 濡傛灉鏈夊緟鎶ュ伐鏁伴噺鍒欏垱寤烘姤宸ヨ褰�
-                        if (!(reportingOkQty > 0)) continue;
-                        var mesReporting = new MesReporting
-                        {
-                            CheckType = 1,
-                            BgDate = DateTime.Now,
-                            AddressCode = womdaa.AddressCode,
-                            MachineNo = womdaa.MachineNo,
-                            // BfQty = reportingOkQty,
-                            BfQty = 0,
-                            BlQty = reportingOkQty,
-                            OkQty = 0,
-                            ItemNo = womdaa.Daa002,
-                            BillNo = womdaa.Daa001,
-                            CjQty = CjQty,
-                            DyQty = reporting.OkQty
-                        };
-                        
-                        // 鎻掑叆鎶ュ伐璁板綍
-                        db.Insertable(mesReporting)
-                            .IgnoreColumns(true).ExecuteCommand();
+                        break;
                     }
-
-                    break;
-                }
             }
 
 

--
Gitblit v1.9.3