kyy
6 天以前 f476ec010c22cd4e3c6a119eea035cbf4594bfbb
MES.Service/service/BasicData/MesDepotsManager.cs
@@ -42,7 +42,7 @@
    }
    // 更新仓库状态的方法
    private bool UpdateDepotStatus(SqlSugarScope db, long depotId,
    private bool UpdateDepotStatus(SqlSugarScope db, decimal depotId,
        string status)
    {
        var result = db.Updateable<MesDepots>()
@@ -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()
                : long.Parse(depots.Id),
            IsFkc = depots.FAllowMinusQty,
            CreateBy = depots.FPrincipal,
            Depottype = depots.FStockProperty,
@@ -103,25 +132,32 @@
            Zuid = depots.FGroup,
            DocumentStatus = depots.FDocumentStatus,
            
            UseOrg = string.IsNullOrEmpty(depots.FUseOrgId)
                ? 1
                : long.Parse(depots.FUseOrgId),
            CreateOrg = string.IsNullOrEmpty(depots.FCreateOrgId)
                ? 1
                : long.Parse(depots.FCreateOrgId),
            CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            LastupdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            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"
        };
        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;