From 527bed43ea41443fd602305cb673fb4337179d51 Mon Sep 17 00:00:00 2001
From: xwt <2740516069@qq.com>
Date: 星期二, 21 十月 2025 14:40:45 +0800
Subject: [PATCH] SJ逻辑补充

---
 StandardInterface/MES.Service/service/QC/SJService.cs |  355 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 353 insertions(+), 2 deletions(-)

diff --git a/StandardInterface/MES.Service/service/QC/SJService.cs b/StandardInterface/MES.Service/service/QC/SJService.cs
index 91e8c9e..aa32e8c 100644
--- a/StandardInterface/MES.Service/service/QC/SJService.cs
+++ b/StandardInterface/MES.Service/service/QC/SJService.cs
@@ -676,8 +676,8 @@
         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),
+        new("P_MNUM", mnum, System.Data.DbType.Decimal, ParameterDirection.Input),
+        new("P_DNUM", dnum, System.Data.DbType.String, ParameterDirection.Input),
         outputResult,
         outputMessage
     };
@@ -735,6 +735,234 @@
         {
             throw new Exception(ex.Message);
         }
+    }
+
+    /// <summary>
+    /// 鑾峰彇闄勪欢淇℃伅
+    /// </summary>
+    /// <param name="itemNo">鐗╂枡缂栫爜</param>
+    /// <returns>闄勪欢鍒楄〃</returns>
+    public List<QamftpDto> GetAttachments(string itemNo, string projName = null)
+    {
+        var db = SqlSugarHelper.GetInstance();
+        try
+        {
+            var query = db.Queryable<MesQamftp>()
+                .Where(x => x.ItemNo == itemNo)
+                .Where(x => x.Ftype == "棣栨");  
+            
+            // 濡傛灉浼犲叆浜唒rojName锛屽垯鎸塅version杩囨护
+            if (!string.IsNullOrEmpty(projName))
+            {
+                query = query.Where(x => x.Fversion == projName);
+            }
+            
+            return query.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,
+                    Pid = x.Pid
+                }).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>
+    /// <param name="projName">椤圭洰鍚嶇О</param>
+    /// <returns>鏂囦欢瀛楄妭鏁扮粍</returns>
+    public byte[] GetFtpFile(string itemNo, string fileName, string ftpServer, string projName = null)
+    {
+        // 鍙傛暟楠岃瘉
+        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鐩綍锛屽鏋滀紶鍏ヤ簡projName鍒欎娇鐢ㄦ柊鏍煎紡
+        string ftpPath;
+        if (!string.IsNullOrEmpty(projName))
+        {
+            ftpPath = $"{normalizedServer}/OPC/{itemNo}/{projName}/{fileName}";
+        }
+        else
+        {
+            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>
@@ -800,4 +1028,127 @@
 
         return result;
     }
+
+    /// <summary>
+    /// 涓婁紶鍥剧墖鍒版楠岄」鐩殑PICTURE瀛楁
+    /// </summary>
+    /// <param name="id">妫�楠岄」鐩甀D</param>
+    /// <param name="imageBytes">鍥剧墖瀛楄妭鏁扮粍</param>
+    /// <param name="fileName">鍘熷鏂囦欢鍚�</param>
+    /// <param name="createBy">鍒涘缓浜�</param>
+    /// <returns>鎿嶄綔缁撴灉</returns>
+    public (int status, string message) UploadImageToPicture(decimal id, byte[] imageBytes, string fileName, string createBy)
+    {
+        try
+        {
+            if (imageBytes == null || imageBytes.Length == 0)
+            {
+                return (1, "鍥剧墖鏁版嵁涓虹┖");
+            }
+
+            if (string.IsNullOrEmpty(fileName))
+            {
+                return (1, "鏂囦欢鍚嶄负绌�");
+            }
+
+            // 楠岃瘉鍥剧墖鏍煎紡
+            var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp" };
+            var extension = System.IO.Path.GetExtension(fileName).ToLower();
+            if (!allowedExtensions.Contains(extension))
+            {
+                return (1, "涓嶆敮鎸佺殑鍥剧墖鏍煎紡锛屼粎鏀寔锛歫pg, jpeg, png, gif, bmp, webp");
+            }
+
+            // 楠岃瘉鍥剧墖澶у皬锛堥檺鍒朵负5MB锛�
+            if (imageBytes.Length > 5 * 1024 * 1024)
+            {
+                return (1, "鍥剧墖澶у皬涓嶈兘瓒呰繃5MB");
+            }
+
+            // 鐢熸垚鏃堕棿鎴虫枃浠跺悕锛屾牸寮忥細1746945271304.jpg
+            var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
+            var timestampFileName = $"{timestamp}{extension}";
+
+            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
+            {
+                // 妫�鏌ユ楠岄」鐩槸鍚﹀瓨鍦�
+                var exists = db.Queryable<QsItemIpiItem>()
+                    .Where(s => s.Id == id)
+                    .Any();
+
+                if (!exists)
+                {
+                    throw new Exception("妫�楠岄」鐩笉瀛樺湪");
+                }
+
+                // 鏇存柊PICTURE瀛楁锛圠ONG RAW绫诲瀷锛夊拰PICTURENAME瀛楁锛堟椂闂存埑鏂囦欢鍚嶏級
+                var updateResult = db.Updateable<QsItemIpiItem>()
+                    .SetColumns(s => s.Picture == imageBytes)
+                    .SetColumns(s => s.Picturename == timestampFileName)
+                    .Where(s => s.Id == id)
+                    .ExecuteCommand();
+
+                return updateResult;
+            });
+
+            if (result > 0)
+            {
+                return (0, "鍥剧墖淇濆瓨鎴愬姛");
+            }
+            else
+            {
+                return (1, "鍥剧墖淇濆瓨澶辫触锛屾湭鎵惧埌瀵瑰簲鐨勬楠岄」鐩�");
+            }
+        }
+        catch (Exception ex)
+        {
+            return (1, $"鍥剧墖淇濆瓨澶辫触锛歿ex.Message}");
+        }
+    }
+
+    /// <summary>
+    /// 鍒犻櫎妫�楠岄」鐩殑鍥剧墖
+    /// </summary>
+    /// <param name="id">妫�楠岄」鐩甀D</param>
+    /// <returns>鎿嶄綔缁撴灉</returns>
+    public (int status, string message) DeleteImageFromPicture(decimal id)
+    {
+        try
+        {
+            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
+            {
+                // 妫�鏌ユ楠岄」鐩槸鍚﹀瓨鍦�
+                var exists = db.Queryable<QsItemIpiItem>()
+                    .Where(s => s.Id == id)
+                    .Any();
+
+                if (!exists)
+                {
+                    throw new Exception("妫�楠岄」鐩笉瀛樺湪");
+                }
+
+                // 娓呯┖PICTURE瀛楁鍜孭ICTURENAME瀛楁
+                var updateResult = db.Updateable<QsItemIpiItem>()
+                    .SetColumns(s => s.Picture == null)
+                    .SetColumns(s => s.Picturename == null)
+                    .Where(s => s.Id == id)
+                    .ExecuteCommand();
+
+                return updateResult;
+            });
+
+            if (result > 0)
+            {
+                return (0, "鍥剧墖鍒犻櫎鎴愬姛");
+            }
+            else
+            {
+                return (1, "鍥剧墖鍒犻櫎澶辫触锛屾湭鎵惧埌瀵瑰簲鐨勬楠岄」鐩�");
+            }
+        }
+        catch (Exception ex)
+        {
+            return (1, $"鍥剧墖鍒犻櫎澶辫触锛歿ex.Message}");
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3