From 5f467d39b6bcca41c0720493c1f6e68c77ea89e4 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期日, 20 七月 2025 18:08:51 +0800
Subject: [PATCH] ftp服务器上传

---
 StandardInterface/MES.Service/service/QC/LljService.cs |  210 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 190 insertions(+), 20 deletions(-)

diff --git a/StandardInterface/MES.Service/service/QC/LljService.cs b/StandardInterface/MES.Service/service/QC/LljService.cs
index 6a7e76f..08a075a 100644
--- a/StandardInterface/MES.Service/service/QC/LljService.cs
+++ b/StandardInterface/MES.Service/service/QC/LljService.cs
@@ -313,28 +313,198 @@
 
     public List<QamftpDto> GetAttachments(string releaseNo)
     {
+        if (string.IsNullOrEmpty(releaseNo))
+        {
+            throw new ArgumentException("妫�楠屽崟鍙蜂笉鑳戒负绌�");
+        }
+        
         var db = SqlSugarHelper.GetInstance();
-        return db.Queryable<MesQamftp>()
-            .Where(x => x.ReleaseNo == releaseNo)
-            .OrderBy(x => x.Fdate, OrderByType.Desc)
-            .Select(x => new QamftpDto
+        try
+        {
+            return db.Queryable<MesQamftp>()
+                .Where(x => x.ReleaseNo == releaseNo)
+                .OrderBy(x => x.Fdate, OrderByType.Desc)
+                // .ThenBy(x => x.CreateDate, 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,
+                    ReleaseNo = x.ReleaseNo,
+                    F_type = x.F_type,
+                    LastupdateBy = x.LastupdateBy,
+                    LastupdateDate = x.LastupdateDate,
+                    ItemId = x.ItemId
+                }).ToList();
+        }
+        catch (Exception ex)
+        {
+            throw new Exception($"鏌ヨ闄勪欢淇℃伅澶辫触: {ex.Message}");
+        }
+    }
+
+    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鏂囦欢璺緞
+        string ftpPath = $"{normalizedServer}/IQC/{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())
             {
-                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,
-                ReleaseNo = x.ReleaseNo,
-                F_type = x.F_type,
-                LastupdateBy = x.LastupdateBy,
-                LastupdateDate = x.LastupdateDate,
-                ItemId = x.ItemId
-            }).ToList();
+                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}");
+        }
+    }
+    
+    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;
+    }
+
+    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; charset=utf-8",
+            ".log" => "text/plain; charset=utf-8",
+            ".md" => "text/markdown; charset=utf-8",
+            ".csv" => "text/csv; charset=utf-8",
+            ".xml" => "text/xml; charset=utf-8",
+            ".json" => "application/json; charset=utf-8",
+            
+            // 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",
+            
+            // 鍘嬬缉鏂囦欢
+            ".zip" => "application/zip",
+            ".rar" => "application/x-rar-compressed",
+            ".7z" => "application/x-7z-compressed",
+            
+            // 鍏朵粬甯歌鏍煎紡
+            ".dwg" => "application/acad",
+            ".dxf" => "application/dxf",
+            ".step" or ".stp" => "application/step",
+            ".iges" or ".igs" => "application/iges",
+            
+            // 榛樿浜岃繘鍒舵祦
+            _ => "application/octet-stream"
+        };
     }
     public int saveItem(LLJDto rkjDto)
     {

--
Gitblit v1.9.3