| | |
| | | using MES.Service.DB; |
| | | using Masuit.Tools; |
| | | using MES.Service.DB; |
| | | using MES.Service.Dto.webApi; |
| | | using MES.Service.Modes; |
| | | using SqlSugar; |
| | |
| | | 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; |
| | | entity.Departmentid = 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) |
| | | { |
| | | var unixTimeSeconds = string.IsNullOrEmpty(department.Id) |
| | | ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() |
| | | : Convert.ToDecimal(department.Id); |
| | | |
| | | 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 = unixTimeSeconds, |
| | | Id = unixTimeSeconds, |
| | | Depextr1 = department.FDeptProperty, |
| | | Depextr2 = department.FGroup, |
| | | Depextr3 = department.FWIPStockID, |
| | | Depextr4 = fForbidStatus, |
| | | FParentID = department.FParentID, |
| | | FSubsidiary = string.IsNullOrEmpty(department.FUseOrgId) |
| | | ? "1" |
| | | : department.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(department.FCreateOrgId) |
| | | ? "1" |
| | | : department.FCreateOrgId, |
| | | FSubsidiary = "1", |
| | | Fumbrella = "1", |
| | | CreateDate = DateTime.Now, |
| | | LastupdateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000" |
| | | }; |
| | | |
| | | var sysDepartment = Db.Queryable<SysDepartment>() |
| | | if (department.FForbidStatus.IsNullOrEmpty()) |
| | | { |
| | | entity.Depextr4 = "A"; |
| | | } |
| | | else |
| | | { |
| | | //我期望的值是A=否,B=是 |
| | | //实际给我的值是0或1,我希望为我转换从A和B的方式 |
| | | entity.Depextr4 = department.FForbidStatus == "1" ? "B" : "A"; |
| | | } |
| | | |
| | | // 查找是否已存在相同部门编码的记录 |
| | | var existingDepartment = Db.Queryable<SysDepartment>() |
| | | .Where(s => s.Departmentcode == entity.Departmentcode) |
| | | .First(); |
| | | |
| | | if (sysDepartment != null) |
| | | if (existingDepartment != null) |
| | | { |
| | | entity.Id = sysDepartment.Id; |
| | | // 如果存在,使用现有的ID,后续将删除后重新插入 |
| | | entity.Id = existingDepartment.Id; |
| | | entity.Departmentid = existingDepartment.Id; |
| | | } |
| | | else |
| | | { |
| | | // 如果不存在,设为0,InsertOrUpdate方法将生成新ID |
| | | entity.Id = 0; |
| | | entity.Departmentid = 0; |
| | | } |
| | | |
| | | return entity; |