kyy
7 天以前 f476ec010c22cd4e3c6a119eea035cbf4594bfbb
MES.Service/service/BasicData/MesItemTypeManager.cs
@@ -15,9 +15,10 @@
        {
            switch (customer.Type)
            {
                case "0":
                case "2":
                case "4":
                    return InsertItemType(entity);
                case "1":
                case "3":
                    return DeleteItemType(entity.Id);
                default:
                    throw new ArgumentNullException(
@@ -30,27 +31,56 @@
        }
    }
    private bool InsertItemType(MesItemType entity)
    /// <summary>
    /// 生成新的ID,确保不重复
    /// </summary>
    private decimal GenerateNewId()
    {
        // 先根据ID删除现有记录
        try
        // 处理空表的情况,从1开始
        var maxId = Db.Queryable<MesItemType>().Max(x => (decimal?)x.Id) ?? 0;
        var newId = maxId + 1;
        // 双重检查,确保生成的ID不存在
        while (Db.Queryable<MesItemType>().Where(x => x.Id == newId).Any())
        {
            Db.Deleteable<MesItemType>()
                .Where(it => it.Id == entity.Id).ExecuteCommand();
            newId++;
        }
        catch (Exception)
        {
            // 删除失败可能是因为记录不存在,继续执行插入操作
        }
        var insert = base.Insert(entity);
        if (insert)
            return true;
        throw new NotImplementedException("插入失败");
        return newId;
    }
    private bool DeleteItemType(long? id)
    private bool InsertItemType(MesItemType entity)
    {
        if (entity.Id == 0)
        {
            // 新增情况:生成新ID并插入
            var newId = GenerateNewId();
            entity.Id = newId;
            return base.Insert(entity);
        }
        else
        {
            // 更新情况:删除后重新插入,保持原有ID
            var originalId = entity.Id;
            // 先删除原记录(如果存在)
            try
            {
                Db.Deleteable<MesItemType>()
                    .Where(it => it.Id == originalId).ExecuteCommand();
            }
            catch (Exception)
            {
                // 删除失败可能是因为记录不存在,继续执行插入操作
            }
            // 重新插入,保持原有ID
            entity.Id = originalId;
            return base.Insert(entity);
        }
    }
    private bool DeleteItemType(decimal? id)
    {
        var deleteById = Db.Deleteable<MesItemType>()
            .Where(it => it.Id == id).ExecuteCommand();
@@ -62,11 +92,8 @@
    private MesItemType GetMesItemType(ErpItemType department)
    {
        return new MesItemType
        var entity = new MesItemType
        {
            Id = string.IsNullOrEmpty(department.Id)
                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                : long.Parse(department.Id),
            Pgroup = department.FParentId,
            Tcode = department.FNumber,
            Tname = department.FName,
@@ -76,47 +103,37 @@
            Company = "1000",
            Factory = "1000"
        };
        // 查找是否已存在相同类型编码的记录
        var existingItemType = Db.Queryable<MesItemType>()
            .Where(it => it.Tcode == entity.Tcode)
            .First();
        if (existingItemType != null)
        {
            // 如果存在,使用现有的ID,后续将删除后重新插入
            entity.Id = existingItemType.Id;
        }
        else
        {
            // 如果不存在,设为0,InsertOrUpdate方法将生成新ID
            entity.Id = 0;
        }
        return entity;
    }
    public bool SaveList(List<ErpItemType> departments)
    {
        var list = new List<MesItemType>();
        departments.ForEach(s =>
        if (departments == null || !departments.Any())
        {
            var entity = GetMesItemType(s);
            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());
        var result = new List<bool>();
        foreach (var itemTypeGroup in groupBy)
            try
            {
                switch (itemTypeGroup.Key)
                {
                    case "0":
                        result.Add(InsertItemTypeBatch(itemTypeGroup.Value));
                        break;
                    case "1":
                        result.Add(DeleteItemTypeBatch(itemTypeGroup.Value));
                        break;
                    default:
                        throw new ArgumentNullException(
                            $"type没有{itemTypeGroup.Key}这个类型的参数");
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException($"批量操作失败: {ex.Message}", ex);
            }
        if (result.All(b => b))
            return true;
        throw new NotImplementedException("接口执行失败");
        // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑)
        var result = departments.Select(Save).ToList();
        return result.All(b => b);
    }
    private bool InsertItemTypeBatch(List<MesItemType> itemTypeList)