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 |  120 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 95 insertions(+), 25 deletions(-)

diff --git a/MES.Service/service/QC/SJService.cs b/MES.Service/service/QC/SJService.cs
index 4e6384f..b18ca30 100644
--- a/MES.Service/service/QC/SJService.cs
+++ b/MES.Service/service/QC/SJService.cs
@@ -127,7 +127,7 @@
     //                 result = "鏈娴�"
     //             }).ToList();
     // }
-    public List<QsItemIpiItem> SetQSItems(string itemNo)
+    public List<QsItemIpiItem> SetQSItems(string itemNo, decimal? workQty = null)
     {
         var db = SqlSugarHelper.GetInstance();
 
@@ -136,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;
     }
 
 

--
Gitblit v1.9.3