From e159c28b6d090dc875c878e462ceb45a809315d7 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期五, 12 九月 2025 11:29:46 +0800
Subject: [PATCH] 111

---
 MES.Service/service/BasicData/MesCustomerManager.cs |  125 ++++++++++++++++++++++++++---------------
 1 files changed, 80 insertions(+), 45 deletions(-)

diff --git a/MES.Service/service/BasicData/MesCustomerManager.cs b/MES.Service/service/BasicData/MesCustomerManager.cs
index 4fd7e79..b05bf0e 100644
--- a/MES.Service/service/BasicData/MesCustomerManager.cs
+++ b/MES.Service/service/BasicData/MesCustomerManager.cs
@@ -43,41 +43,15 @@
 
     public bool SaveList(List<ErpCustomer> customers)
     {
-        var list = customers.Select(GetSysDepartment).ToList();
-        var groupBy = list.GroupBy(s => s.Type)
-            .ToDictionary(g => g.Key, g => g.ToList());
-
-        return UseTransaction(db =>
+        if (customers == null || !customers.Any())
         {
-            foreach (var customerGroup in groupBy)
-                switch (customerGroup.Key)
-                {
-                    case "0":
-                        if (!UpdateCustomerStatusBatch(db, customerGroup.Value,
-                                "A"))
-                            throw new NotImplementedException("鍚敤澶辫触");
-                        break;
-                    case "1":
-                        if (!UpdateCustomerStatusBatch(db, customerGroup.Value,
-                                "B"))
-                            throw new NotImplementedException("绂佺敤澶辫触");
-                        break;
-                    case "3":
-                        if (!DeleteCustomerBatch(db, customerGroup.Value))
-                            throw new NotImplementedException("鍒犻櫎澶辫触");
-                        break;
-                    case "2":
-                    case "4":
-                        if (!InsertOrUpdateBatch(db, customerGroup.Value))
-                            throw new NotImplementedException("鍚屾澶辫触");
-                        break;
-                    default:
-                        throw new ArgumentNullException(
-                            $"type娌℃湁{customerGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�");
-                }
+            Console.WriteLine("璀﹀憡: 浼犲叆鐨勫垪琛ㄤ负绌�");
+            return false;
+        }
 
-            return 1;
-        }) > 0;
+        // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛�
+        var result = customers.Select(Save).ToList();
+        return result.All(b => b);
     }
 
     private bool UpdateCustomerStatus(SqlSugarScope db, decimal customerId,
@@ -115,19 +89,31 @@
     private MesCustomer GetSysDepartment(ErpCustomer customer)
     {
         //ERP鍌宠几鐨勬帴鍙f暩鎿氳綁鎻涚偤MES鏁告摎搴瓧娈�
-
-        return new MesCustomer
+        var fForbidStatus = customer.FForbidStatus;
+        if (customer.FForbidStatus == "0")
         {
-            Id = Convert.ToDecimal(customer.Id),
+            fForbidStatus = "A";
+        }
+        else if (customer.FForbidStatus == "1")
+        {
+            fForbidStatus = "B";
+        }
+
+        var entity = new MesCustomer
+        {
             CustNo = customer.FNumber,
             CustSname = customer.FShortName,
             CustName = customer.FName,
             Anred = customer.FTContact,
             Telf1 = customer.Fmobilephone,
             Fseller = customer.Fseller,
-            Fforbidstatus = customer.FForbidStatus,
-            FSubsidiary = customer.FUseOrgId,
-            Fumbrella = customer.FCreateOrgId,
+            Fforbidstatus = fForbidStatus,
+            FSubsidiary = string.IsNullOrEmpty(customer.FUseOrgId)
+                ? "1"
+                : customer.FUseOrgId,
+            Fumbrella = string.IsNullOrEmpty(customer.FCreateOrgId)
+                ? "1"
+                : customer.FCreateOrgId,
             CreateDate = DateTime.Now,
             LastupdateDate = DateTime.Now,
             Company = "1000",
@@ -135,6 +121,42 @@
             DataType = customer.FDocumentStatus,
             Type = customer.Type,
         };
+
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽鎴风紪鐮佺殑璁板綍
+        var existingCustomer = Db.Queryable<MesCustomer>()
+            .Where(s => s.CustNo == entity.CustNo)
+            .First();
+
+        if (existingCustomer != null)
+        {
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            entity.Id = existingCustomer.Id;
+        }
+        else
+        {
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            entity.Id = 0;
+        }
+
+        return entity;
+    }
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesCustomer>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+        
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesCustomer>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+        
+        return newId;
     }
 
     private bool UpdateCustomerStatusBatch(SqlSugarScope db,
@@ -175,12 +197,25 @@
 
     private bool InsertOrUpdate(SqlSugarScope db, MesCustomer entity)
     {
-        db.Deleteable<MesCustomer>().Where(s => s.Id == entity.Id)
-            .ExecuteCommand();
-
-        var insert = db.Insertable(entity).ExecuteCommand();
-
-        return insert > 0;
+        if (entity.Id == 0)
+        {
+            // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏�
+            var newId = GenerateNewId();
+            entity.Id = newId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
+            
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesCustomer>().Where(s => s.Id == originalId).ExecuteCommand();
+            
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
     }
 
     private bool InsertOrUpdateBatch(SqlSugarScope db,

--
Gitblit v1.9.3