From ab01d75a3fbc8467d81a895cba5d6dde76fbe053 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期二, 16 九月 2025 23:29:30 +0800
Subject: [PATCH] 1111

---
 MES.Service/service/BasicData/MesCustomerManager.cs |   98 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 68 insertions(+), 30 deletions(-)

diff --git a/MES.Service/service/BasicData/MesCustomerManager.cs b/MES.Service/service/BasicData/MesCustomerManager.cs
index 56600d6..3ed16be 100644
--- a/MES.Service/service/BasicData/MesCustomerManager.cs
+++ b/MES.Service/service/BasicData/MesCustomerManager.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;
@@ -89,34 +90,22 @@
     private MesCustomer GetSysDepartment(ErpCustomer customer)
     {
         //ERP鍌宠几鐨勬帴鍙f暩鎿氳綁鎻涚偤MES鏁告摎搴瓧娈�
-        var fForbidStatus = customer.FForbidStatus;
-        if (customer.FForbidStatus == "0")
-        {
-            fForbidStatus = "A";
-        }
-        else if (customer.FForbidStatus == "1")
-        {
-            fForbidStatus = "B";
-        }
-
         var entity = new MesCustomer
         {
-            Id = string.IsNullOrEmpty(customer.Id)
-                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-                : Convert.ToDecimal(customer.Id),
             CustNo = customer.FNumber,
             CustSname = customer.FShortName,
             CustName = customer.FName,
             Anred = customer.FTContact,
             Telf1 = customer.Fmobilephone,
             Fseller = customer.Fseller,
-            Fforbidstatus = fForbidStatus,
-            FSubsidiary = string.IsNullOrEmpty(customer.FUseOrgId)
-                ? "1"
-                : customer.FUseOrgId,
-            Fumbrella = string.IsNullOrEmpty(customer.FCreateOrgId)
-                ? "1"
-                : customer.FCreateOrgId,
+            // FSubsidiary = string.IsNullOrEmpty(customer.FUseOrgId)
+            //     ? "1"
+            //     : customer.FUseOrgId,
+            // Fumbrella = string.IsNullOrEmpty(customer.FCreateOrgId)
+            //     ? "1"
+            //     : customer.FCreateOrgId,
+            FSubsidiary = "1",
+            Fumbrella = "1",
             CreateDate = DateTime.Now,
             LastupdateDate = DateTime.Now,
             Company = "1000",
@@ -125,16 +114,52 @@
             Type = customer.Type,
         };
 
-        var mesCustomer = Db.Queryable<MesCustomer>()
+        if (customer.FForbidStatus.IsNullOrEmpty())
+        {
+            entity.Fforbidstatus = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.Fforbidstatus = customer.FForbidStatus == "1" ? "B" : "A";
+        }
+
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽鎴风紪鐮佺殑璁板綍
+        var existingCustomer = Db.Queryable<MesCustomer>()
             .Where(s => s.CustNo == entity.CustNo)
             .First();
 
-        if (mesCustomer != null)
+        if (existingCustomer != null)
         {
-            entity.Id = mesCustomer.Id;
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑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 +200,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