From f92369eeb7587ce03ae5bb09d74524fe6fb0f3e1 Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期五, 12 十二月 2025 11:04:14 +0800
Subject: [PATCH] 物料信息:工作中心编码转ID

---
 MES.Service/service/BasicData/MesItemsManager.cs |  312 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 291 insertions(+), 21 deletions(-)

diff --git a/MES.Service/service/BasicData/MesItemsManager.cs b/MES.Service/service/BasicData/MesItemsManager.cs
index e405e58..cf1dd48 100644
--- a/MES.Service/service/BasicData/MesItemsManager.cs
+++ b/MES.Service/service/BasicData/MesItemsManager.cs
@@ -1,10 +1,14 @@
-锘縰sing MES.Service.DB;
+锘縰sing AngleSharp.Dom;
+using MES.Service.DB;
 using MES.Service.Dto.webApi;
 using MES.Service.Modes;
+using SharpCompress.Common;
 using SqlSugar;
 
 namespace MES.Service.service.BasicData;
-
+/// <summary>
+/// 鐗╂枡淇℃伅
+/// </summary>
 public class MesItemsManager : Repository<MesItems>
 {
     public bool Save(ErpItems item)
@@ -113,16 +117,23 @@
 
     private MesItems GetMesItems(ErpItems item)
     {
-        return new MesItems
+        // 鏌ユ壘鏄惁宸插瓨鍦ㄧ浉鍚屽鎴风紪鐮佺殑璁板綍銆侷D銆乧reate_date
+        var existingCustomer = Db.Queryable<MesItems>()
+            .Where(s => s.ItemNo == item.FNumber)
+            .First();
+
+        var entity = new MesItems
         {
-            Id = Convert.ToDecimal(item.Id),
-            EItemId = long.Parse(item.Id),
-            ItemId = long.Parse(item.Id),
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑ID锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            // 濡傛灉涓嶅瓨鍦紝璁句负0锛孖nsertOrUpdate鏂规硶灏嗙敓鎴愭柊ID
+            Id = existingCustomer?.Id ?? 0,
+            EItemId = existingCustomer?.Id ?? 0,
+            ItemId = existingCustomer?.Id ?? 0,
             Type = item.Type,
             ItemNo = item.FNumber,
             ItemName = item.FName,
             ItemModel = item.FSpecification,
-            ItemUnit = item.FBaseUnitId,
+            ItemUnit = item.FBaseUnitId,//鐗╂枡鍩烘湰鍗曚綅缂栫爜杞琁D
             Lowlimit = Convert.ToDouble(item.FSafeStock),
             Highlimit = Convert.ToDouble(item.FMaxStock),
             PrdPack = Convert.ToDouble(item.FMinPackCount),
@@ -131,29 +142,249 @@
             Remarks = item.FDescription,
             Ffinishreceiptoverrate =
                 Convert.ToDecimal(item.FFinishReceiptOverRate),
-            Fissuetype = item.FIssueType,
-            Fisbatchmanage = Convert.ToInt32(item.FIsBatchManage),
+            //鍙戞枡鏂瑰紡
+            Fissuetype = item.FIssueType switch
+            {
+                "1" => "閫愭壒棰嗘枡",
+                "2" => "鑷姩鎵f枡",
+                _ => item.FIssueType
+            },
+            //Fissuetype = item.FIssueType,
+
+            //鎵瑰彿绠$悊
+            Fisbatchmanage = item.FIsBatchManage switch
+            {
+                var v when string.Equals(v, "N", StringComparison.OrdinalIgnoreCase) => 0,
+                var v when string.Equals(v, "Y", StringComparison.OrdinalIgnoreCase) => 1,
+                var v when string.Equals(v, "W", StringComparison.OrdinalIgnoreCase) => 2,
+                var v when string.Equals(v, "T", StringComparison.OrdinalIgnoreCase) => 3,
+                _ => int.TryParse(item.FIsBatchManage, out var val) ? val : 0
+            },
             Fpurchaserid = item.FPurchaserId,
-            Fpurchaseunitid = Convert.ToDecimal(item.FPurchaseUnitId),
+            Fpurchaseunitid = item.FPurchaseUnitId,
             Storeunit = item.FStoreUnitID,
             Saleunit = item.FSaleUnitId,
-            Fforbidstatus = item.FForbidStatus,
+            Fforbidstatus = item.FForbidStatus,//鍚敤涓庣鐢�
             MaterialProperti = item.FErpClsID,
             ProductionWorkshop = item.FWorkShopId,
             ProduceUnit = item.FPRODUCEUNITID,
             SubconUnit = item.FSUBCONUNITID,
-            FSubsidiary = item.FUseOrgId,
-            Fumbrella = item.FCreateOrgId,
+            //FSubsidiary = item.FUseOrgId,
+            //Fumbrella = item.FCreateOrgId,
             LastupdateDate = DateTime.Now,
-            CreateDate = DateTime.Now,
+            // 濡傛灉瀛樺湪锛屼娇鐢ㄧ幇鏈夌殑CreateDate锛屽悗缁皢鍒犻櫎鍚庨噸鏂版彃鍏�
+            // 濡傛灉涓嶅瓨鍦紝璁句负褰撳墠鏃堕棿
+            CreateDate = existingCustomer?.CreateDate ?? DateTime.Now,
             MnemonicCode = item.FMnemonicCode,
             Company = "1000",
-            Factory = "1000"
+            Factory = "1000",
+
+            InspectionMethod= item.Inspectionmethod,
+            ExpirationDate= item.Expirationdate,
+            ReviewPeriod= item.Reviewperiod,
+            LeadDays= item.FLeadDays,
+            Remark5 = item.FRemark5,
+
+            FSubsidiary = string.IsNullOrEmpty(item.FUseOrgId) ? "1" : item.FUseOrgId,
+            Fumbrella = string.IsNullOrEmpty(item.FCreateOrgId) ? "1" : item.FCreateOrgId,
         };
+        //鐗╂枡鍩烘湰鍗曚綅缂栫爜杞琁D,ItemUnit = item.FBaseUnitId
+        var entityItemUnit = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FBaseUnitId)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityItemUnit))
+        {
+            entity.ItemUnit = entityItemUnit;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FBaseUnitId))
+        {
+            entity.ItemUnit = item.FBaseUnitId;
+        }
+        else
+        {
+            entity.ItemUnit = "0";
+        }
+
+        //鐗╂枡閿�鍞崟浣嶇紪鐮佽浆ID,Saleunit = item.FSaleUnitId
+        var entitySaleunit = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FSaleUnitId)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entitySaleunit))
+        {
+            entity.Saleunit = entitySaleunit;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FSaleUnitId))
+        {
+            entity.Saleunit = item.FSaleUnitId;
+        }
+        else
+        {
+            entity.Saleunit = "0";
+        }
+
+        //鐗╂枡搴撴埧鍗曚綅缂栫爜杞琁D,Storeunit = item.FStoreUnitID
+        var entityStoreunit = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FStoreUnitID)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityStoreunit))
+        {
+            entity.Storeunit = entityStoreunit;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FStoreUnitID))
+        {
+            entity.Storeunit = item.FStoreUnitID;
+        }
+        else
+        {
+            entity.Storeunit = "0";
+        }
+
+        //鐗╂枡閲囪喘鍗曚綅缂栫爜杞琁D,Fpurchaseunitid = item.FPurchaseUnitId
+        var entityFpurchaseunitid = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FPurchaseUnitId)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityFpurchaseunitid))
+        {
+            entity.Fpurchaseunitid = entityFpurchaseunitid;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FPurchaseUnitId))
+        {
+            entity.Fpurchaseunitid = item.FPurchaseUnitId;
+        }
+        else
+        {
+            entity.Fpurchaseunitid = "0";
+        }
+
+        //鐗╂枡鐢熶骇鍗曚綅缂栫爜杞琁D,ProduceUnit = item.FPRODUCEUNITID
+        var entityProduceUnit = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FPRODUCEUNITID)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityProduceUnit))
+        {
+            entity.ProduceUnit = entityProduceUnit;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FPRODUCEUNITID))
+        {
+            entity.ProduceUnit = item.FPRODUCEUNITID;
+        }
+        else
+        {
+            entity.ProduceUnit = "0";
+        }
+
+        //鐗╂枡濮斿鍗曚綅缂栫爜杞琁D,SubconUnit = item.FSUBCONUNITID
+        var entitySubconUnit = Db.Queryable<MesUnit>()
+        .Where(x => x.Fnumber == item.FSUBCONUNITID)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entitySubconUnit))
+        {
+            entity.SubconUnit = entitySubconUnit;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FSUBCONUNITID))
+        {
+            entity.SubconUnit = item.FSUBCONUNITID;
+        }
+        else
+        {
+            entity.SubconUnit = "0";
+        }
+
+        //榛樿浠撳簱缂栫爜杞琁D,DepotCode = item.FStockId,
+        var entityDepotCode = Db.Queryable<MesDepots>()
+        .Where(x => x.DepotCode == item.FStockId)
+        .Select(x => x.DepotId.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityDepotCode))
+        {
+            entity.DepotCode = entityDepotCode;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FStockId))
+        {
+            entity.DepotCode = item.FStockId;
+        }
+        else
+        {
+            entity.DepotCode = "0";
+        }
+
+        //閲囪喘鍛樼紪鐮佽浆ID,Fpurchaserid = item.FPurchaserId,
+        var entityFpurchaserid = Db.Queryable<MesStaff>()
+        .Where(x => x.StaffNo == item.FPurchaserId)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityFpurchaserid))
+        {
+            entity.Fpurchaserid = entityFpurchaserid;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FPurchaserId))
+        {
+            entity.Fpurchaserid = item.FPurchaserId;
+        }
+        else
+        {
+            entity.Fpurchaserid = "0";
+        }
+
+        //宸ヤ綔涓績缂栫爜杞琁D,ProductionWorkshop = item.FWorkShopId,
+        var entityProductionWorkshop = Db.Queryable<SysDepartment>()
+        .Where(x => x.WorkshopCenterCode == item.FWorkShopId)
+        .Select(x => x.Id.ToString())
+        .First();
+        if (!string.IsNullOrWhiteSpace(entityProductionWorkshop))
+        {
+            entity.ProductionWorkshop = entityProductionWorkshop;
+        }
+        else if (!string.IsNullOrWhiteSpace(item.FWorkShopId))
+        {
+            entity.ProductionWorkshop = item.FWorkShopId;
+        }
+        else
+        {
+            entity.ProductionWorkshop = "0";
+        }
+
+        //// ERP: Y=鏈鐢�, N=绂佺敤
+        //// MES: A=鏈鐢�, B=绂佺敤
+        //if (string.IsNullOrEmpty(item.FForbidStatus))
+        //{
+        //    entity.Fforbidstatus = "A";
+        //}
+        //else
+        //{
+        //    //鎴戞湡鏈涚殑鍊兼槸A=鍚敤,B=绂佺敤
+        //    //瀹為檯缁欐垜鐨勫�兼槸Y鎴朜锛屾垜甯屾湜涓烘垜杞崲浠嶢鍜孊鐨勬柟寮�
+        //    entity.Fforbidstatus = item.FForbidStatus == "N" ? "B" : "A";
+        //}
+
+        return entity;
     }
 
-    private bool UpdateItemStatusBatch(SqlSugarScope db,
-        List<MesItems> itemList, string status)
+    /// <summary>
+    /// 鐢熸垚鏂扮殑ID锛岀‘淇濅笉閲嶅
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+        var maxId = Db.Queryable<MesItems>().Max(x => (decimal?)x.Id) ?? 0;
+        var newId = maxId + 1;
+
+        // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+        while (Db.Queryable<MesItems>().Where(x => x.Id == newId).Any())
+        {
+            newId++;
+        }
+
+        return newId;
+    }
+
+    private bool UpdateItemStatusBatch(SqlSugarScope db,List<MesItems> itemList, string status)
     {
         var ids = itemList.Select(it => it.Id).ToArray();
         var result = db.Updateable<MesItems>()
@@ -186,15 +417,54 @@
         throw new NotImplementedException("鍒犻櫎澶辫触");
     }
 
+    /// <summary>
+    /// 鍚屾鐗╂枡淇℃伅new_0/4
+    /// </summary>
+    /// <param name="db"></param>
+    /// <param name="entity"></param>
+    /// <returns></returns>
     private bool InsertOrUpdate(SqlSugarScope db, MesItems entity)
     {
-        db.Deleteable<MesItems>().Where(s => s.Id == entity.Id)
-            .ExecuteCommand();
+        if (entity.Id == 0)
+        {
+            // 鏂板鎯呭喌锛氱敓鎴愭柊ID骞舵彃鍏�
+            var newId = GenerateNewId();
+            entity.Id = newId;
+            entity.EItemId = newId;
+            entity.ItemId = newId;
+            return db.Insertable(entity).ExecuteCommand() > 0;
+        }
+        else
+        {
+            // 鏇存柊鎯呭喌锛氬垹闄ゅ悗閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            var originalId = entity.Id;
 
-        var insert = db.Insertable(entity).ExecuteCommand();
-        return insert > 0;
+            // 鍏堝垹闄ゅ師璁板綍锛堝鏋滃瓨鍦級
+            db.Deleteable<MesItems>().Where(s => s.Id == originalId).ExecuteCommand();
+
+            // 閲嶆柊鎻掑叆锛屼繚鎸佸師鏈塈D
+            entity.Id = originalId;
+            entity.EItemId = originalId;
+            entity.ItemId = 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, MesItems entity)
+    //{
+    //    db.Deleteable<MesItems>().Where(s => s.Id == entity.Id)
+    //        .ExecuteCommand();
+
+    //    var insert = db.Insertable(entity).ExecuteCommand();
+    //    return insert > 0;
+    //}
+
     private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesItems> itemList)
     {
         return itemList.All(entity => InsertOrUpdate(db, entity));

--
Gitblit v1.9.3