From dd3461812f84ac4f930d3c51e5b8eba92fea4dab Mon Sep 17 00:00:00 2001
From: kyy <3283105747@qq.com>
Date: 星期五, 12 九月 2025 15:47:29 +0800
Subject: [PATCH] 1、采购订单日期转换 2、收料通知修改

---
 MES.Service/service/BasicData/MesDepotsManager.cs |  131 ++++++++++++++++++++++++-------------------
 1 files changed, 72 insertions(+), 59 deletions(-)

diff --git a/MES.Service/service/BasicData/MesDepotsManager.cs b/MES.Service/service/BasicData/MesDepotsManager.cs
index d89e904..16b4919 100644
--- a/MES.Service/service/BasicData/MesDepotsManager.cs
+++ b/MES.Service/service/BasicData/MesDepotsManager.cs
@@ -42,7 +42,7 @@
     }
 
     // 鏇存柊浠撳簱鐘舵�佺殑鏂规硶
-    private bool UpdateDepotStatus(SqlSugarScope db, long depotId,
+    private bool UpdateDepotStatus(SqlSugarScope db, decimal depotId,
         string status)
     {
         var result = db.Updateable<MesDepots>()
@@ -76,26 +76,55 @@
         throw new NotImplementedException("鍙嶅鏍稿け璐�");
     }
 
+    /// <summary>
+    /// 鐢熸垚鏂扮殑DepotId锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesDepots>().Max(x => (decimal?)x.DepotId) ?? 0;
+        var newId = maxId + 1;
+        
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨凞epotId涓嶅瓨鍦�
+        while (Db.Queryable<MesDepots>().Where(x => x.DepotId == newId).Any())
+        {
+            newId++;
+        }
+        
+        return newId;
+    }
+
     // 鎻掑叆鎴栨洿鏂颁粨搴撶殑鏂规硶
     private bool InsertOrUpdate(SqlSugarScope db, MesDepots entity)
     {
-        db.Deleteable<MesDepots>()
-            .Where(s => s.DepotId == entity.DepotId)
-            .ExecuteCommand();
-        var insert = db.Insertable(entity).ExecuteCommand();
-        return insert > 0;
+        if (entity.DepotId == 0)
+        {
+            // 鏂板鎯呭喌锛氱敓鎴愭柊DepotId骞舵彃鍏�
+            var newId = GenerateNewId();
+            entity.DepotId = newId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塂epotId
+            var originalId = entity.DepotId;
+            
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesDepots>().Where(s => s.DepotId == originalId).ExecuteCommand();
+            
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塂epotId
+            entity.DepotId = originalId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
     }
 
     // 灏� ErpDepots 瀵硅薄杞崲涓� MesDepots 瀵硅薄鐨勬柟娉�
     private MesDepots GetMesDepots(ErpDepots depots)
     {
-        return new MesDepots
+        var entity = new MesDepots
         {
             DepotCode = depots.FNumber,
             DepotName = depots.FName,
-            DepotId = string.IsNullOrEmpty(depots.Id)
-                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-                : long.Parse(depots.Id),
             IsFkc = depots.FAllowMinusQty,
             CreateBy = depots.FPrincipal,
             Depottype = depots.FStockProperty,
@@ -103,65 +132,49 @@
             Zuid = depots.FGroup,
             DocumentStatus = depots.FDocumentStatus,
             
-            UseOrg = string.IsNullOrEmpty(depots.FUseOrgId)
-                ? 1
-                : long.Parse(depots.FUseOrgId),
-            CreateOrg = string.IsNullOrEmpty(depots.FCreateOrgId)
-                ? 1
-                : long.Parse(depots.FCreateOrgId),
-            CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-            LastupdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+            FSubsidiary = string.IsNullOrEmpty(depots.FUseOrgId)
+                ? "1"
+                :  depots.FUseOrgId,
+            Fumbrella = string.IsNullOrEmpty(depots.FCreateOrgId)
+                ? "1"
+                : depots.FCreateOrgId,
+            CreateDate = DateTime.Now,
+            LastupdateDate = DateTime.Now,
             Company = "1000",
             Factory = "1000"
         };
+
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屼粨搴撶紪鐮佺殑璁板綍
+        var existingDepot = Db.Queryable<MesDepots>()
+            .Where(s => s.DepotCode == entity.DepotCode)
+            .First();
+
+        if (existingDepot != null)
+        {
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑DepotId锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            entity.DepotId = existingDepot.DepotId;
+        }
+        else
+        {
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊DepotId
+            entity.DepotId = 0;
+        }
+
+        return entity;
     }
 
     // SaveList 鏂规硶鐢ㄤ簬淇濆瓨澶氫釜浠撳簱璁板綍锛屾牴鎹被鍨嬫壒閲忔墽琛屼笉鍚岀殑鎿嶄綔
     public bool SaveList(List<ErpDepots> erpDepots)
     {
-        var list = new List<MesDepots>();
-        erpDepots.ForEach(s =>
+        if (erpDepots == null || !erpDepots.Any())
         {
-            var entity = GetMesDepots(s); // 灏� ErpDepots 杞崲涓� MesDepots
-            entity.Type = s.Type;
-            list.Add(entity);
-        });
+            Console.WriteLine("璀﹀憡: 浼犲叆鐨勫垪琛ㄤ负绌�");
+            return false;
+        }
 
-        var groupBy = list.GroupBy(s => s.Type)
-            .ToDictionary(g => g.Key, g => g.ToList());
-        return UseTransaction(db =>
-        {
-            foreach (var depotsGroup in groupBy)
-                switch (depotsGroup.Key)
-                {
-                    case "0":
-                        if (!UpdateDepotStatusBatch(db, depotsGroup.Value,
-                                "A")) // 鎵归噺鍚敤浠撳簱
-                            throw new NotImplementedException("鍚敤澶辫触");
-                        break;
-                    case "1":
-                        if (!UpdateDepotStatusBatch(db, depotsGroup.Value,
-                                "B")) // 鎵归噺绂佺敤浠撳簱
-                            throw new NotImplementedException("绂佺敤澶辫触");
-                        break;
-                    case "3":
-                        if (!DeleteDepotBatch(db,
-                                depotsGroup.Value)) // 鎵归噺鍒犻櫎浠撳簱
-                            throw new NotImplementedException("鍒犻櫎澶辫触");
-                        break;
-                    case "2":
-                    case "4":
-                        if (!InsertOrUpdateBatch(db,
-                                depotsGroup.Value)) // 鎵归噺鎻掑叆鎴栨洿鏂颁粨搴�
-                            throw new NotImplementedException("鍚屾澶辫触");
-                        break;
-                    default:
-                        throw new ArgumentNullException(
-                            $"type娌℃湁{depotsGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�");
-                }
-
-            return 1;
-        }) > 0;
+        // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛�
+        var result = erpDepots.Select(Save).ToList();
+        return result.All(b => b);
     }
 
     // 鎵归噺鏇存柊浠撳簱鐘舵�佺殑鏂规硶

--
Gitblit v1.9.3