From 1db62c18bc7bcee7d7be2cd34b6f35d0750f7ad1 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期二, 16 九月 2025 15:49:49 +0800
Subject: [PATCH] 11

---
 MES.Service/service/BasicData/MesItemsManager.cs |  111 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 72 insertions(+), 39 deletions(-)

diff --git a/MES.Service/service/BasicData/MesItemsManager.cs b/MES.Service/service/BasicData/MesItemsManager.cs
index 7ba11c5..c8da48f 100644
--- a/MES.Service/service/BasicData/MesItemsManager.cs
+++ b/MES.Service/service/BasicData/MesItemsManager.cs
@@ -1,4 +1,5 @@
-锘縰sing MES.Service.DB;
+锘縰sing Masuit.Tools;
+using MES.Service.DB;
 using MES.Service.Dto.webApi;
 using MES.Service.Modes;
 using SqlSugar;
@@ -49,7 +50,7 @@
             Console.WriteLine("璀﹀憡: 浼犲叆鐨勭墿鏂欏垪琛ㄤ负绌�");
             return false;
         }
-        
+
         // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛�
         var result = items.Select(Save).ToList();
         return result.All(b => b);
@@ -89,73 +90,86 @@
 
     private MesItems GetMesItems(ErpItems item)
     {
-        // var id = string.IsNullOrEmpty(item.Id)
-        //     ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-        //     : long.Parse(item.Id);
         var entity = new MesItems
         {
-            // Id = id,
+            Guid = Guid.NewGuid(),
             Type = item.Type,
             ItemNo = item.FNumber,
             ItemName = item.FName,
             ItemModel = item.FSpecification,
             ItemUnit = item.FBaseUnitId,
-            // Lowlimit = !string.IsNullOrEmpty(item.FSafeStock)
-            //     ? Convert.ToDecimal(item.FSafeStock)
-            //     : null,
-            // Highlimit = !string.IsNullOrEmpty(item.FMaxStock)
-            //     ? Convert.ToDecimal(item.FMaxStock)
-            //     : null,
-            // PrdPack = !string.IsNullOrEmpty(item.FMinPackCount)
-            //     ? Convert.ToDecimal(item.FMinPackCount)
-            //     : null,
             DepotCode = item.FStockId,
             Fmaterialgroup = item.FMaterialGroup,
             Remarks = item.FDescription,
-            // Ffinishreceiptoverrate =
-            //     !string.IsNullOrEmpty(item.FFinishReceiptOverRate)
-            //         ? Convert.ToInt32(item.FFinishReceiptOverRate)
-            //         : null,
             Fissuetype = item.FIssueType,
-            // Fisbatchmanage = !string.IsNullOrEmpty(item.FIsBatchManage)
-            //     ? Convert.ToInt32(item.FIsBatchManage)
-            //     : null,
             Fpurchaserid = item.FPurchaserId,
             Fpurchaseunitid = item.FPurchaseUnitId,
             Storeunit = item.FStoreUnitID,
             Saleunit = item.FSaleUnitId,
-            Fforbidstatus = item.FForbidStatus,
+            // Fforbidstatus = item.FForbidStatus,
             MaterialProperti = item.FErpClsID,
             ProductionWorkshop = item.FWorkShopId,
             ProduceUnit = item.FPRODUCEUNITID,
             SubconUnit = item.FSUBCONUNITID,
-            FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId)
-                ? "1"
-                : item.FUseOrgId,
-            Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId)
-                ? "1"
-                : item.FCreateOrgId,
+            FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId) ? "1" : item.FUseOrgId,
+            Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId) ? "1" : item.FCreateOrgId,
             LossPercent = item.FLOSSPERCENT,
             MnemonicCode = item.FMnemonicCode,
             ExpPeriod = item.FExpPeriod,
-            // EItemId = id,
-            // ItemId = id,
             LastupdateDate = DateTime.Now,
             CreateDate = DateTime.Now,
             Company = "1000",
             Factory = "1000"
         };
+        
+        if (item.FForbidStatus.IsNullOrEmpty())
+        {
+            entity.Fforbidstatus = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.Fforbidstatus = item.FForbidStatus == "1" ? "B" : "A";
+        }
 
-        var mesItems = Db.Queryable<MesItems>()
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岀墿鏂欑紪鐮佺殑璁板綍
+        var existingItem = Db.Queryable<MesItems>()
             .Where(s => s.ItemNo == entity.ItemNo)
             .First();
 
-        if (mesItems != null)
+        if (existingItem != null)
         {
-            entity.Id = mesItems.Id;
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            entity.Id = existingItem.Id;
+            entity.ItemId = existingItem.Id;
+        }
+        else
+        {
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            entity.Id = 0;
+            entity.ItemId = 0;
         }
 
         return entity;
+    }
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesItems>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+        
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesItems>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+        
+        return newId;
     }
 
     private bool UpdateItemStatusBatch(SqlSugarScope db,
@@ -194,11 +208,30 @@
 
     private bool InsertOrUpdate(SqlSugarScope db, MesItems entity)
     {
-        db.Deleteable<MesItems>().Where(s => s.Id == entity.Id)
-            .ExecuteCommand();
-
-        var insert = db.Insertable(entity).ExecuteCommand();
-        return insert > 0;
+        // 纭繚ItemId涓嶪d淇濇寔涓�鑷�
+        entity.ItemId = entity.Id;
+        
+        if (entity.Id == 0)
+        {
+            // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏�
+            var newId = GenerateNewId();
+            entity.Id = newId;
+            entity.ItemId = newId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
+            
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesItems>().Where(s => s.Id == originalId).ExecuteCommand();
+            
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            entity.ItemId = originalId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
     }
 
     private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesItems> itemList)

--
Gitblit v1.9.3