From e853dd2fecef3a0c446d161248d0498a5a081e66 Mon Sep 17 00:00:00 2001
From: xwt <2740516069@qq.com>
Date: 星期三, 17 十二月 2025 18:49:49 +0800
Subject: [PATCH] SJ,XJ,RKJ优化修改

---
 StandardInterface/MES.Service/service/QC/XJService.cs |  339 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 311 insertions(+), 28 deletions(-)

diff --git a/StandardInterface/MES.Service/service/QC/XJService.cs b/StandardInterface/MES.Service/service/QC/XJService.cs
index 8d3ddfc..9de79c9 100644
--- a/StandardInterface/MES.Service/service/QC/XJService.cs
+++ b/StandardInterface/MES.Service/service/QC/XJService.cs
@@ -153,10 +153,23 @@
                 )
             );
             
-        // 娣诲姞绾夸綋绛涢�夋潯浠�
+        // 娣诲姞绾夸綋绛涢�夋潯浠讹紙鍩轰簬鐢ㄦ埛鏉冮檺锛�
         if (lineNo != null && lineNo.Length > 0)
         {
             query = query.Where((s, a, c, b) => lineNo.Contains(c.LineNo));
+        }
+        
+        // 娣诲姞QC绛涢�夋潯浠讹細杞﹂棿鍜岀嚎浣�
+        if (!string.IsNullOrEmpty(queryObj.departmentId))
+        {
+            // 濡傛灉鎸囧畾浜嗚溅闂达紝绛涢�夎杞﹂棿涓嬬殑绾夸綋
+            query = query.Where((s, a, c, b) => c.DepotId.ToString() == queryObj.departmentId);
+        }
+        
+        if (!string.IsNullOrEmpty(queryObj.lineId))
+        {
+            // 濡傛灉鎸囧畾浜嗙嚎浣擄紝鐩存帴绛涢�夌嚎浣�
+            query = query.Where((s, a, c, b) => c.LineNo == queryObj.lineId);
         }
         
         // 娣诲姞鐘舵�佺瓫閫夋潯浠� - 鏄庣‘鍖归厤鍓嶇浼犻�掔殑鍊�
@@ -287,6 +300,7 @@
         var qsQaItemXj01s = db.Queryable<QsQaItemXj01>()
             .WhereIF(pid > 0, a => a.Pid == pid)
             .WhereIF(id > 0, a => a.Id == id)
+            .OrderBy(a => a.Forder ?? 0)
             .ToList();
 
         var array = qsQaItemXj01s.Select(s => s.Id).ToArray();
@@ -323,8 +337,52 @@
                 s.imageData = Convert.ToBase64String(s.Picture);
         });
 
-        //鎺掑簭锛屾湭瀹屾垚鐨勬帓鍦ㄥ墠闈�
-        qsQaItemXj01s = qsQaItemXj01s.OrderBy(s => s.isCheck).ToList();
+        // 浠� QS_ITEM_IPI_IMAGE 琛ㄤ腑鑾峰彇鍥剧墖鏁版嵁锛堟敮鎸佸鍥剧墖锛屽贰妫� QS_TYPE=2锛�
+        var itemIds = qsQaItemXj01s.Select(s => s.Id).ToArray();
+        var imageList = db.Queryable<QsItemIpiImage>()
+            .Where(s => itemIds.Contains(s.Fid) && s.QsType == 2 && s.ImageData != null)
+            .ToList();
+        
+        if (imageList.Count > 0)
+        {
+            // 鎸夋楠岄」鐩甀D鍒嗙粍
+            var imageGroups = imageList.GroupBy(img => img.Fid);
+            foreach (var group in imageGroups)
+            {
+                var xjItem = qsQaItemXj01s.Find(t => t.Id == group.Key);
+                if (xjItem != null)
+                {
+                    // 鍒濆鍖栧浘鐗囧垪琛�
+                    xjItem.imageList = new List<ImageInfo>();
+                    
+                    foreach (var img in group)
+                    {
+                        if (img.ImageData != null && img.ImageData.Length > 0)
+                        {
+                            xjItem.imageList.Add(new ImageInfo
+                            {
+                                Id = img.Id ?? 0,
+                                FileName = img.Picturename,
+                                Base64Data = Convert.ToBase64String(img.ImageData),
+                                CreateDate = img.CreateDate,
+                                CreateBy = img.CreateBy
+                            });
+                        }
+                    }
+                    
+                    // 淇濇寔鍚戝悗鍏煎锛氬鏋滄湁鍥剧墖锛岃缃涓�寮犲浘鐗囦负 imageData
+                    if (xjItem.imageList.Count > 0)
+                    {
+                        xjItem.imageData = xjItem.imageList[0].Base64Data;
+                    }
+                }
+            }
+        }
+
+        //鎺掑簭锛氬彧鎸塅ORDER椤哄簭鎺掑簭
+        qsQaItemXj01s = qsQaItemXj01s
+            .OrderBy(s => s.Forder ?? 0)
+            .ToList();
 
         return qsQaItemXj01s;
     }
@@ -418,8 +476,35 @@
         var xjDto = new XJDto();
 
         var db = SqlSugarHelper.GetInstance();
-        var qsQaItemXj01 =
-            db.Queryable<QsQaItemXj01>().Single(s => s.Id == id);
+        
+        // 浣跨敤JOIN鏌ヨ鑾峰彇瀛愯〃鏁版嵁鍜屼富琛ㄧ殑ItemNo瀛楁
+        var qsQaItemXj01 = db
+            .Queryable<QsQaItemXj01, QsQaItemXj>((a, b) => new JoinQueryInfos(JoinType.Left, a.Pid == b.Id))
+            .Where((a, b) => a.Id == id)
+            .Select((a, b) => new QsQaItemXj01
+            {
+                Id = a.Id,
+                Pid = a.Pid,
+                ProjName = a.ProjName,
+                ItemMod = a.ItemMod,
+                InspectionMethod = a.InspectionMethod,
+                UsingInstruments = a.UsingInstruments,
+                LevelNum = a.LevelNum,
+                MaxValue = a.MaxValue,
+                StandardValue = a.StandardValue,
+                MinValue = a.MinValue,
+                Notes = a.Notes,
+                FcheckLevel = a.FcheckLevel,
+                FacLevel = a.FacLevel,
+                QsCode = a.QsCode,
+                QsName = a.QsName,
+                Picture = a.Picture,
+                Picturename = a.Picturename,
+                IsPass = a.IsPass,
+                Remarks = a.Remarks,
+                // 娣诲姞涓昏〃鐨処temNo瀛楁
+                ItemNo = b.ItemNo
+            }).Single();
 
         if (qsQaItemXj01.IsPass == 0)
             qsQaItemXj01.result = "涓嶅悎鏍�";
@@ -432,8 +517,36 @@
             qsQaItemXj01.imageData =
                 Convert.ToBase64String(qsQaItemXj01.Picture);
 
-        xjDto.ItemXj01 = qsQaItemXj01;
+        // 浠� QS_ITEM_IPI_IMAGE 琛ㄤ腑鑾峰彇鍥剧墖鍒楄〃锛堝贰妫� QS_TYPE=2锛�
+        var imageRecords = db.Queryable<QsItemIpiImage>()
+            .Where(s => s.Fid == id && s.QsType == 2 && s.ImageData != null)
+            .ToList();
+        
+        if (imageRecords.Count > 0)
+        {
+            qsQaItemXj01.imageList = new List<ImageInfo>();
+            foreach (var img in imageRecords)
+            {
+                if (img.ImageData != null && img.ImageData.Length > 0)
+                {
+                    qsQaItemXj01.imageList.Add(new ImageInfo
+                    {
+                        Id = img.Id ?? 0,
+                        FileName = img.Picturename,
+                        Base64Data = Convert.ToBase64String(img.ImageData),
+                        CreateDate = img.CreateDate,
+                        CreateBy = img.CreateBy
+                    });
+                }
+            }
+            // 淇濇寔鍚戝悗鍏煎
+            if (qsQaItemXj01.imageList.Count > 0)
+            {
+                qsQaItemXj01.imageData = qsQaItemXj01.imageList[0].Base64Data;
+            }
+        }
 
+        xjDto.ItemXj01 = qsQaItemXj01;
 
         xjDto.ItemXj02s = db.Queryable<QsQaItemXj02>().Where(s => s.Pid == id)
             .ToList();
@@ -652,24 +765,39 @@
     {
         try
         {
-            var db = SqlSugarHelper.GetInstance();
-            
-            // 鐩存帴鏇存柊Fsubmit瀛楁涓�1锛堝凡鎻愪氦鐘舵�侊級
-            var result = db.Updateable<QsQaItemXj>()
-                .SetColumns(s => s.Fsubmit == 1)
-                .SetColumns(s => s.FsubmitBy == dto.userNo)
-                .SetColumns(s => s.FsubmitDate == DateTime.Now)
-                .Where(s => s.Id == dto.id)
-                .ExecuteCommand();
+            // 瀹氫箟杈撳嚭鍙傛暟
+            var outputResult = new SugarParameter("c_res", null,
+                System.Data.DbType.Int32, ParameterDirection.Output,
+                4000);
 
-            if (result > 0)
+            var outputMessage = new SugarParameter("c_msg", null,
+                System.Data.DbType.String,
+                ParameterDirection.Output, 4000);
+
+            // 瀹氫箟杈撳叆鍙傛暟锛屽浐瀹欶LAG涓�1锛堝鏍革級
+            var parameters = new List<SugarParameter>
             {
-                return true;
-            }
-            else
-            {
-                throw new Exception("鎻愪氦澶辫触锛氭湭鎵惧埌瀵瑰簲鐨勬楠屽崟");
-            }
+                new("P_ID", dto.id, System.Data.DbType.Decimal, ParameterDirection.Input),
+                new("P_FLAG", 1, System.Data.DbType.Int32, ParameterDirection.Input),
+                new("P_USER", dto.userNo, System.Data.DbType.String, ParameterDirection.Input),
+                outputResult,
+                outputMessage
+            };
+
+            var db = SqlSugarHelper.GetInstance();
+
+            // 浣跨敤 SqlSugar 鎵ц瀛樺偍杩囩▼
+            db.Ado.ExecuteCommand(
+                "BEGIN PRC_WOMDAA_XJ_UPDATE_RES(:P_ID, :P_FLAG, :P_USER, :c_res, :c_msg); END;",
+                parameters.ToArray());
+
+            // 鑾峰彇杈撳嚭鍙傛暟鐨勫��
+            var resultValue = outputResult.Value?.ToString();
+            var messageValue = outputMessage.Value?.ToString();
+
+            if ("1".Equals(resultValue)) throw new Exception(messageValue);
+
+            return true;
         }
         catch (Exception ex)
         {
@@ -751,15 +879,24 @@
     /// 鑾峰彇闄勪欢淇℃伅
     /// </summary>
     /// <param name="itemNo">鐗╂枡缂栫爜</param>
+    /// <param name="projName">椤圭洰鍚嶇О锛堝彲閫夛紝鐢ㄤ簬杩囨护锛�</param>
     /// <returns>闄勪欢鍒楄〃</returns>
-    public List<QamftpDto> GetAttachments(string itemNo)
+    public List<QamftpDto> GetAttachments(string itemNo, string projName = null)
     {
         var db = SqlSugarHelper.GetInstance();
         try
         {
-            return db.Queryable<MesQamftp>()
+            var query = db.Queryable<MesQamftp>()
                 .Where(x => x.ItemNo == itemNo)
-                .OrderBy(x => x.Fdate, OrderByType.Desc)
+                .Where(x => x.Ftype == "宸℃");  // 娣诲姞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,
@@ -790,8 +927,9 @@
     /// <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)
+    public byte[] GetFtpFile(string itemNo, string fileName, string ftpServer, string projName = null)
     {
         // 鍙傛暟楠岃瘉
         if (string.IsNullOrEmpty(itemNo) || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(ftpServer))
@@ -805,8 +943,16 @@
         // 鏍囧噯鍖朏TP鏈嶅姟鍣ㄥ湴鍧�
         string normalizedServer = NormalizeFtpServer(ftpServer);
         
-        // 鏋勫缓FTP鏂囦欢璺緞 - 宸℃浣跨敤OPC鐩綍
-        string ftpPath = $"{normalizedServer}/OPC/{itemNo}/{fileName}";
+        // 鏋勫缓FTP鏂囦欢璺緞 - 宸℃浣跨敤OPC鐩綍锛屽鏋滀紶鍏ヤ簡projName鍒欎娇鐢ㄦ柊鏍煎紡
+        string ftpPath;
+        if (!string.IsNullOrEmpty(projName))
+        {
+            ftpPath = $"{normalizedServer}/OPC/{itemNo}/{projName}/{fileName}";
+        }
+        else
+        {
+            ftpPath = $"{normalizedServer}/OPC/{itemNo}/{fileName}";
+        }
         
         try
         {
@@ -957,5 +1103,142 @@
         };
     }
 
+    /// <summary>
+    /// 涓婁紶鍥剧墖鍒� QS_ITEM_IPI_IMAGE 琛紙宸℃ QS_TYPE=2锛�
+    /// </summary>
+    /// <param name="id">妫�楠岄」鐩甀D (FID)</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");
+            }
+
+            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
+            {
+                // 妫�鏌ユ楠岄」鐩槸鍚﹀瓨鍦紝骞惰幏鍙栨楠屽崟ID (GID) 鍜� 妫�楠岄」鐩悕绉�
+                var xjItem = db.Queryable<QsQaItemXj01>()
+                    .Where(s => s.Id == id)
+                    .Select(s => new { s.Id, s.Pid, s.ProjName })
+                    .First();
+
+                if (xjItem == null)
+                {
+                    throw new Exception("妫�楠岄」鐩笉瀛樺湪");
+                }
+
+                // 姣忔閮藉垱寤烘柊鐨勫浘鐗囪褰曪紝鏀寔澶氬浘鐗囦笂浼�
+                var newImage = new QsItemIpiImage
+                {
+                    Fid = id,                        // 妫�楠岄」鐩甀D
+                    Gid = xjItem.Pid,                // 妫�楠屽崟ID
+                    QsType = 2,                      // 宸℃绫诲瀷
+                    ImageData = imageBytes,          // 鍥剧墖浜岃繘鍒舵暟鎹�
+                    Picturename = fileName,          // 淇濈暀鍘熷鏂囦欢鍚�
+                    JyName = xjItem.ProjName,        // 妫�楠岄」鐩悕绉�
+                    CreateDate = DateTime.Now,       // 鍒涘缓鏃堕棿
+                    CreateBy = createBy              // 鍒涘缓浜�
+                };
+
+                var insertResult = db.Insertable(newImage).ExecuteCommand();
+                return insertResult;
+            });
+
+            if (result > 0)
+            {
+                return (0, "鍥剧墖淇濆瓨鎴愬姛");
+            }
+            else
+            {
+                return (1, "鍥剧墖淇濆瓨澶辫触");
+            }
+        }
+        catch (Exception ex)
+        {
+            return (1, $"鍥剧墖淇濆瓨澶辫触锛歿ex.Message}");
+        }
+    }
+
+    /// <summary>
+    /// 鍒犻櫎妫�楠岄」鐩殑鍥剧墖锛堜粠 QS_ITEM_IPI_IMAGE 琛ㄤ腑鍒犻櫎锛屽贰妫� QS_TYPE=2锛�
+    /// </summary>
+    /// <param name="id">妫�楠岄」鐩甀D (FID)锛屽綋imageId涓虹┖鏃跺垹闄よ妫�楠岄」鐩殑鎵�鏈夊浘鐗�</param>
+    /// <param name="imageId">鍥剧墖ID锛岀敤浜庡垹闄ゅ崟寮犲浘鐗囷紙鍙�夛級</param>
+    /// <returns>鎿嶄綔缁撴灉</returns>
+    public (int status, string message) DeleteImageFromPicture(decimal id, decimal? imageId = null)
+    {
+        try
+        {
+            var result = SqlSugarHelper.UseTransactionWithOracle(db =>
+            {
+                // 妫�鏌ユ楠岄」鐩槸鍚﹀瓨鍦�
+                var exists = db.Queryable<QsQaItemXj01>()
+                    .Where(s => s.Id == id)
+                    .Any();
+
+                if (!exists)
+                {
+                    throw new Exception("妫�楠岄」鐩笉瀛樺湪");
+                }
+
+                int deleteResult;
+                if (imageId.HasValue)
+                {
+                    // 鍒犻櫎鎸囧畾鐨勫崟寮犲浘鐗�
+                    deleteResult = db.Deleteable<QsItemIpiImage>()
+                        .Where(s => s.Id == imageId.Value && s.Fid == id && s.QsType == 2)
+                        .ExecuteCommand();
+                }
+                else
+                {
+                    // 鍒犻櫎璇ユ楠岄」鐩殑鎵�鏈夊浘鐗�
+                    deleteResult = db.Deleteable<QsItemIpiImage>()
+                        .Where(s => s.Fid == id && s.QsType == 2)
+                        .ExecuteCommand();
+                }
+
+                return deleteResult;
+            });
+
+            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