From 80b5db679d95b5eaf0da83c1a078e7a105bf327d Mon Sep 17 00:00:00 2001
From: kyy <3283105747@qq.com>
Date: 星期三, 17 九月 2025 17:50:38 +0800
Subject: [PATCH] 1、条码删除增加校验

---
 MES.Service/service/BasicData/DeliveryBarcodeManager.cs |  133 ++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 122 insertions(+), 11 deletions(-)

diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
index 35bb9b1..d7179b0 100644
--- a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
+++ b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -117,12 +117,28 @@
         }
         try
         {
-            // 2. 鎵ц鍒犻櫎锛氭牴鎹� DeliveryNo 鍖归厤琛ㄤ腑鏁版嵁锛堝搴斿疄浣撶殑 DeliveryNo 瀛楁锛�
+            // 2. 鏌ヨ瑕佸垹闄ょ殑鏉$爜淇℃伅
+            var barcodesToDelete = Db.Queryable<BarcodeInformation>()
+                .Where(barcode => barcode.DeliveryNo == deliveryNo)
+                .ToList();
+
+            if (!barcodesToDelete.Any())
+            {
+                throw new KeyNotFoundException($"鏈壘鍒伴�佽揣鍗曞彿涓恒�寋deliveryNo}銆嶇殑鏉$爜鏁版嵁锛屽垹闄ゆ搷浣滄湭鎵ц");
+            }
+
+            // 3. 鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+            foreach (var barcode in barcodesToDelete)
+            {
+                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
+            }
+
+            // 4. 鎵ц鍒犻櫎锛氭牴鎹� DeliveryNo 鍖归厤琛ㄤ腑鏁版嵁锛堝搴斿疄浣撶殑 DeliveryNo 瀛楁锛�
             int deletedCount = Db.Deleteable<BarcodeInformation>()
                 .Where(barcode => barcode.DeliveryNo == deliveryNo) // 琛ㄥ瓧娈典笌閫佽揣鍗曞彿鍖归厤
                 .ExecuteCommand(); // 杩斿洖鍙楀奖鍝嶇殑琛屾暟
 
-            // 3. 鍒ゆ柇缁撴灉锛氳嚦灏戝垹闄�1鏉″垯瑙嗕负鎴愬姛锛屽惁鍒欐姏鍑衡�滄湭鎵惧埌鏁版嵁鈥濆紓甯�
+            // 5. 鍒ゆ柇缁撴灉锛氳嚦灏戝垹闄�1鏉″垯瑙嗕负鎴愬姛锛屽惁鍒欐姏鍑�"鏈壘鍒版暟鎹�"寮傚父
             if (deletedCount > 0)
             {
                 return true;
@@ -131,7 +147,7 @@
         }
         catch (Exception ex)
         {
-            // 4. 鍖呰涓氬姟寮傚父锛岃ˉ鍏呬笂涓嬫枃淇℃伅
+            // 6. 鍖呰涓氬姟寮傚父锛岃ˉ鍏呬笂涓嬫枃淇℃伅
             throw new ApplicationException($"鏍规嵁閫佽揣鍗曞彿鍒犻櫎鏉$爜鏁版嵁澶辫触锛圖eliveryNo锛歿deliveryNo}锛夛細{ex.Message}", ex);
         }
     }
@@ -162,13 +178,20 @@
         }
         try
         {
-            // 2. 鏌ヨ鏁版嵁搴撲腑瀛樺湪鐨勯�佽揣鍗曞彿
+            // 2. 鏌ヨ鏁版嵁搴撲腑瀛樺湪鐨勯�佽揣鍗曞彿鍜屽搴旂殑鏉$爜淇℃伅
             var existingNos = Db.Queryable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).Select(barcode => barcode.DeliveryNo).Distinct().ToList();
+            var barcodesToDelete = Db.Queryable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).ToList();
 
-            // 3. 鎵ц鎵归噺鍒犻櫎
+            // 3. 鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+            foreach (var barcode in barcodesToDelete)
+            {
+                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
+            }
+
+            // 4. 鎵ц鎵归噺鍒犻櫎
             int totalDeleted = Db.Deleteable<BarcodeInformation>().Where(barcode => deliveryNoList.Contains(barcode.DeliveryNo)).ExecuteCommand();
 
-            // 4. 杩斿洖缁撴灉
+            // 5. 杩斿洖缁撴灉
             return new BatchDeleteResult
             {
                 TotalRequested = deliveryNoList.Count,
@@ -203,11 +226,27 @@
 
         try
         {
-            // 2. 鑱斿悎鏉′欢鍒犻櫎锛氬尮閰� DeliveryNo锛堥�佽揣鍗曞彿锛夊拰 DnLines锛堣鍐呯爜锛屽疄浣撳瓧娈靛搴旇〃鐨� dnLines 鍒楋級
+            // 2. 鏌ヨ瑕佸垹闄ょ殑鏉$爜淇℃伅
+            var barcodesToDelete = Db.Queryable<BarcodeInformation>()
+                .Where(barcode => barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo)
+                .ToList();
+
+            if (!barcodesToDelete.Any())
+            {
+                throw new KeyNotFoundException($"鏈壘鍒伴�佽揣鍗曞彿銆寋deliveryNo}銆嶄笖琛屽唴鐮併�寋lineNo}銆嶅搴旂殑鏉$爜鏁版嵁锛屽垹闄ゆ搷浣滄湭鎵ц");
+            }
+
+            // 3. 鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+            foreach (var barcode in barcodesToDelete)
+            {
+                CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
+            }
+
+            // 4. 鑱斿悎鏉′欢鍒犻櫎锛氬尮閰� DeliveryNo锛堥�佽揣鍗曞彿锛夊拰 DnLines锛堣鍐呯爜锛屽疄浣撳瓧娈靛搴旇〃鐨� dnLines 鍒楋級
             int deletedCount = Db.Deleteable<BarcodeInformation>().Where(barcode =>barcode.DeliveryNo == deliveryNo && barcode.DnLines == lineNo) // 鍖归厤琛屽唴鐮侊紙瀹炰綋 DnLines 瀵瑰簲琛� dnLines 瀛楁锛�
                 .ExecuteCommand(); // 杩斿洖鍙楀奖鍝嶇殑琛屾暟
 
-            // 3. 缁撴灉鍒ゆ柇锛氭湁鏁版嵁琚垹闄ゅ垯鎴愬姛锛屽惁鍒欐姏鈥滄湭鎵惧埌鏁版嵁鈥濆紓甯�
+            // 5. 缁撴灉鍒ゆ柇锛氭湁鏁版嵁琚垹闄ゅ垯鎴愬姛锛屽惁鍒欐姏"鏈壘鍒版暟鎹�"寮傚父
             if (deletedCount > 0)
             {
                 return true;
@@ -216,7 +255,7 @@
         }
         catch (Exception ex)
         {
-            // 4. 鍖呰寮傚父锛氳ˉ鍏呰仈鍚堟潯浠朵笂涓嬫枃锛屼究浜庡畾浣嶉棶棰�
+            // 6. 鍖呰寮傚父锛氳ˉ鍏呰仈鍚堟潯浠朵笂涓嬫枃锛屼究浜庡畾浣嶉棶棰�
             throw new ApplicationException($"鎸夐�佽揣鍗曞彿+琛屽唴鐮佸垹闄ゆ潯鐮佹暟鎹け璐ワ紙DeliveryNo锛歿deliveryNo}锛孡ineNo锛歿lineNo}锛夛細{ex.Message}",ex);
         }
     }
@@ -256,10 +295,18 @@
             foreach (var item in validItems)
             {
                 // 妫�鏌ュ綋鍓嶇粍鍚堟槸鍚﹀瓨鍦�
-                var exists = Db.Queryable<BarcodeInformation>().Any(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo);
+                var barcodesToDelete = Db.Queryable<BarcodeInformation>()
+                    .Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo)
+                    .ToList();
 
-                if (exists)
+                if (barcodesToDelete.Any())
                 {
+                    // 鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+                    foreach (var barcode in barcodesToDelete)
+                    {
+                        CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
+                    }
+
                     // 鎵ц鍒犻櫎
                     var deleted = Db.Deleteable<BarcodeInformation>().Where(b => b.DeliveryNo == item.DeliveryNo && b.DnLines == item.LineNo).ExecuteCommand();
                     totalDeleted += deleted;
@@ -366,6 +413,17 @@
         {
             throw new ArgumentException("鍒犻櫎鎿嶄綔鐨勬潯鐮両d涓嶈兘涓虹┖");
         }
+
+        // 鏌ヨ瑕佸垹闄ょ殑鏉$爜淇℃伅
+        var barcodeToDelete = Db.Queryable<BarcodeInformation>().Where(entity => entity.Id == id).First();
+        if (barcodeToDelete == null)
+        {
+            throw new NotImplementedException($"鏉$爜鍒犻櫎澶辫触锛氭湭鎵惧埌Id涓恒�寋id}銆嶇殑鏉$爜璁板綍");
+        }
+
+        // 鏍¢獙鏉$爜鏄惁鍦∕esInvItemBarcode琛ㄤ腑瀛樺湪
+        CheckBarcodeInInventory(barcodeToDelete.SmallBarcode, barcodeToDelete.OuterBarcode);
+
         var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => entity.Id == id).ExecuteCommand();
 
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鏉$爜鍒犻櫎澶辫触锛氭湭鎵惧埌Id涓恒�寋id}銆嶇殑鏉$爜璁板綍");
@@ -404,6 +462,20 @@
         {
             throw new ArgumentException("鎵归噺鍒犻櫎鐨勬潯鐮両d鏁扮粍涓嶈兘涓虹┖");
         }
+
+        // 鏌ヨ瑕佸垹闄ょ殑鏉$爜淇℃伅
+        var barcodesToDelete = Db.Queryable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ToList();
+        if (!barcodesToDelete.Any())
+        {
+            throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氭湭鎵惧埌浠讳綍鍖归厤鐨勬潯鐮佽褰�");
+        }
+
+        // 鎵归噺鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+        foreach (var barcode in barcodesToDelete)
+        {
+            CheckBarcodeInInventory(barcode.SmallBarcode, barcode.OuterBarcode);
+        }
+
         var deleteRowCount = Db.Deleteable<BarcodeInformation>().Where(entity => ids.Contains(entity.Id)).ExecuteCommand();
 
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氬叡{ids.Length}涓狪d");
@@ -422,5 +494,44 @@
                      (!string.IsNullOrEmpty(entity.OuterBarcode) && x.OuterBarcode == entity.OuterBarcode));
     }
 
+    /// <summary>
+    /// 鏍¢獙鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦紝濡傛灉瀛樺湪鍒欐姏鍑哄紓甯镐笉鍏佽鍒犻櫎
+    /// </summary>
+    /// <param name="smallBarcode">灏忓寘瑁呮潯鐮�</param>
+    /// <param name="outerBarcode">澶栧寘瑁呮潯鐮�</param>
+    private void CheckBarcodeInInventory(string smallBarcode, string outerBarcode)
+    {
+        var existsInInventory = false;
+        var existingBarcode = string.Empty;
+
+        // 妫�鏌ュ皬鍖呰鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦�
+        if (!string.IsNullOrEmpty(smallBarcode))
+        {
+            existsInInventory = Db.Queryable<MesInvItemBarcodes>()
+                .Any(x => x.ItemBarcode == smallBarcode);
+            if (existsInInventory)
+            {
+                existingBarcode = smallBarcode;
+            }
+        }
+
+        // 妫�鏌ュ鍖呰鏉$爜鏄惁鍦ㄥ簱瀛樿〃涓瓨鍦紙濡傛灉灏忓寘瑁呮潯鐮佷笉瀛樺湪鐨勮瘽锛�
+        if (!existsInInventory && !string.IsNullOrEmpty(outerBarcode))
+        {
+            existsInInventory = Db.Queryable<MesInvItemBarcodes>()
+                .Any(x => x.ItemBarcode == outerBarcode);
+            if (existsInInventory)
+            {
+                existingBarcode = outerBarcode;
+            }
+        }
+
+        // 濡傛灉鏉$爜鍦ㄥ簱瀛樿〃涓瓨鍦紝鍒欐姏鍑哄紓甯镐笉鍏佽鍒犻櫎
+        if (existsInInventory)
+        {
+            throw new InvalidOperationException($"鏉$爜銆寋existingBarcode}銆嶅凡鍦ㄥ簱瀛樹腑锛屼笉鍏佽鍒犻櫎");
+        }
+    }
+
     #endregion
 }
\ No newline at end of file

--
Gitblit v1.9.3