11
啊鑫
3 天以前 9d296c888a8ac49f606c0a3ebd843e617cfc0a40
MES.Service/service/BasicData/MesDepotsManager.cs
@@ -76,14 +76,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,9 +125,6 @@
        {
            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,
@@ -115,13 +144,20 @@
            Factory = "1000"
        };
        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;