From 1e270a1329bbb77b594df2290f15caa34c56668e Mon Sep 17 00:00:00 2001
From: kyy <3283105747@qq.com>
Date: 星期三, 17 九月 2025 16:23:19 +0800
Subject: [PATCH] 1、条码增加唯一判断

---
 MES.Service/service/BasicData/DeliveryBarcodeManager.cs |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
index c5a82a5..35bb9b1 100644
--- a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
+++ b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -348,6 +348,12 @@
     /// </summary>
     private bool InsertBarcode(BarcodeInformation entity)
     {
+        // 妫�鏌ユ潯鐮佹槸鍚﹀凡瀛樺湪
+        if (IsBarcodeExists(entity))
+        {
+            throw new InvalidOperationException($"鏉$爜宸插瓨鍦紝鏃犳硶閲嶅鎻掑叆锛歋mallBarcode={entity.SmallBarcode}, OuterBarcode={entity.OuterBarcode}");
+        }
+        
         var isInsertSuccess = base.Insert(entity);
         return isInsertSuccess ? true : throw new NotImplementedException("鏉$爜鏂板澶辫触锛氭暟鎹簱鎻掑叆鎿嶄綔鏈墽琛屾垚鍔�");
     }
@@ -370,6 +376,21 @@
     /// </summary>
     private bool InsertBarcodeBatch(List<BarcodeInformation> entityList)
     {
+        // 妫�鏌ユ壒閲忔潯鐮佷腑鏄惁鏈夐噸澶�
+        var duplicateBarcodes = new List<string>();
+        foreach (var entity in entityList)
+        {
+            if (IsBarcodeExists(entity))
+            {
+                duplicateBarcodes.Add($"SmallBarcode={entity.SmallBarcode}, OuterBarcode={entity.OuterBarcode}");
+            }
+        }
+        
+        if (duplicateBarcodes.Any())
+        {
+            throw new InvalidOperationException($"鍙戠幇閲嶅鏉$爜锛屾棤娉曟壒閲忔彃鍏ワ細{string.Join("; ", duplicateBarcodes)}");
+        }
+        
         var isBatchInsertSuccess = base.InsertRange(entityList);
         return isBatchInsertSuccess ? true : throw new NotImplementedException($"鎵归噺鏉$爜鏂板澶辫触锛氬叡{entityList.Count}鏉¤褰�");
     }
@@ -388,5 +409,18 @@
         return deleteRowCount > 0 ? true : throw new NotImplementedException($"鎵归噺鏉$爜鍒犻櫎澶辫触锛氬叡{ids.Length}涓狪d");
     }
 
+    /// <summary>
+    /// 妫�鏌ユ潯鐮佹槸鍚﹀凡瀛樺湪
+    /// </summary>
+    /// <param name="entity">鏉$爜瀹炰綋</param>
+    /// <returns>true=宸插瓨鍦紝false=涓嶅瓨鍦�</returns>
+    private bool IsBarcodeExists(BarcodeInformation entity)
+    {
+        // 鏍规嵁SmallBarcode鎴朞uterBarcode妫�鏌ユ槸鍚﹀瓨鍦ㄩ噸澶嶆潯鐮�
+        return Db.Queryable<BarcodeInformation>()
+            .Any(x => (!string.IsNullOrEmpty(entity.SmallBarcode) && x.SmallBarcode == entity.SmallBarcode) ||
+                     (!string.IsNullOrEmpty(entity.OuterBarcode) && x.OuterBarcode == entity.OuterBarcode));
+    }
+
     #endregion
 }
\ No newline at end of file

--
Gitblit v1.9.3