啊鑫
2025-08-27 eab13f60bbdc8ea275c6dd7b6424cdfc7769f6e1
MES.Service/service/BasicData/MesDepotsManager.cs
@@ -42,7 +42,7 @@
    }
    // 更新仓库状态的方法
    private bool UpdateDepotStatus(SqlSugarScope db, decimal depotId,
    private bool UpdateDepotStatus(SqlSugarScope db, long depotId,
        string status)
    {
        var result = db.Updateable<MesDepots>()
@@ -89,78 +89,56 @@
    // 将 ErpDepots 对象转换为 MesDepots 对象的方法
    private MesDepots GetMesDepots(ErpDepots depots)
    {
        return new MesDepots
        var entity = new MesDepots
        {
            Guid = depots.Id,
            DepotCode = depots.FNumber,
            DepotName = depots.FName,
            DepotId = Convert.ToDecimal(depots.Id),
            DepotId = string.IsNullOrEmpty(depots.Id)
                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                : long.Parse(depots.Id),
            IsFkc = depots.FAllowMinusQty,
            CreateBy = depots.FPrincipal,
            Depottype = depots.FStockProperty,
            IsNg = depots.FForbidStatus,
            Zuid = depots.FGroup,
            FSubsidiary = depots.FUseOrgId,
            Fumbrella = depots.FCreateOrgId,
            CreateDate = DateTime.Now,
            LastupdateDate = DateTime.Now,
            SupplierId = depots.FSUPPLIERID,
            Company = "1000",
            Factory = "1000",
            FCustomerId = depots.FCustomerId,
            FDocumentStatus = depots.FDocumentStatus,
            FStockStatusType = depots.FStockStatusType
            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"),
            Company = "1000",
            Factory = "1000"
        };
        var mesDepots = Db.Queryable<MesDepots>()
            .Where(s => s.DepotCode == entity.DepotCode)
            .First();
        if (mesDepots != null)
        {
            entity.DepotId = mesDepots.DepotId;
        }
        return entity;
    }
    // SaveList 方法用于保存多个仓库记录,根据类型批量执行不同的操作
    public bool SaveList(List<ErpDepots> erpDepots)
    {
        var list = new List<MesDepots>();
        erpDepots.ForEach(s =>
        if (erpDepots == null || !erpDepots.Any())
        {
            var entity = GetMesDepots(s); // 将 ErpDepots 转换为 MesDepots
            entity.Type = s.Type;
            list.Add(entity);
        });
            Console.WriteLine("警告: 传入的列表为空");
            return false;
        }
        var groupBy = list.GroupBy(s => s.Type)
            .ToDictionary(g => g.Key, g => g.ToList());
        return UseTransaction(db =>
        {
            foreach (var depotsGroup in groupBy)
                switch (depotsGroup.Key)
                {
                    case "0":
                        if (!UpdateDepotStatusBatch(db, depotsGroup.Value,
                                "A")) // 批量启用仓库
                            throw new NotImplementedException("启用失败");
                        break;
                    case "1":
                        if (!UpdateDepotStatusBatch(db, depotsGroup.Value,
                                "B")) // 批量禁用仓库
                            throw new NotImplementedException("禁用失败");
                        break;
                    case "3":
                        if (!DeleteDepotBatch(db,
                                depotsGroup.Value)) // 批量删除仓库
                            throw new NotImplementedException("删除失败");
                        break;
                    case "2":
                    case "4":
                        if (!InsertOrUpdateBatch(db,
                                depotsGroup.Value)) // 批量插入或更新仓库
                            throw new NotImplementedException("同步失败");
                        break;
                    default:
                        throw new ArgumentNullException(
                            $"type没有{depotsGroup.Key}这个类型的参数");
                }
            return 1;
        }) > 0;
        // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑)
        var result = erpDepots.Select(Save).ToList();
        return result.All(b => b);
    }
    // 批量更新仓库状态的方法