/// <summary> /// 添加组织机构信息信息 /// </summary> /// <param name="dtos">要添加的组织机构信息DTO信息</param> /// <returns>业务操作结果</returns> public OperationResult AddOrganizations(params OrganizationDto[] dtos) { dtos.CheckNotNull("dtos"); List <Organization> organizations = new List <Organization>(); OperationResult result = OrganizationRepository.Insert(dtos, dto => { if (OrganizationRepository.CheckExists(m => m.Name == dto.Name)) { throw new Exception("名称为“{0}”的组织机构已存在,不能重复添加。".FormatWith(dto.Name)); } }, (dto, entity) => { if (dto.ParentId.HasValue && dto.ParentId.Value > 0) { Organization parent = OrganizationRepository.GetByKey(dto.ParentId.Value); if (parent == null) { throw new Exception("指定父组织机构不存在。"); } entity.Parent = parent; } organizations.Add(entity); return(entity); }); if (result.ResultType == OperationResultType.Success) { int[] ids = organizations.Select(m => m.Id).ToArray(); RefreshOrganizationsTreePath(ids); } return(result); }
/// <summary> /// 保存组织机构信息 /// </summary> /// <param name="dtos">待保存的组织机构Dto信息</param> /// <returns>业务操作结果</returns> public async Task <OperationResult> SaveOrganizations(params SysOrganizationDto[] dtos) { dtos.CheckNotNull("dtos"); var addDtos = dtos.Where(p => p.Id == 0).ToArray(); var updateDtos = dtos.Where(p => p.Id > 0).ToArray(); List <SysOrganization> saveOrganizations = new List <SysOrganization>(); try { //dto检查委托 Action <SysOrganizationDto> checkAction = dto => { if (OrganizationRepository.CheckExists(m => m.Name == dto.Name, dto.Id)) { throw new Exception("名称为“{0}”的组织机构已存在,不能重复添加。".FormatWith(dto.Name)); } }; //dto更新委托 Func <SysOrganizationDto, SysOrganization, SysOrganization> updateFunc = (dto, entity) => { if (!dto.ParentId.HasValue || dto.ParentId == 0) { entity.Parent = null; } else if (entity.Parent == null || entity.Parent.Id != dto.ParentId) { var parentId = dto.ParentId.Value; SysOrganization parent = OrganizationRepository.GetByKey(parentId); if (parent != null) { entity.Parent = parent; } } saveOrganizations.Add(entity); return(entity); }; OrganizationRepository.UnitOfWork.TransactionEnabled = true; if (addDtos.Length > 0) { OrganizationRepository.Insert(addDtos, checkAction, updateFunc); } if (updateDtos.Length > 0) { OrganizationRepository.Update(updateDtos, checkAction, updateFunc); } await RoleRepository.UnitOfWork.SaveChangesAsync(); int[] ids = saveOrganizations.Select(m => m.Id).ToArray(); //RefreshSysOrganizationsTreePath(ids); return(new OperationResult(OperationResultType.Success, "保存成功")); } catch (Exception e) { return(new OperationResult(OperationResultType.Error, e.Message)); } }
/// <summary> /// 更新组织机构信息信息 /// </summary> /// <param name="inputDtos">包含更新信息的组织机构信息DTO信息</param> /// <returns>业务操作结果</returns> public OperationResult EditOrganizations(params OrganizationInputDto[] inputDtos) { inputDtos.CheckNotNull("dtos"); List <Organization> organizations = new List <Organization>(); OperationResult result = OrganizationRepository.Update(inputDtos, (dto, entity) => { if (OrganizationRepository.CheckExists(m => m.Name == dto.Name, dto.Id)) { throw new Exception("名称为“{0}”的组织机构已存在,不能重复添加。".FormatWith(dto.Name)); } }, (dto, entity) => { if (!dto.ParentId.HasValue || dto.ParentId == 0) { entity.Parent = null; } else if (entity.Parent != null && entity.Parent.Id != dto.ParentId) { Organization parent = OrganizationRepository.GetByKey(dto.Id); if (parent == null) { throw new Exception("指定父组织机构不存在。"); } entity.Parent = parent; } organizations.Add(entity); return(entity); }); if (result.ResultType == OperationResultType.Success) { int[] ids = organizations.Select(m => m.Id).ToArray(); RefreshOrganizationsTreePath(ids); } return(result); }
/// <summary> /// 检查组织机构信息信息是否存在 /// </summary> /// <param name="predicate">检查谓语表达式</param> /// <param name="id">更新的组织机构信息编号</param> /// <returns>组织机构信息是否存在</returns> public bool CheckOrganizationExists(Expression <Func <Organization, bool> > predicate, int id = 0) { return(OrganizationRepository.CheckExists(predicate, id)); }