From b7c6607e6937df8f60d99a076178f3173213af1e Mon Sep 17 00:00:00 2001
From: kyy <3283105747@qq.com>
Date: 星期三, 17 九月 2025 15:59:20 +0800
Subject: [PATCH] 1、送货条码操作类型修改

---
 MES.Service/service/BasicData/DeliveryBarcodeManager.cs |  193 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 147 insertions(+), 46 deletions(-)

diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
index 515e5fb..c5a82a5 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;
 
@@ -28,15 +29,12 @@
             // 鎸塗ype鍖哄垎鎿嶄綔绫诲瀷锛�0=鏂板锛�1=鍒犻櫎锛�
             switch (barcodeDto.Type)
             {
-                case "0":
-                    return InsertBarcode(barcodeEntity);
                 case "1":
+                    return InsertBarcode(barcodeEntity);
+                case "2":
                     return DeleteBarcode(barcodeEntity.Id);
                 default:
-                    throw new ArgumentOutOfRangeException(
-                        nameof(barcodeDto.Type), 
-                        barcodeDto.Type, 
-                        "鏉$爜鎿嶄綔绫诲瀷閿欒锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級");
+                    throw new ArgumentOutOfRangeException( nameof(barcodeDto.Type), barcodeDto.Type, "鏉$爜鎿嶄綔绫诲瀷閿欒锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級");
             }
         }
         catch (Exception ex)
@@ -71,12 +69,7 @@
             }
 
             // 2. 鎸夋搷浣滅被鍨婽ype鍒嗙粍锛堜娇鐢ㄥ己绫诲瀷閬垮厤绫诲瀷鎺ㄦ柇闂锛�
-            var typeGroupDict = entityTypePairs
-                .GroupBy(pair => pair.Type)
-                .ToDictionary(
-                    group => group.Key, 
-                    group => group.Select(pair => pair.Entity).ToList()
-                );
+            var typeGroupDict = entityTypePairs.GroupBy(pair => pair.Type).ToDictionary(group => group.Key,group => group.Select(pair => pair.Entity).ToList());
 
             // 3. 瀛樺偍鍚勭粍鎿嶄綔缁撴灉
             var groupResultList = new List<bool>();
@@ -85,17 +78,14 @@
             {
                 switch (type)
                 {
-                    case "0":
+                    case "1":
                         groupResultList.Add(InsertBarcodeBatch(entityGroup));
                         break;
-                    case "1":
+                    case "2":
                         groupResultList.Add(DeleteBarcodeBatch(entityGroup.Select(e => e.Id).ToArray()));
                         break;
                     default:
-                        throw new ArgumentOutOfRangeException(
-                            nameof(type), 
-                            type, 
-                            "鎵归噺鎿嶄綔涓瓨鍦ㄩ潪娉昑ype锛氫粎鏀寔 0锛堟柊澧烇級銆�1锛堝垹闄わ級");
+                        throw new ArgumentOutOfRangeException(nameof(type),type,"鎵归噺鎿嶄綔涓瓨鍦ㄩ潪娉昑ype锛氫粎鏀寔 1锛堟柊澧烇級銆�2锛堝垹闄わ級");
                 }
             }
 
@@ -112,7 +102,7 @@
             throw new ApplicationException($"鎵归噺鏉$爜鎿嶄綔澶辫触锛堟�绘潯鏁帮細{barcodeDtoList.Count}锛夛細{ex.Message}", ex);
         }
     }
-    
+
     /// <summary>
     /// 鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁
     /// </summary>
@@ -122,8 +112,9 @@
     {
         // 1. 鏍¢獙鍙傛暟锛氶�佽揣鍗曞彿涓嶈兘涓虹┖
         if (string.IsNullOrWhiteSpace(deliveryNo))
+        {
             throw new ArgumentException("閫佽揣鍗曞彿 DeliveryNo 涓嶈兘涓虹┖鎴栫┖鏍�", nameof(deliveryNo));
-
+        }
         try
         {
             // 2. 鎵ц鍒犻櫎锛氭牴鎹� DeliveryNo 鍖归厤琛ㄤ腑鏁版嵁锛堝搴斿疄浣撶殑 DeliveryNo 瀛楁锛�
@@ -133,8 +124,9 @@
 
             // 3. 鍒ゆ柇缁撴灉锛氳嚦灏戝垹闄�1鏉″垯瑙嗕负鎴愬姛锛屽惁鍒欐姏鍑衡�滄湭鎵惧埌鏁版嵁鈥濆紓甯�
             if (deletedCount > 0)
+            {
                 return true;
-
+            }
             throw new KeyNotFoundException($"鏈壘鍒伴�佽揣鍗曞彿涓恒�寋deliveryNo}銆嶇殑鏉$爜鏁版嵁锛屽垹闄ゆ搷浣滄湭鎵ц");
         }
         catch (Exception ex)
@@ -143,7 +135,54 @@
             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>
@@ -154,36 +193,103 @@
     {
         // 1. 鍙屽弬鏁版牎楠岋細閬垮厤绌哄��/绌哄瓧绗︿覆
         if (string.IsNullOrWhiteSpace(deliveryNo))
+        {
             throw new ArgumentException("閫佽揣鍗曞彿 DeliveryNo 涓嶈兘涓虹┖鎴栫┖鏍�", nameof(deliveryNo));
+        }
         if (string.IsNullOrWhiteSpace(lineNo))
+        {
             throw new ArgumentException("閫佽揣鍗曡鍐呯爜 LineNo 涓嶈兘涓虹┖鎴栫┖鏍�", nameof(lineNo));
+        }
 
         try
         {
             // 2. 鑱斿悎鏉′欢鍒犻櫎锛氬尮閰� DeliveryNo锛堥�佽揣鍗曞彿锛夊拰 DnLines锛堣鍐呯爜锛屽疄浣撳瓧娈靛搴旇〃鐨� dnLines 鍒楋級
-            int deletedCount = Db.Deleteable<BarcodeInformation>()
-                .Where(barcode => 
-                        barcode.DeliveryNo == deliveryNo // 鍖归厤閫佽揣鍗曞彿
-                        && barcode.DnLines == lineNo     // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛�
-                )
+            int deletedCount = Db.Deleteable<BarcodeInformation>().Where(barcode =>barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo) // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛�
                 .ExecuteCommand(); // 杩斿洖鍙楀奖鍝嶇殑琛屾暟
 
             // 3. 缁撴灉鍒ゆ柇锛氭湁鏁版嵁琚垹闄ゅ垯鎴愬姛锛屽惁鍒欐姏鈥滄湭鎵惧埌鏁版嵁鈥濆紓甯�
             if (deletedCount > 0)
+            {
                 return true;
-
-            throw new KeyNotFoundException(
-                $"鏈壘鍒伴�佽揣鍗曞彿銆寋deliveryNo}銆嶄笖琛屽唴鐮併�寋lineNo}銆嶅搴旂殑鏉$爜鏁版嵁锛屽垹闄ゆ搷浣滄湭鎵ц");
+            }
+            throw new KeyNotFoundException($"鏈壘鍒伴�佽揣鍗曞彿銆寋deliveryNo}銆嶄笖琛屽唴鐮併�寋lineNo}銆嶅搴旂殑鏉$爜鏁版嵁锛屽垹闄ゆ搷浣滄湭鎵ц");
         }
         catch (Exception ex)
         {
             // 4. 鍖呰寮傚父锛氳ˉ鍏呰仈鍚堟潯浠朵笂涓嬫枃锛屼究浜庡畾浣嶉棶棰�
+            throw new ApplicationException($"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙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(
-                $"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}", 
+                $"鎵归噺鍒犻櫎閫佽揣鍗曡澶辫触锛堝叡{validItems.Count}鏉★級锛歿ex.Message}",
                 ex);
         }
     }
+
     #region 绉佹湁杈呭姪鏂规硶鍜屽唴閮ㄧ被
+
     /// <summary>
     /// 鍐呴儴杈呭姪绫伙細鐢ㄤ簬鍏宠仈瀹炰綋鍜屾搷浣滅被鍨嬶紙瑙e喅dynamic绫诲瀷杞崲闂锛�
     /// </summary>
@@ -198,9 +304,7 @@
     /// </summary>
     private BarcodeInformation ConvertDtoToEntity(DeliveryBarcodeInfo dto)
     {
-        var entityId = dto.Type == "0" 
-            ? Guid.NewGuid() 
-            : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.Parse(dto.SmallBarcode));
+        var entityId = dto.Type == "1" ? Guid.NewGuid() : (string.IsNullOrEmpty(dto.SmallBarcode) ? Guid.Empty : Guid.NewGuid());
 
         return new BarcodeInformation
         {
@@ -212,9 +316,8 @@
             DeliveryNo = dto.DeliveryNo,
             DnLines = dto.LineNo,
             PackLevel = dto.BarcodeType,
-            CreateTime = dto.Type == "0" ? DateTime.Now : (DateTime?)null,
+            CreateTime = dto.Type == "1" ? DateTime.Now : (DateTime?)null,
             UpdateTime = DateTime.Now,
-            
             // 鎵╁睍瀛楁璧嬮粯璁ゅ��
             BigBarcode = null,
             SmallPackageLength = null,
@@ -248,18 +351,16 @@
         var isInsertSuccess = base.Insert(entity);
         return isInsertSuccess ? true : throw new NotImplementedException("鏉$爜鏂板澶辫触锛氭暟鎹簱鎻掑叆鎿嶄綔鏈墽琛屾垚鍔�");
     }
-
     /// <summary>
     /// 鍗曚釜鏉$爜鍒犻櫎锛堟寜涓婚敭Id锛�
     /// </summary>
     private bool DeleteBarcode(Guid id)
     {
         if (id == Guid.Empty)
+        {
             throw new ArgumentException("鍒犻櫎鎿嶄綔鐨勬潯鐮両d涓嶈兘涓虹┖");
-
-        var deleteRowCount = Db.Deleteable<BarcodeInformation>()
-            .Where(entity => entity.Id == id)
-            .ExecuteCommand();
+        }
+        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => entity.Id == id).ExecuteCommand();
 
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鏉$爜鍒犻櫎澶辫触锛氭湭鎵惧埌Id涓恒�寋id}銆嶇殑鏉$爜璁板綍");
     }
@@ -279,13 +380,13 @@
     private bool DeleteBarcodeBatch(Guid[] ids)
     {
         if (ids == null || ids.Length == 0)
+        {
             throw new ArgumentException("鎵归噺鍒犻櫎鐨勬潯鐮両d鏁扮粍涓嶈兘涓虹┖");
-
-        var deleteRowCount = Db.Deleteable<BarcodeInformation>()
-            .Where(entity => ids.Contains(entity.Id))
-            .ExecuteCommand();
+        }
+        var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ExecuteCommand();
 
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氬叡{ids.Length}涓狪d");
     }
+
     #endregion
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3