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

---
 MES.Service/service/BasicData/MesSupplierManager.cs |   91 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 69 insertions(+), 22 deletions(-)

diff --git a/MES.Service/service/BasicData/MesSupplierManager.cs b/MES.Service/service/BasicData/MesSupplierManager.cs
index 6a1aa6a..d0c065b 100644
--- a/MES.Service/service/BasicData/MesSupplierManager.cs
+++ b/MES.Service/service/BasicData/MesSupplierManager.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;
@@ -90,9 +91,6 @@
     {
         var entity = new MesSupplier
         {
-            // Id = string.IsNullOrEmpty(supplier.Id)
-            //     ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-            //     : Convert.ToDecimal(supplier.Id),
             Type = supplier.Type,
             SuppNo = supplier.FNumber,
             SuppSname = supplier.FShortName,
@@ -100,27 +98,47 @@
             Lxr = supplier.FTContact,
             Telf1 = supplier.Fmobilephone,
             Fstaffid = supplier.FStaffId,
-            Fforbidstatus = supplier.FForbidStatus,
-            FSubsidiary = string.IsNullOrEmpty(supplier.FUseOrgId)
-                ? "1"
-                : supplier.FUseOrgId,
-            Fumbrella = string.IsNullOrEmpty(supplier.FCreateOrgId)
-                ? "1"
-                : supplier.FCreateOrgId,
+            // FSubsidiary = string.IsNullOrEmpty(supplier.FUseOrgId)
+            //     ? "1"
+            //     : supplier.FUseOrgId,
+            // Fumbrella = string.IsNullOrEmpty(supplier.FCreateOrgId)
+            //     ? "1"
+            //     : supplier.FCreateOrgId,
+            FSubsidiary = "1",
+            Fumbrella = "1",
             Remark = supplier.Remark,
             CreateDate = DateTime.Now,
             LastupdateDate = DateTime.Now,
             Company = "1000",
             Factory = "1000",
         };
+        
+        //Fforbidstatus = supplier.FForbidStatus,
+        if (supplier.FForbidStatus.IsNullOrEmpty())
+        {
+            entity.Fforbidstatus = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.Fforbidstatus = supplier.FForbidStatus == "1" ? "B" : "A";
+        }
 
-        var mesSupplier = Db.Queryable<MesSupplier>()
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屼緵搴斿晢缂栫爜鐨勮褰�
+        var existingSupplier = Db.Queryable<MesSupplier>()
             .Where(s => s.SuppNo == entity.SuppNo)
             .First();
 
-        if (mesSupplier != null)
+        if (existingSupplier != null)
         {
-            entity.Id = mesSupplier.Id;
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            entity.Id = existingSupplier.Id;
+        }
+        else
+        {
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            entity.Id = 0;
         }
 
         return entity;
@@ -162,16 +180,45 @@
         throw new NotImplementedException("鍒犻櫎澶辫触");
     }
 
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesSupplier>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+        
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesSupplier>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+        
+        return newId;
+    }
+
     private bool InsertOrUpdate(SqlSugarScope db, MesSupplier entity)
     {
-        db.Deleteable<MesSupplier>().Where(s => s.Id == entity.Id)
-            .ExecuteCommand();
-
-        var insert = db.Insertable(entity).ExecuteCommand();
-        if (insert > 0)
-            return true;
-
-        return false;
+        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<MesSupplier>().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