/// <summary> /// Получить дочерние ящики подразделения. /// </summary> /// <param name="status">Статус ящика подразделения.</param> /// <returns>Список дочерних ящиков подразделений.</returns> public List <IDepartmentBox> GetChildBoxes(Sungero.Core.Enumeration status) { var resultBoxes = new List <IDepartmentBox>(); var boxes = DepartmentBoxes.GetAll(b => b.Status == status).ToList(); var childBoxes = boxes.Where(b => Equals(b.ParentBox, _obj)).ToList(); while (childBoxes.Any()) { resultBoxes.AddRange(childBoxes); var newBoxes = boxes.Where(b => childBoxes.Any(x => Equals(BoxBases.As(x), b.ParentBox))).ToList(); childBoxes = newBoxes; } return(resultBoxes); }
/// <summary> /// Создать ящик подразделения. /// </summary> /// <param name="box">Абонентский ящик нашей организации.</param> public static void CreateDepartmentBoxes(IBusinessUnitBox box) { var createdDepartments = DepartmentBoxes.GetAll(d => Equals(d.RootBox, box)).ToList(); var newDepartmentBoxes = new List <IDepartmentBox>(); var changedDepartmentBoxes = new List <IDepartmentBox>(); var deletedDepartmentBoxes = new List <IDepartmentBox>(); var client = Functions.BusinessUnitBox.GetClient(box); // Обходим кэширование клиента DCX для получения актуальной оргструктуры. var allDepartments = client.GetOrganizationStructure(client.OurSubscriber.CounteragentId); var serviceHead = allDepartments.SingleOrDefault(d => d.IsHead); var departments = allDepartments.Where(d => !d.IsHead); // Создание / обновление абонентских ящиков подразделений. foreach (var department in departments) { var departmentBox = createdDepartments.SingleOrDefault(d => Equals(d.ServiceId, department.Id)) ?? DepartmentBoxes.Create(); if (!Equals(departmentBox.ServiceId, department.Id)) { departmentBox.ServiceId = department.Id; } if (!Equals(departmentBox.RootBox, box)) { departmentBox.RootBox = box; } if (!Equals(departmentBox.ServiceName, department.Name)) { if (!string.IsNullOrEmpty(departmentBox.Name)) { changedDepartmentBoxes.Add(departmentBox); } departmentBox.ServiceName = department.Name; } if (!departmentBox.AccessRights.IsGranted(DefaultAccessRightsTypes.Change, box.Responsible)) { departmentBox.AccessRights.Grant(box.Responsible, DefaultAccessRightsTypes.Change); } var exchangeRole = Sungero.ExchangeCore.PublicFunctions.Module.GetExchangeServiceUsersRole(); if (!exchangeRole.AccessRights.IsGranted(DefaultAccessRightsTypes.Change, box.Responsible)) { exchangeRole.AccessRights.Grant(box.Responsible, DefaultAccessRightsTypes.Change); exchangeRole.Save(); } if (!createdDepartments.Contains(departmentBox)) { createdDepartments.Add(departmentBox); newDepartmentBoxes.Add(departmentBox); } } // Проверка иерархии. foreach (var department in departments) { var departmentBox = createdDepartments.Single(d => Equals(d.ServiceId, department.Id)); var parentbox = createdDepartments.SingleOrDefault(d => Equals(d.ServiceId, department.ParentDepartmentId)); if (department.ParentDepartmentId == null || department.ParentDepartmentId == Guid.Empty.ToString() || (serviceHead != null && department.ParentDepartmentId == serviceHead.Id)) { if (!Equals(departmentBox.ParentBox, box)) { departmentBox.ParentBox = box; } } else if (!Equals(departmentBox.ParentBox, parentbox)) { departmentBox.ParentBox = parentbox; } } // Закрытие удаленных ящиков. foreach (var department in createdDepartments) { var exists = departments.Any(d => Equals(d.Id, department.ServiceId)); if (!exists && (department.IsDeleted == false || department.Status != Status.Closed)) { department.IsDeleted = true; department.Status = Status.Closed; deletedDepartmentBoxes.Add(department); } } // Сохранение одной сущности может потянуть за собой остальные, так что парамсы обновляем до первого сохранения. foreach (var department in createdDepartments) { ((Domain.Shared.IExtendedEntity)department).Params[Constants.BoxBase.DisableSaveValidation] = true; } foreach (var department in createdDepartments.Where(x => x.State.IsChanged)) { department.Save(); } if (newDepartmentBoxes.Any() || changedDepartmentBoxes.Any() || deletedDepartmentBoxes.Any()) { CreateDepartmentBoxNotice(box, newDepartmentBoxes, changedDepartmentBoxes, deletedDepartmentBoxes); } }