From 0c999fbeed306e212e30baabf90792afa52412dc Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期三, 17 十二月 2025 08:37:53 +0800
Subject: [PATCH] 主表ID改为序列化生成:生产订单、采购订单。采购订单整单删除功能日志消息补全。

---
 MES.Service/service/BasicData/MesRohInManager.cs |  314 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 292 insertions(+), 22 deletions(-)

diff --git a/MES.Service/service/BasicData/MesRohInManager.cs b/MES.Service/service/BasicData/MesRohInManager.cs
index 8e79bdc..cdc658e 100644
--- a/MES.Service/service/BasicData/MesRohInManager.cs
+++ b/MES.Service/service/BasicData/MesRohInManager.cs
@@ -1,4 +1,5 @@
-锘縰sing Masuit.Tools;
+锘縰sing AngleSharp.Dom;
+using Masuit.Tools;
 using MES.Service.DB;
 using MES.Service.Dto.webApi;
 using MES.Service.Modes;
@@ -8,7 +9,9 @@
 using System.Security.AccessControl;
 
 namespace MES.Service.service.BasicData;
-
+/// <summary>
+/// 閲囪喘璁㈠崟
+/// </summary>
 public class MesRohInManager : Repository<MesRohIn>
 {
     private readonly MesRohInDataManager rohInDataManager = new();
@@ -17,9 +20,10 @@
     public bool Save(RohIn rohIn)
     {
         var rohInErpRohIn = rohIn.ErpRohIn;
+
         var mesRohIn = GetMesRohIn(rohInErpRohIn);
         var mesRohInDatas =
-            GetMesRohInDatas(rohIn.ErpRohinDatas);
+            GetMesRohInDatas(rohIn.ErpRohinDatas, mesRohIn);
 
         //1    | 鏈疄鐜�     | 鎶涘嚭寮傚父
         //2    | 瀹℃牳       | 璋冪敤 SaveOrUpdateData锛屾甯告彃鍏�/鏇存柊
@@ -65,14 +69,19 @@
     }
 
     // 鎻掑叆鎴栨洿鏂版暟鎹殑鏂规硶
-    private bool SaveOrUpdateData(SqlSugarScope db, MesRohIn mesRohIn,
-        List<MesRohInData> mesRohInDatas, string type)
+    private bool SaveOrUpdateData(SqlSugarScope db, MesRohIn mesRohIn,List<MesRohInData> mesRohInDatas, string type)
     {
+        //浼犱粈涔堬紝c灏辨敼鎴愪粈涔�
+        //if (type == "3" || (mesRohIn.DocumentStatus != null && mesRohIn.DocumentStatus != "C"))//C琛ㄧず宸插鏍哥姸鎬�
+        //{
+        //    mesRohIn.BillNo = mesRohIn.BillNo + "F" + mesRohIn.EbelnK3id.ToString();
+        //}
 
-        if (type == "3" || (mesRohIn.DocumentStatus != null && mesRohIn.DocumentStatus != "C"))
-        {
-            mesRohIn.BillNo = mesRohIn.BillNo + "F" + mesRohIn.EbelnK3id.ToString();
-        }
+        ////浼犱粈涔堬紝c灏辨敼鎴愪粈涔�
+        //if (type == "3" || (mesRohIn.DocumentStatus != null && mesRohIn.DocumentStatus != "Y"))//Y琛ㄧず宸插鏍哥姸鎬�
+        //{
+        //    mesRohIn.BillNo = mesRohIn.BillNo + "F" + mesRohIn.EbelnK3id.ToString();
+        //}
 
         if (mesRohIn.Guid != null)
             db.Deleteable<MesRohIn>().Where(s => s.Guid == mesRohIn.Guid)
@@ -104,6 +113,23 @@
     // 灏� ErpRohIn 瀵硅薄杞崲涓� MesRohIn 瀵硅薄鐨勬柟娉�
     private MesRohIn GetMesRohIn(ErpRohIn rohIn)
     {
+        //鏍规嵁鍗曞彿+鍗曞埆锛岃幏鍙栧搴旂殑id
+        var singleId = Db.Queryable<MesRohIn>()
+            .Where(x => x.BillNo == rohIn.FBillNo && x.DocumentType == rohIn.FBillTypeID)
+            .Select(x => x.EbelnK3id)
+            .First();
+
+        //濡傛灉娌℃湁鍒欑敓鎴愪竴涓柊鐨刬d
+        if (singleId == null || string.IsNullOrWhiteSpace(singleId.ToString()))
+        {
+            rohIn.id = GenerateNewId().ToString();
+            //rohIn.id = GenerateNewId().ToString("0");
+        }
+        else//濡傛灉鏈夊垯浣跨敤宸叉湁鐨刬d
+        {
+            rohIn.id = singleId.ToString();
+        }
+
         var eid = long.Parse(rohIn.id);
         var mesRohIn = new MesRohIn();
 
@@ -113,18 +139,70 @@
 
         mesRohIn.EbelnK3id = eid;
         mesRohIn.BillNo = rohIn.FBillNo;
-        mesRohIn.DocumentStatus = rohIn.FDocumentStatus;
+        
         mesRohIn.DocumentType = rohIn.FBillTypeID;
         mesRohIn.BusinessType = rohIn.FBusinessType;
+
+
+        ////erp浼犺繃鏉ョ殑Y->C,琛ㄧず瀹℃牳銆侼->A銆傦紙涓庨噾铦堕�昏緫淇濇寔涓�鑷达級
+        //mesRohIn.DocumentStatus = rohIn.FDocumentStatus;
+
+        //erp浼犺繃鏉ョ殑Y->C,琛ㄧず瀹℃牳銆侼->A銆傦紙涓庨噾铦堕�昏緫淇濇寔涓�鑷达級
+        if (string.IsNullOrEmpty(rohIn.FDocumentStatus))
+        {
+            mesRohIn.DocumentStatus = "A";
+        }
+        else
+        {
+            mesRohIn.DocumentStatus = rohIn.FDocumentStatus == "Y" ? "C" : "A";
+        }
+
+
 
         if (rohIn.FDate != null)
             mesRohIn.PurchaseDate = DateTime.ParseExact(rohIn.FDate,
                 "yyyy-MM-dd HH:mm:ss", null);
 
-        mesRohIn.Supplier = rohIn.FSupplierId;
+
+
+        //渚涘簲鍟嗙紪鐮佽浆ID
+        var mesRohInSupplier = Db.Queryable<MesSupplier>()
+            .Where(x => x.SuppNo == rohIn.FSupplierId)
+            .Select(x => x.Id.ToString())
+            .First();
+        if (!string.IsNullOrWhiteSpace(mesRohInSupplier))
+        {
+            mesRohIn.Supplier = mesRohInSupplier;
+        }
+        else if (!string.IsNullOrWhiteSpace(rohIn.FSupplierId))
+        {
+            mesRohIn.Supplier = rohIn.FSupplierId;
+        }
+        else
+        {
+            mesRohIn.Supplier = "0";
+        }
+        //mesRohIn.Supplier = rohIn.FSupplierId;
+
         mesRohIn.CloseStatus = rohIn.FCloseStatus;
-        mesRohIn.PurchaseOrg = rohIn.FPurchaseOrgId;
-        mesRohIn.PurchaseDept = rohIn.FPurchaseDeptId;
+        //mesRohIn.PurchaseOrg = rohIn.FPurchaseOrgId;
+        mesRohIn.PurchaseOrg = string.IsNullOrEmpty(rohIn.FPurchaseOrgId) ? "1" : rohIn.FPurchaseOrgId;//閲囪喘缁勭粐
+
+        //閲囪喘閮ㄩ棬缂栫爜杞琁D
+        var mesRohInPurchaseDept = Db.Queryable<SysDepartment>()
+            .Where(x => x.Departmentcode == rohIn.FPurchaseDeptId)
+            .Select(x => x.Departmentid.ToString())
+            .First();
+        if (!string.IsNullOrWhiteSpace(mesRohInPurchaseDept))
+        {
+            mesRohIn.PurchaseDept = mesRohInPurchaseDept;
+        }
+        else
+        {
+            mesRohIn.PurchaseDept = "0";
+        }
+        //mesRohIn.PurchaseDept = rohIn.FPurchaseDeptId;
+
         mesRohIn.PurchaseGroup = rohIn.FPurchaserGroupId;
         mesRohIn.Purchaser = rohIn.FPurchaserId;
         mesRohIn.SettlementParty = rohIn.FSettleId;
@@ -185,15 +263,14 @@
     }
 
     // 灏� ErpRohinData 瀵硅薄杞崲涓� MesRohInData 瀵硅薄鐨勬柟娉�
-    private List<MesRohInData> GetMesRohInDatas(
-        List<ErpRohinData> erpRohinDatas)
+    private List<MesRohInData> GetMesRohInDatas(List<ErpRohinData> erpRohinDatas, MesRohIn mesRohIn)
     {
         return erpRohinDatas.Select(s =>
         {
             var entity = new MesRohInData
             {
                 EbelnK3id = Convert.ToDecimal(s.id),
-                ErpId = Convert.ToDecimal(s.Eid),
+                ErpId = Convert.ToDecimal(mesRohIn.EbelnK3id),//浣跨敤涓昏〃鐨凟belnK3id浣滀负瀛愯〃鐨凟rpId
                 BillNo = s.FBillNo,
                 ItemId = s.FMaterialId,
                 PurchaseUnit = s.FUnitId,
@@ -253,7 +330,7 @@
                 ChangeFlag = s.FChangeFlag,
                 DemandSource = s.FDEMANDTYPE,
                 DemandDocumentId = s.FDEMANDBILLNO,
-                DemandDocumentLineId = s.FDEMANDBILLENTRYSEQ,
+                OrderLineId = s.FDEMANDBILLENTRYSEQ,
                 DemandOrg = s.FRequireOrgId,
                 ReceivingOrg = s.FReceiveOrgId,
                 SettlementOrg = s.FEntrySettleOrgId,
@@ -268,6 +345,63 @@
                 SalesOrderId = s.F_UNW_Text_xsddh
             };
 
+            //搴撳瓨鍗曚綅缂栫爜杞琁D
+            //InventoryUnit = s.FStockUnitID,
+            var entityInventoryUnit = Db.Queryable<MesUnit>()
+                .Where(x => x.Fnumber == s.FUnitId)
+                .Select(x => x.Id.ToString())
+                .First();
+            if (!string.IsNullOrWhiteSpace(entityInventoryUnit))
+            {
+                entity.InventoryUnit = entityInventoryUnit;
+            }
+            else
+            {
+                entity.InventoryUnit = "0";
+            }
+
+            //閲囪喘鍗曚綅缂栫爜杞琁D
+            var entityPurchaseUnit = Db.Queryable<MesUnit>()
+                .Where(x => x.Fnumber == s.FUnitId)
+                .Select(x => x.Id.ToString())
+                .First();
+            if (!string.IsNullOrWhiteSpace(entityPurchaseUnit))
+            {
+                entity.PurchaseUnit = entityPurchaseUnit;
+            }
+            else
+            {
+                entity.PurchaseUnit = "0";
+            }
+
+            //璁′环鍗曚綅缂栫爜杞琁D
+            var entityPricingUnit = Db.Queryable<MesUnit>()
+                .Where(x => x.Fnumber == s.FPriceUnitId)
+                .Select(x => x.Id.ToString())
+                .First();
+            if (!string.IsNullOrWhiteSpace(entityPricingUnit))
+            {
+                entity.PricingUnit = entityPricingUnit;
+            }
+            else
+            {
+                entity.PricingUnit = "0";
+            }
+
+            //鐗╂枡缂栫爜杞琁D
+            var entityItemId = Db.Queryable<MesItems>()
+                .Where(x => x.ItemNo == s.FMaterialId)
+                .Select(x => x.Id.ToString())
+                .First();
+            if (!string.IsNullOrWhiteSpace(entityItemId))
+            {
+                entity.ItemId = entityItemId;
+            }
+            else
+            {
+                entity.ItemId = "0";
+            }
+
             if (s.FFreezeDate != null)
                 if (!s.FFreezerId.IsNullOrEmpty())
                     entity.FreezeTime =
@@ -280,12 +414,118 @@
                         DateTime.ParseExact(s.FTerminateDate,
                             "yyyy-MM-dd HH:mm:ss", null);
 
-            var single = rohInDataManager.GetSingle(it =>
-                it.EbelnK3id == entity.EbelnK3id);
+            //鏍规嵁鍗曞彿+鍗曞埆+琛屽彿锛岃幏鍙栧搴旂殑id
+            var singleId = Db.Queryable<MesRohInData>()
+            .Where(x => x.BillNo == s.FBillNo && x.SourceDocumentType == s.FBillTypeID && x.OrderLineId == s.FDEMANDBILLENTRYSEQ)
+            .Select(x => x.EbelnK3id)
+            .First();
+
+            if (singleId == null)//濡傛灉娌℃湁鍒欑敓鎴愪竴涓柊鐨刬d
+            {
+                //entity.EbelnK3id = GenerateNewId2().ToString();
+                entity.EbelnK3id = GenerateNewId2();
+            }
+            else//濡傛灉鏈夊垯浣跨敤宸叉湁鐨刬d
+            {
+                //entity.EbelnK3id = singleId.EbelnK3id;
+                entity.EbelnK3id = singleId;
+            }
+
+            //鏌ヨEbelnK3id瀵瑰簲鐨凣uid锛岃祴鍊肩粰entity.Guid銆傚疄鐜颁富閿鐢�
+            var single = rohInDataManager.GetSingle(it => it.EbelnK3id == entity.EbelnK3id);
             if (single != null) entity.Guid = single.Guid;
 
             return entity;
         }).ToList();
+    }
+
+    ///// <summary>
+    ///// 鐢熸垚鏂扮殑涓昏〃ID锛岀‘淇濅笉閲嶅
+    ///// </summary>
+    //private decimal GenerateNewId()
+    //{
+    //    // 澶勭悊绌鸿〃鐨勬儏鍐碉紝浠�1寮�濮�
+    //    var maxId = Db.Queryable<MesRohIn>().Max(x => (decimal?)x.EbelnK3id) ?? 0;
+    //    var newId = maxId + 1;
+
+    //    // 鍙岄噸妫�鏌ワ紝纭繚鐢熸垚鐨処D涓嶅瓨鍦�
+    //    while (Db.Queryable<MesRohIn>().Where(x => x.EbelnK3id == newId).Any())
+    //    {
+    //        newId++;
+    //    }
+
+    //    return newId;
+    //}
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑涓昏〃ID锛岄�氳繃鏁版嵁搴撳簭鍒楄幏鍙栧敮涓�ID
+    /// </summary>
+    private decimal GenerateNewId()
+    {
+        try
+        {
+            // 鏇挎崲涓猴細
+            var sequenceValueObj = Db.Ado.GetScalar("SELECT NEXT VALUE FOR MES_ROH_IN_seq");
+            var sequenceValue = Convert.ToDecimal(sequenceValueObj);
+            // 楠岃瘉搴忓垪鍊兼槸鍚︽湁鏁�
+            if (sequenceValue <= 0)
+            {
+                throw new InvalidOperationException($"鏁版嵁搴撳簭鍒� MES_ROH_IN_seq 杩斿洖浜嗘棤鏁堢殑鍊�: {sequenceValue}");
+            }
+
+            return sequenceValue;
+        }
+        catch (Exception ex)
+        {
+            // 璁板綍寮傚父淇℃伅
+            Console.WriteLine($"璋冪敤鏁版嵁搴撳簭鍒� MES_ROH_IN_seq 澶辫触: {ex.Message}");
+
+            // 鍚戜笂灞傛姏鍑烘槑纭殑寮傚父淇℃伅
+            throw new InvalidOperationException($"鐢熸垚瀛愯〃ID澶辫触锛屾棤娉曡幏鍙栨暟鎹簱搴忓垪鍊�: {ex.Message}", ex);
+        }
+    }
+
+    /// <summary>
+    /// 鐢熸垚鏂扮殑瀛愯〃ID锛岄�氳繃鏁版嵁搴撳簭鍒楄幏鍙栧敮涓�ID
+    /// </summary>
+    private decimal GenerateNewId2()
+    {
+        try
+        {
+            // 鑾峰彇褰撳墠琛ㄤ腑宸插瓨鍦ㄧ殑鏈�澶� EBELN_K3ID
+            //var maxId = Db.Queryable<MesRohInData>().Max(x => (decimal?)x.EbelnK3id) ?? 0m;
+
+            // 鍏堝彇涓�涓簭鍒楀��
+            var sequenceValueObj = Db.Ado.GetScalar("SELECT NEXT VALUE FOR MES_ROH_IN_DATA_seq");
+            var sequenceValue = Convert.ToDecimal(sequenceValueObj);
+
+            if (sequenceValue <= 0)
+            {
+                throw new InvalidOperationException($"鏁版嵁搴撳簭鍒� MES_ROH_IN_DATA_seq 杩斿洖浜嗘棤鏁堢殑鍊�: {sequenceValue}");
+            }
+
+            //// 濡傛灉搴忓垪鍊艰惤鍚庝簬褰撳墠鏈�澶D锛屾寔缁幏鍙栫洿鍒拌秴杩� maxId
+            //while (sequenceValue <= maxId || Db.Queryable<MesRohInData>().Where(x => x.EbelnK3id == sequenceValue).Any())
+            //{
+            //    sequenceValueObj = Db.Ado.GetScalar("SELECT NEXT VALUE FOR MES_ROH_IN_DATA_seq");
+            //    sequenceValue = Convert.ToDecimal(sequenceValueObj);
+
+            //    if (sequenceValue <= 0)
+            //    {
+            //        throw new InvalidOperationException($"鏁版嵁搴撳簭鍒� MES_ROH_IN_DATA_seq 杩炵画杩斿洖鏃犳晥鐨勫��: {sequenceValue}");
+            //    }
+            //}
+
+            return sequenceValue;
+        }
+        catch (Exception ex)
+        {
+            // 璁板綍寮傚父淇℃伅
+            Console.WriteLine($"璋冪敤鏁版嵁搴撳簭鍒� MES_ROH_IN_DATA_seq 澶辫触: {ex.Message}");
+
+            // 鍚戜笂灞傛姏鍑烘槑纭殑寮傚父淇℃伅
+            throw new InvalidOperationException($"鐢熸垚瀛愯〃ID澶辫触锛屾棤娉曡幏鍙栨暟鎹簱搴忓垪鍊�: {ex.Message}", ex);
+        }
     }
 
     /// <summary>
@@ -295,6 +535,8 @@
     /// <returns>琚垹闄ょ殑鍗曞彿</returns>
     public (int outSum, string outMsg) Delete(string FBillNo, string FBillTypeID)
     {
+        try
+        { 
         var outMsg = string.Empty;
         var outSum = 0;
 
@@ -309,8 +551,36 @@
         Db.Ado.UseStoredProcedure().ExecuteCommand("ERP_DeleteMesRohInByBillNo", parameters);
 
         outMsg = parameters[2].Value?.ToString() ?? "";
-        outSum = parameters[3].Value != null ? Convert.ToInt32(parameters[2].Value) : -1;
-
-        return (outSum, outMsg);
+        outSum = parameters[3].Value != null ? Convert.ToInt32(parameters[index: 3].Value) : -1;
+            return (outSum, outMsg);
+        }
+        catch (Exception ex)
+        {
+            throw new InvalidOperationException($"璋冪敤瀛樺偍杩囩▼ ERP_DeleteMesRohInByBillNo 澶辫触: {ex.Message}", ex);
+        }
     }
+
+
+    // 鏁村崟鍒犻櫎
+    //private bool Delete(SqlSugarScope db, MesRohIn mesRohIn,List<MesRohInData> mesRohInDatas)
+    //{
+    //    if (mesRohIn.Guid != null)
+    //        db.Deleteable<MesRohIn>().Where(s => s.Guid == mesRohIn.Guid)
+    //            .ExecuteCommand();
+
+    //    if (mesRohInDatas.Count > 0)
+    //        db.Deleteable<MesRohInData>()
+    //            .Where(s => s.ErpId == mesRohIn.EbelnK3id).ExecuteCommand();
+
+    //    var orUpdate = db.Insertable(mesRohIn)
+    //        .IgnoreColumns(true).ExecuteCommand() > 0;
+
+
+    //    var baOrUpdate = db.Insertable(mesRohInDatas).PageSize(1)
+    //        .IgnoreColumnsNull()
+    //        .ExecuteCommand() > 0;
+
+    //    if (orUpdate && baOrUpdate) return true;
+    //    throw new NotImplementedException("鍒犻櫎");
+    //}
 }
\ No newline at end of file

--
Gitblit v1.9.3