public static void Start()
        {
            DateTime current = DateTime.Now;
            int      day     = DateTime.Now.Day;
            //  DateTime year;

            TimeSpan start = new TimeSpan(09, 30, 0);
            TimeSpan end   = new TimeSpan(21, 30, 0);


            // Check if the system already have  today time table  -- if not, create this month + five month = 6 month//
            TimeSlot checkTodayExits = _TimeSlotRepo.GetByDate(current.Day, current.Month, current.Year);

            //To day is not exits --> Create for six month
            if (checkTodayExits == null)
            {
                int totalmonth = current.Month + 5;
                //Loop for this month + next 5 month
                for (int i = current.Month; i <= totalmonth; i++)
                {
                    //Loop for day in month
                    for (int j = 1; j <= DateTime.DaysInMonth(current.Year, i); j++)
                    {
                        //Start from today
                        DateTime dt         = new DateTime(current.Year, current.Month, j);
                        TimeSlot checkExits = _TimeSlotRepo.GetByDate(dt.Day, dt.Month, dt.Year);

                        //Loop for hour in day
                        if (checkExits == null)
                        {
                            DateTime startTime = new DateTime(dt.Year, dt.Month, dt.Day) + start;
                            DateTime endTime   = new DateTime(dt.Year, dt.Month, dt.Day) + end;
                            var      hours     = new List <DateTime>();
                            hours.Add(startTime);
                            var next = new DateTime(startTime.Year, startTime.Month, startTime.Day,
                                                    startTime.Hour, startTime.Minute, 0, startTime.Kind);

                            while ((next = next.AddHours(0.5)) < endTime)
                            {
                                hours.Add(next);
                            }
                            hours.Add(endTime);

                            foreach (var hour in hours)
                            {
                                TimeSlot timeblock = new TimeSlot();
                                timeblock.startTime    = hour;
                                timeblock.endTime      = hour.AddHours(0.5);
                                timeblock.numberofCase = 0;
                                timeblock.status       = "Free";
                                _TimeSlotRepo.Add(timeblock);
                                Console.WriteLine("From " + timeblock.startTime + " To " + timeblock.endTime);
                            }
                        }
                    }
                }
            }

            //If current date is 1 we will create the 5 month from now timetable
            if (day == 1)
            {
                DateTime NewMonth = DateTime.Now.AddMonths(5);
                for (int i = 1; i <= DateTime.DaysInMonth(NewMonth.Year, NewMonth.Month); i++)
                {
                    DateTime dt         = new DateTime(NewMonth.Year, NewMonth.Month, i);
                    TimeSlot checkExits = _TimeSlotRepo.GetByDate(dt.Day, dt.Month, dt.Year);

                    if (checkExits == null)
                    {
                        DateTime startTime = new DateTime(dt.Year, dt.Month, dt.Day) + start;
                        DateTime endTime   = new DateTime(dt.Year, dt.Month, dt.Day) + end;
                        var      hours     = new List <DateTime>();
                        hours.Add(startTime);
                        var next = new DateTime(startTime.Year, startTime.Month, startTime.Day,
                                                startTime.Hour, startTime.Minute, 0, startTime.Kind);

                        while ((next = next.AddHours(0.5)) < endTime)
                        {
                            hours.Add(next);
                        }
                        hours.Add(endTime);
                        foreach (var hour in hours)
                        {
                            TimeSlot timeblock = new TimeSlot();
                            timeblock.startTime    = hour;
                            timeblock.endTime      = hour.AddHours(0.5);
                            timeblock.numberofCase = 0;
                            timeblock.status       = "Free";
                            _TimeSlotRepo.Add(timeblock);
                        }
                    }
                }
            }
            ///
        }
Пример #2
0
        /* public ActionResult UpcomingAppointment(int? memberId)
         * {
         *   DateTime today = DateTime.Now;
         *
         *   var appointmentList = _AppRepo.GetByMemberIdAndDayAndMonthAndYearNoStatus(memberId.Value, today.Year, today.Month, today.Day);
         *
         *   var a = appointmentList.Select(s => new AppointmentViewModel
         *   {
         *       appointmentId = s.id,
         *       start = s.startTime,
         *       end = s.endTime,
         *       petName = s.Pet.name,
         *       petSpecie = s.Pet.Specie.name,
         *       service = s.VAService.description,
         *       detail = s.detail,
         *       suggestion = s.suggestion
         *
         *   });
         *
         *   return Json(JsonConvert.SerializeObject(a), JsonRequestBehavior.AllowGet);
         * }
         */
        public ActionResult CheckTimetble(int?day, int?month, int?year)
        {
            if (month == null)
            {
                month = DateTime.Now.Month;
            }
            if (year == null)
            {
                year = DateTime.Now.Year;
            }
            if (day == null)
            {
                day = DateTime.Now.Day;
            }
            // Check if the system already have that day time table -- if not, create one//
            TimeSpan start = new TimeSpan(09, 30, 0);
            TimeSpan end   = new TimeSpan(21, 00, 0);
            DateTime today = new DateTime(year.Value, month.Value, day.Value);

            TimeSlot checkExits = _TimeSlotRepo.GetByDate(day.Value, month.Value, year.Value);

            int      offset     = today.DayOfWeek - DayOfWeek.Monday;
            DateTime lastMonday = today.AddDays(-offset);
            DateTime endWeek    = lastMonday.AddDays(6);

            if (checkExits == null)
            {
                //Loop for this week
                while (lastMonday <= endWeek)
                {
                    //Start from monday -> sunday

                    DateTime dt = new DateTime(lastMonday.Year, lastMonday.Month, lastMonday.Day);
                    //Loop for hour in day

                    DateTime startTime = new DateTime(dt.Year, dt.Month, dt.Day) + start;
                    DateTime endTime   = new DateTime(dt.Year, dt.Month, dt.Day) + end;
                    var      hours     = new List <DateTime>();
                    hours.Add(startTime);
                    var next = new DateTime(startTime.Year, startTime.Month, startTime.Day,
                                            startTime.Hour, startTime.Minute, 0, startTime.Kind);
                    while ((next = next.AddHours(0.5)) < endTime)
                    {
                        hours.Add(next);
                    }
                    hours.Add(endTime);

                    foreach (var hour in hours)
                    {
                        TimeSlot timeblock = new TimeSlot();
                        timeblock.startTime    = hour;
                        timeblock.endTime      = hour.AddHours(0.5);
                        timeblock.numberofCase = 0;
                        timeblock.status       = "Free";
                        _TimeSlotRepo.Add(timeblock);
                    }
                    lastMonday = lastMonday.AddDays(1);
                }
            }

            var timetable = _TimeSlotRepo.GetListByDate(day.Value, month.Value, year.Value);

            var t = timetable.Select(s => new TimeSlot
            {
                id        = s.id,
                startTime = s.startTime,
                endTime   = s.endTime,
                status    = s.status
            });

            return(Json(JsonConvert.SerializeObject(t), JsonRequestBehavior.AllowGet));
        }
        /////////////   Check tim slot to create if not exits

        private void CheckExitsTimeSlot(int?day, int?month, int?year)
        {
            TimeSpan start = new TimeSpan(09, 30, 0);
            TimeSpan end   = new TimeSpan(21, 00, 0);
            DateTime today = new DateTime(year.Value, month.Value, day.Value);
            // Check if the system already have that day time table -- if not, create one --of the week//
            DateTime lastMonday;
            DateTime endWeek;
            TimeSlot checkExits = _TimeSlotRepo.GetByDate(day.Value, month.Value, year.Value);

            if (today.DayOfWeek == DayOfWeek.Sunday)
            {
                DateTime nextSunday = today.AddDays(7 - (int)today.DayOfWeek);
                lastMonday = nextSunday.AddDays(-13);
                endWeek    = lastMonday.AddDays(6);
            }
            else
            {
                int offset = today.DayOfWeek - DayOfWeek.Monday;
                lastMonday = today.AddDays(-offset);
                endWeek    = lastMonday.AddDays(6);
            }



            //  var monday = today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);

            // int lastDay = lastMonday.Day + 6;
            //  var monday = today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);
            if (checkExits == null)
            {
                //Loop for this week
                while (lastMonday <= endWeek)
                {
                    //Start from monday -> sunday

                    DateTime dt = new DateTime(lastMonday.Year, lastMonday.Month, lastMonday.Day);
                    //Loop for hour in day

                    DateTime startTime = new DateTime(dt.Year, dt.Month, dt.Day) + start;
                    DateTime endTime   = new DateTime(dt.Year, dt.Month, dt.Day) + end;
                    var      hours     = new List <DateTime>();
                    hours.Add(startTime);
                    var next = new DateTime(startTime.Year, startTime.Month, startTime.Day,
                                            startTime.Hour, startTime.Minute, 0, startTime.Kind);

                    while ((next = next.AddHours(0.5)) < endTime)
                    {
                        hours.Add(next);
                    }
                    hours.Add(endTime);

                    foreach (var hour in hours)
                    {
                        TimeSlot timeblock = new TimeSlot();
                        timeblock.startTime    = hour;
                        timeblock.endTime      = hour.AddHours(0.5);
                        timeblock.numberofCase = 0;
                        timeblock.status       = "Free";
                        _TimeSlotRepo.Add(timeblock);
                    }

                    lastMonday = lastMonday.AddDays(1);
                }
            }
        }