Ejemplo n.º 1
0
        public ActionResult GetTree(int?id)
        {
            if (CurrentUser.Get().IsCompanyManager&& !_userPermissionGroupService.IsUserHasActivePermission(CurrentUser.Get().Id))
            {
                return(null);
            }

            var ctvm             = new PermissionTreeViewModel();
            var role_buildingIds = GetUserBuildings(_buildingRepository, _userRepository, CurrentUser.Get().Id);

            if (CurrentUser.Get().IsCompanyManager || CurrentUser.Get().IsCommonUser)
            {
                UserPermissionGroup upg;
                if (id.HasValue)
                {
                    upg = _userPermissionGroupRepository.FindById(id.Value);
                }
                else
                {
                    if (_userPermissionGroupRepository.FindAll(x => !x.IsDeleted && x.IsOriginal && x.UserId == CurrentUser.Get().Id).ToList().Count() == 0)
                    {
                        return(null);
                    }
                    upg = _userPermissionGroupRepository.FindAll(x => !x.IsDeleted && x.IsOriginal && x.UserId == CurrentUser.Get().Id).FirstOrDefault();
                    id  = upg.Id;
                }

                ctvm.IsOriginal = upg.IsOriginal;

                List <int> companyBuildingObjectsIds = (from cbo in upg.UserPermissionGroupTimeZones.Where(x => !x.IsDeleted) select cbo.BuildingObjectId).ToList();
                var        companyBuildingObjects    = _buildingObjectRepository.FindAll(x => !x.IsDeleted && role_buildingIds.Contains(x.BuildingId) &&
                                                                                         companyBuildingObjectsIds.Contains(x.Id));

                List <int> floorsId            = (from cbo in companyBuildingObjects.Where(x => x.ParentObjectId.HasValue) select cbo.ParentObjectId.Value).ToList();
                var        companyFloorObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && floorsId.Contains(x.Id));

                ctvm.Buildings =
                    (from b in
                     (from cbo in companyBuildingObjects select cbo.Building)
                     where !b.IsDeleted
                     select
                     new Node
                {
                    ParentId = b.LocationId,
                    MyId = b.Id,
                    Name = b.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Towns =
                    (from t in
                     (from cbo in companyBuildingObjects select cbo.Building.Location)
                     where !t.IsDeleted
                     select
                     new Node
                {
                    ParentId = t.CountryId,
                    MyId = t.Id,
                    Name = t.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Countries =
                    (from c in
                     (from cbo in companyBuildingObjects select cbo.Building.Location.Country).OrderBy(cc => cc.Name)
                     where !c.IsDeleted
                     select
                     new Node
                {
                    ParentId = 0,
                    MyId = c.Id,
                    Name = c.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Floors =
                    (from f in companyFloorObjects
                     where f.TypeId == (int)BuildingObjectTypes.Floor
                     select
                     new Node
                {
                    ParentId = f.BuildingId,
                    MyId = f.Id,
                    Name = f.Description,
                    Comment = f.Comment
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Objects =
                    (from o in companyBuildingObjects
                     where o.ParentObjectId.HasValue && (o.TypeId == (int)BuildingObjectTypes.Door || o.TypeId == (int)BuildingObjectTypes.Lift || o.TypeId == (int)BuildingObjectTypes.Room)
                     orderby o.TypeId descending
                     select
                     new Node
                {
                    ParentId = o.ParentObjectId.Value,
                    MyId = o.Id,
                    Name = o.Description,
                    Comment = o.Comment,
                    IsDefaultTimeZone = _userPermissionGroupService.IsDefaultUserTimeZone(o.Id, id.Value),
                    IsRoom = o.TypeId == (int)BuildingObjectTypes.Room ? 1 : 0,
                    StatusIcon = o.StatusIconId.HasValue ? String.Format("../../img/status/{0}.ico", o.StatusIconId) : String.Empty
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.ActiveObjectIds = _userPermissionGroupService.GetUserBuildingObjectIds(id.Value);
                foreach (var obn in ctvm.Objects)
                {
                    if (obn.IsRoom == 1)
                    {
                        // illi 08.05.2018 UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindAll(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                        int idd = id.GetValueOrDefault();
                        UserPermissionGroupTimeZone upgtz = _userPermissionGroupTimeZoneRepository.FindByPGId(idd).Where(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();

                        obn.IsArming           = upgtz.IsArming;
                        obn.IsDefaultArming    = upgtz.IsDefaultArming;
                        obn.IsDisarming        = upgtz.IsDisarming;
                        obn.IsDefaultDisarming = upgtz.IsDefaultDisarming;
                    }
                }

                ctvm.Towns     = ctvm.Towns.Where(town => ctvm.Buildings.Any(b => b.ParentId == town.MyId));
                ctvm.Countries = ctvm.Countries.Where(country => ctvm.Towns.Any(town => town.ParentId == country.MyId));
            }
            else
            if (!(CurrentUser.Get().IsCompanyManager || CurrentUser.Get().IsCommonUser))
            {
                IEnumerable <Building> userBuildings;

                if (CurrentUser.Get().IsSuperAdmin)
                {
                    userBuildings = _buildingRepository.FindAll(x => !x.IsDeleted).ToList();
                }
                else
                if (CurrentUser.Get().IsBuildingAdmin)
                {
                    userBuildings = _buildingRepository.FindAll(x => role_buildingIds.Contains(x.Id));
                }
                else
                {
                    return(null);
                }

                ctvm.Buildings =
                    (from b in userBuildings
                     select
                     new Node
                {
                    ParentId = b.LocationId,
                    MyId = b.Id,
                    Name = b.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Towns =
                    (from t in
                     (from ub in userBuildings select ub.Location)
                     where !t.IsDeleted
                     select
                     new Node
                {
                    ParentId = t.CountryId,
                    MyId = t.Id,
                    Name = t.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Countries =
                    (from c in
                     (from cbo in userBuildings select cbo.Location.Country).OrderBy(cc => cc.Name)
                     where !c.IsDeleted
                     select
                     new Node
                {
                    ParentId = 0,
                    MyId = c.Id,
                    Name = c.Name
                }).Distinct(new NodeEqualityComparer()).ToList();

                List <int> userBuildingIds     = (from ub in userBuildings select ub.Id).ToList();
                var        userBuildingObjects = _buildingObjectRepository.FindAll(x => !x.IsDeleted && userBuildingIds.Contains(x.BuildingId)).ToList();

                ctvm.Floors =
                    (from f in userBuildingObjects
                     where f.TypeId == (int)BuildingObjectTypes.Floor
                     select
                     new Node
                {
                    ParentId = f.BuildingId,
                    MyId = f.Id,
                    Name = f.Description,
                    Comment = f.Comment
                }).Distinct(new NodeEqualityComparer()).ToList();

                ctvm.Objects =
                    (from o in userBuildingObjects
                     where o.ParentObjectId.HasValue && (o.TypeId == (int)BuildingObjectTypes.Door || o.TypeId == (int)BuildingObjectTypes.Lift || o.TypeId == (int)BuildingObjectTypes.Room)
                     orderby o.TypeId descending
                     select
                     new Node
                {
                    ParentId = o.ParentObjectId.Value,
                    MyId = o.Id,
                    Name = o.ObjectNr.HasValue ? "#" + o.ObjectNr + " " + o.Description : o.Description,
                    Comment = o.Comment,
                    IsDefaultTimeZone = id.HasValue ? _userPermissionGroupService.IsDefaultUserTimeZone(o.Id, id.Value) : true,
                    IsRoom = o.TypeId == (int)BuildingObjectTypes.Room ? 1 : 0,
                    StatusIcon = o.StatusIconId.HasValue ? String.Format("../../img/status/{0}.ico", o.StatusIconId) : String.Empty
                }).Distinct(new NodeEqualityComparer()).ToList();

                if (id.HasValue)
                {
                    ctvm.ActiveObjectIds = _userPermissionGroupService.GetUserBuildingObjectIds(id.Value);

                    foreach (var obn in ctvm.Objects)
                    {
                        if (obn.IsRoom == 1)
                        {
                            // illi 08.05.2018  UserPermissionGroupTimeZone pgtz = _userPermissionGroupTimeZoneRepository.FindAll(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();
                            int idd = id.GetValueOrDefault();
                            UserPermissionGroupTimeZone pgtz = _userPermissionGroupTimeZoneRepository.FindByPGId(idd).Where(x => !x.IsDeleted && x.BuildingObjectId == obn.MyId && x.UserPermissionGroupId == id).FirstOrDefault();

                            obn.IsArming           = pgtz == null ? false : pgtz.IsArming;
                            obn.IsDefaultArming    = pgtz == null ? false : pgtz.IsDefaultArming;
                            obn.IsDisarming        = pgtz == null ? false : pgtz.IsDisarming;
                            obn.IsDefaultDisarming = pgtz == null ? false : pgtz.IsDefaultDisarming;
                        }
                    }
                }

                ctvm.Towns     = ctvm.Towns.Where(town => ctvm.Buildings.Any(b => b.ParentId == town.MyId));
                ctvm.Countries = ctvm.Countries.Where(country => ctvm.Towns.Any(town => town.ParentId == country.MyId));
            }
            return(PartialView("Tree", ctvm));
        }