| | |
| | | throw new NotImplementedException("删除失败"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成新的ID,确保不重复 |
| | | /// </summary> |
| | | private decimal GenerateNewId() |
| | | { |
| | | // 处理空表的情况,从1开始 |
| | | var maxId = Db.Queryable<SysDepartment>().Max(x => (decimal?)x.Id) ?? 0; |
| | | var newId = maxId + 1; |
| | | |
| | | // 双重检查,确保生成的ID不存在 |
| | | while (Db.Queryable<SysDepartment>().Where(x => x.Id == newId).Any()) |
| | | { |
| | | newId++; |
| | | } |
| | | |
| | | return newId; |
| | | } |
| | | |
| | | // 插入或更新部门的方法 |
| | | private bool InsertOrUpdateDepartment(SqlSugarScope db, |
| | | SysDepartment entity) |
| | | { |
| | | db.Deleteable<SysDepartment>() |
| | | .Where(s => s.Id == entity.Id).ExecuteCommand(); |
| | | |
| | | var insert = db.Insertable(entity).ExecuteCommand(); |
| | | return insert > 0; |
| | | if (entity.Id == 0) |
| | | { |
| | | // 新增情况:生成新ID并插入 |
| | | var newId = GenerateNewId(); |
| | | entity.Id = newId; |
| | | return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; |
| | | } |
| | | else |
| | | { |
| | | // 更新情况:删除后重新插入,保持原有ID |
| | | var originalId = entity.Id; |
| | | |
| | | // 先删除原记录(如果存在) |
| | | db.Deleteable<SysDepartment>().Where(s => s.Id == originalId).ExecuteCommand(); |
| | | |
| | | // 重新插入,保持原有ID |
| | | entity.Id = originalId; |
| | | return db.Insertable(entity).IgnoreColumns(ignoreNullColumn:true).ExecuteCommand() > 0; |
| | | } |
| | | } |
| | | |
| | | // 将 ErpDepartment 对象转换为 SysDepartment 对象的方法 |
| | | private SysDepartment GetSysDepartment(ErpDepartment department) |
| | | { |
| | | return new SysDepartment |
| | | var fForbidStatus = department.FForbidStatus; |
| | | if (department.FForbidStatus == "0") |
| | | { |
| | | fForbidStatus = "A"; |
| | | } |
| | | else if (department.FForbidStatus == "1") |
| | | { |
| | | fForbidStatus = "B"; |
| | | } |
| | | |
| | | var entity = new SysDepartment |
| | | { |
| | | Departmentcode = department.FNumber, |
| | | Departmentname = department.FName, |
| | | Departmentid = Convert.ToDecimal(department.Id), |
| | | Id = Convert.ToDecimal(department.Id), |
| | | Depextr1 = department.FDeptProperty, |
| | | Depextr2 = department.FGroup, |
| | | Depextr3 = department.FWIPStockID, |
| | | Depextr4 = department.FForbidStatus, |
| | | Depextr4 = fForbidStatus, |
| | | FParentID = department.FParentID, |
| | | FSubsidiary = department.FUseOrgId, |
| | | Fumbrella = department.FCreateOrgId, |
| | | FSubsidiary = string.IsNullOrEmpty(department.FUseOrgId) |
| | | ? "1" |
| | | : department.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(department.FCreateOrgId) |
| | | ? "1" |
| | | : department.FCreateOrgId, |
| | | CreateDate = DateTime.Now, |
| | | LastupdateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000", |
| | | FDocumentStatus = department.FDocumentStatus |
| | | Factory = "1000" |
| | | }; |
| | | |
| | | // 查找是否已存在相同部门编码的记录 |
| | | var existingDepartment = Db.Queryable<SysDepartment>() |
| | | .Where(s => s.Departmentcode == entity.Departmentcode) |
| | | .First(); |
| | | |
| | | if (existingDepartment != null) |
| | | { |
| | | // 如果存在,使用现有的ID,后续将删除后重新插入 |
| | | entity.Id = existingDepartment.Id; |
| | | } |
| | | else |
| | | { |
| | | // 如果不存在,设为0,InsertOrUpdate方法将生成新ID |
| | | entity.Id = 0; |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | // SaveList 方法用于保存多个部门记录,根据类型批量执行不同的操作 |
| | | public bool SaveList(List<ErpDepartment> departments) |
| | | { |
| | | var list = new List<SysDepartment>(); |
| | | departments.ForEach(s => |
| | | if (departments == null || !departments.Any()) |
| | | { |
| | | var entity = |
| | | GetSysDepartment(s); // 将 ErpDepartment 转换为 SysDepartment |
| | | 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 departmentGroup in groupBy) |
| | | switch (departmentGroup.Key) |
| | | { |
| | | case "0": |
| | | if (!UpdateDepartmentStatusBatch(db, |
| | | departmentGroup.Value, "A")) // 批量启用部门 |
| | | throw new NotImplementedException("启用失败"); |
| | | break; |
| | | case "1": |
| | | if (!UpdateDepartmentStatusBatch(db, |
| | | departmentGroup.Value, "B")) // 批量禁用部门 |
| | | throw new NotImplementedException("禁用失败"); |
| | | break; |
| | | case "3": |
| | | if (!DeleteDepartmentBatch(db, |
| | | departmentGroup.Value)) // 批量删除部门 |
| | | throw new NotImplementedException("删除失败"); |
| | | break; |
| | | case "2": |
| | | case "4": |
| | | if (!InsertOrUpdateDepartmentBatch(db, |
| | | departmentGroup.Value)) // 批量插入或更新部门 |
| | | throw new NotImplementedException("同步失败"); |
| | | break; |
| | | default: |
| | | throw new ArgumentNullException( |
| | | $"type没有{departmentGroup.Key}这个类型的参数"); |
| | | } |
| | | |
| | | return 1; |
| | | }) > 0; |
| | | // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑) |
| | | var result = departments.Select(Save).ToList(); |
| | | return result.All(b => b); |
| | | } |
| | | |
| | | // 批量更新部门状态的方法 |
| | |
| | | private bool InsertOrUpdateDepartmentBatch(SqlSugarScope db, |
| | | List<SysDepartment> departmentList) |
| | | { |
| | | return departmentList.All( |
| | | entity => InsertOrUpdateDepartment(db, entity)); |
| | | return departmentList.All(entity => |
| | | InsertOrUpdateDepartment(db, entity)); |
| | | } |
| | | } |