快乐的昕的电脑
4 天以前 8684d3db5566311dcf7631b5da52ca3bc2228423
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,70 @@
        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;
@@ -269,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,
@@ -283,6 +344,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,7 +432,7 @@
            }
            //查询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;
@@ -346,14 +464,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 +507,8 @@
    /// <returns>被删除的单号</returns>
    public (int outSum, string outMsg) Delete(string FBillNo, string FBillTypeID)
    {
        try
        {
        var outMsg = string.Empty;
        var outSum = 0;
@@ -388,9 +523,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);
        }
    }