/// <summary>
 /// Gets the corporate physician roles.
 /// </summary>
 /// <param name="corporateId">The corporate identifier.</param>
 /// <returns></returns>
 public List <UserRole> GetCorporatePhysicianRoles(int corporateId)
 {
     using (var rolebal = new RoleBal())
     {
         using (var userRoleBal = new UserRoleBal())
         {
             var roleIdlist          = rolebal.GetPhysicianRolesByCorporateId(corporateId);
             var physicianTypeUserId =
                 userRoleBal.GetUserIdByCorporateAndRoleTypeId(corporateId, roleIdlist).ToList();
             return(physicianTypeUserId);
         }
     }
 }
        /// <summary>
        /// Gets the facility role list custom.
        /// </summary>
        /// <param name="corporateId">The corporate identifier.</param>
        /// <param name="facilityId">The facility identifier.</param>
        /// <param name="roleId">The role identifier.</param>
        /// <returns></returns>
        public List <FacilityRoleCustomModel> GetFacilityRoleListCustom(int corporateId, int facilityId, int roleId)
        {
            var list = new List <FacilityRoleCustomModel>();

            using (var rep = UnitOfWork.FacilityRoleRepository)
            {
                var facilityRoleList = corporateId > 0 ?
                                       (facilityId == 0
                    ? rep.Where(fr => !fr.IsDeleted && fr.IsActive && fr.CorporateId == corporateId).ToList()
                    : rep.Where(fr => !fr.IsDeleted && fr.IsActive && fr.CorporateId == corporateId &&
                                fr.FacilityId == facilityId).ToList())
                                      :
                                       rep.Where(fr => !fr.IsDeleted && fr.IsActive).ToList();

                if (roleId > 0)
                {
                    facilityRoleList = facilityRoleList.Where(r => r.RoleId == roleId).ToList();
                }


                using (var facBal = new FacilityBal())
                {
                    using (var corpBal = new CorporateBal())
                    {
                        using (var roleBal = new RoleBal())
                        {
                            list.AddRange(facilityRoleList.Select(item => new FacilityRoleCustomModel
                            {
                                FacilityRoleId = item.FacilityRoleId,
                                FacilityId     = item.FacilityId,
                                RoleId         = item.RoleId,
                                CorporateId    = item.CorporateId,
                                CreatedBy      = item.CreatedBy,
                                CreatedDate    = item.CreatedDate,
                                ModifiedBy     = item.ModifiedBy,
                                ModifiedDate   = item.ModifiedDate,
                                IsDeleted      = item.IsDeleted,
                                DeletedBy      = item.DeletedBy,
                                DeletedDate    = item.DeletedDate,
                                IsActive       = item.IsActive,
                                FacilityName   = facBal.GetFacilityNameById(item.FacilityId),
                                CorporateName  = corpBal.GetCorporateNameById(item.CorporateId),
                                RoleName       = roleBal.GetRoleNameById(item.RoleId)
                            }));
                        }
                    }
                }
                list = list.GroupBy(x => new { x.RoleName, x.FacilityId }).Select(x => x.FirstOrDefault()).ToList();
                return(list.OrderBy(x => x.RoleName).ToList());
            }
        }
        public List <FacilityRoleCustomModel> GetUserTypeRoleDropDownInTaskPlan(int corporateId, int facilityId, bool carePlanAccessible)
        {
            var list = new List <FacilityRoleCustomModel>();

            using (var rep = UnitOfWork.FacilityRoleRepository)
            {
                var facilityRoleList = rep.Where(x => x.IsDeleted != true && x.CorporateId == corporateId && x.FacilityId == facilityId && x.CarePlanAccessible == carePlanAccessible).ToList();
                using (var roleBal = new RoleBal())
                {
                    list.AddRange(facilityRoleList.Select(item => new FacilityRoleCustomModel
                    {
                        RoleId   = item.RoleId,
                        RoleName = roleBal.GetRoleNameById(item.RoleId)
                    }));
                }
                return(list);
            }
        }
        /// <summary>
        /// Adds the update facility role custom model.
        /// </summary>
        /// <param name="vm">The model.</param>
        /// <returns></returns>
        public int AddUpdateFacilityRoleCustomModel(FacilityRoleCustomModel vm)
        {
            var newRoleId = 0;
            var frModel   = FacilityRoleMapper.MapViewModelToModel(vm);

            using (var rep = UnitOfWork.FacilityRoleRepository)
            {
                if (vm.RoleId == 0 && !string.IsNullOrEmpty(vm.RoleName))
                {
                    var rModel = new Role
                    {
                        RoleID       = vm.RoleId,
                        CorporateId  = vm.CorporateId,
                        FacilityId   = vm.FacilityId, // new column added on Nov 07 2014 by Shashank
                        IsActive     = vm.IsActive,
                        RoleName     = vm.RoleName,
                        CreatedBy    = vm.CreatedBy,
                        CreatedDate  = vm.CreatedDate,
                        ModifiedBy   = vm.ModifiedBy,
                        ModifiedDate = vm.ModifiedDate,
                        IsDeleted    = vm.IsDeleted,
                        DeletedBy    = vm.DeletedBy,
                        DeletedDate  = vm.DeletedDate,
                    };

                    using (var roleBal = new RoleBal())
                    {
                        if (!roleBal.CheckDuplicateRole(vm.RoleId, vm.RoleName))
                        {
                            newRoleId = roleBal.AddUpdateRole(rModel);
                        }
                        else
                        {
                            return(0);
                        }

                        //Add newly added role to other facilities of this corporate
                        if (vm.AddToAll)
                        {
                            var facIds = roleBal.GetFacilityIdsByCorporateId(vm.CorporateId);
                            if (facIds != null && facIds.Any())
                            {
                                facIds = facIds.Where(f => f != vm.FacilityId).ToList();
                                foreach (var facId in facIds)
                                {
                                    //Add entries in the table Role for other facilities too.
                                    var rModel1 = new Role
                                    {
                                        RoleID       = 0,
                                        CorporateId  = vm.CorporateId,
                                        FacilityId   = facId, // new column added on Nov 07 2014 by Shashank
                                        IsActive     = vm.IsActive,
                                        RoleName     = vm.RoleName,
                                        CreatedBy    = vm.CreatedBy,
                                        CreatedDate  = vm.CreatedDate,
                                        ModifiedBy   = vm.ModifiedBy,
                                        ModifiedDate = vm.ModifiedDate,
                                        IsDeleted    = vm.IsDeleted,
                                        DeletedBy    = vm.DeletedBy,
                                        DeletedDate  = vm.DeletedDate,
                                        IsGeneric    = false
                                    };

                                    var nRoleId = roleBal.AddUpdateRole(rModel1);

                                    //Add Entries in the table FacilityRole table for other facilities.
                                    frModel.FacilityId = facId;
                                    frModel.RoleId     = nRoleId;
                                    rep.Create(frModel);
                                }
                            }
                        }
                    }
                }

                frModel.FacilityId = vm.FacilityId;
                frModel.RoleId     = newRoleId > 0 ? newRoleId : vm.RoleId;

                if (vm.FacilityRoleId > 0)
                {
                    rep.UpdateEntity(frModel, frModel.FacilityRoleId);
                }
                else
                {
                    rep.Create(frModel);
                }

                return(1);
            }
        }