啊鑫
5 天以前 ab01d75a3fbc8467d81a895cba5d6dde76fbe053
MES.Service/service/BasicData/MesDepotsManager.cs
@@ -1,4 +1,5 @@
using MES.Service.DB;
using Masuit.Tools;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
@@ -76,14 +77,46 @@
        throw new NotImplementedException("反审核失败");
    }
    /// <summary>
    /// 生成新的DepotId,确保不重复
    /// </summary>
    private decimal GenerateNewId()
    {
        // 处理空表的情况,从1开始
        var maxId = Db.Queryable<MesDepots>().Max(x => (decimal?)x.DepotId) ?? 0;
        var newId = maxId + 1;
        // 双重检查,确保生成的DepotId不存在
        while (Db.Queryable<MesDepots>().Where(x => x.DepotId == newId).Any())
        {
            newId++;
        }
        return newId;
    }
    // 插入或更新仓库的方法
    private bool InsertOrUpdate(SqlSugarScope db, MesDepots entity)
    {
        db.Deleteable<MesDepots>()
            .Where(s => s.DepotId == entity.DepotId)
            .ExecuteCommand();
        var insert = db.Insertable(entity).ExecuteCommand();
        return insert > 0;
        if (entity.DepotId == 0)
        {
            // 新增情况:生成新DepotId并插入
            var newId = GenerateNewId();
            entity.DepotId = newId;
            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
        }
        else
        {
            // 更新情况:删除后重新插入,保持原有DepotId
            var originalId = entity.DepotId;
            // 先删除原记录(如果存在)
            db.Deleteable<MesDepots>().Where(s => s.DepotId == originalId).ExecuteCommand();
            // 重新插入,保持原有DepotId
            entity.DepotId = originalId;
            return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0;
        }
    }
    // 将 ErpDepots 对象转换为 MesDepots 对象的方法
@@ -93,35 +126,54 @@
        {
            DepotCode = depots.FNumber,
            DepotName = depots.FName,
            // DepotId = string.IsNullOrEmpty(depots.Id)
            //     ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
            //     : Convert.ToInt32(depots.Id),
            IsFkc = depots.FAllowMinusQty,
            CreateBy = depots.FPrincipal,
            Depottype = depots.FStockProperty,
            IsNg = depots.FForbidStatus,
            Zuid = depots.FGroup,
            DocumentStatus = depots.FDocumentStatus,
            
            FSubsidiary = string.IsNullOrEmpty(depots.FUseOrgId)
                ? "1"
                :  depots.FUseOrgId,
            Fumbrella = string.IsNullOrEmpty(depots.FCreateOrgId)
                ? "1"
                : depots.FCreateOrgId,
            IsNg = "A",
            FSubsidiary = "1",
            Fumbrella = "1",
            // IsNg = depots.FForbidStatus,
            // FSubsidiary = string.IsNullOrEmpty(depots.FUseOrgId)
            //     ? "1"
            //     :  depots.FUseOrgId,
            // Fumbrella = string.IsNullOrEmpty(depots.FCreateOrgId)
            //     ? "1"
            //     : depots.FCreateOrgId,
            CreateDate = DateTime.Now,
            LastupdateDate = DateTime.Now,
            Company = "1000",
            Factory = "1000"
        };
        //IsNg = depots.FForbidStatus,
        if (depots.FForbidStatus.IsNullOrEmpty())
        {
            entity.IsNg = "A";
        }
        else
        {
            //我期望的值是A=否,B=是
            //实际给我的值是0或1,我希望为我转换从A和B的方式
            entity.IsNg = depots.FForbidStatus == "1" ? "B" : "A";
        }
        var mesDepots = Db.Queryable<MesDepots>()
        // 查找是否已存在相同仓库编码的记录
        var existingDepot = Db.Queryable<MesDepots>()
            .Where(s => s.DepotCode == entity.DepotCode)
            .First();
        if (mesDepots != null)
        if (existingDepot != null)
        {
            entity.DepotId = mesDepots.DepotId;
            // 如果存在,使用现有的DepotId,后续将删除后重新插入
            entity.DepotId = existingDepot.DepotId;
        }
        else
        {
            // 如果不存在,设为0,InsertOrUpdate方法将生成新DepotId
            entity.DepotId = 0;
        }
        return entity;