From b2cca1709665f24f539593081c3278e5073328cf Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期四, 27 十一月 2025 10:25:56 +0800
Subject: [PATCH] 物料单位信息

---
 MES.Service/service/BasicData/MesUnitManager.cs |   95 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/MES.Service/service/BasicData/MesUnitManager.cs b/MES.Service/service/BasicData/MesUnitManager.cs
index 1223f7e..2176fd2 100644
--- a/MES.Service/service/BasicData/MesUnitManager.cs
+++ b/MES.Service/service/BasicData/MesUnitManager.cs
@@ -1,4 +1,5 @@
-锘縰sing MES.Service.DB;
+锘縰sing AngleSharp.Dom;
+using MES.Service.DB;
 using MES.Service.Dto.webApi;
 using MES.Service.Modes;
 using SqlSugar;
@@ -112,9 +113,17 @@
 
     private MesUnit GetMesUnit(ErpUnit unit)
     {
-        return new MesUnit
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚岀紪鐮佺殑璁板綍銆�
+        var existingCustomer = Db.Queryable<MesUnit>()
+            .Where(s => s.Fnumber == unit.FNumber)
+            .First();
+
+        var entity = new MesUnit
         {
-            Id = Convert.ToDecimal(unit.Id),
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            Id = existingCustomer?.Id ?? 0,
+            //Id = Convert.ToDecimal(unit.Id),
             Fnumber = unit.FNumber,
             Fname = unit.FName,
             Funitgroupid = unit.FUnitGroupId,
@@ -126,12 +135,47 @@
             Fprecision = unit.FPrecision,
             Fdescription = unit.FDescription,
             Fforbidstatus = unit.FForbidStatus,
-            CreateDate = DateTime.Now,
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑CreateDate锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            // 濡傛灉涓嶅瓨鍦紝璁句负褰撳墠鏃堕棿
+            CreateDate = existingCustomer?.CreateDate ?? DateTime.Now,
             LastupdateDate = DateTime.Now,
             Company = "1000",
             Factory = "1000",
             Type = unit.Type
         };
+
+        // ERP: 0=鏈鐢�, 1=绂佺敤
+        // MES: A=鏈鐢�, B=绂佺敤
+        if (string.IsNullOrEmpty(unit.FForbidStatus))
+        {
+            entity.Fforbidstatus = "A";
+        }
+        else
+        {
+            //鎴戞湡鏈涚殑鍊兼槸A=鍚�,B=鏄�
+            //瀹為檯缁欐垜鐨勫�兼槸0鎴�1锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+            entity.Fforbidstatus = unit.FForbidStatus == "1" ? "B" : "A";
+        }
+
+        return entity;
+    }
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesUnit>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesUnit>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+
+        return newId;
     }
 
     private bool UpdateUnitStatusBatch(SqlSugarScope db, List<MesUnit> unitList,
@@ -165,14 +209,49 @@
         return deleteByIds > 0;
     }
 
+    /// <summary>
+    /// 鍚屾鐗╂枡鍗曚綅淇℃伅new_0/4
+    /// </summary>
+    /// <param name="db"></param>
+    /// <param name="entity"></param>
+    /// <returns></returns>
     private bool InsertOrUpdate(SqlSugarScope db, MesUnit entity)
     {
-        db.Deleteable<MesUnit>()
-            .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).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
+
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesUnit>().Where(s => s.Id == originalId).ExecuteCommand();
+
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            return db.Insertable(entity).ExecuteCommand() > 0;
+        }
     }
 
+    /// <summary>
+    /// 鍚屾鐗╂枡鍗曚綅淇℃伅old_0/4
+    /// </summary>
+    /// <param name="db"></param>
+    /// <param name="entity"></param>
+    /// <returns></returns>
+    //private bool InsertOrUpdate(SqlSugarScope db, MesUnit entity)
+    //{
+    //    db.Deleteable<MesUnit>()
+    //        .Where(s => s.Id == entity.Id).ExecuteCommand();
+    //    var insert = db.Insertable(entity).ExecuteCommand();
+    //    return insert > 0;
+    //}
+
     private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesUnit> unitList)
     {
         return unitList.All(entity => InsertOrUpdate(db, entity));

--
Gitblit v1.9.3