From 37379aa1d6e95c1964419b32031aaccc302888c3 Mon Sep 17 00:00:00 2001
From: kyy <3283105747@qq.com>
Date: 星期六, 30 八月 2025 09:43:32 +0800
Subject: [PATCH] 1、新增批量按送货单删除条码、按明细删除条码

---
 MES.Service/service/BasicData/DeliveryBarcodeManager.cs |  160 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 146 insertions(+), 14 deletions(-)

diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
index 515e5fb..6352d5f 100644
--- a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
+++ b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -2,6 +2,7 @@
 using MES.Service.Dto.webApi;
 using MES.Service.Modes;
 using System.Linq;
+using MES.Service.util;
 
 namespace MES.Service.service.BasicData;
 
@@ -34,8 +35,8 @@
                     return DeleteBarcode(barcodeEntity.Id);
                 default:
                     throw new ArgumentOutOfRangeException(
-                        nameof(barcodeDto.Type), 
-                        barcodeDto.Type, 
+                        nameof(barcodeDto.Type),
+                        barcodeDto.Type,
                         "鏉$爜鎿嶄綔绫诲瀷閿欒锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級");
             }
         }
@@ -74,7 +75,7 @@
             var typeGroupDict = entityTypePairs
                 .GroupBy(pair => pair.Type)
                 .ToDictionary(
-                    group => group.Key, 
+                    group => group.Key,
                     group => group.Select(pair => pair.Entity).ToList()
                 );
 
@@ -93,8 +94,8 @@
                         break;
                     default:
                         throw new ArgumentOutOfRangeException(
-                            nameof(type), 
-                            type, 
+                            nameof(type),
+                            type,
                             "鎵归噺鎿嶄綔涓瓨鍦ㄩ潪娉昑ype锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級");
                 }
             }
@@ -112,7 +113,7 @@
             throw new ApplicationException($"鎵归噺鏉$爜鎿嶄綔澶辫触锛堟�绘潯鏁帮細{barcodeDtoList.Count}锛夛細{ex.Message}", ex);
         }
     }
-    
+
     /// <summary>
     /// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁
     /// </summary>
@@ -143,7 +144,60 @@
             throw new ApplicationException($"鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁澶辫触锛圖eliveryNo锛歿deliveryNo}锛夛細{ex.Message}", ex);
         }
     }
-    
+
+    /// <summary>
+    /// 鎵归噺鎸夐�佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁锛堟帴鏀舵暟缁勯」鍒楄〃锛�
+    /// </summary>
+    /// <param name="deliveryNoItems">鍖呭惈DeliveryNo鐨勫璞″垪琛�</param>
+    /// <returns>鎵归噺鍒犻櫎缁撴灉</returns>
+    public BatchDeleteResult DeleteListByDeliveryNo(List<DeleteByDeliveryNoRequest> deliveryNoItems)
+    {
+        // 1. 鍙傛暟鏍¢獙涓庡鐞嗭紙浠庢帶鍒跺櫒绉昏繃鏉ョ殑閫昏緫锛�
+        if (deliveryNoItems == null || !deliveryNoItems.Any())
+            throw new ArgumentException("閫佽揣鍗曞彿鍒楄〃涓嶈兘涓虹┖", nameof(deliveryNoItems));
+
+        // 鎻愬彇骞堕獙璇侀�佽揣鍗曞彿鍒楄〃
+        var deliveryNoList = deliveryNoItems
+            .Select(item => item.DeliveryNo) // 浠庡璞′腑鎻愬彇DeliveryNo瀛楁
+            .Where(no => !string.IsNullOrWhiteSpace(no)) // 杩囨护绌哄��/绌烘牸
+            .Distinct() // 鍘婚噸锛岄伩鍏嶉噸澶嶅垹闄�
+            .ToList();
+
+        // 鍒ゆ柇鏄惁鏈夋湁鏁堝崟鍙�
+        if (!deliveryNoList.Any())
+            throw new ArgumentException("璇锋眰涓病鏈夋湁鏁堢殑閫佽揣鍗曞彿锛堝潎涓虹┖鎴栫┖鏍硷級");
+
+        try
+        {
+            // 2. 鏌ヨ鏁版嵁搴撲腑瀛樺湪鐨勯�佽揣鍗曞彿
+            var existingNos = Db.Queryable<BarcodeInformation>()
+                .Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo))
+                .Select(barcode => barcode.DeliveryNo)
+                .Distinct()
+                .ToList();
+
+            // 3. 鎵ц鎵归噺鍒犻櫎
+            int totalDeleted = Db.Deleteable<BarcodeInformation>()
+                .Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo))
+                .ExecuteCommand();
+
+            // 4. 杩斿洖缁撴灉
+            return new BatchDeleteResult
+            {
+                TotalRequested = deliveryNoList.Count,
+                TotalDeleted = totalDeleted,
+                DeletedNos = existingNos,
+                NotFoundNos = deliveryNoList.Except(existingNos).ToList()
+            };
+        }
+        catch (Exception ex)
+        {
+            throw new ApplicationException(
+                $"鎵归噺鍒犻櫎澶辫触锛堝叡{deliveryNoList.Count}涓崟鍙凤級锛歿ex.Message}",
+                ex);
+        }
+    }
+
     /// <summary>
     /// 鎸夐�佽揣鍗曞彿+琛屽唴鐮佽仈鍚堝垹闄ゆ潯鐮佹暟鎹�
     /// </summary>
@@ -162,9 +216,9 @@
         {
             // 2. 鑱斿悎鏉′欢鍒犻櫎锛氬尮閰� DeliveryNo锛堥�佽揣鍗曞彿锛夊拰 DnLines锛堣鍐呯爜锛屽疄浣撳瓧娈靛搴旇〃鐨� dnLines 鍒楋級
             int deletedCount = Db.Deleteable<BarcodeInformation>()
-                .Where(barcode => 
+                .Where(barcode =>
                         barcode.DeliveryNo == deliveryNo // 鍖归厤閫佽揣鍗曞彿
-                        && barcode.DnLines == lineNo     // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛�
+                        && barcode.DnLines == lineNo // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛�
                 )
                 .ExecuteCommand(); // 杩斿洖鍙楀奖鍝嶇殑琛屾暟
 
@@ -179,11 +233,88 @@
         {
             // 4. 鍖呰寮傚父锛氳ˉ鍏呰仈鍚堟潯浠朵笂涓嬫枃锛屼究浜庡畾浣嶉棶棰�
             throw new ApplicationException(
-                $"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}", 
+                $"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}",
                 ex);
         }
     }
+
+    /// <summary>
+    /// 鎵归噺鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹�
+    /// </summary>
+    /// <param name="deliveryItems">鍖呭惈DeliveryNo鍜孡ineNo鐨勫璞″垪琛�</param>
+    /// <returns>鎵归噺鍒犻櫎缁撴灉</returns>
+    public BatchDeleteResult DeleteListByDeliveryItem(List<DeleteByDeliveryItemRequest> deliveryItems)
+    {
+        // 1. 鍙傛暟鏍¢獙涓庡鐞�
+        if (deliveryItems == null || !deliveryItems.Any())
+            throw new ArgumentException("閫佽揣鍗曡鍒楄〃涓嶈兘涓虹┖", nameof(deliveryItems));
+
+        // 鎻愬彇骞堕獙璇侀�佽揣鍗曞彿+琛屽唴鐮佺粍鍚�
+        var validItems = deliveryItems
+            .Where(item =>
+                !string.IsNullOrWhiteSpace(item.DeliveryNo) &&
+                !string.IsNullOrWhiteSpace(item.LineNo)
+            )
+            .Select(item => new
+            {
+                DeliveryNo = item.DeliveryNo,
+                LineNo = item.LineNo
+            })
+            .Distinct() // 鍘婚噸鐩稿悓鐨勭粍鍚�
+            .ToList();
+
+        if (!validItems.Any())
+            throw new ArgumentException("璇锋眰涓病鏈夋湁鏁堢殑鏁堢殑閫佽揣鍗曡鏁版嵁锛堝潎涓虹┖鎴栫┖鏍硷級");
+
+        try
+        {
+            // 2. 鎵ц鎵归噺鍒犻櫎锛堟寜DeliveryNo+LineNo缁勫悎鏉′欢锛�
+            int totalDeleted = 0;
+            var deletedItems = new List<string>();
+            var notFoundItems = new List<string>();
+
+            // 閬嶅巻姣忎釜缁勫悎鎵ц鍒犻櫎锛堟垨浣跨敤鎵归噺鏉′欢鍒犻櫎锛�
+            foreach (var item in validItems)
+            {
+                // 妫�鏌ュ綋鍓嶇粍鍚堟槸鍚﹀瓨鍦�
+                var exists = Db.Queryable<BarcodeInformation>()
+                    .Any(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo);
+
+                if (exists)
+                {
+                    // 鎵ц鍒犻櫎
+                    var deleted = Db.Deleteable<BarcodeInformation>()
+                        .Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo)
+                        .ExecuteCommand();
+
+                    totalDeleted += deleted;
+                    deletedItems.Add($"{item.DeliveryNo}_{item.LineNo}");
+                }
+                else
+                {
+                    notFoundItems.Add($"{item.DeliveryNo}_{item.LineNo}");
+                }
+            }
+
+            // 3. 杩斿洖缁撴灉
+            return new BatchDeleteResult
+            {
+                TotalRequested = validItems.Count,
+                TotalDeleted = totalDeleted,
+                DeletedNos = deletedItems, // 瀛樺偍鏍煎紡锛�"DeliveryNo_LineNo"
+                NotFoundNos = notFoundItems // 瀛樺偍鏍煎紡锛�"DeliveryNo_LineNo"
+            };
+        }
+        catch (Exception ex)
+        {
+            throw new ApplicationException(
+                $"鎵归噺鍒犻櫎閫佽揣鍗曡澶辫触锛堝叡{validItems.Count}鏉★級锛歿ex.Message}",
+                ex);
+        }
+    }
+
     #region 绉佹湁杈呭姪鏂规硶鍜屽唴閮ㄧ被
+
     /// <summary>
     /// 鍐呴儴杈呭姪绫伙細鐢ㄤ簬鍏宠仈瀹炰綋鍜屾搷浣滅被鍨嬶紙瑙e喅dynamic绫诲瀷杞崲闂锛�
     /// </summary>
@@ -198,8 +329,8 @@
     /// </summary>
     private BarcodeInformation ConvertDtoToEntity(DeliveryBarcodeInfo dto)
     {
-        var entityId = dto.Type == "0" 
-            ? Guid.NewGuid() 
+        var entityId = dto.Type == "0"
+            ? Guid.NewGuid()
             : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.Parse(dto.SmallBarcode));
 
         return new BarcodeInformation
@@ -214,7 +345,7 @@
             PackLevel = dto.BarcodeType,
             CreateTime = dto.Type == "0" ? DateTime.Now : (DateTime?)null,
             UpdateTime = DateTime.Now,
-            
+
             // 鎵╁睍瀛楁璧嬮粯璁ゅ��
             BigBarcode = null,
             SmallPackageLength = null,
@@ -287,5 +418,6 @@
 
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氬叡{ids.Length}涓狪d");
     }
+
     #endregion
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3