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 |  784 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 748 insertions(+), 36 deletions(-)

diff --git a/StandardInterface/MES.Service/service/QC/XJService.cs b/StandardInterface/MES.Service/service/QC/XJService.cs
index 9e9aa40..9de79c9 100644
--- a/StandardInterface/MES.Service/service/QC/XJService.cs
+++ b/StandardInterface/MES.Service/service/QC/XJService.cs
@@ -1,4 +1,5 @@
-锘縰sing MES.Service.DB;
+锘縰sing System.Data;
+using MES.Service.DB;
 using MES.Service.Dto.service;
 using MES.Service.Modes;
 using MES.Service.util;
@@ -28,14 +29,14 @@
         var number = "0001";
         var olReleaseNo = executeScalar.ToString();
         //鍒ゆ柇浠婂ぉ鏄惁鐢熸垚浜嗗贰妫�鍗�
-        if (string.IsNullOrEmpty(olReleaseNo)) return "XJN" + date + number;
+        if (string.IsNullOrEmpty(olReleaseNo)) return "XJ" + date + number;
         //鎴彇鍚庡洓浣嶇殑娴佹按鍙风疮鍔�
         var substring = Convert.ToInt32(olReleaseNo.Substring(10));
         substring += 1;
         number = substring.ToString("D4");
 
         //濡傛灉涓虹┖鐩存帴杩斿洖0001
-        return "XJN" + date + number;
+        return "XJ" + date + number;
     }
 
     public List<Womdaa> getItem(string daa001)
@@ -70,21 +71,43 @@
             }).OrderBy("b.LINE_NO").ToList();
     }
 
-    public List<Womdaa> getDaa001(string daa020, string item)
+    public List<Womdaa> getDaa001(string daa020)
     {
         var db = SqlSugarHelper.GetInstance();
-        return db.Queryable<Womdaa, MesItems>((a, b) =>
-                new JoinQueryInfos(JoinType.Inner, a.Daa002 == b.Id.ToString()))
-            .Where((a, b) =>
-                a.Daa015 == daa020 &&
-                b.ItemNo == item &&
-                statusArray.Contains(a.Daa018))
-            .Select<Womdaa>(a => new Womdaa
+        return db.Queryable<Womdaa>()
+            .Where(a => a.Daa015 == daa020 && statusArray.Contains(a.Daa018))
+            .Select(a => new Womdaa
             {
                 Id = a.Id,
                 Daa001 = a.Daa001,
                 Daa003 = a.Daa003,
                 Daa008 = a.Daa008
+            }).ToList();
+    }
+
+    /// <summary>
+    /// 鑾峰彇宸ュ崟璇︾粏淇℃伅锛堝寘鍚墿鏂欎俊鎭級
+    /// </summary>
+    /// <param name="daa020">绾夸綋缂栧彿</param>
+    /// <returns>宸ュ崟鍒楄〃锛堝寘鍚墿鏂欎俊鎭級</returns>
+    public List<WorkOrderWithItemDto> getWorkOrderWithItem(string daa020)
+    {
+        var db = SqlSugarHelper.GetInstance();
+        return db.Queryable<Womdaa, MesItems>((a, b) =>
+                new JoinQueryInfos(JoinType.Inner, a.Daa002 == b.Id.ToString()))
+            .Where((a, b) => a.Daa015 == daa020 && statusArray.Contains(a.Daa018))
+            .Select((a, b) => new WorkOrderWithItemDto
+            {
+                Id = a.Id, 
+                Daa001 = a.Daa001, 
+                Daa002 = a.Daa002, 
+                Daa003 = a.Daa003, 
+                Daa008 = a.Daa008, 
+                Daa015 = a.Daa015, 
+                Daa020 = a.Daa020,
+                ItemNo = b.ItemNo, 
+                ItemName = b.ItemName, 
+                ItemModel = b.ItemModel
             }).ToList();
     }
 
@@ -106,7 +129,7 @@
                 }).ToList();
     }
 
-    public List<QsQaItemXj> getPage(XJPageResult queryObj)
+    public (List<QsQaItemXj> item, int TotalCount) getPage(XJPageResult queryObj)
     {
         var db = SqlSugarHelper.GetInstance();
 
@@ -117,43 +140,120 @@
         if (StringUtil.IsNotNullOrEmpty(queryObj.createUser))
             lineNo = _baseService.getUserLineNo(queryObj.createUser);
 
-        return db
+        var totalCount = 0;
+        
+        // 鏋勫缓鍩虹鏌ヨ - 浣跨敤LEFT JOIN閬垮厤鍥犲叧鑱旀暟鎹己澶卞鑷存煡璇笉鍒�
+        var query = db
             .Queryable<QsQaItemXj, Womdaa, MesLine,
                 MesItems>((s, a, c, b) =>
                 new JoinQueryInfos(
-                    JoinType.Inner, s.BillNo == a.Daa001,
+                    JoinType.Left, s.BillNo == a.Daa001,
                     JoinType.Left, a.Daa015 == c.LineNo,
                     JoinType.Left, s.ItemId == b.Id
                 )
-            )
-            .WhereIF(lineNo != null && lineNo.Length > 0,
-                (s, a, c, b) => lineNo.Contains(c.LineNo))
-            .WhereIF(
-                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
-                "鏈畬鎴�".Equals(queryObj.result),
-                (s, a, c, b) => s.FcheckResu == null)
-            .WhereIF(
-                StringUtil.IsNotNullOrEmpty(queryObj.result) &&
-                !"鏈畬鎴�".Equals(queryObj.result),
-                (s, a, c, b) => s.FcheckResu != null)
-            .WhereIF(id > 0, (s, a, c, b) => s.Id == id)
+            );
+            
+        // 娣诲姞绾夸綋绛涢�夋潯浠讹紙鍩轰簬鐢ㄦ埛鏉冮檺锛�
+        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);
+        }
+        
+        // 娣诲姞鐘舵�佺瓫閫夋潯浠� - 鏄庣‘鍖归厤鍓嶇浼犻�掔殑鍊�
+        if (StringUtil.IsNotNullOrEmpty(queryObj.result))
+        {
+            if ("鏈畬鎴�".Equals(queryObj.result))
+            {
+                query = query.Where((s, a, c, b) => s.Fsubmit == 0);
+            }
+            else if ("宸插畬鎴�".Equals(queryObj.result))
+            {
+                query = query.Where((s, a, c, b) => s.Fsubmit == 1);
+            }
+        }
+        
+        // 娣诲姞鎼滅储鏉′欢 - 鏍规嵁閫夋嫨鐨勬悳绱㈠瓧娈佃繘琛岀簿纭悳绱�
+        if (!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField))
+        {
+            switch (queryObj.searchField)
+            {
+                case "billNo":  // 宸ュ崟
+                    query = query.Where((s, a, c, b) => s.BillNo.Contains(queryObj.SearchValue));
+                    break;
+                case "releaseNo":  // 妫�楠屽崟鍙�
+                    query = query.Where((s, a, c, b) => s.ReleaseNo.Contains(queryObj.SearchValue));
+                    break;
+                case "daa020":    // 浜х嚎
+                    query = query.Where((s, a, c, b) => c.LineNo.Contains(queryObj.SearchValue));
+                    break;
+                case "itemNo":  // 鐗╂枡缂栫爜
+                    query = query.Where((s, a, c, b) => s.ItemNo.Contains(queryObj.SearchValue));
+                    break;
+                case "itemName":  // 鐗╂枡鍚嶇О
+                    query = query.Where((s, a, c, b) => b.ItemName.Contains(queryObj.SearchValue));
+                    break;
+                default:
+                    // 濡傛灉娌℃湁鎸囧畾瀛楁鎴栧瓧娈典笉鍖归厤锛屼娇鐢ㄥ師鏈夌殑妯$硦鏌ヨ閫昏緫浣滀负鍏滃簳鏂规
+                    query = query.Where((s, a, c, b) => 
+                        s.ItemNo.Contains(queryObj.SearchValue) ||
+                        b.ItemName.Contains(queryObj.SearchValue) ||
+                        s.BillNo.Contains(queryObj.SearchValue) ||
+                        s.ReleaseNo.Contains(queryObj.SearchValue) ||
+                        c.LineNo.Contains(queryObj.SearchValue));
+                    break;
+            }
+        }
+        // 涓轰簡鍏煎鏃х増鏈紝濡傛灉娌℃湁浼犻�� searchField锛屼娇鐢ㄥ師鏉ョ殑鏌ヨ閫昏緫
+        else if (string.IsNullOrEmpty(queryObj.searchField) && !string.IsNullOrEmpty(queryObj.SearchValue))
+        {
+            // 淇濇寔鍘熸湁鐨勫瀛楁妯$硦鏌ヨ閫昏緫
+            query = query.Where((s, a, c, b) => 
+                s.ItemNo.Contains(queryObj.SearchValue) ||
+                b.ItemName.Contains(queryObj.SearchValue) ||
+                s.BillNo.Contains(queryObj.SearchValue) ||
+                s.ReleaseNo.Contains(queryObj.SearchValue) ||
+                c.LineNo.Contains(queryObj.SearchValue));
+        }
+        
+        // 娣诲姞ID绛涢�夋潯浠�
+        if (id > 0)
+        {
+            query = query.Where((s, a, c, b) => s.Id == id);
+        }
+        
+        var data = query
             .Select((s, a, c, b) => new QsQaItemXj
             {
-                Daa020 = c.LineNo,
+                Daa020 = SqlFunc.IsNull(c.LineNo, ""),
                 Id = s.Id,
-                PlanQty = a.Daa008,
+                PlanQty = SqlFunc.IsNull(a.Daa008, 0),
                 CreateBy = s.CreateBy,
                 CreateDate = s.CreateDate,
                 ReleaseNo = s.ReleaseNo,
                 ItemNo = s.ItemNo,
                 BillNo = s.BillNo,
-                ItemName = b.ItemName,
-                ItemModel = b.ItemModel,
+                ItemName = SqlFunc.IsNull(b.ItemName, ""),
+                ItemModel = SqlFunc.IsNull(b.ItemModel, ""),
                 FcheckBy = s.FcheckBy,
                 FcheckResu = s.FcheckResu,
                 Remarks = s.Remarks
             }).OrderBy(s => s.CreateDate, OrderByType.Desc)
-            .ToPageList(queryObj.PageIndex, queryObj.Limit);
+            .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount);
+
+        return (data, totalCount);
     }
 
 
@@ -200,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();
@@ -236,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;
     }
@@ -255,6 +400,7 @@
 
             xj.CreateBy = userNo;
             xj.CreateDate = DateTime.Now;
+            xj.Fsubmit = 0; // 璁剧疆榛樿鐘舵�佷负鏈彁浜�
 
             var pid = db.Insertable(xj).ExecuteReturnIdentity();
 
@@ -269,8 +415,10 @@
             return commit;
         });
 
+        // 閲嶆柊鑾峰彇妫�楠岄」鐩紙鍖呭惈ID锛�
         xjDto.items = getJYItem(xj.Id, null);
 
+        // 鑷姩涓烘病鏈変笂涓嬮檺鐨勬楠岄」鐩敓鎴愰粯璁ょ殑鍚堟牸璁板綍
         xjDto.items.ForEach(s =>
         {
             if (s.MaxValue != null || s.StandardValue != null ||
@@ -284,6 +432,9 @@
             detail.count = (int?)s.LevelNum;
             SetQSItemDetail(detail);
         });
+
+        // 閲嶆柊鑾峰彇妫�楠岄」鐩紙鍖呭惈鑷姩鐢熸垚鐨勭粨鏋滐級
+        xjDto.items = getJYItem(xj.Id, null);
 
         return Convert.ToInt32(xj.Id);
     }
@@ -325,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 = "涓嶅悎鏍�";
@@ -339,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();
@@ -529,4 +735,510 @@
         });
     }
 
+    //鍒锋柊妫�楠岄」鐩�
+    public (int result, string message) GenUpdate(decimal? id, string? no, string? user)
+    {
+        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);
+
+        var parameters = new List<SugarParameter>
+    {
+        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),
+        outputResult,
+        outputMessage
+    };
+
+        var db = SqlSugarHelper.GetInstance();
+        db.Ado.ExecuteCommand(
+            "BEGIN PRC_GEN_UPDATE_XJ(:P_ID, :P_NO, :P_USER, :PO_RESULT, :PO_TEXT); END;",
+            parameters.ToArray());
+
+        int result = outputResult.Value == null ? -1 : Convert.ToInt32(outputResult.Value);
+        string message = outputMessage.Value?.ToString() ?? "";
+
+        return (result, message);
+    }
+
+    public bool SjSubmit(SJDto dto)
+    {
+        try
+        {
+            // 瀹氫箟杈撳嚭鍙傛暟
+            var outputResult = new SugarParameter("c_res", null,
+                System.Data.DbType.Int32, ParameterDirection.Output,
+                4000);
+
+            var outputMessage = new SugarParameter("c_msg", null,
+                System.Data.DbType.String,
+                ParameterDirection.Output, 4000);
+
+            // 瀹氫箟杈撳叆鍙傛暟锛屽浐瀹欶LAG涓�1锛堝鏍革級
+            var parameters = new List<SugarParameter>
+            {
+                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)
+        {
+            throw new Exception(ex.Message);
+        }
+    }
+
+    /// <summary>
+    /// 鑾峰彇鏈夌嚎浣撶殑閮ㄩ棬鍒楄〃
+    /// </summary>
+    /// <returns>閮ㄩ棬鍒楄〃</returns>
+    public List<SysDepartment> GetDepartmentsWithLines()
+    {
+        var db = SqlSugarHelper.GetInstance();
+
+        // 鏌ヨ鏈夌嚎浣撶殑閮ㄩ棬锛岄摼鎺ユ潯浠舵槸绾夸綋鐨凞EPOT_ID绛変簬閮ㄩ棬鐨凞epartmentid
+        var departments = db.Queryable<SysDepartment, MesLine>((dept, line) =>
+                new JoinQueryInfos(JoinType.Inner, line.DepotId == dept.Departmentid))
+            .Where((dept, line) => line.LineNo != null && line.LineName != null)
+            .GroupBy((dept, line) => new
+            {
+                dept.Departmentid,
+                dept.Departmentname
+            })
+            .Select((dept, line) => new SysDepartment
+            {
+                Departmentid = dept.Departmentid,
+                Departmentname = dept.Departmentname
+            })
+            .OrderBy(dept => dept.Departmentname)
+            .ToList();
+
+        return departments;
+    }
+
+    /// <summary>
+    /// 淇濆瓨閮ㄩ棬閫夋嫨
+    /// </summary>
+    /// <param name="id">宸℃鍗旾D</param>
+    /// <param name="departmentId">閮ㄩ棬ID</param>
+    /// <param name="departmentName">閮ㄩ棬鍚嶇О</param>
+    /// <returns>淇濆瓨缁撴灉</returns>
+    public int SaveDepartmentSelection(decimal id, string departmentId, string departmentName)
+    {
+        return SqlSugarHelper.UseTransactionWithOracle(db =>
+        {
+            return db.Updateable<QsQaItemXj>()
+                .SetColumns(it => it.WorkShop == departmentName)
+                .SetColumns(it => it.DepartmentId == departmentId)
+                .Where(it => it.Id == id)
+                .ExecuteCommand();
+        });
+    }
+
+    /// <summary>
+    /// 鏍规嵁閮ㄩ棬ID鑾峰彇璇ラ儴闂ㄤ笅鐨勭嚎浣撳垪琛�
+    /// </summary>
+    /// <param name="departmentId">閮ㄩ棬ID</param>
+    /// <returns>绾夸綋鍒楄〃</returns>
+    public List<MesLine> GetLinesByDepartment(string departmentId)
+    {
+        var db = SqlSugarHelper.GetInstance();
+
+        // 灏嗗瓧绗︿覆杞崲涓篸ecimal杩涜姣旇緝
+        if (!decimal.TryParse(departmentId, out var deptId))
+        {
+            return new List<MesLine>();
+        }
+
+        return db.Queryable<MesLine>()
+            .Where(line => line.DepotId == deptId && 
+                          line.LineNo != null && 
+                          line.LineName != null)
+            .OrderBy(line => line.LineNo)
+            .ToList();
+    }
+
+    /// <summary>
+    /// 鑾峰彇闄勪欢淇℃伅
+    /// </summary>
+    /// <param name="itemNo">鐗╂枡缂栫爜</param>
+    /// <param name="projName">椤圭洰鍚嶇О锛堝彲閫夛紝鐢ㄤ簬杩囨护锛�</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 == "宸℃");  // 娣诲姞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
+                }).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>
+    /// 涓婁紶鍥剧墖鍒� 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