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 |   48 +++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
index b4212dd..35bb9b1 100644
--- a/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
+++ b/MES.Service/service/BasicData/DeliveryBarcodeManager.cs
@@ -29,9 +29,9 @@
             // 鎸塗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锛堝垹闄わ級");
@@ -78,14 +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锛堝垹闄わ級");
                 }
             }
 
@@ -304,7 +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
         {
@@ -316,7 +316,7 @@
             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,
@@ -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