public static Festival GetActiveFestival(DateTime dt)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         var q = from m in ctx.Festivals
                 where (m.FromDate <= dt) && (m.ToDate >= dt)
                 select m;
         return q.FirstOrDefault();
     }
 }
        public static List<BankStoryExam_Question> GetExamsQuestion(int? QuestionId)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                var q = from m in ctx.BankStoryExam_Question.Include("BankStoryExam") 
                        where (!QuestionId.HasValue || m.QuestionId == QuestionId.Value)
                        select m;

                return q.ToList();
            }
        }
        public static IEnumerable<SystemRole> GetRolesWithUsers(int? roleId)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.SystemRoles.Include("SystemUsers")
                        where (!roleId.HasValue || m.RoleId == roleId.Value)
                        select m;

                return q.ToList();
            }
        }
        public static List<ScoreTypeCategory> GetScoreTypeCategory(int? ScoreTypeCatId = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.ScoreTypeCategories
                        where (!ScoreTypeCatId.HasValue || m.CategoryId == ScoreTypeCatId.Value)
                        select m;

                return q.ToList();
            }
        }
        public static SystemUser GetValidSystemUser(string SSOUserName)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.SystemUsers.Include("SystemRoles")
                        where (m.SSOUserName == SSOUserName) && (m.Active)
                        select m;

                return q.FirstOrDefault();
            }
        }
        public static List<SystemRole> GetRoles(int? roleId = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.SystemRoles
                        where (!roleId.HasValue || m.RoleId == roleId.Value)
                        select m;

                return q.ToList();
            }
        }
        public static List<QuestionnaireForms_ResponseItems> GetQuestionnaireForms_ResponseItem(long? ItemId = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.QuestionnaireForms_ResponseItems.Include("QuestionnaireForm_Questions")
                        where (!ItemId.HasValue || m.ItemId == ItemId.Value)
                        select m;

                return q.ToList();
            }
        }
        public static List<Config> GetConfig(string ConfigName = null)
        {
            using (var ctx = new BMIKidsEntities(BaseDataProvider.ConnectionString))
            {

                var q = from m in ctx.Configs
                        where (string.IsNullOrEmpty(ConfigName) || m.ConfigName == ConfigName)
                        select m;

                return q.ToList();
            }
        }
 public static List<Festival_Pictures> GetFestivalPics(out int PageCount, Festival Curentfsv, bool? Approved = null, int Currentpage = 1, int PageSize = DefaultPageSize)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         var q = from m in ctx.Festival_Pictures
                 where
                 (m.FestivalId == Curentfsv.FestivalId) &&
                 (!Approved.HasValue || m.IsApproved == Approved.Value)
                 select m;
         PageCount = q.Count();
         return q.OrderBy(x => Guid.NewGuid()).Skip((Currentpage - 1) * PageSize).Take(PageSize).ToList();
     }
 }
        public static List<FAQCategory> GetFAQCategory(int? FAQCatId = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                var q = from m in ctx.FAQCategories
                        where (!FAQCatId.HasValue || m.CategoryId == FAQCatId.Value)
                        orderby m.SortOrderId descending
                        select m;

                return q.ToList();

            }
        }
        public static List<QuestionnaireForm> GetQuestionnaireForm(long? FormId = null, QuestionnaireStatusType? Status = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.QuestionnaireForms.Include("QuestionnaireForm_Questions").Include("QuestionnaireForm_Questions.QuestionnaireForms_ResponseItems")
                        where (!FormId.HasValue || m.FormId == FormId.Value)
                        && (!Status.HasValue || m.Status == (int)Status.Value)
                        select m;

                return q.ToList();
            }
        }
        public static IEnumerable<ScoreType> GetScoresTypes(long? ScoreTypeId = null, long? ScoreTypeCategoryId = null, string ScoreEnName = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.ScoreTypes
                        where (!ScoreTypeId.HasValue || m.Id == ScoreTypeId.Value) &&
                              (!ScoreTypeCategoryId.HasValue || m.CategoryId == ScoreTypeCategoryId.Value) &&
                              (string.IsNullOrEmpty(ScoreEnName) || m.ScoreEnName == ScoreEnName)
                        select m;

                return q.ToList();
            }
        }
        public static List<BankStoryExam> GetExams(int? ExamId = null, DateTime? CurrentdateTime = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                var q = from m in ctx.BankStoryExams.Include("BankStoryExam_Question")
                        where (!ExamId.HasValue || m.ExamId == ExamId.Value) &&
                        (
                                !CurrentdateTime.HasValue ||
                                (m.IsActiveFromDate <= CurrentdateTime.Value && m.IsActiveToDate >= CurrentdateTime.Value)
                        )
                        select m;

                return q.ToList();
            }
        }
        public static IEnumerable<Kids_Scores> GetKids_Scores(long? KidsUserId = null, int? ScoreTypeId = null, DateTime? FromDate = null, DateTime? ToDate = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.Kids_Scores
                        where (!KidsUserId.HasValue || m.KidsUserId == KidsUserId.Value) &&
                        (!ScoreTypeId.HasValue || m.ScoreTypeId == ScoreTypeId.Value) &&
                         (!FromDate.HasValue || m.CreateDateTime >= FromDate.Value) &&
                         (!ToDate.HasValue || m.CreateDateTime <= ToDate.Value)
                        select m;

                return q.ToList();
            }
        }
        public static List<SystemUser> GetSystemUser(string SSOUserName = null, long? UserId = null, string FirstName = null, string LastName = null)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.SystemUsers.Include("SystemRoles")
                        where
                        (string.IsNullOrEmpty(SSOUserName) || m.SSOUserName.Contains(SSOUserName)) &&
                              (!UserId.HasValue || m.UserId == UserId.Value) &&
                              (string.IsNullOrEmpty(FirstName) || m.Name == FirstName) &&
                              (string.IsNullOrEmpty(LastName) || m.Family == LastName)
                        select m;

                return q.ToList();
            }
        }
        public static List<Game> GetGame(out long PageCount, int? GameId = null, string GameName = null,
                                         int Currentpage = 1, int PageSize = DefaultPageSize)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.Games.Include("ScoreTypes")
                        where (!GameId.HasValue || m.GameId == GameId.Value) &&
                        (string.IsNullOrEmpty(GameName) || m.Name.Contains(GameName))
                        orderby m.GameId descending
                        select m;
                PageCount = q.LongCount();
                return q.Skip((Currentpage - 1) * PageSize).Take(PageSize).ToList();


            }
        }
        public static List<Wish> GetWish(out int PageCount, int? WishId = null, string WishName = null,
                                         int Currentpage = 1, int PageSize = DefaultPageSize)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.Wishes
                        where (!WishId.HasValue || m.WishId == WishId.Value) &&
                         (string.IsNullOrEmpty(WishName) || m.WishName == WishName)
                        orderby m.WishId descending
                        select m;
                PageCount = q.Count();
                return q.Skip((Currentpage - 1) * PageSize).Take(PageSize).ToList();


            }
        }
        public static List<PollQuestion> GetPoll(long? QuestionId = null, bool? IsActive = null)
        {

            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.PollQuestions.Include("PollResponseItems")
                        where (!IsActive.HasValue || m.IsActive == IsActive.Value)
                        && (!QuestionId.HasValue || m.QuestionId == QuestionId.Value)
                        orderby m.CreateDateTime descending 
                        select m;

                return q.ToList();
            }



        }
 public static void SavePostalCard(PostalCard PostalCard)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.PostalCards.ApplyChanges(PostalCard);
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("PostalCard_DataProvider_DataProvider", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
 public static void Save(FAQCategory faqCat)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.FAQCategories.ApplyChanges(faqCat);
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("faqCat_DataProvider_DataProvider", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
        public static IEnumerable<UserResponsResult> GetPollUserResponse(PollQuestion Question, out long? TotalUserResp)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                TotalUserResp = ctx.PollUserResponses.LongCount(o => (long?)o.QuestionId == Question.QuestionId);


                var q = from m in ctx.PollUserResponses.Where(o => o.QuestionId == Question.QuestionId).GroupBy(o => o.ResponseItemId)
                        select new UserResponsResult
                        {
                            itemId = m.Key,
                            count = m.Count(),

                        };

                return q.ToList();
            }
        }
 public static void SaveSystemUser(SystemUser user)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.SystemUsers.ApplyChanges(user);
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("SaveSystemUser_DataProvider_UpdateTransaction", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
        public static void SaveUserResponse(QuestionnaireForm_UserResponses resp)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                try
                {
                    ctx.QuestionnaireForm_UserResponses.ApplyChanges(resp);
                    ctx.SaveChanges();

                }
                catch (Exception ex)
                {
                    LogUtility.WriteEntryEventLog("Poll_DataProvider_SavePoll", ex, EventLogEntryType.Information);
                    if (ex.InnerException != null)
                        throw ex.InnerException;
                    throw;
                }
            }
        }
 public static void SavePollResponse(PollUserResponse UserResp)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             UserResp.Serial = Guid.NewGuid();
             ctx.PollUserResponses.ApplyChanges(UserResp);
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("Poll_DataProvider_SavePollResponse", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
 public static long SavePoll(PollQuestion p)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.PollQuestions.ApplyChanges(p);
             ctx.SaveChanges();
             return p.QuestionId;
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("Poll_DataProvider_SavePoll", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
 public static int SaveExam(BankStoryExam Exam)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.BankStoryExams.ApplyChanges(Exam);
             ctx.SaveChanges();
             return Exam.ExamId;
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("BankStory_DataProvider_SaveExam", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
        public static void SaveExamQuestion(BankStoryExam_Question Question)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                try
                {
                    ctx.BankStoryExam_Question.ApplyChanges(Question);
                    ctx.SaveChanges();

                }
                catch (Exception ex)
                {
                    LogUtility.WriteEntryEventLog("BankStory_DataProvider_SaveExamQuestion", ex, EventLogEntryType.Information);
                    if (ex.InnerException != null)
                        throw ex.InnerException;
                    throw;
                }
            }
        }
        public static void SaveFestival(Festival festival)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {
                try
                {
                    if (festival.ChangeTracker.State == ObjectState.Unchanged)
                        festival.MarkAsModified();

                    ctx.Festivals.ApplyChanges(festival);
                    ctx.SaveChanges();
                }
                catch (Exception ex)
                {
                    LogUtility.WriteEntryEventLog("Festival_DataProvider_DataProvider", ex, EventLogEntryType.Information);
                    if (ex.InnerException != null)
                        throw ex.InnerException;
                    throw;
                }
            }
        }
        public static void SaveConfig(Config c)
        {
            using (var ctx = new BMIKidsEntities(BaseDataProvider.ConnectionString))
            {
                try
                {
                    if (c.ChangeTracker.State == ObjectState.Unchanged)
                        c.MarkAsModified();

                    ctx.Configs.ApplyChanges(c);
                    ctx.SaveChanges();
                    c.AcceptChanges();
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null)
                        throw ex.InnerException;
                    throw;
                }
            }
        }
        public static List<Festival> GetFestival(out int PageCount, int? FestivalId = null, string FestivalName = null,
                                                 DateTime? FromDate = null, DateTime? ToDate = null,
                                         int Currentpage = 1, int PageSize = DefaultPageSize)
        {
            using (var ctx = new BMIKidsEntities(ConnectionString))
            {

                var q = from m in ctx.Festivals
                        where

                        (!FestivalId.HasValue || m.FestivalId == FestivalId.Value) &&
                        (!FromDate.HasValue || m.FromDate >= FromDate.Value) &&
                        (!ToDate.HasValue || m.ToDate >= ToDate.Value) &&
                        (!FestivalId.HasValue || m.FestivalId == FestivalId.Value) &&
                        (string.IsNullOrEmpty(FestivalName) || m.Name.Contains(FestivalName))
                        orderby m.FestivalId descending
                        select m;
                PageCount = q.Count();
                return q.Skip((Currentpage - 1) * PageSize).Take(PageSize).ToList();


            }
        }