| | |
| | | using SqlSugar; |
| | | |
| | | namespace MES.Service.service.BasicData; |
| | | |
| | | /// <summary> |
| | | /// 部门信息 |
| | | /// </summary> |
| | | public class SysDepartmentManager : Repository<SysDepartment> |
| | | { |
| | | // 当前类已经继承了 Repository 增、删、查、改的方法 |
| | |
| | | { |
| | | var result = db.Updateable<SysDepartment>() |
| | | .SetColumns(s => s.Depextr4 == status) |
| | | .Where(s => s.Id == departmentId).ExecuteCommand(); |
| | | .Where(s => s.Id == departmentId && s.TypeByTable==0).ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | return true; |
| | |
| | | private bool DeleteDepartment(SqlSugarScope db, decimal departmentId) |
| | | { |
| | | var deleteById = db.Deleteable<SysDepartment>() |
| | | .Where(s => s.Id == departmentId).ExecuteCommand(); |
| | | .Where(s => s.Id == departmentId && s.TypeByTable==0).ExecuteCommand(); |
| | | if (deleteById > 0) |
| | | return true; |
| | | |
| | | throw new NotImplementedException("删除失败"); |
| | | } |
| | | |
| | | // 插入或更新部门的方法 |
| | | private bool InsertOrUpdateDepartment(SqlSugarScope db, |
| | | SysDepartment entity) |
| | | /// <summary> |
| | | /// 同步物料信息new_0/4 |
| | | /// </summary> |
| | | /// <param name="db"></param> |
| | | /// <param name="entity"></param> |
| | | /// <returns></returns> |
| | | private bool InsertOrUpdateDepartment(SqlSugarScope db, SysDepartment entity) |
| | | { |
| | | db.Deleteable<SysDepartment>() |
| | | .Where(s => s.Id == entity.Id).ExecuteCommand(); |
| | | if (entity.Id == 0) |
| | | { |
| | | // 新增情况:生成新ID并插入 |
| | | var newId = GenerateNewId(); |
| | | entity.Id = newId; |
| | | entity.Departmentid = newId; |
| | | return db.Insertable(entity).ExecuteCommand() > 0; |
| | | } |
| | | else |
| | | { |
| | | // 更新情况:删除后重新插入,保持原有ID |
| | | var originalId = entity.Id; |
| | | |
| | | var insert = db.Insertable(entity).ExecuteCommand(); |
| | | return insert > 0; |
| | | // 先删除原记录(如果存在) |
| | | db.Deleteable<SysDepartment>().Where(s => s.Id == originalId).ExecuteCommand(); |
| | | |
| | | // 重新插入,保持原有ID |
| | | entity.Id = originalId; |
| | | entity.Departmentid = originalId; |
| | | return db.Insertable(entity).ExecuteCommand() > 0; |
| | | } |
| | | } |
| | | |
| | | // 插入或更新部门的方法 |
| | | //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; |
| | | //} |
| | | |
| | | // 将 ErpDepartment 对象转换为 SysDepartment 对象的方法 |
| | | private SysDepartment GetSysDepartment(ErpDepartment department) |
| | | { |
| | | return new SysDepartment |
| | | // 查找是否已存在相同编码的记录。 |
| | | var existingCustomer = Db.Queryable<SysDepartment>() |
| | | .Where(s => s.Departmentcode == department.FNumber && s.TypeByTable==0) |
| | | .First(); |
| | | |
| | | var entity = new SysDepartment |
| | | { |
| | | Departmentcode = department.FNumber, |
| | | TypeByTable=0,//用于区分部门表与工作中心信息(0/1) |
| | | // 如果存在,使用现有的ID,后续将删除后重新插入 |
| | | // 如果不存在,设为0,InsertOrUpdate方法将生成新ID |
| | | Id = existingCustomer?.Id ?? 0, |
| | | Departmentid = existingCustomer?.Departmentid ?? 0, |
| | | //Id = Convert.ToDecimal(department.Id), |
| | | //Departmentid = Convert.ToDecimal(department.Id), |
| | | 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, |
| | | FParentID = department.FParentID, |
| | | FSubsidiary = department.FUseOrgId, |
| | | Fumbrella = department.FCreateOrgId, |
| | | CreateDate = DateTime.Now, |
| | | //FSubsidiary = department.FUseOrgId, |
| | | //Fumbrella = department.FCreateOrgId, |
| | | // 如果存在,使用现有的CreateDate,后续将删除后重新插入 |
| | | // 如果不存在,设为当前时间 |
| | | CreateDate = existingCustomer?.CreateDate ?? DateTime.Now, |
| | | //CreateDate = DateTime.Now, |
| | | LastupdateDate = DateTime.Now, |
| | | Company = "1000", |
| | | Factory = "1000" |
| | | |
| | | Remark4 = department.remark4, |
| | | Remark5 = department.remark5, |
| | | |
| | | |
| | | //不存在(为空或 null),则使用 existingCustomer 的原值,否则用新值。 |
| | | WorkshopCenterCode = string.IsNullOrEmpty(department.FNumber_WORKSHOP) |
| | | ? existingCustomer?.WorkshopCenterCode |
| | | : department.FNumber_WORKSHOP, |
| | | WorkshopCenterName = string.IsNullOrEmpty(department.FName_WORKSHOP) |
| | | ? existingCustomer?.WorkshopCenterName |
| | | : department.FName_WORKSHOP, |
| | | FactoryName = string.IsNullOrEmpty(department.factory_name) |
| | | ? existingCustomer?.FactoryName |
| | | : department.factory_name, |
| | | Factory = string.IsNullOrEmpty(department.Factory) |
| | | ? existingCustomer?.Factory |
| | | : department.Factory, |
| | | |
| | | |
| | | FSubsidiary = string.IsNullOrEmpty(department.FUseOrgId) ? "1" : department.FUseOrgId, |
| | | Fumbrella = string.IsNullOrEmpty(department.FCreateOrgId) ? "1" : department.FCreateOrgId, |
| | | }; |
| | | |
| | | // ERP: 0=未禁用, 1=禁用 |
| | | // MES: A=未禁用, B=禁用 |
| | | //传入0、1或者A、B时都会转为A、B,其他情况为A |
| | | if (string.IsNullOrEmpty(department.FForbidStatus)) |
| | | { |
| | | entity.Depextr4 = "A"; |
| | | } |
| | | else |
| | | { |
| | | // 回传B或1时写入B,其它写A |
| | | entity.Depextr4 = (department.FForbidStatus == "1" || department.FForbidStatus == "B") |
| | | ? "B" |
| | | : "A"; |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /// <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; |
| | | } |
| | | |
| | | // SaveList 方法用于保存多个部门记录,根据类型批量执行不同的操作 |