From 8e5e2fb892df6cc8c61d7c03baa2412522ad11c4 Mon Sep 17 00:00:00 2001
From: xwt <2740516069@qq.com>
Date: 星期四, 25 九月 2025 19:17:11 +0800
Subject: [PATCH] SJ,XJ,RKJ优化

---
 StandardInterface/MES.Service/service/QC/SJService.cs |  370 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 350 insertions(+), 20 deletions(-)

diff --git a/StandardInterface/MES.Service/service/QC/SJService.cs b/StandardInterface/MES.Service/service/QC/SJService.cs
index e53d43e..9ea4fb8 100644
--- a/StandardInterface/MES.Service/service/QC/SJService.cs
+++ b/StandardInterface/MES.Service/service/QC/SJService.cs
@@ -206,14 +206,14 @@
                 IsPass = a.IsPass,
                 ItemId = a.ItemId,
                 Mnum = b.Mnum,  // 浠庝富琛ㄨ幏鍙朚NUM
-                Dnum = b.Dnum,  // 浠庝富琛ㄨ幏鍙朌NUM
+                Dnum = b.Dnum,  // 浠庡瓙琛ㄨ幏鍙朌NUM
                 Snum = a.Snum,  // 閫佹鎵规暟
                 Remarks = a.Remarks
             }).ToList();
 
         var array = qsItemIpiItems.Select(s => s.Id).ToArray();
         var qsItemIpiItemDetails = db.Queryable<QsItemIpiItemDetail>()
-            .Where(s => array.Contains(s.Pid))
+            .Where(s => array.Contains(s.Pid) && s.FcheckResu != "/")
             .GroupBy(s => s.Pid)
             .Select(s => new
             {
@@ -232,12 +232,59 @@
             else
             {
                 s.isCheck = find.count;
-                if (find.count == s.LevelNum && s.IsPass == 1)
-                    s.result = "鍚堟牸";
-                else if (find.count == s.LevelNum && s.IsPass == 0)
-                    s.result = "涓嶅悎鏍�";
+                
+                // 璁$畻瀹為檯闇�瑕佹楠岀殑鏁伴噺锛堟帓闄ゅ牭绌达級
+                int actualRequiredCount = (int)(s.LevelNum ?? 0);
+                if (s.HoleNumbers != null && s.HoleNumbers.Count > 0)
+                {
+                    // 濡傛灉鏈夌┐鍙蜂俊鎭紝璁$畻闈炲牭绌寸殑鏁伴噺
+                    actualRequiredCount = s.HoleNumbers.Count(h => !h.IsBlocked);
+                }
+                
+                // 璋冭瘯淇℃伅锛氱粨鏋滃垽瀹�
+                Console.WriteLine($"getQSItems: Pid={s.Id}, find.count={find.count}, actualRequiredCount={actualRequiredCount}, IsPass={s.IsPass}");
+                
+                // 濡傛灉鏈夋楠岃褰曪紝鏍规嵁鍚堟牸鎯呭喌鍒ゆ柇鐘舵��
+                if (find.count > 0)
+                {
+                    // 鑾峰彇鍚堟牸鏁伴噺
+                    var passCount = db.Queryable<QsItemIpiItemDetail>()
+                        .Where(a => a.Pid == s.Id && a.Fstand == "鈭�" && a.FcheckResu != "/").Count();
+                    
+                    // 鑾峰彇涓嶅悎鏍兼暟閲�
+                    var failCount = db.Queryable<QsItemIpiItemDetail>()
+                        .Where(a => a.Pid == s.Id && a.Fstand == "脳" && a.FcheckResu != "/").Count();
+                    
+                    // 濡傛灉鏈変笉鍚堟牸璁板綍锛岀洿鎺ユ樉绀轰笉鍚堟牸
+                    if (failCount > 0)
+                    {
+                        s.result = "涓嶅悎鏍�";
+                    }
+                    // 濡傛灉鍏ㄩ儴鍚堟牸涓旀暟閲忓尮閰嶏紝鏄剧ず鍚堟牸
+                    else if (passCount == actualRequiredCount && find.count == actualRequiredCount)
+                    {
+                        s.result = "鍚堟牸";
+                    }
+                    // 濡傛灉閮ㄥ垎瀹屾垚锛屾樉绀鸿繘琛屼腑
+                    else if (find.count < actualRequiredCount)
+                    {
+                        s.result = "杩涜涓�";
+                    }
+                    // 濡傛灉鍏ㄩ儴瀹屾垚浣嗗悎鏍兼暟閲忎笉瓒筹紝鏄剧ず涓嶅悎鏍�
+                    else if (find.count == actualRequiredCount && passCount < actualRequiredCount)
+                    {
+                        s.result = "涓嶅悎鏍�";
+                    }
+                    // 鍏朵粬鎯呭喌鏄剧ず杩涜涓�
+                    else
+                    {
+                        s.result = "杩涜涓�";
+                    }
+                }
                 else
+                {
                     s.result = "鏈畬鎴�";
+                }
             }
         });
 
@@ -301,7 +348,24 @@
             detail.Fstand = "鈭�";
             detail.FcheckResu = "1";
             detail.UpdateBy = item.From.StatusUser;
-            detail.count = (int?)s.LevelNum;
+            
+            // 璁$畻榛樿鍚堟牸鐨勬暟閲忥細(妫�楠屾暟 * 寮�绌存暟) - (妫�楠屾暟 * 鍫电┐鏁�)
+            if (s.HoleNumbers != null && s.HoleNumbers.Count > 0)
+            {
+                // 濡傛灉鏈夌┐鍙蜂俊鎭紝璁$畻寮�绌存暟鍜屽牭绌存暟
+                int openHoles = s.HoleNumbers.Count(h => !h.IsBlocked);  // 寮�绌存暟
+                int blockedHoles = s.HoleNumbers.Count(h => h.IsBlocked);  // 鍫电┐鏁�
+                int checkCount = (int)(s.LevelNum ?? 1);  // 妫�楠屾暟
+                
+                // 璁$畻锛�(妫�楠屾暟 * 寮�绌存暟) - (妫�楠屾暟 * 鍫电┐鏁�)
+                detail.count = (checkCount * openHoles) - (checkCount * blockedHoles);
+            }
+            else
+            {
+                // 鍚﹀垯浣跨敤LevelNum
+                detail.count = (int?)s.LevelNum;
+            }
+            
             SetQSItemDetail(detail);
         });
 
@@ -337,7 +401,24 @@
             detail.Fstand = "鈭�";
             detail.FcheckResu = "1";
             detail.UpdateBy = item.StatusUser;
-            detail.count = (int?)s.LevelNum;
+            
+            // 璁$畻榛樿鍚堟牸鐨勬暟閲忥細(妫�楠屾暟 * 寮�绌存暟) - (妫�楠屾暟 * 鍫电┐鏁�)
+            if (s.HoleNumbers != null && s.HoleNumbers.Count > 0)
+            {
+                // 濡傛灉鏈夌┐鍙蜂俊鎭紝璁$畻寮�绌存暟鍜屽牭绌存暟
+                int openHoles = s.HoleNumbers.Count(h => !h.IsBlocked);  // 寮�绌存暟
+                int blockedHoles = s.HoleNumbers.Count(h => h.IsBlocked);  // 鍫电┐鏁�
+                int checkCount = (int)(s.LevelNum ?? 1);  // 妫�楠屾暟
+                
+                // 璁$畻锛�(妫�楠屾暟 * 寮�绌存暟) - (妫�楠屾暟 * 鍫电┐鏁�)
+                detail.count = (checkCount * openHoles) - (checkCount * blockedHoles);
+            }
+            else
+            {
+                // 鍚﹀垯浣跨敤LevelNum
+                detail.count = (int?)s.LevelNum;
+            }
+            
             SetQSItemDetail(detail);
         });
 
@@ -402,17 +483,38 @@
         if (qsItemIpiItem == null) return 0;
 
         var count = db.Queryable<QsItemIpiItemDetail>()
-            .Where(s => s.Pid == detail.Pid).Count();
+            .Where(s => s.Pid == detail.Pid && s.FcheckResu != "/").Count();
 
+        // 璁$畻瀹為檯闇�瑕佹楠岀殑鏁伴噺锛堟帓闄ゅ牭绌达級
+        var actualRequiredCount = qsItemIpiItem.LevelNum;
+        if (qsItemIpiItem.HoleNumbers != null && qsItemIpiItem.HoleNumbers.Count > 0)
+        {
+            // 濡傛灉鏈夌┐鍙蜂俊鎭紝璁$畻闈炲牭绌寸殑鏁伴噺
+            actualRequiredCount = qsItemIpiItem.HoleNumbers.Count(h => !h.IsBlocked);
+        }
 
         var result = 0;
 
-        if (qsItemIpiItem.LevelNum != count) return 0;
+        // 姣旇緝瀹為檯闇�瑕佹楠岀殑鏁伴噺鍜屽疄闄呮楠岃褰曟暟閲�
+        if (actualRequiredCount != count) 
+        {
+            // 璋冭瘯淇℃伅锛氳褰曟暟閲忎笉鍖归厤
+            Console.WriteLine($"autoResult: actualRequiredCount={actualRequiredCount}, count={count}, Pid={detail.Pid}");
+            // 鍗充娇鏁伴噺涓嶅尮閰嶏紝涔熻缁х画鎵ц鍚庣画閫昏緫鏉ユ洿鏂癐S_PASS瀛楁
+            // return 0; // 娉ㄩ噴鎺夎繖琛岋紝璁╂柟娉曠户缁墽琛�
+        }
 
         var passCount = db.Queryable<QsItemIpiItemDetail>()
-            .Where(s => s.Pid == detail.Pid && s.Fstand == "鈭�").Count();
+            .Where(s => s.Pid == detail.Pid && s.Fstand == "鈭�" && s.FcheckResu != "/").Count();
 
-        if (count == passCount) result = 1;
+        // 璋冭瘯淇℃伅锛氱粺璁′俊鎭�
+        Console.WriteLine($"autoResult: count={count}, passCount={passCount}, Pid={detail.Pid}");
+
+        // 鍙湁鍦ㄦ暟閲忓尮閰嶆椂鎵嶈繘琛屽悎鏍兼�у垽鏂�
+        if (actualRequiredCount == count && count == passCount) 
+        {
+            result = 1;
+        }
 
         var useTransactionWithOracle = SqlSugarHelper.UseTransactionWithOracle(
             db =>
@@ -427,14 +529,30 @@
 
 
         //鑷姩鍒ゅ畾鏄惁鍚堟牸
-        //鑾峰彇妫�楠屽崟鐨勬楠岄」鐩悊璁轰釜鏁�
-        var sum = db.Queryable<QsItemIpiItem>()
-            .Where(s => s.Pid == detail.Gid).Sum(it => it.LevelNum);
-        if (sum == null || sum == 0) return 1;
+        //鑾峰彇妫�楠屽崟鐨勬楠岄」鐩疄闄呴渶瑕佹楠屼釜鏁帮紙鎺掗櫎鍫电┐锛�
+        var sum = 0;
+        var qsItems = db.Queryable<QsItemIpiItem>()
+            .Where(s => s.Pid == detail.Gid).ToList();
+        
+        foreach (var item in qsItems)
+        {
+            if (item.HoleNumbers != null && item.HoleNumbers.Count > 0)
+            {
+                // 濡傛灉鏈夌┐鍙蜂俊鎭紝璁$畻闈炲牭绌寸殑鏁伴噺
+                sum += item.HoleNumbers.Count(h => !h.IsBlocked);
+            }
+            else
+            {
+                // 鍚﹀垯浣跨敤LevelNum
+                sum += (int)(item.LevelNum ?? 0);
+            }
+        }
+        
+        if (sum == 0) return 1;
 
-        //鑾峰彇妫�楠屽崟涓嬬殑妫�楠岄」鐩疄闄呬釜鏁�
+        //鑾峰彇妫�楠屽崟涓嬬殑妫�楠岄」鐩疄闄呬釜鏁帮紙鎺掗櫎鍫电┐锛�
         var icount = db.Queryable<QsItemIpiItemDetail>()
-            .Where(s => s.Gid == detail.Gid).Count();
+            .Where(s => s.Gid == detail.Gid && s.FcheckResu != "/").Count();
 
         if (icount == 0) return 1;
 
@@ -548,7 +666,7 @@
         return withOracle;
     }
     //鍒锋柊妫�楠岄」鐩�
-    public (int result, string message) GenUpdate(decimal? id, string? no, string? user)
+    public (int result, string message) GenUpdate(decimal? id, string? no, string? user, decimal? mnum = null, string? dnum = null)
     {
         var outputResult = new SugarParameter("PO_RESULT", null, System.Data.DbType.Int32, ParameterDirection.Output, 4000);
         var outputMessage = new SugarParameter("PO_TEXT", null, System.Data.DbType.String, ParameterDirection.Output, 4000);
@@ -558,13 +676,15 @@
         new("P_ID", id, System.Data.DbType.Decimal, ParameterDirection.Input),
         new("P_NO", no, System.Data.DbType.String, ParameterDirection.Input),
         new("P_USER", user, System.Data.DbType.String, ParameterDirection.Input),
+        new("P_MNUM", mnum ?? 1, System.Data.DbType.Decimal, ParameterDirection.Input),
+        new("P_DNUM", dnum ?? "", System.Data.DbType.String, ParameterDirection.Input),
         outputResult,
         outputMessage
     };
 
         var db = SqlSugarHelper.GetInstance();
         db.Ado.ExecuteCommand(
-            "BEGIN PRC_GEN_UPDATE(:P_ID, :P_NO, :P_USER, :PO_RESULT, :PO_TEXT); END;",
+            "BEGIN PRC_GEN_UPDATE(:P_ID,:P_NO,:P_MNUM,:P_DNUM,:P_USER, :PO_RESULT, :PO_TEXT); END;",
             parameters.ToArray());
 
         int result = outputResult.Value == null ? -1 : Convert.ToInt32(outputResult.Value);
@@ -618,6 +738,216 @@
     }
 
     /// <summary>
+    /// 鑾峰彇闄勪欢淇℃伅
+    /// </summary>
+    /// <param name="itemNo">鐗╂枡缂栫爜</param>
+    /// <returns>闄勪欢鍒楄〃</returns>
+    public List<QamftpDto> GetAttachments(string itemNo)
+    {
+        var db = SqlSugarHelper.GetInstance();
+        try
+        {
+            return db.Queryable<MesQamftp>()
+                .Where(x => x.ItemNo == itemNo)
+                .OrderBy(x => x.Fdate, OrderByType.Desc)
+                .Select(x => new QamftpDto
+                {
+                    Id = x.Id,
+                    itemNo = x.ItemNo,
+                    Ftype = x.Ftype,
+                    Fattach = x.Fattach,
+                    Fversion = x.Fversion,
+                    Fdate = x.Fdate,
+                    CreateBy = x.CreateBy,
+                    CreateDate = x.CreateDate,
+                    Company = x.Company,
+                    Factory = x.Factory,
+                    F_type = x.F_type,
+                    LastupdateBy = x.LastupdateBy,
+                    LastupdateDate = x.LastupdateDate,
+                    ItemId = x.ItemId
+                }).ToList();
+        }
+        catch (Exception ex)
+        {
+            throw new Exception($"鏌ヨ闄勪欢淇℃伅澶辫触: {ex.Message}");
+        }
+    }
+
+    /// <summary>
+    /// 浠嶧TP鏈嶅姟鍣ㄨ幏鍙栨枃浠讹紙棣栨浣跨敤OPC鐩綍锛�
+    /// </summary>
+    /// <param name="itemNo">鐗╂枡缂栫爜</param>
+    /// <param name="fileName">鏂囦欢鍚�</param>
+    /// <param name="ftpServer">FTP鏈嶅姟鍣ㄥ湴鍧�</param>
+    /// <returns>鏂囦欢瀛楄妭鏁扮粍</returns>
+    public byte[] GetFtpFile(string itemNo, string fileName, string ftpServer)
+    {
+        // 鍙傛暟楠岃瘉
+        if (string.IsNullOrEmpty(itemNo) || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(ftpServer))
+        {
+            throw new ArgumentException("鍙傛暟涓嶈兘涓虹┖: itemNo, fileName, ftpServer");
+        }
+        
+        string ftpUser = "hm_ftp";
+        string ftpPwd = "dell_123";
+        
+        // 鏍囧噯鍖朏TP鏈嶅姟鍣ㄥ湴鍧�
+        string normalizedServer = NormalizeFtpServer(ftpServer);
+        
+        // 鏋勫缓FTP鏂囦欢璺緞 - 棣栨浣跨敤OPC鐩綍
+        string ftpPath = $"{normalizedServer}/OPC/{itemNo}/{fileName}";
+        
+        try
+        {
+            var request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(ftpPath);
+            request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
+            request.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPwd);
+            request.UseBinary = true;
+            request.UsePassive = false;
+            request.Timeout = 30000; // 30绉掕秴鏃�
+            request.ReadWriteTimeout = 30000;
+            
+            using (var response = (System.Net.FtpWebResponse)request.GetResponse())
+            using (var ftpStream = response.GetResponseStream())
+            using (var ms = new System.IO.MemoryStream())
+            {
+                if (ftpStream == null)
+                {
+                    throw new Exception("FTP鍝嶅簲娴佷负绌�");
+                }
+                
+                ftpStream.CopyTo(ms);
+                var fileBytes = ms.ToArray();
+                
+                if (fileBytes.Length == 0)
+                {
+                    return null; // 鏂囦欢涓虹┖鎴栦笉瀛樺湪
+                }
+                
+                return fileBytes;
+            }
+        }
+        catch (System.Net.WebException ex)
+        {
+            if (ex.Response is System.Net.FtpWebResponse ftpResponse)
+            {
+                switch (ftpResponse.StatusCode)
+                {
+                    case System.Net.FtpStatusCode.ActionNotTakenFileUnavailable:
+                        return null; // 鏂囦欢涓嶅瓨鍦�
+                    case System.Net.FtpStatusCode.NotLoggedIn:
+                        throw new Exception("FTP璁よ瘉澶辫触锛岃妫�鏌ョ敤鎴峰悕鍜屽瘑鐮�");
+                    case System.Net.FtpStatusCode.ActionNotTakenFilenameNotAllowed:
+                        throw new Exception("鏂囦欢鍚嶄笉琚厑璁告垨璺緞鏃犳晥");
+                    default:
+                        throw new Exception($"FTP閿欒 ({ftpResponse.StatusCode}): {ftpResponse.StatusDescription}");
+                }
+            }
+            
+            // 澶勭悊瓒呮椂鍜岀綉缁滈敊璇�
+            if (ex.Status == System.Net.WebExceptionStatus.Timeout)
+            {
+                throw new Exception("FTP杩炴帴瓒呮椂锛岃绋嶅悗閲嶈瘯");
+            }
+            
+            throw new Exception($"FTP杩炴帴澶辫触: {ex.Message}");
+        }
+        catch (Exception ex)
+        {
+            throw new Exception($"鑾峰彇鏂囦欢澶辫触: {ex.Message}");
+        }
+    }
+    
+    /// <summary>
+    /// 鏍囧噯鍖朏TP鏈嶅姟鍣ㄥ湴鍧�
+    /// </summary>
+    /// <param name="ftpServer">FTP鏈嶅姟鍣ㄥ湴鍧�</param>
+    /// <returns>鏍囧噯鍖栧悗鐨凢TP鏈嶅姟鍣ㄥ湴鍧�</returns>
+    private string NormalizeFtpServer(string ftpServer)
+    {
+        if (string.IsNullOrEmpty(ftpServer))
+        {
+            throw new ArgumentException("FTP鏈嶅姟鍣ㄥ湴鍧�涓嶈兘涓虹┖");
+        }
+        
+        // 纭繚浠tp://寮�澶�
+        string normalizedServer = ftpServer.StartsWith("ftp://") ? ftpServer : $"ftp://{ftpServer}";
+        
+        // 鐗规畩澶勭悊宸茬煡鏈嶅姟鍣ㄥ湴鍧�
+        if (normalizedServer == "ftp://36.26.21.214")
+        {
+            normalizedServer = "ftp://36.26.21.214:21";
+        }
+        else if (!normalizedServer.Contains(":") && normalizedServer.StartsWith("ftp://"))
+        {
+            normalizedServer += ":21"; // 榛樿FTP绔彛
+        }
+        
+        // 寮�鍙戠幆澧冧娇鐢ㄦ湰鍦版湇鍔″櫒
+        normalizedServer = "ftp://192.168.1.22:21";
+        
+        return normalizedServer;
+    }
+
+    /// <summary>
+    /// 鑾峰彇鏂囦欢鐨勫唴瀹圭被鍨�
+    /// </summary>
+    /// <param name="fileName">鏂囦欢鍚�</param>
+    /// <returns>MIME绫诲瀷</returns>
+    public string GetContentType(string fileName)
+    {
+        if (string.IsNullOrEmpty(fileName))
+            return "application/octet-stream";
+            
+        var extension = System.IO.Path.GetExtension(fileName).ToLower();
+        
+        return extension switch
+        {
+            // PDF鏂囦欢
+            ".pdf" => "application/pdf",
+            
+            // 鍥剧墖鏂囦欢
+            ".jpg" or ".jpeg" => "image/jpeg",
+            ".png" => "image/png",
+            ".gif" => "image/gif",
+            ".bmp" => "image/bmp",
+            ".webp" => "image/webp",
+            ".svg" => "image/svg+xml",
+            ".ico" => "image/x-icon",
+            
+            // 鏂囨湰鏂囦欢
+            ".txt" => "text/plain",
+            ".log" => "text/plain",
+            ".md" => "text/markdown",
+            ".html" or ".htm" => "text/html",
+            ".css" => "text/css",
+            ".js" => "application/javascript",
+            ".json" => "application/json",
+            ".xml" => "application/xml",
+            
+            // Office鏂囨。
+            ".doc" => "application/msword",
+            ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+            ".xls" => "application/vnd.ms-excel",
+            ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+            ".ppt" => "application/vnd.ms-powerpoint",
+            ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
+            
+            // 鍏朵粬甯歌鏍煎紡
+            ".csv" => "text/csv",
+            ".zip" => "application/zip",
+            ".rar" => "application/x-rar-compressed",
+            ".7z" => "application/x-7z-compressed",
+            ".tar" => "application/x-tar",
+            ".gz" => "application/gzip",
+            
+            // 榛樿
+            _ => "application/octet-stream"
+        };
+    }
+
+    /// <summary>
     /// 鐢熸垚绌村彿淇℃伅
     /// </summary>
     /// <param name="mnum">寮�绌存�绘暟</param>

--
Gitblit v1.9.3