快乐的昕的电脑
4 小时以前 8328aed1bca2a30c3105b19f41d324acb59437c9
MES.Service/service/BasicData/MesRohInManager.cs
@@ -1,4 +1,5 @@
using Masuit.Tools;
using 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();
@@ -68,11 +71,17 @@
    // 插入或更新数据的方法
    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)
@@ -130,18 +139,67 @@
        mesRohIn.EbelnK3id = eid;
        mesRohIn.BillNo = rohIn.FBillNo;
        mesRohIn.DocumentStatus = rohIn.FDocumentStatus;
        mesRohIn.DocumentType = rohIn.FBillTypeID;
        mesRohIn.BusinessType = rohIn.FBusinessType;
        ////erp传过来的Y->C,表示审核。N->A。(与金蝶逻辑保持一致)
        //mesRohIn.DocumentStatus = rohIn.FDocumentStatus;
        //erp传过来的Y->C,表示审核。N->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;//采购组织
        //采购部门编码转ID
        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;
@@ -198,6 +256,12 @@
        mesRohIn.FixtureMoldProcurement = rohIn.F_UNW_Combo_zjmj;//治具丶模具加工及采购
        mesRohIn.urgent_material = rohIn.FUrgent_Material;//急料
        //mesRohIn.DocumentType = rohIn.FBillTypeID;
        mesRohIn.BusinessType = rohIn.FBusinessType;//业务类型
        mesRohIn.DocumentType = rohIn.FBusinessType;//业务类型
        mesRohIn.MBillNo = $"{rohIn.FBillNo}-{rohIn.FBillTypeID}";//组合单号:组合单号:单号-单别
        mesRohIn.NumberType= rohIn.FBillTypeID;//单据类型:330:采购单、331:采购补货单、332:手工采购单
        return mesRohIn;
    }
@@ -269,10 +333,16 @@
                ChangeFlag = s.FChangeFlag,
                DemandSource = s.FDEMANDTYPE,
                DemandDocumentId = s.FDEMANDBILLNO,
                DemandDocumentLineId = s.FDEMANDBILLENTRYSEQ,
                DemandOrg = s.FRequireOrgId,
                ReceivingOrg = s.FReceiveOrgId,
                SettlementOrg = s.FEntrySettleOrgId,
                OrderLineId = s.FDEMANDBILLENTRYSEQ,
                //需求组织默认为1
                DemandOrg = string.IsNullOrEmpty(s.FRequireOrgId) ? "1" : s.FRequireOrgId,
                //DemandOrg = s.FRequireOrgId,
                //收料组织默认为1
                ReceivingOrg = string.IsNullOrEmpty(s.FEntrySettleOrgId) ? "1" : s.FEntrySettleOrgId,
                //ReceivingOrg = s.FReceiveOrgId,
                //结算组织默认为1
                SettlementOrg = string.IsNullOrEmpty(s.FEntrySettleOrgId) ? "1" : s.FEntrySettleOrgId,
                //SettlementOrg = s.FEntrySettleOrgId,
                PurchaseOrderLineNumber = s.FSEQ,
                Demand = s.FRequireOrgId,
                Receiving = s.FReceiveOrgId,
@@ -283,6 +353,63 @@
                Remark5 = s.FUrgent_Material, //急料
                SalesOrderId = s.F_UNW_Text_xsddh
            };
            //库存单位编码转ID
            //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";
            }
            //采购单位编码转ID
            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";
            }
            //计价单位编码转ID
            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";
            }
            //物料编码转ID
            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())
@@ -314,29 +441,57 @@
            }
            //查询EbelnK3id对应的Guid,赋值给entity.Guid。实现主键复用
            var single = rohInDataManager.GetSingle(it =>it.EbelnK3id == entity.EbelnK3id);
            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;
    //    // 双重检查,确保生成的ID不存在
    //    while (Db.Queryable<MesRohIn>().Where(x => x.EbelnK3id == newId).Any())
    //    {
    //        newId++;
    //    }
    //    return newId;
    //}
    /// <summary>
    /// 生成新的主表ID,确保不重复
    /// 生成新的主表ID,通过数据库序列获取唯一ID
    /// </summary>
    private decimal GenerateNewId()
    {
        // 处理空表的情况,从1开始
        var maxId = Db.Queryable<MesRohIn>().Max(x => (decimal?)x.EbelnK3id) ?? 0;
        var newId = maxId + 1;
        // 双重检查,确保生成的ID不存在
        while (Db.Queryable<MesRohIn>().Where(x => x.EbelnK3id == newId).Any())
        try
        {
            newId++;
        }
            // 替换为:
            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 newId;
            return sequenceValue;
        }
        catch (Exception ex)
        {
            // 记录异常信息
            Console.WriteLine($"调用数据库序列 MES_ROH_IN_seq 失败: {ex.Message}");
            // 向上层抛出明确的异常信息
            throw new InvalidOperationException($"生成子表ID失败,无法获取数据库序列值: {ex.Message}", ex);
        }
    }
    /// <summary>
@@ -346,14 +501,29 @@
    {
        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}");
            }
            //// 如果序列值落后于当前最大ID,持续获取直到超过 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;
        }
@@ -374,6 +544,8 @@
    /// <returns>被删除的单号</returns>
    public (int outSum, string outMsg) Delete(string FBillNo, string FBillTypeID)
    {
        try
        {
        var outMsg = string.Empty;
        var outSum = 0;
@@ -388,9 +560,13 @@
        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);
        }
    }