public static List <SchedularTasks> GetSetSchedularTask(SchedularTasks Schedular, int lessonId, int resourceId, int type) { using (var Context = new Context()) { if (type == 1) { Lesson CurrentLesson = Context.Lessons.Where(u => u.Id == lessonId).FirstOrDefault(); DeleteAll(Schedular, true, Context, lessonId, CurrentLesson); var res = ReopenLessonsByInstructorMazkirut(Schedular, CurrentLesson, resourceId, Context); if (!string.IsNullOrEmpty(res)) { List <SchedularTasks> ERROR = new List <SchedularTasks>(); SchedularTasks eR = new SchedularTasks(); eR.Id = -1; eR.Title = res; ERROR.Add(eR); return(ERROR); } } if (type == 2) { Lesson CurrentLesson = Context.Lessons.Where(u => u.Id == lessonId).FirstOrDefault(); DeleteAll(Schedular, Schedular.AffectChildren, Context, lessonId, CurrentLesson); } var SchedularTaskListRes = Context.SchedularTasks.Where(u => u.LessonId == lessonId && u.ResourceId == resourceId).ToList(); return(SchedularTaskListRes); } }
public static string ReopenLessonsByInstructorMazkirut(SchedularTasks Schedular, Lesson CurrentLesson, int resourceId, Context Context) { // Context.Configuration.AutoDetectChangesEnabled = false; DateTime CurrentDate = CurrentLesson.Start;//new DateTime(CurrentDate.Year, CurrentDate.Month, CurrentDate.Day, 07, 00, 0);// DateTime.Now; int ParentId = 0; DateTime?LastDay = new DateTime(CurrentDate.Year, 12, 31, 23, 59, 59); if (Schedular.EndDate != null) { LastDay = Schedular.EndDate; } if (Schedular.Days > 0) { LastDay = CurrentDate.AddDays(Schedular.Days - 1); } string html = @"<div style='border:solid 1px gray;border-radius:5px;padding:2px;margin-bottom:2px;background:white'> <div style ='font-weight:bold;'><input type='checkbox' @simbol title='עדיין לא בוצע' /> " + Schedular.Title + @"</div></div>"; // Context.Configuration.AutoDetectChangesEnabled = false; List <SchedularTasks> stList = new List <SchedularTasks>(); List <Lesson> lList = new List <Lesson>(); //CurrentLesson.Details = html; //Context.Entry(CurrentLesson).State = System.Data.Entity.EntityState.Modified; while (CurrentDate.Date <= LastDay.Value.Date) { var StartDate = new DateTime(CurrentDate.Year, CurrentDate.Month, CurrentDate.Day, CurrentLesson.Start.Hour, CurrentLesson.Start.Minute, 0); var EndDate = new DateTime(CurrentDate.Year, CurrentDate.Month, CurrentDate.Day, CurrentLesson.End.Hour, CurrentLesson.End.Minute, 0); // var res = Context.Lessons.Where(x => x.Instructor_Id == resourceId && ((StartDate >= x.Start && StartDate < x.End) //|| (StartDate <= x.Start && EndDate >= x.End) || (EndDate > x.Start && EndDate <= x.End))).FirstOrDefault(); // if (res != null) // { // return res.Start.ToString("dd/MM/yyyy HH:mm"); // } //if(CurrentLesson.Start== CurrentDate) //{ //} Lesson less = new Lesson(); less.Instructor_Id = resourceId; less.Start = StartDate; less.End = EndDate; less.Details = html; less.ParentId = ParentId; lList.Add(less); //Context.Lessons.Add(less); // Context.SaveChanges(); // ParentId = less.Id; if (Schedular.EveryDay) { CurrentDate = CurrentDate.AddDays(1); } else if (Schedular.EveryWeek) { CurrentDate = CurrentDate.AddDays(7); } else if (Schedular.EveryMonth) { CurrentDate = CurrentDate.AddMonths(1); } else if (Schedular.Days > 0) { CurrentDate = CurrentDate.AddDays(1); } else { CurrentDate = ((DateTime)LastDay).AddDays(1); } //SchedularTasks st = new SchedularTasks(); //st.LessonId = less.Id; //st.ResourceId = resourceId; //st.Title = Schedular.Title; //st.Desc = Schedular.Desc; //st.Days = Schedular.Days; //st.EveryDay = Schedular.EveryDay; //st.EveryWeek = Schedular.EveryWeek; //st.EveryMonth = Schedular.EveryMonth; //st.EndDate = Schedular.EndDate; //st.IsExe = false; //stList.Add(st); // Context.SchedularTasks.Add(st); // Context.SaveChanges(); } // EntityFrameworkManager.DefaultEntityFrameworkPropagationValue = false; Context.Lessons.AddRange(lList); // Context.SchedularTasks.AddRange(stList); // Context.BulkSaveChanges(false); Context.SaveChanges(); int startHour = CurrentLesson.Start.Hour; int startMinutes = CurrentLesson.Start.Minute; // int resourceId = CurrentLesson.Instructor_Id; // var LessonsList = Context.Lessons.Where(u => u.Instructor_Id == resourceId && u.Start >= CurrentLesson.Start && u.Start.Hour == startHour && u.Start.Minute == startMinutes).ToList(); foreach (var item in lList) { SchedularTasks st = new SchedularTasks(); st.LessonId = item.Id; st.ResourceId = resourceId; st.Title = Schedular.Title; st.Desc = Schedular.Desc; st.Days = Schedular.Days; st.EveryDay = Schedular.EveryDay; st.EveryWeek = Schedular.EveryWeek; st.EveryMonth = Schedular.EveryMonth; st.EndDate = Schedular.EndDate; st.IsExe = false; stList.Add(st); } Context.SchedularTasks.AddRange(stList); // Context.BulkSaveChanges(false); Context.SaveChanges(); return(""); }
public static void DeleteAll(SchedularTasks schedularTaskList, bool affectChildren, Context Context, int lessonId, Lesson CurrentLesson) { if (affectChildren) { int startHour = CurrentLesson.Start.Hour; int startMinutes = CurrentLesson.Start.Minute; int resourceId = CurrentLesson.Instructor_Id; var CurrentSchedularTasks = Context.SchedularTasks.Where(u => u.ResourceId == resourceId && u.Id >= schedularTaskList.Id && u.Title == schedularTaskList.Title && u.Desc == schedularTaskList.Desc).ToList(); Context.SchedularTasks.RemoveRange(CurrentSchedularTasks); var LessonsList = Context.Lessons.Where(u => u.Instructor_Id == resourceId && u.Start >= CurrentLesson.Start && u.Start.Hour == startHour && u.Start.Minute == startMinutes).ToList(); Context.Lessons.RemoveRange(LessonsList); //EntityFrameworkManager.DefaultEntityFrameworkPropagationValue = false; } else { Context.Lessons.Remove(CurrentLesson); if (schedularTaskList.Id != 0) { var schedular = Context.SchedularTasks.Where(x => x.Id == schedularTaskList.Id).FirstOrDefault(); Context.SchedularTasks.Remove(schedular); } } // Context.BulkSaveChanges(); Context.SaveChanges(); // //var CurrentSchedularTasks = Context.SchedularTasks.Where(u => u.LessonId == lessonId).FirstOrDefault(); //if (CurrentSchedularTasks != null) Context.SchedularTasks.Remove(CurrentSchedularTasks); //Lesson CurrentLesson = Context.Lessons.Where(u => u.Id == lessonId).FirstOrDefault(); //Lesson ParentLesson = Context.Lessons.Where(u => u.ParentId == lessonId).FirstOrDefault(); //if (CurrentLesson != null) Context.Lessons.Remove(CurrentLesson); //if (affectChildren && ParentLesson != null) //{ // DeleteAll(schedularTaskList, affectChildren, Context, ParentLesson.Id); //} //EntityFrameworkManager.DefaultEntityFrameworkPropagationValue = false; //Context.BulkSaveChanges(); // Context.SaveChanges(); //if (schedularTaskList != null) //{ // //var lessonId = schedularTaskList.LessonId; // var resourceId = schedularTaskList.ResourceId; // var Title = schedularTaskList.Title; // var Desc = schedularTaskList.Desc; // var EveryDay = schedularTaskList.EveryDay; // var EveryWeek = schedularTaskList.EveryWeek; // var EveryMonth = schedularTaskList.EveryMonth; // var EndDate = schedularTaskList.EndDate; // List<Lesson> LessonsAll = new List<Lesson>(); // var LessonsAllGen = Context.Lessons.Where(x => x.Instructor_Id == resourceId && x.Id >= lessonId && (EndDate == null || (EndDate != null && x.Start < EndDate))).ToList(); // if (EveryDay && affectChildren) // { // LessonsAll = LessonsAllGen; // } // else if (EveryWeek && affectChildren) // { // foreach (Lesson item in LessonsAllGen) // { // if (item.Start.DayOfWeek == CurrentLesson.Start.DayOfWeek) // LessonsAll.Add(item); // } // } // else if (EveryMonth && affectChildren) // { // foreach (Lesson item in LessonsAllGen) // { // if (item.Start.Day == CurrentLesson.Start.Day) // LessonsAll.Add(item); // } // } // else // { // LessonsAll.Add(CurrentLesson); // } // //string html = @"<div style = 'border:solid 1px gray;border-radius:5px;padding:2px;margin-bottom:2px;background:white'> // // <div style ='font-weight:bold;text-decoration:underline'>" + Title + @"</div> // // <div>" + Desc + @" </div> // // </div>"; // //string html = @"<div style='border:solid 1px gray;border-radius:5px;padding:2px;margin-bottom:2px;background:white'> // // <div style ='font-weight:bold;text-decoration:underline'>" + Title + @"</div> // // <div>" + Desc + @"</div> // // </div>"; // //string html2 = @"<div style='border:solid 1px gray;border-radius:5px;padding:2px;margin-bottom:2px;background:white'> // // <div style ='font-weight:bold;text-decoration:underline'>" + Title + @"<div style ='float:left'><img src='../../../../images/approve-icon.png'/></div></div> // // <div>" + Desc + @"</div> // // </div>"; // foreach (var item in LessonsAll) // { // var CurrentSchedularTasks = Context.SchedularTasks.Where(u => u.LessonId == item.Id && u.ResourceId == resourceId && u.Title == Title && u.Desc == Desc).FirstOrDefault(); // if (CurrentSchedularTasks != null) Context.SchedularTasks.Remove(CurrentSchedularTasks); // Context.Lessons.Remove(item); // } //} //else { Context.Lessons.Remove(CurrentLesson); } }