啊鑫
2025-09-01 3b3e5ca0fc0addf20cb5f7308b7636562746fe4c
MES.Service/service/BasicData/MesDepotsManager.cs
@@ -89,13 +89,13 @@
    // 将 ErpDepots 对象转换为 MesDepots 对象的方法
    private MesDepots GetMesDepots(ErpDepots depots)
    {
        return new MesDepots
        var entity = new MesDepots
        {
            DepotCode = depots.FNumber,
            DepotName = depots.FName,
            DepotId = string.IsNullOrEmpty(depots.Id)
                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                : long.Parse(depots.Id),
                : Convert.ToInt32(depots.Id),
            IsFkc = depots.FAllowMinusQty,
            CreateBy = depots.FPrincipal,
            Depottype = depots.FStockProperty,
@@ -103,65 +103,42 @@
            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>()
            .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);
    }
    // 批量更新仓库状态的方法