Beispiel #1
0
        private double SumLessonPeriod(List <Lesson> list)
        {
            var res = 0.00;

            foreach (var item in list)
            {
                res += LessonHelper.GetTimeOfLesson(item.LessonPeriod);
            }
            return(res);
        }
        public async Task <bool> UpdateObjStatus()
        {
            try
            {
                var courses = await _context.Courses
                              .Where(x => !x.VirtualDeleted &&
                                     (x.Status == CourseStatus.NotStarted ||
                                      x.Status == CourseStatus.Started))
                              .Include(x => x.Customer)
                              .Include(x => x.Lessons)
                              .ToListAsync();

                foreach (var course in courses)
                {
                    if (course.Status == CourseStatus.NotStarted)
                    {
                        course.Status = course.StartCourse.HasValue && course.StartCourse.Value <= DateTime.Now.Date ?
                                        CourseStatus.Started :
                                        CourseStatus.NotStarted;
                        _context.Entry(course).State = EntityState.Modified;
                    }
                    else
                    {
                        if (course.Lessons.Any(x => x.Status == LessonStatus.NotStarted || x.Status == LessonStatus.Active))
                        {
                            var nextLessonNotStatrted = course.Lessons.Any(x => !x.VirtualDeleted && x.Status == LessonStatus.NotStarted) ?
                                                        course.Lessons.Where(x => x.Status == LessonStatus.NotStarted && x.Date.HasValue).OrderBy(y => y.Date).ThenBy(y => y.Time).FirstOrDefault() : null;
                            var nextLessonStatrted = course.Lessons.Any(x => !x.VirtualDeleted && x.Status == LessonStatus.Active) ?
                                                     course.Lessons.Where(x => x.Status == LessonStatus.Active && x.Date.HasValue).OrderBy(y => y.Date).ThenBy(y => y.Time).FirstOrDefault() : null;
                            var timeAfterAddLessonTime = nextLessonStatrted != null?DateTime.Now.AddHours(LessonHelper.GetTimeOfLesson(nextLessonStatrted.LessonPeriod)) : DateTime.Now;

                            if (nextLessonNotStatrted != null &&
                                nextLessonNotStatrted.Date.HasValue &&
                                nextLessonNotStatrted.Time.HasValue &&
                                (nextLessonNotStatrted.Date.Value < DateTime.Now.Date ||
                                 (nextLessonNotStatrted.Date.Value == DateTime.Now.Date && nextLessonNotStatrted.Time.Value.TimeOfDay <= DateTime.Now.TimeOfDay)))
                            {
                                nextLessonNotStatrted.Status = LessonStatus.Active;
                                _context.Entry(nextLessonNotStatrted).State = EntityState.Modified;
                            }
                            //if (nextLessonStatrted != null &&
                            //    nextLessonStatrted.Date.HasValue &&
                            //    nextLessonStatrted.Time.HasValue &&
                            //    (nextLessonStatrted.Date.Value < DateTime.Now.Date ||
                            //    (nextLessonStatrted.Date.Value == DateTime.Now.Date && nextLessonStatrted.Time.Value.TimeOfDay <= timeAfterAddLessonTime.TimeOfDay)))
                            //{
                            //    nextLessonStatrted.Status = LessonStatus.Finished;
                            //    _context.Entry(nextLessonStatrted).State = EntityState.Modified;
                            //}
                        }
                    }
                }
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }