快乐的昕的电脑
10 天以前 bb6f9d3a6c01c9cff72d14c2fdc679408eea6db3
MES.Service/service/BasicData/MesRohInManager.cs
@@ -8,7 +8,9 @@
using System.Security.AccessControl;
namespace MES.Service.service.BasicData;
/// <summary>
/// 采购订单
/// </summary>
public class MesRohInManager : Repository<MesRohIn>
{
    private readonly MesRohInDataManager rohInDataManager = new();
@@ -17,9 +19,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,13 +68,18 @@
    }
    // 插入或更新数据的方法
    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"))
        //传什么,c就改成什么
        if (type == "3" || (mesRohIn.DocumentStatus != null && mesRohIn.DocumentStatus != "Y"))//Y表示已审核状态
        {
            mesRohIn.BillNo = mesRohIn.BillNo + "F" + mesRohIn.EbelnK3id.ToString();
            mesRohIn.BillNo = mesRohIn.BillNo + "Y" + mesRohIn.EbelnK3id.ToString();
        }
        if (mesRohIn.Guid != null)
@@ -104,6 +112,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();
        //如果没有则生成一个新的id
        if (singleId == null || string.IsNullOrWhiteSpace(singleId.ToString()))
        {
            rohIn.id = GenerateNewId().ToString();
            //rohIn.id = GenerateNewId().ToString("0");
        }
        else//如果有则使用已有的id
        {
            rohIn.id = singleId.ToString();
        }
        var eid = long.Parse(rohIn.id);
        var mesRohIn = new MesRohIn();
@@ -121,10 +146,48 @@
            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 if (!string.IsNullOrWhiteSpace(rohIn.FPurchaseDeptId))
        {
            mesRohIn.PurchaseDept = rohIn.FPurchaseDeptId;
        }
        else
        {
            mesRohIn.PurchaseDept = "0";
        }
        //mesRohIn.PurchaseDept = rohIn.FPurchaseDeptId;
        mesRohIn.PurchaseGroup = rohIn.FPurchaserGroupId;
        mesRohIn.Purchaser = rohIn.FPurchaserId;
        mesRohIn.SettlementParty = rohIn.FSettleId;
@@ -185,15 +248,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),//使用主表的EbelnK3id作为子表的ErpId
                BillNo = s.FBillNo,
                ItemId = s.FMaterialId,
                PurchaseUnit = s.FUnitId,
@@ -253,7 +315,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 +330,60 @@
                SalesOrderId = s.F_UNW_Text_xsddh
            };
            //采购单位编码转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 if (!string.IsNullOrWhiteSpace(s.FUnitId))
            {
                entity.PurchaseUnit = s.FUnitId;
            }
            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 if (!string.IsNullOrWhiteSpace(s.FPriceUnitId))
            {
                entity.PricingUnit = s.FPriceUnitId;
            }
            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 if (!string.IsNullOrWhiteSpace(s.FMaterialId))
            {
                entity.ItemId = s.FMaterialId;
            }
            else
            {
                entity.ItemId = "0";
            }
            if (s.FFreezeDate != null)
                if (!s.FFreezerId.IsNullOrEmpty())
                    entity.FreezeTime =
@@ -280,12 +396,75 @@
                        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)//如果没有则生成一个新的id
            {
                //entity.EbelnK3id = GenerateNewId2().ToString();
                entity.EbelnK3id = GenerateNewId2();
            }
            else//如果有则使用已有的id
            {
                //entity.EbelnK3id = singleId.EbelnK3id;
                entity.EbelnK3id = singleId;
            }
            //查询EbelnK3id对应的Guid,赋值给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;
        // 双重检查,确保生成的ID不存在
        while (Db.Queryable<MesRohIn>().Where(x => x.EbelnK3id == newId).Any())
        {
            newId++;
        }
        return newId;
    }
    /// <summary>
    /// 生成新的子表ID,通过数据库序列获取唯一ID
    /// </summary>
    private decimal GenerateNewId2()
    {
        try
        {
            // 替换为:
            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}");
            }
            return sequenceValue;
        }
        catch (Exception ex)
        {
            // 记录异常信息
            Console.WriteLine($"调用数据库序列 MES_ROH_IN_DATA_seq 失败: {ex.Message}");
            // 向上层抛出明确的异常信息
            throw new InvalidOperationException($"生成子表ID失败,无法获取数据库序列值: {ex.Message}", ex);
        }
    }
    /// <summary>
@@ -313,4 +492,28 @@
        return (outSum, outMsg);
    }
    // 整单删除
    //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("删除");
    //}
}