From caed1e3200578fae1e2ee6c8bf24826707d7596a Mon Sep 17 00:00:00 2001
From: 如洲 陈 <1278080563@qq.com>
Date: 星期二, 30 九月 2025 11:10:52 +0800
Subject: [PATCH] 首检巡检和平板自动更新优化

---
 MES.Service/service/QC/SJService.cs |  203 +++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 157 insertions(+), 46 deletions(-)

diff --git a/MES.Service/service/QC/SJService.cs b/MES.Service/service/QC/SJService.cs
index f853ea4..b18ca30 100644
--- a/MES.Service/service/QC/SJService.cs
+++ b/MES.Service/service/QC/SJService.cs
@@ -6,6 +6,8 @@
 using SqlSugar;
 using System.Net;
 using System.Xml.Linq;
+using System.Data;
+using System.Data.Common;
 
 namespace MES.Service.service.QC;
 
@@ -125,7 +127,7 @@
     //                 result = "鏈娴�"
     //             }).ToList();
     // }
-    public List<QsItemIpiItem> SetQSItems(string itemNo)
+    public List<QsItemIpiItem> SetQSItems(string itemNo, decimal? workQty = null)
     {
         var db = SqlSugarHelper.GetInstance();
 
@@ -134,31 +136,101 @@
 
         if (count <= 0) return [];
 
-        return db
-            .Queryable<MesQualityStandard>()
-            .Where(b => b.QsType == "1" && b.ItemNo == itemNo).Select(
-                b => new QsItemIpiItem
+        // 濡傛灉娌℃湁浼犻�掑伐鍗曟暟閲忥紝灏濊瘯浠庡伐鍗曡〃涓幏鍙�
+        if (workQty == null || workQty <= 0)
+        {
+            System.Diagnostics.Debug.WriteLine($"SJService.SetQSItems - itemNo: {itemNo}, workQty: {workQty}");
+            
+            // 閫氳繃鐗╂枡缂栫爜锛圖aa002锛夊尮閰嶅伐鍗�
+            var workOrder = db.Queryable<Womdaa>()
+                .Where(w => w.Daa002 == itemNo)
+                .OrderByDescending(w => w.Id)
+                .First();
+            
+            if (workOrder != null)
+            {
+                workQty = workOrder.Daa008; // 宸ュ崟鏁伴噺
+                System.Diagnostics.Debug.WriteLine($"Found work order by Daa002 (鐗╂枡缂栫爜): {workOrder.Daa001}, Daa002: {workOrder.Daa002}, Daa003: {workOrder.Daa003}, workQty: {workQty}");
+            }
+            else
+            {
+                System.Diagnostics.Debug.WriteLine($"No work order found for itemNo (鐗╂枡缂栫爜): {itemNo}");
+                
+                // 濡傛灉閫氳繃鐗╂枡缂栫爜鎵句笉鍒帮紝灏濊瘯閫氳繃浜у搧鍚嶇О鍖归厤
+                var workOrderByName = db.Queryable<Womdaa>()
+                    .Where(w => w.Daa003.Contains(itemNo) || itemNo.Contains(w.Daa003))
+                    .OrderByDescending(w => w.Id)
+                    .First();
+                
+                if (workOrderByName != null)
                 {
-                    ProjName = b.ProjName,
-                    ItemMod = b.ItemMod,
-                    InspectionMethod = b.InspectionMethod,
-                    UsingInstruments = b.UsingInstruments, 
-                    LevelNum = SqlFunc.IsNull(
-                        SqlFunc.IsNull(b.LevelNum * b.InspectionLevel, 1),
-                        b.InspectionLevel),
-                    MaxValue = b.MaxValue,
-                    StandardValue = b.StandardValue,
-                    MinValue = b.MinValue,
-                    Notes = b.Notes,
-                    FcheckLevel = b.FcheckLevel,
-                    FacLevel = b.FacLevel,
-                    QsCode = b.QsCode,
-                    QsName = b.QsName,
-                    Picture = b.Picture,
-                    Picturename = b.Picturename,
-                    result = "鏈娴�",
-                    isCheck = 0
-                }).ToList();
+                    workQty = workOrderByName.Daa008; // 宸ュ崟鏁伴噺
+                    System.Diagnostics.Debug.WriteLine($"Found work order by name: {workOrderByName.Daa001}, Daa002: {workOrderByName.Daa002}, Daa003: {workOrderByName.Daa003}, workQty: {workQty}");
+                }
+                else
+                {
+                    System.Diagnostics.Debug.WriteLine($"No work order found by name for itemNo: {itemNo}");
+                }
+            }
+        }
+
+        // 鍏堣幏鍙栧熀纭�鏁版嵁
+        var qualityStandards = db
+            .Queryable<MesQualityStandard>()
+            .Where(b => b.QsType == "1" && b.ItemNo == itemNo)
+            .ToList();
+
+        // 鍦ㄥ唴瀛樹腑璁$畻妫�楠屾暟閲�
+        return qualityStandards.Select(b => new QsItemIpiItem
+        {
+            ProjName = b.ProjName,
+            ItemMod = b.ItemMod,
+            InspectionMethod = b.InspectionMethod,
+            UsingInstruments = b.UsingInstruments,
+            LevelNum = CalculateInspectionQuantity(b.LevelNum, b.InspectionLevel, workQty),
+            MaxValue = b.MaxValue,
+            StandardValue = b.StandardValue,
+            MinValue = b.MinValue,
+            Notes = b.Notes,
+            FcheckLevel = b.FcheckLevel,
+            FacLevel = b.FacLevel,
+            QsCode = b.QsCode,
+            QsName = b.QsName,
+            Picture = b.Picture,
+            Picturename = b.Picturename,
+            result = "鏈娴�",
+            isCheck = 0
+        }).ToList();
+    }
+
+    /// <summary>
+    /// 璁$畻妫�楠屾暟閲忥細濡傛灉宸ュ崟鏁伴噺灏忎簬鎶介獙鏁伴噺锛屽垯妫�楠屾暟閲忎负宸ュ崟鏁伴噺锛屽惁鍒欎负鎶芥鏁伴噺
+    /// </summary>
+    /// <param name="levelNum">姣忔ā澶氬皯涓�</param>
+    /// <param name="inspectionLevel">妯℃暟</param>
+    /// <param name="workQty">宸ュ崟鏁伴噺</param>
+    /// <returns>妫�楠屾暟閲�</returns>
+    private decimal? CalculateInspectionQuantity(decimal? levelNum, decimal? inspectionLevel, decimal? workQty)
+    {
+        System.Diagnostics.Debug.WriteLine($"SJService.CalculateInspectionQuantity - INPUT: levelNum={levelNum}, inspectionLevel={inspectionLevel}, workQty={workQty}");
+        
+        // 鐩存帴浣跨敤levelNum浣滀负鎶芥鏁伴噺锛屼笉鍐嶈绠�
+        var samplingQuantity = levelNum ?? 1;
+        
+        System.Diagnostics.Debug.WriteLine($"SJService.CalculateInspectionQuantity - samplingQuantity (from levelNum): {samplingQuantity}");
+
+        // 濡傛灉娌℃湁宸ュ崟鏁伴噺锛屼娇鐢ㄦ娊妫�鏁伴噺
+        if (workQty == null || workQty <= 0)
+        {return samplingQuantity;
+        }
+
+        // 濡傛灉宸ュ崟鏁伴噺灏忎簬鎶芥鏁伴噺锛屽垯妫�楠屾暟閲忎负宸ュ崟鏁伴噺
+        // 濡傛灉宸ュ崟鏁伴噺澶т簬绛変簬鎶芥鏁伴噺锛屽垯妫�楠屾暟閲忎负鎶芥鏁伴噺
+        var result = workQty < samplingQuantity ? workQty : samplingQuantity;
+        
+        System.Diagnostics.Debug.WriteLine($"SJService.CalculateInspectionQuantity - FINAL: workQty={workQty}, samplingQuantity={samplingQuantity}, result={result}");
+        
+        return result;
     }
 
 
@@ -439,7 +511,6 @@
         var count = db.Queryable<QsItemIpiItemDetail>()
             .Where(s => s.Pid == detail.Pid).Count();
 
-
         var result = 0;
 
         if (qsItemIpiItem.LevelNum != count) return 0;
@@ -460,7 +531,6 @@
                 return commit;
             });
 
-
         //鑷姩鍒ゅ畾鏄惁鍚堟牸
         //鑾峰彇妫�楠屽崟鐨勬楠岄」鐩悊璁轰釜鏁�
         var sum = db.Queryable<QsItemIpiItem>()
@@ -476,30 +546,71 @@
         //瀹為檯涓暟绛変簬鐞嗚涓暟鏃跺妫�楠屽崟杩涜鍒ゅ畾
         if (sum == icount)
         {
-            result = 0;
-
-            //鑾峰彇杩欎釜妫�楠屽崟涓嬬殑鎵�鏈夊悎鏍肩殑妫�楠岀粨鏋�
-            passCount = db.Queryable<QsItemIpiItemDetail>()
-                .Where(s => s.Gid == detail.Gid && s.Fstand == "鈭�").Count();
-
-            //鍚堟牸鐨勬楠岀粨鏋滅瓑浜庢�绘楠屾暟瑙嗕负鍚堟牸
-            if (icount == passCount) result = 1;
-
-            useTransactionWithOracle += SqlSugarHelper.UseTransactionWithOracle(
-                db =>
-                {
-                    return db.Updateable<QsItemIpiReq>()
-                        .SetColumns(s => s.IsPass == result)
-                        .SetColumns(s => s.StatusUser == detail.CreateBy)
-                        .SetColumns(s => s.CompleteTime == DateTime.Now)
-                        .Where(s => s.Id == detail.Gid)
-                        .ExecuteCommand();
-                });
+            // 璋冪敤瀛樺偍杩囩▼杩涜鑷姩鍒ゅ畾鍜屾彁浜�
+            CallSJAutoResultStoredProcedure(detail.Gid, detail.CreateBy);
         }
 
         return useTransactionWithOracle;
     }
 
+    /// <summary>
+    /// 璋冪敤棣栨鑷姩鍒ゅ畾鍜屾彁浜ゅ瓨鍌ㄨ繃绋�
+    /// </summary>
+    /// <param name="gid">棣栨涓昏〃ID</param>
+    /// <param name="createBy">鎿嶄綔浜�</param>
+    private void CallSJAutoResultStoredProcedure(decimal? gid, string createBy)
+    {
+        try
+        {
+            var db = SqlSugarHelper.GetInstance();
+            
+            // 鑾峰彇棣栨鍗曚俊鎭�
+            var sjInfo = db.Queryable<QsItemIpiReq>()
+                .Where(s => s.Id == gid)
+                .First();
+                
+            if (sjInfo == null) return;
+
+            // 瀹氫箟杈撳嚭鍙傛暟
+            var outputResult = new SugarParameter("o_Result", null,
+                System.Data.DbType.Int32, ParameterDirection.Output, 4000);
+
+            var outputMessage = new SugarParameter("o_Msg", null,
+                System.Data.DbType.String, ParameterDirection.Output, 4000);
+
+            // 瀹氫箟杈撳叆鍙傛暟
+            var parameters = new List<SugarParameter>
+            {
+                new("p_Gid", gid, System.Data.DbType.Decimal, ParameterDirection.Input),
+                new("p_Bill_No", sjInfo.BillNo, System.Data.DbType.String, ParameterDirection.Input),
+                new("p_User", createBy, System.Data.DbType.String, ParameterDirection.Input),
+                outputResult,
+                outputMessage
+            };
+
+            // 鎵ц瀛樺偍杩囩▼
+            db.Ado.ExecuteCommand(
+                "BEGIN PRC_MES_SJ_AUTO_RESULT(:p_Gid, :p_Bill_No, :p_User, :o_Result, :o_Msg); END;",
+                parameters.ToArray());
+
+            // 鑾峰彇杈撳嚭鍙傛暟鐨勫��
+            var resultValue = outputResult.Value?.ToString();
+            var messageValue = outputMessage.Value?.ToString();
+
+            if ("1".Equals(resultValue)) 
+            {
+                throw new Exception($"棣栨鑷姩鍒ゅ畾澶辫触: {messageValue}");
+            }
+        }
+        catch (Exception ex)
+        {
+            // 璁板綍鏃ュ織浣嗕笉褰卞搷涓绘祦绋�
+            Console.WriteLine($"棣栨鑷姩鍒ゅ畾瀛樺偍杩囩▼璋冪敤澶辫触: {ex.Message}");
+            // 鍙互鏍规嵁闇�瑕佸喅瀹氭槸鍚︽姏鍑哄紓甯�
+            // throw new Exception($"棣栨鑷姩鍒ゅ畾澶辫触: {ex.Message}");
+        }
+    }
+
     public int UpdateQSItemDetail(QsItemIpiItemDetail detail)
     {
         var withOracle = SqlSugarHelper.UseTransactionWithOracle(db =>

--
Gitblit v1.9.3