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/SysDepartmentManager.cs |   97 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 65 insertions(+), 32 deletions(-)

diff --git a/MES.Service/service/BasicData/SysDepartmentManager.cs b/MES.Service/service/BasicData/SysDepartmentManager.cs
index 2b87924..6f29c07 100644
--- a/MES.Service/service/BasicData/SysDepartmentManager.cs
+++ b/MES.Service/service/BasicData/SysDepartmentManager.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;
@@ -80,64 +81,96 @@
         throw new NotImplementedException("鍒犻櫎澶辫触");
     }
 
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<SysDepartment>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+        
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<SysDepartment>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+        
+        return newId;
+    }
+
     // 鎻掑叆鎴栨洿鏂伴儴闂ㄧ殑鏂规硶
     private bool InsertOrUpdateDepartment(SqlSugarScope db,
         SysDepartment entity)
     {
-        db.Deleteable<SysDepartment>()
-            .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;
+            entity.Departmentid = newId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
+            
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<SysDepartment>().Where(s => s.Id == originalId).ExecuteCommand();
+            
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
+        }
     }
 
     // 灏� ErpDepartment 瀵硅薄杞崲涓� SysDepartment 瀵硅薄鐨勬柟娉�
     private SysDepartment GetSysDepartment(ErpDepartment department)
     {
-        var unixTimeSeconds = string.IsNullOrEmpty(department.Id)
-            ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-            : Convert.ToDecimal(department.Id);
-
-        var fForbidStatus = department.FForbidStatus;
-        if (department.FForbidStatus == "0")
-        {
-            fForbidStatus = "A";
-        }
-        else if (department.FForbidStatus == "1")
-        {
-            fForbidStatus = "B";
-        }
-
         var entity = new SysDepartment
         {
             Departmentcode = department.FNumber,
             Departmentname = department.FName,
-            Departmentid = unixTimeSeconds,
-            Id = unixTimeSeconds,
             Depextr1 = department.FDeptProperty,
             Depextr2 = department.FGroup,
             Depextr3 = department.FWIPStockID,
-            Depextr4 = fForbidStatus,
             FParentID = department.FParentID,
-            FSubsidiary = string.IsNullOrEmpty(department.FUseOrgId)
-                ? "1"
-                : department.FUseOrgId,
-            Fumbrella = string.IsNullOrEmpty(department.FCreateOrgId)
-                ? "1"
-                : department.FCreateOrgId,
+            FSubsidiary = "1",
+            Fumbrella = "1",
             CreateDate = DateTime.Now,
             LastupdateDate = DateTime.Now,
             Company = "1000",
             Factory = "1000"
         };
 
-        var sysDepartment = Db.Queryable<SysDepartment>()
+        if (department.FForbidStatus.IsNullOrEmpty())
+        {
+            entity.Depextr4 = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.Depextr4 = department.FForbidStatus == "1" ? "B" : "A";
+        }
+
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岄儴闂ㄧ紪鐮佺殑璁板綍
+        var existingDepartment = Db.Queryable<SysDepartment>()
             .Where(s => s.Departmentcode == entity.Departmentcode)
             .First();
 
-        if (sysDepartment != null)
+        if (existingDepartment != null)
         {
-            entity.Id = sysDepartment.Id;
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            entity.Id = existingDepartment.Id;
+            entity.Departmentid = existingDepartment.Id;
+        }
+        else
+        {
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            entity.Id = 0;
+            entity.Departmentid = 0;
         }
 
         return entity;

--
Gitblit v1.9.3